Question: How would I do this with the provided classes? Write another class named Test that contains a main method. Use this as a driver program
How would I do this with the provided classes? Write another class named Test that contains a main method. Use this as a driver program to test the methods of the GenericStack class. You need to use a class of objects to put in your stack structure for your tests. This may be the Listing from the text or your Student class for testing but you would have to make sure they implement the GenericNode interface from the text or you will get error messages.
import java.util.*; public class GenericStack{ private T[ ] data; private int size; private int top; private MainStack[] s; GenericStack stack = new GenericStack (); public GenericStack() { this.size = 3; top = -1; size = 3; data = (T[ ]) new Object[3]; } public int getSize() { return this.size; } public int getNoOfElement() { return this.top + 1; } public GenericStack(int n) { top = -1; size = n; data = (T[ ]) new Object[n]; } public boolean push(T newNode) { GenericNode node = (GenericNode) newNode; if(top == size-1) return false; // ** overflow error ** else { top = top +1; data[top] = (T) node.deepCopy(); return true; // push operation successful } } public T pop( ) { int topLocation; if(top == -1) return null; // ** underflow error ** else { topLocation = top; top = top -1; return data[topLocation]; } } public void showAll( ) { for(int i = top; i >= 0; i--) System.out.println(data[i].toString()); } public void reset( ) { System.out.println("Before the stack is reset:" + stack); stack.reset(); System.out.println("After the stack is reset:" + stack); } public T peek( ) { return stack.peek(); } boolean isEmpty( ) { if (top == -1) { return true ; } else return false; } boolean isFull( ) { if(top == (data.length - 1)) { return true; } else return false; } private boolean expand() { int s=this.size; T [] d=this.data; this.size*=2; this.data=(T[]) new Object[this.size]; for(int i=0;i public interface GenericNode { public abstract GenericNode deepCopy(); // clones the invoking object public abstract String toString(); // added to prevent Objects toString method from // executing. Technically not syntactically necessary. }public class MainStack { public static void main(String[] args) { Stack s = new Stack(3); Listing l; Listing l1 = new Listing("Bill", "1st Avenue", "123 4567"); Listing l2 = new Listing("Al", "2nd Avenue", "456 3232"); Listing l3 = new Listing("Mike", "3rd Avenue", "333 3333"); Listing l4 = new Listing("Carol", "4th Avenue", "444 4444"); // an attempt to perform a pop on an initialized (empty) stack will return null System.out.println(s.pop()); // perform three pushes to fill the stack and then output the stack System.out.println(s.push(l1)); System.out.println(s.push(l2)); System.out.println(s.push(l3)); s.showAll(); // perform three pop operations to empty the stack l = s.pop(); System.out.println(l.toString()); l = s.pop(); System.out.println(l.toString()); l = s.pop(); System.out.println(l.toString()); // an attempt to perform a pop on an empty stack will return null l = s.pop(); System.out.println(l); System.exit(0); } }public class Listing { private String name; private String address; private String number; public Listing() { name = ""; address = ""; number = ""; } public Listing(String n, String a, String num ) { name = n; address = a; number = num; } public String toString( ) { return ("Name is " + name + ' ' + "Address is " + address + ' ' + "Number is " + number + ' '); } public Listing deepCopy( ) { Listing clone = new Listing(name, address, number); return clone; } }public class MainGenericStack { public static void main(String[] args) { GenericStacks = new GenericStack (3); ListingGS l; ListingGS l1 = new ListingGS("Bill", "1st Avenue", "123 4567" ); ListingGS l2 = new ListingGS("Al", "2nd Avenue", "456 3232"); ListingGS l3 = new ListingGS("Mike", "3rd Avenue", "333 3333"); ListingGS l4 = new ListingGS("Carol", "4th Avenue", "444 4444"); // an attempt to perform a pop on an initialized (empty) stack will return null System.out.println(s.pop()); // perform three pushes to fill the stack and then output the stack System.out.println(s.push(l1)); System.out.println(s.push(l2)); System.out.println(s.push(l3)); s.showAll(); // perform three pop operations to empty the stack l = s.pop(); System.out.println(l); l = s.pop(); System.out.println(l); l = s.pop(); System.out.println(l); // an attempt to perform a pop on an empty stack will return null l = s.pop(); System.out.println(l); System.exit(0); } } public class Queue { private Listing[] data; private int size; private int numOfNodes; private int front; private int rear; public Queue() { size = 100; numOfNodes = 0; front = 0; rear = 0; data = new Listing[100]; } public Queue(int n) { size = n; numOfNodes = 0; front = 0; rear = 0; data = new Listing[n]; } public boolean enque(Listing newNode) { if(numOfNodes == size) return false; // ** overflow error ** else { numOfNodes = numOfNodes +1; data[rear] = newNode.deepCopy(); rear = (rear +1) % size; return true; // push operation successful } } public Listing deque( ) { int frontLocation; if(numOfNodes == 0) return null; // ** underflow error ** else { frontLocation = front; front = (front + 1) % size; numOfNodes = numOfNodes -1; return data[frontLocation]; } } public void showAll() { int i = front; for(int c = 1; c <= numOfNodes; c++) { System.out.println(data[i].toString( )); i = (i + 1) % size; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
