Question

In: Computer Science

To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and...

To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and test it using a stack of Car, Integer, and String

Stack<E>

For Programming Lab 6, you are to write your own Generic Collection for a Stack that will use your Node<E> from Lab 5

UML for Stack<E>

Stack<E>

- top : Node<E>
- numElements : int

+ Stack( )
+ push( element : E ) : void
+ pop( ) : E
+ size( ) : int

Here is an additional description of what your stack should contain/how it should perform:

private fields:

- top which points to a Node<E>
- numElements an int

a no-arg Constructor:

- sets top to null
- numElements to 0

push(receives element of type E)

- adds a new node to top of stack
- adds 1 to numElements

pop( ) returns a value of type E

- removes node from top of stack
- decrements numElements by 1

- <<unless the stack is empty-throw EmptyStack exception (provided in Canvas)>>

size( ) returns an int

returns value of numElements

Stack<E> (Stack.java):

Item

Comment with name (2 points)

Comment each method / constructor in javadoc format (8 points)

2 private Fields created as requested
-top:Node<E> and – numElements as int (5 points)

No-arg Constructor
initializes top and numElements to 0 (5 points)

push() method

adds element of type E to stack and increases numElements (15 points)

pop() method
removes item from top of stack, decrements numElements, returns value of type E
or throws EmptyStack exception if stack was empty (15 points)

size() method
returns numElements (5 points)

following the naming guidelines for Classes and identifiers (8 points)

proper indenting (methods indented inside class, statements indented inside method, conditionally executed code indented) (8 points)

organization of your Class( instance variables up top / Constructor / setters/getters ) (8 points)

Lab must compile and run in order to receive any credit

Solutions

Expert Solution

import java.util.Scanner;
public class DriverClass
{
   public static void main(String[] args)
   {
       Scanner in = new Scanner(System.in);
       Stack stack = new Stack();
       int data;
       while(true)
       {
           System.out.println("1.Push\n2.pop\n3.size\n4.exit\nEnter choice::");
           int choice = in.nextInt();
           switch(choice){
               case 1: System.out.println("Enter element:\t");
                       data= in.nextInt();
                       stack.push(data);
                       break;
               case 2: System.out.println("Popped Element:\t"+ stack.pop());
                       break;
               case 3: System.out.println("Size of Stack:\t"+stack.size());
                       break;
               case 4: System.exit(1);
           }
       }
    
   }
}

////////////:::::::: Stack class
import java.util.NoSuchElementException;
public class Stack
{
   private Node top;
   private int count;
   class Node
   {
       public int data;
       public Node next;
   }
   // Constructs an empty stack:
   public Stack()
   {
       top = null;
       count =0;
   }
   /**
   *Push operation:::::
   * Adds an element to the top of the stack
   * @param e int to top of stack.
   */
   public void push(int e)
   {
       Node newNode = new Node();
       newNode.data = e;
       newNode.next = top;
       top = newNode;
       count++;
   }
   /**
   * Pop Operation::::
   * Removes the element from the top of the stack
   * @return the removed String
   * If stack empty throw NoSuchElementException
   */
   public int pop()
   {
       if (top == null) {throw new NoSuchElementException();}
       int e = top.data;
       top = top.next;
       count--;
       return e;
   }
   public int size()
   {
       return count;
   }
}

Outputss:::::


Related Solutions

Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
Implement a generic MyStack class using templates. A stack allows elements to be added and removed...
Implement a generic MyStack class using templates. A stack allows elements to be added and removed in a last-in, first-out (LIFO) manner. Stacks have an operation called push to place elements at the top of the stack, and another operation called pop to remove and return the element at the top of the stack. The only element on the stack that may be referenced is the one on the top. This means that if two elements are pushed onto the...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may...
Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may write this in jGrasp Create a Stack of String called, myStacks Read input from keyboard, 10 names and then add to myStacks As you remove each name out, you will print the name in uppercase along with a number of characters the name has in parenthesis. (one name per line).   e.g.     Kennedy (7) Using existing Stack Java Collection Framework, write Java Code segment to...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT