Question: Modify and enhance the GenericStack implementation from Chapter 3 so that it conforms to the diagram shown below. Download the Java source code for Chapter

Modify and enhance the GenericStack implementation from Chapter 3 so that it conforms to the diagram shown below.

Modify and enhance the GenericStack implementation from Chapter 3 so that it

Download the Java source code for Chapter 3 from the Chapter 3 folder on our Blackboard site, so that you can use the GenericStack class to begin. The default constructor should be modified so that it initializes the array size to 3. The reset method should reinitialize the stack to empty. The peek method should return the object at the top of the stack without removing it. The isEmpty method should test if the stack is empty (underflow condition) and return a boolean (true or false) value. The isFull method should test if the stack is full (overflow condition) and return a boolean value. The expand method is private to this class and you will modifiy the code so that it will only be called when needed. It should expand the stack every time a push operation is performed that would cause an overflow condition.

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. Paste your test runs into a Word document to include with your assignment submission.

Include your Java source code files (.java) and your Word document with your program test runs within a Zip file archive (.zip). Name the Zip file according to the convention in the syllabus. This one should be called Assign3B_FirstnameLastname. Upload that in Blackboard to submit your assignment.

// GenericStack.java public class GenericStack { private T[ ] data; private int top; private int size; public GenericStack( ) { top = -1; size = 100; data = (T[ ]) new Object[100]; } 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()); } }

------------------------------------------------------------------------------------------------------------

// GenericNode.java

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. }

------------------------------------------------------------------------------------------------------------

//Listing.java

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; } }

------------------------------------------------------------------------------------------------------------

//MainGenericStack.java

public class MainGenericStack { public static void main(String[] args) { GenericStack s = 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 initalized (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); } }

------------------------------------------------------------------------------------------------------------

//ListingGS.java

public class ListingGS implements GenericNode { private String name; private String address; private String number; public ListingGS(String n, String a, String num ) { name = n; address = a; number = num; } public String getKey( ) { return name; } public ListingGS deepCopy() { ListingGS clone = new ListingGS(name, address, number); return clone; } public String toString() { return("Name is " + name + ' ' + "Address is " + address + ' ' + "Number is " + number + ' '); }

public String getNumber() { return number; } public void setNumber(String n) { number = n; } }

------------------------------------------------------------------------------------------------------------

If you need more Java Classes please let me know there is more in the Chapter 3 text but I don't believe that they are needed. Thank you, I will upvote!

GenericStack T[ ] data int size // Array of generic objects 1/default size of array is 3 1/top of the stack int top + + + + Genericstack() Generic Stack (int) boolean push (T) + Tpop) void showAll() + void reset() + T peek) boolean isEmpty + boolean isFull) boolean expand) //Default constructor //Constructor that sets stack capacity 1/Add object to top of the stack //Remove and return object from top of stack // Console output of all objects in stack //Resets stack to empty //Return object from top of stack //Tests if stack is empty //Tests if stack is full //Expands size of Stack when needed

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!