HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Stack with getMin method

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withgetminstackmethod

Problem

This should implement a stack that has a \$O(1)\$ getMin method. I used an auxiliary stack. Please provide comments on my solution.

```
import org.junit.Test;

public class Solution {

@Test
public void testEmptyStack(){
StackMin s = new StackMin();
System.out.println("----------------------");
System.out.println("Empty stack");
System.out.println(s.getMin());
}

@Test
public void testRandomStack(){
System.out.println("----------------------");
StackMin s = new StackMin();
s.push(2);
s.push(-1);
s.push(0);
s.push(7);
s.push(22);
System.out.println("Random stack");
s.printStack();
System.out.println("Min element");
System.out.println(s.getMin());
}

@Test
public void testEqualities(){
System.out.println("----------------------");
StackMin s = new StackMin();
s.push(2);
s.push(-1);
s.push(0);
s.push(0);
s.push(0);
s.push(7);
s.push(22);
s.push(-1);
s.push(-1);
s.push(33);
System.out.println("Stack with equalities");
s.printStack();
System.out.println("Min element");
System.out.println(s.getMin());
}

public static void main(String[] args) {
Solution e = new Solution();
e.testEmptyStack();
e.testRandomStack();
e.testEqualities();
}
}

class NodeV2 {
private final T data;

private final NodeV2 next;

public NodeV2(T d, NodeV2 n) {
data = d;
next = n;
}

public NodeV2(T d) {
this(d, null);
}

public T getData() {
return data;
}

public NodeV2 getNext() {
return next;
}

}

class StackMin> extends StackV2 {
StackV2 auxiliaryStack = new StackV2();

void push(T element) {
if (auxiliaryStack.isEmpty() || element.compareTo(auxiliaryStack.top.getData()) > {
NodeV2 top;

void push(T element) {
NodeV2 t = new NodeV2(element, top);
top = t;
}

T pop() {
if (top != null) {
T item = top.getData();
top = top.getNext();
return item;

Solution

Yay, you use tests!

Darn it, you don't use tests.

Confusing? Allow me to explain. While I totally approve of you using tests, there is a lot more to it than simply putting the @Test annotation above them.

A test is comprised of 3 aspects: Arrange, Act and Assert. Typically a test (whether it's unit, integration, functional, end-to-end, etc) will have these aspects in explicit or implicit form. Personally I like to explicitly separate them with comments (// Arrange).

Why do I mention this? Note the last aspect of AAA: Assert. This tells us that, as the last step in our test, we will verify the result we have and assert that it is what we expected.

Doing this allows us to run the tests and automatically compare the result of each test with the expected outcome. In your situation you have to manually verify that each test works as expected; can you see yourself doing that when you have 300 tests instead of 3?

This brings us to another point: all modern IDE's have integrated test runners. If you have tests, you can just run these using that instead of having to use a main() method as entrypoint for your tests.

By adapting these changes you also won't need those println's anymore which takes away focus from your actual content.

  • Junit documentation



  • Junit tutorial

Context

StackExchange Code Review Q#84910, answer score: 8

Revisions (0)

No revisions yet.