+1 (315) 557-6473 

Create A Program to Implement Stack ADT In Java Assignment Solution.


Instructions

Objective
Write a Java assignment program to implement the stack ADT in the Java language. This assignment will involve creating a Java program that demonstrates the use of stacks, a fundamental data structure. Stacks follow the Last In, First Out (LIFO) principle and are commonly used for various applications such as expression evaluation, backtracking algorithms, and more. By completing this assignment, you will gain a deeper understanding of how to work with classes, methods, and data structures in Java, enhancing your programming skills along the way.

Requirements and Specifications

Program to implement stack ADT in java

Screenshot

Program to implement stack ADT in java 1
Program to implement stack ADT in java 2

Source Code

TEST ENGINE

import java.io.StringReader;

import edu.uwm.cs.junit.LockedTestCase;

import edu.uwm.cs351.ps.Array;

import edu.uwm.cs351.ps.Engine;

import edu.uwm.cs351.ps.Interpreter;

import edu.uwm.cs351.ps.Scanner;

public class TestEngine extends LockedTestCase {

    Engine engine;

    protected void setUp() {

        engine = new Engine(10,10);

    }

    protected void execute(String s) {

        Interpreter i = new Interpreter(new Scanner(new StringReader(s)), engine);

        i.run();

    }

    public void test00() {

        engine.push("a");

        engine.push("b");

        engine.push("c");

        // debug string prints the stack:

        assertEquals(Ts(1246496985),engine.debugString());

        execute("pop");

        assertEquals("[a, b]",engine.debugString());

        execute("(d)"); // Recall that parens are PostScript's way to show string literals

        assertEquals(Ts(1069313328),engine.debugString());

    }

    public void test01() {

        engine.push("a");

        engine.push("b");

        engine.push("c");

        execute("0 index");

        assertEquals("[a, b, c, c]",engine.debugString());

    }

    public void test02() {

        engine.push("a");

        engine.push("b");

        engine.push("c");

        execute("1 index");

        assertEquals(Ts(1810631773), engine.debugString());

    }

    public void test03() {

        engine.push("a");

        engine.push("b");

        engine.push("c");

        execute("2 index");

        assertEquals("[a, b, c, a]", engine.debugString());

    }

    public void test09() {

        execute("(7) (6) (5) (4) (3) (2) (1) 3 index");

        assertEquals("[7, 6, 5, 4, 3, 2, 1, 4]", engine.debugString());

    }

    public void test10() {

        execute("count");

        Double d = (Double)engine.pop();

        assertEquals(Ti(839574740),d.intValue());

    }

    public void test11() {

        engine.push("a");

        execute("count");

        Double d = (Double)engine.pop();

        assertEquals(1,d.intValue());

    }

    public void test13() {

        engine.push("a");

        engine.push("b");

        engine.push("c");

        execute("count");

        Double d = (Double)engine.pop();

        assertEquals(3,d.intValue());

    }

    public void test20() {

        execute("[ counttomark");

        assertEquals("[, 0.0]",engine.debugString());

        Double d = (Double)engine.pop();

        assertEquals(0,d.intValue());

    }

    public void test21() {

        execute("[ (a) (b) [ (c) counttomark");

        Double d = (Double)engine.pop();

        assertEquals(1,d.intValue());

        assertEquals("[, a, b, , c]",engine.debugString());

    }

    public void test22() {

        execute("[ (a) [ (b) (c) counttomark");

        Double d = (Double)engine.pop();

        assertEquals(Ti(1368049932),d.intValue());

        assertEquals("[, a, , b, c]",engine.debugString());

    }

    public void test26() {

        execute("[ (a) [ (b) (c) (d) (e) (f) (g) (h) counttomark");

        Double d = (Double)engine.pop();

        assertEquals(7,d.intValue());

        assertEquals("[, a, , b, c, d, e, f, g, h]",engine.debugString());

    }

    public void test27() {

        execute("[ (0, 1) [ (a) (b, c) ([) (d, e) counttomark");

        Double d = (Double)engine.pop();

        assertEquals(4, d.intValue());

        assertEquals("[, 0, 1, , a, b, c, [, d, e]", engine.debugString());

    }

    public void test30() {

        execute("(a) [ cleartomark");

        assertEquals("[a]",engine.debugString());

    }

    public void test36() {

        execute("[ (z) [ 1 2 3 4 5 (c) cleartomark");

        assertEquals("[, z]",engine.debugString());

    }

