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...
The first part of this lab is making your stack generic. Take the code from your...
The first part of this lab is making your stack generic. Take the code from your working StringStack class and paste it into the GenericStack class. Change the name of the constructors to match the new name of the class (this has been done to this point), then modify the whole class so it uses generics, and can store any type that a programmer asks for. Until you successfully complete this, the main method will give you nasty compiler errors....
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...
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
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.
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
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...
JAVA Generic versions of allOf() and anyOf() In this lab, you will write generic versions of...
JAVA Generic versions of allOf() and anyOf() In this lab, you will write generic versions of the allOf() and anyOf() methods you wrote in an earlier lab. Then you will take those generic versions and create versions that work with a predicate rather than direct comparison of values. Part 1 - Create generic versions of allOf() and anyOf() Recall that the integer-only versions of allOf() and anyOf() looked like this: // Returns true if any element of 'a' is equal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT