Question: Programming Steps: 1. Modify the Stack class provided below so that it can be used for different types. 2. Construct a class 'Step2.java' from scratch

Programming Steps:

1. Modify the Stack class provided below so that it can be used for different types.

2. Construct a class 'Step2.java' from scratch to test your Stack class, using methods and constructors shown below. This following class contains only the constructors and methods templates. that contains all your test statements. Need to print the output to output file 'out2b.txt'. I have tried to do this but I am not getting the desired output.

Please Add required code in Step2.java (Sample program below)

Have to implement this step by step as shown below.

String myName = "(Name) ";

char chA = 'A'; char chB = 'B'; char chC = 'C'; char chD = 'D'; double dA = 93.7; double dB = 7; double dC = 10; double dD = 6;

// Need to make methods for these cStack(); // a Stack for Character Class dStack(); // a Stack for Double Class vStack(); // a Stack for Variable Class //Print name to the first line of output file cStack.push('A'); cStack.push('B'); cStack.push('C'); // Display the contents of cStack to the output file cStack.push('D'); // Display the contents of cStack to the output file

// Print to ouput file ("Character popped: " + cStack.pop());

// Print the contents of cStack to output file

// Print to ouput file ("Character popped: " + cStack.pop());

// Print to ouput file ("Character popped: " + cStack.pop());

// Print to ouput file ("Character popped: " + cStack.pop());

// Print to ouput file ("Character popped: " + cStack.pop());

// Print to ouput file ("Character popped: " + cStack.pop());

dStack.push(dA); dStack.push(dC); // Print the contents of dStack to output file vStack.push(new Variable(chA, dA));

vStack.push(new Variable(chB, dB));

vStack.push(new Variable(chC, dC));

// print the contents of vSatck to output file

Sample output to output file 'out2b.txt': (Step-by-Step shown)

(Name)

Stack Pointer Value 97 C 98 B 99 A

Stack Pointer Value 96 D 97 C 98 B 99 A

Character Popped: D

Stack Pointer Value 97 C 98 B 99 A

Character Popped: C Character Popped: B Character Popped: A

Stack is Empty. Nothing to pop!

Character Popped: null

Stack is Empty. Nothing to pop!

Character Popped: null

Stack Pointer Value

98 10.0 99 93.7

Stack Pointer Value 97 C 10.0 98 B 7.0 99 A 93.7

Required Programs: (Modify code as required)

Stack.java:

import java.util.NoSuchElementException;

public class Stack { private int size; // size of the stack private int array[], first, sp; // top of the stack, stack, and stack pointerz // constructor for declaring values for variables public Stack(int n) { size = 5; // constant size of the stack array = new int[size]; first = -1; sp = 0; } /** * Returns true if this stack is empty. * * @return true if this stack is empty; false otherwise */ public boolean isEmpty() { return first == -1; }

/* Function to check if stack is full */ public boolean isFull() { return first == size -1 ; } /** * Returns the number of items in this stack. * * @return the number of items in this stack */ public int size() { return size; } /** * Adds the item to this stack. * * @param item the item to add */ public void push(int i) { if(first + 1 >= size) throw new IndexOutOfBoundsException("Overflow Exception"); if(first + 1 < size ) array[++first] = i; sp++ ; } /** * Removes and returns the item most recently added to this stack. * * @return the item most recently added * @throws NoSuchElementException if this stack is empty */ public int pop() { if( isEmpty() ) throw new NoSuchElementException("Underflow Exception"); sp-- ; return array[first--]; } /* Function to check the top element of the stack */ public int peek() { if( isEmpty() ) throw new NoSuchElementException("Underflow Exception"); return array[first]; }

/** * Returns a string representation of this stack. */ public void Display() { System.out.print(" Stack : "); if (sp == 0) { System.out.print("Empty "); } for (int i = first; i >= 0; i--) System.out.print(array[i]+" "); System.out.println(); } }

Step2.java: (This is what I tried but I do not think this is the right way to do it, so feel free to modify the code)

Also, please add the code to generate the output to out2b.txt file.

import java.util.EmptyStackException; import java.util.NoSuchElementException;

public class Step2 { static class cStack { char stackData[]; // holds the stack private int size; int sp2, topOfTheStack; // index of top-of-stack public cStack() { sp2 = 0; size = 5; topOfTheStack = -1; stackData = new char[size]; } void push(char ch) { /*if(sp2==size) { System.out.println( "Stack is full "); return; } stackData[sp2] = ch; sp2++;*/ if(topOfTheStack + 1 >= size) throw new IndexOutOfBoundsException("Overflow Exception"); if(topOfTheStack + 1 < size ) stackData[++topOfTheStack] = ch; sp2++ ; } public boolean isEmpty() { return topOfTheStack == -1; }

char pop() { if( isEmpty() ) throw new NoSuchElementException("Underflow Exception"); sp2-- ; return stackData[topOfTheStack--]; } public void Display() { // TODO Auto-generated method stub System.out.print(" Stack Pointer" + "\tValue "); if (sp2 == 0) { System.out.print("Stack is Empty. Nothing to pop! "); } for (int i = topOfTheStack ; i >= 0; i--) System.out.println( " " + sp2 + "\t\t " + stackData[i]+" "); System.out.println(); } }

static class vStack{ private double[ ][] variableData; private int manyItems; private int sp1; private int first; private double dA; private char chA; public vStack() { final int INITIAL_CAPACITY = 10; manyItems = 0; variableData = new double[INITIAL_CAPACITY][INITIAL_CAPACITY]; sp1 = 0; first = -1; } public void push(char item1,double item) { if(first + 1 >= manyItems) ///throw new IndexOutOfBoundsException("Overflow Exception"); if(first + 1 < manyItems ) variableData[++first][++first] = item; sp1++ ; }

public void Variable(char chA, double dA) { this.chA = chA; this.dA = dA; }

public char getFirst() { return chA; }

public double getSecond() { return dA; }

public void push(Variable variable) { // TODO Auto-generated method stub } /*public void push(Variable Variable) { // TODO Auto-generated method stub if(first + 1 >= manyItems) ///throw new IndexOutOfBoundsException("Overflow Exception"); if(first + 1 < manyItems) variableData[++first] = Variable; sp1++ ; }*/ } static class dStack{ private double[ ] data; private int manyItems; private int sp1; private int first; public dStack( ) { final int INITIAL_CAPACITY = 10; manyItems = 0; data = new double[INITIAL_CAPACITY]; sp1 = 0; first = -1; }

public boolean isEmpty( ) { return (manyItems == 0); } public double peek( ) { if (manyItems == 0) // EmptyStackException is from java.util and its constructor has no argument. throw new EmptyStackException( ); return data[manyItems-1]; } public double pop( ) { if (manyItems == 0) // EmptyStackException is from java.util and its constructor has no argument. throw new EmptyStackException( ); return data[--manyItems]; } public void push(double item) { if(first + 1 >= manyItems) ///throw new IndexOutOfBoundsException("Overflow Exception"); if(first + 1 < manyItems ) data[++first] = item; sp1++ ; }

public void Display() { // TODO Auto-generated method stub System.out.print(" Stack Pointer" + "\tValue "); if (sp1 == 0) { System.out.print("Stack is Empty. Nothing to pop! "); } for (int i = first ; i >= 0; i--) System.out.print(data[i]+" "); System.out.println(); } } public static void main(String[] args) { String myName = "(Aashiv Patel) "; char chA = 'A'; char chB = 'B'; char chC = 'C'; char chD = 'D'; double dA = 93.7; double dB = 7; double dC = 10; double dD = 6; cStack cStack = new cStack(); // a Stack for Character Class dStack dStack = new dStack(); // a Stack for Double Class vStack vStack = new vStack(); // a Stack for Variable Class System.out.println(myName); cStack.push('A'); cStack.push('B'); cStack.push('C'); cStack.Display(); cStack.push('D'); cStack.Display(); dStack.push(dA); dStack.push(dC); dStack.Display(); System.out.println("Character Popped: " + cStack.pop()); cStack.Display(); System.out.println("Character Popped: " + cStack.pop()); System.out.println("Character Popped: " + cStack.pop()); System.out.println("Character Popped: " + cStack.pop()); cStack.Display(); System.out.println("Character Popped: " + cStack.pop()); System.out.println("Character Popped: " + cStack.pop()); vStack.push(new Variable(chA, dA)); vStack.push(new Variable(chB, dB)); vStack.push(new Variable(chC, dC));

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!