    public void test40() {

        execute("[]");

        Array a = (Array)engine.pop();

        assertEquals(0,a.length());

        assertEquals("[]",engine.debugString()); // coincidence

    }

    public void test41() {

        execute("(hello) [ ]");

        Array a = (Array)engine.pop();

        assertEquals(0,a.length());

        assertEquals("[hello]",engine.debugString());

    }

    public void test42() {

        execute("[ (one) [ (two) ]");

        Array a = (Array)engine.pop();

        assertEquals(1, a.length());

        assertEquals("two", a.get(0));

        assertEquals("[, one]",engine.debugString());

    }

    public void test44() {

        execute("(a) [ 1 2 3 4 ]");

        Array a = (Array)engine.pop();

        assertEquals(4,a.length());

        assertEquals(Double.valueOf(1),a.get(0));

        assertEquals(Double.valueOf(2),a.get(1));

        assertEquals(Double.valueOf(3),a.get(2));

        assertEquals(Double.valueOf(4),a.get(3));

        assertEquals("[a]",engine.debugString());

    }

    public void test49() {

        execute("[ 9 [ (b) [ (c) (d) ] 10 20 30]");

        Array a = (Array)engine.pop();

        assertEquals(5,a.length());

        assertEquals("b",a.get(0));

        Array a2 = (Array)a.get(1);

        assertEquals(2, a2.length());

        assertEquals("c",a2.get(0));

        assertEquals("d",a2.get(1));

        assertEquals(Double.valueOf(10),a.get(2));

        assertEquals(Double.valueOf(20),a.get(3));

        assertEquals(Double.valueOf(30),a.get(4));

        assertEquals("[, 9.0]",engine.debugString());

    }

    public void test50() {

        execute("(a) (b) (c) (d) 2 0 roll");

        assertEquals(Ts(1670987571),engine.debugString());

    }

    public void test51() {

        execute("(a) (b) (c) (d) 2 1 roll");

        assertEquals("[a, b, d, c]",engine.debugString());

    }

    public void test52() {

        execute("(a) (b) (c) (d) 2 -1 roll");

        assertEquals("[a, b, d, c]",engine.debugString());

    }

    public void test53() {

        execute("(a) (b) (c) (d) 3 1 roll");

        assertEquals(Ts(763956264),engine.debugString());

    }

    public void test54() {

        execute("(a) (b) (c) (d) 3 2 roll");

        assertEquals("[a, c, d, b]",engine.debugString());

    }

    public void test55() {

        execute("(a) (b) (c) (d) 3 -1 roll");

        assertEquals("[a, c, d, b]",engine.debugString());

    }

    public void test56() {

        execute("(a) (b) (c) (d) 4 1 roll");

        assertEquals("[d, a, b, c]",engine.debugString());

    }

    public void test57() {

        execute("(a) (b) (c) (d) 4 -2 roll");

        assertEquals("[c, d, a, b]",engine.debugString());

    }

    public void test58() {

        execute("(a) (b) (c) (d) 4 -1 roll");

        assertEquals("[b, c, d, a]",engine.debugString());

    }

    public void test59() {

        execute("(1) (2) (3) (4) (5) (6) (7) (8) (9) 7 -5 roll");

        assertEquals("[1, 2, 8, 9, 3, 4, 5, 6, 7]",engine.debugString());

    }

}

TEST STACK

import edu.uwm.cs.junit.LockedTestCase;

import edu.uwm.cs351.util.Stack;

public class TestStack extends LockedTestCase {

    protected void assertException(Class c, Runnable r) {

        try {

            r.run();

            assertFalse("Exception should have been thrown",true);

        } catch (RuntimeException ex) {

            assertTrue("should throw exception of " + c + ", not of " + ex.getClass(), c.isInstance(ex));

        }

    }

    private Stack s;

    @Override

    protected void setUp() {

        try {

            assert s.isEmpty();

            assertFalse("Please turn on assertions!",true);

        } catch (NullPointerException ex) {

            assertTrue(true);

        }

        s = new Stack();

    }

    public void testS00() {

        Stack s = new Stack();

        assertTrue(s.isEmpty());

    }

    public void testS01() {

        assertTrue(s.isEmpty());

    }

    public void testS10() {

        s.push(77);

        assertEquals(Tb(1948889376), s.isEmpty());

        assertEquals(Ti(717212931), s.peek().intValue());

        assertEquals(Tb(1575342336), s.isEmpty());

        assertEquals(Ti(1634853426), s.pop().intValue());

        assertEquals(Tb(176940846), s.isEmpty());

    }

