Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You need the source code to the Murach\'s Java Programming book found here: http

ID: 3551113 • Letter: Y

Question

You need the source code to the Murach's Java Programming book found here:

http://www.murach.com/downloads/javp.htm


In this exercise, you'll create a class called GenericStack that uses a linked list to implement a stack, which is a collection that lets you access entries on a first-in, last-out basis. Then, you'll create another class that uses the GenericStack class. The GenericStack class should implement these methods: Create the GenericStack class Open the project named ch 12_ex3_GenericStack that's in the ex_starts directory. Create a new class named GenericStack that specifies a type variable that provides for generics. Declare a linked list that will hold the elements in the stack. Then, use the linked list to implement the methods shown above. Create a class that uses the GenericStack class Open the GenericStackApp class. Then, declare a generic stack at the beginning of the main method that will store String objects. Add code to the main method that uses the push method to add at least three items to the stack. After each item is added, display its value at the console (you'll need to use a string literal to do this). Then, use the size method to return the number of items in the stack and display that value. Use the peek method to return the first item and display that item, and use the size method to return the number of items again and display that value. Use the pop method to return each item, displaying it as it's returned, and display the number of items one more time. Run the project. If it works correctly, your output should look something like this:

Explanation / Answer

package algorithm.stack; /** * @author demo * */ public class TNStack {     private int size;     private int[] stackArr;     private int top = -1;          public TNStack(int size) {         this.size = size;         stackArr = new int[size];     }          /**      * increment the ctr and push element into stack      * @param i element to be pushed      */     public void push(int i) {         top++;         System.out.println("Pushing "+i);         stackArr[top] = i;     }          /**      * pop the element from stack and decrement the ctr      * @return the popped element      */     public int pop() {         int i = stackArr[top];         top--;         System.out.println("Popping "+i);         return i;     }          public int peek() {         System.out.println("Peek "+stackArr[top]);         return stackArr[top];     }          public boolean isFull() {         return (top == size-1);     }          public boolean isEmpty() {         return (top == -1);     } } package algorithm.stack; /** * @author ntallapa * */ public class TNStackClient {     /**      * @param args      */     public static void main(String[] args) {         TNStack tns = new TNStack(3);         // push some elements         if(!tns.isFull())             tns.push(4);         if(!tns.isFull())             tns.push(5);         if(!tns.isFull())             tns.push(3);         if(!tns.isFull())             tns.push(6);         else             System.out.println("Stack is full, cannot push element");                  // pop some elements         if(!tns.isEmpty())             tns.pop();         if(!tns.isEmpty())             tns.pop();         if(!tns.isEmpty())             tns.pop();         if(!tns.isEmpty())             tns.pop();         else             System.out.println("Stack is empty, cannot pop element");                  //reinsert to verify peek method         if(!tns.isFull())             tns.push(6);                  // peek couple of times; result should be same         tns.peek();         tns.peek();     } } package algorithm.stack; /** * @author demo * */ public class TNStack {     private int size;     private int[] stackArr;     private int top = -1;          public TNStack(int size) {         this.size = size;         stackArr = new int[size];     }          /**      * increment the ctr and push element into stack      * @param i element to be pushed      */     public void push(int i) {         top++;         System.out.println("Pushing "+i);         stackArr[top] = i;     }          /**      * pop the element from stack and decrement the ctr      * @return the popped element      */     public int pop() {         int i = stackArr[top];         top--;         System.out.println("Popping "+i);         return i;     }          public int peek() {         System.out.println("Peek "+stackArr[top]);         return stackArr[top];     }          public boolean isFull() {         return (top == size-1);     }          public boolean isEmpty() {         return (top == -1);     } } package algorithm.stack; /** * @author ntallapa * */ public class TNStackClient {     /**      * @param args      */     public static void main(String[] args) {         TNStack tns = new TNStack(3);         // push some elements         if(!tns.isFull())             tns.push(4);         if(!tns.isFull())             tns.push(5);         if(!tns.isFull())             tns.push(3);         if(!tns.isFull())             tns.push(6);         else             System.out.println("Stack is full, cannot push element");                  // pop some elements         if(!tns.isEmpty())             tns.pop();         if(!tns.isEmpty())             tns.pop();         if(!tns.isEmpty())             tns.pop();         if(!tns.isEmpty())             tns.pop();         else             System.out.println("Stack is empty, cannot pop element");                  //reinsert to verify peek method         if(!tns.isFull())             tns.push(6);                  // peek couple of times; result should be same         tns.peek();         tns.peek();     } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote