Java Assignment Chapter 6 - New Jersey Institute of …nbg7/JavaAssignments/Java... · Java...

9
Nisarg B. Gor CS 602 Java Assignment Chapter 6 Submitted By: Nisarg B. Gor

Transcript of Java Assignment Chapter 6 - New Jersey Institute of …nbg7/JavaAssignments/Java... · Java...

NisargB.Gor

CS602

Java Assignment

Chapter 6

Submitted By: Nisarg B. Gor

NisargB.Gor

CS602

Code: import java.util.Scanner; public class stackInterface implements Stack { int top; public stackInterface() { // TODO Auto-generated constructor stub top=0; } static stackInterface s1 = new stackInterface(); Object[] stack = new Object[10]; public static void main(String args[]) { int topStack=0; int ch; l1: while(true) { System.out.println("Enter your Choice:\n1. Push\n2. Pop" + "\n3. Size of the Stack\n4. Top of the Stack\n5. Display\n6. Exit"); Scanner sc = new Scanner(System.in); ch = sc.nextInt(); switch(ch) { case 1: System.out.println("Enter the number"); Object input = sc.next(); s1.push(input,topStack); break; case 2: s1.pop(topStack); break; case 3: s1.size(topStack); break; case 4: s1.top(topStack); break; case 5: s1.traverse(topStack); case 6: break l1; default: System.out.println("Invalid Input!"); } } }

NisargB.Gor

CS602

@Override public void push(Object input, int topStack) { // TODO Auto-generated method stub top++; stack[top] = input; } @Override public Object pop(int topStack) { // TODO Auto-generated method stub if(top==0) { System.out.println("Stack Empty"); return 0; } else { Object pop = stack[top]; top--; System.out.println(pop); return pop; } } @Override public Object top(int topStack) { // TODO Auto-generated method stub if (top ==0) { System.out.println("Empty stack"); return 0; } else { System.out.println( "Top of the stack is:"+stack[top]); } return stack[top]; } @Override public int size(int topStack) { // TODO Auto-generated method stub if(top==0) { System.out.println("Empty Stack"); return 0; } else

NisargB.Gor

CS602

{ int size = top; System.out.println("Size of the Stack is:"+size); return size; } } public boolean isEmpty() { if(stack.length==0) { return true; } else return false; } public void traverse(int topStack) { for(int i=1; i<=top;i++) { System.out.println(stack[i]+"\n"); } } }

NisargB.Gor

CS602

Output Screenshots: Push:

Pop:

Size:

NisargB.Gor

CS602

Top:

NisargB.Gor

CS602

Display:

NisargB.Gor

CS602

JUnit Test: Stack Test Case: import org.junit.Test; import junit.framework.TestCase; public class StackTestCase extends TestCase { stackInterface s1 = new stackInterface(); int topStackTest = 0; @Test public void testNewStackEmpty() { assertEquals(s1.size(topStackTest), 0); } @Test public void testpush() { int count = 2; for(topStackTest=0;topStackTest<count;topStackTest++) { s1.push("pushInput", topStackTest); } assertEquals(s1.size(topStackTest), count); assertFalse(s1.isEmpty()); } @Test public void testpop() { int count = 4; for(topStackTest=0;topStackTest<count;topStackTest++) { s1.push("popInput", topStackTest); } assertEquals(s1.pop(topStackTest-2),"popInput"); } } StackTestRunner: import org.junit.runner.Result; import org.junit.runner.notification.Failure; import org.junit.runner.JUnitCore; public class stackTestRunner { public static void main(String[] args) { Result result=null; result = JUnitCore.runClasses(StackTestCase.class);

NisargB.Gor

CS602

for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } } Screenshot:

Scores: GeneralScoreEx.5.1Score

A 2 D 3 B 1 E 2 C 1 F 4 Total 19 G 6