    public void testS12() {

        s.push(42);

        s.push(-9);

        assertEquals(Ti(1627457663),s.peek().intValue());

        assertFalse(s.isEmpty());

        assertEquals(Ti(1230102286),s.pop().intValue());

        assertFalse(s.isEmpty());

        assertEquals(Ti(2006237031),s.peek().intValue());

        assertEquals(42,s.pop().intValue());

        assertTrue(s.isEmpty());

    }

    public void testS13() {

        s.push(0);

        s.push(-32768);

        s.push(88);

        assertEquals(Ti(1667245384),s.peek().intValue());

        assertFalse(s.isEmpty());

        assertEquals(88,s.pop().intValue());

        assertFalse(s.isEmpty());

        assertEquals(-32768,s.peek().intValue());

        assertEquals(Ti(1859343223),s.pop().intValue());

        assertEquals(Ti(2050818480),s.pop().intValue());

        assertTrue(s.isEmpty());

    }

    public void testS19() {

        s.push(null);

        assertFalse(s.isEmpty());

        assertNull(s.peek());

        assertNull(s.pop());

        assertTrue(s.isEmpty());

    }

    public void testS20() {

        try {

            s.pop();

            assertFalse("pop of an empty stack returned",true);

        } catch (Exception e) {

            //what exception should be thrown?

            assertEquals(Ts(570783829), e.getClass().getSimpleName());

        }

    }

    public void testS21() {

        try {

            s.peek();

            assertFalse("peek on an empty stack returned",true);

        } catch (Exception e) {

            //what exception should be thrown?

            assertEquals(Ts(1903488762), e.getClass().getSimpleName());

        }

    }

    public void testS30() {

        assertTrue(s.clone().isEmpty());

    }

    public void testS31() {

        s.push(1001);

        Stack c = s.clone();

        assertEquals(1001,c.pop().intValue());

        assertEquals(1001,s.peek().intValue());

    }

    public void testS32() {

        s.push(169);

        Stack c = s.clone();

        s.push(343);

        c.push(-55);

        assertEquals(Ti(1620679750),c.peek().intValue());

        assertEquals(Ti(324933978),s.pop().intValue());

        assertEquals(-55,c.pop().intValue());

        assertEquals(Ti(1020844101),s.pop().intValue());

        assertEquals(Ti(695736991),c.pop().intValue());

    }

    public void testS33() {

        class Hidden extends Stack {

            public boolean isEmpty() {

                return true;

            }

        }

        Hidden h = new Hidden();

        Stack st = h.clone();

        assertTrue("clone didn't use super.clone()",st instanceof Hidden);

    }

    public void testS34() {

        Stack s1 = new Stack();

        Stack s2 = s1.clone();

        s1.push("hello");

        s2.push("bye");

        assertEquals(Ts(2131980359),s1.pop());

        assertEquals(Ts(1875506833),s2.pop());

    }

    public void testS39() {

        for (int i=0; i < 1000; ++i) {

            s.push(i);

        }

        Stack c = s.clone();

        for (int i=1; i < 500; ++i) {

            assertEquals(1000,i+c.pop());

            assertEquals(1000,i+s.pop());

        }

        s.push(-17);

        c.push(33);

        assertEquals(-17,s.pop().intValue());

        assertEquals(33,c.pop().intValue());

    }

    public void testS40() {

        for (int i=0; i < 1000; ++i) {

            s.push(i);

            assertFalse(s.isEmpty());

        }

        for (int i=0; i < 1000; ++i) {

            if ((i & 1) == 0) {

                assertEquals(999,i+s.peek());

            }

            assertEquals(999,i+s.pop());

        }

    }

    public void testS42() {

        for (int i=0; i < 1000; ++i) {

            s.push(i);

        }

        s.clear();

        assertTrue(s.isEmpty());

        s.clear();

        assertTrue(s.isEmpty());

    }

    public void testS50() {

        // elements separated by ", " in square brackets

        assertEquals(Ts(1790144628),s.toString());

    }

    public void testS51() {

        // elements separated by ", " in square brackets

        s.push(-4);

        assertEquals(Ts(789077849),s.toString());

    }

    public void testS52() {

        // elements separated by ", " in square brackets

        s.push(13);

        s.push(20);

        assertEquals(Ts(328978396),s.toString());

    }

    public void testS53() {

        s.push(null);

        s.push(-1492);

        s.push(null);

        assertEquals("[null, -1492, null]",s.toString());

    }

}