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

The purpose of this Java application is to create a new class that is similar to

ID: 3801783 • Letter: T

Question

The purpose of this Java application is to create a new class that is similar to the Stack<T> class – but your new class is to keep the items pushed onto the stack in priority order. The item with the lowest value would be popped from the stack first.

For example, if you push the following integers onto the stack in this order – 55,1,32,7,44,2 – When pop() is called the very first time, the number 1 will be returned. The 2nd call to pop() will return the number 2. The 3rd call to pop() will return 7. The 4th call to pop() will return 32, and so on.

You must create a PriortyStack class with at least three methods.

A method to push a value onto the stack

A method to pop a value off the stack

A method to determine if the stack is empty

Print the contents of the stack by repeatedly calling the pop() method to demonstrate the stack has its items in priority order.

HINTS

Use the Stack<T> class – as a starting point.

The items in your list must be comparable, meaning your class will be PriorityStack<T extends Comparable<T>> not just PriorityStack<T>

Remember, Collections.sort() will sort the contents of a list

Watch a demonstration of the application:

https://youtu.be/wosbU5qoWvE

Your application must follow these general requirements

All classes must be in a package following these rules:

The package name is your last name plus the first letter of your first name. For example, if you name is Ritta Red, her package is “red.r”

Package names are all lowercase letters

All class names must start with an UPPERCASE letter then camel-cased after that.

All property names must start with a lowercase letter then came-cased after that.

All method names must start with a lowercase letter then came-cased after that.

Output must match the examples. Watch out for spaces and punctuation.

Explanation / Answer

HI, Please find my implementation.

Please let me know in case of any issue.

###############################################

import java.util.Collections;

import java.util.LinkedList;

public class PriorityStack<T extends Comparable<T>>{

  

   private LinkedList<T> stack;

  

   public PriorityStack() {

       stack = new LinkedList<>();

   }

  

   public void push(T item){

      

       stack.addFirst(item); // adding item at first

       Collections.sort(stack); // sorting stack

   }

  

   public boolean empty(){

       return stack.size() == 0;

   }

  

   public T pop(){

      

       if(empty())

           return null;

      

       return stack.removeFirst();

      

   }

  

}

##################################################

public class PriorityStackTest {

  

   public static void main(String[] args) {

      

       PriorityStack<Integer> stack = new PriorityStack<>();

      

       stack.push(55);

       stack.push(1);

       stack.push(32);

       stack.push(7);

       stack.push(2);

      

       while(!stack.empty())

           System.out.println(stack.pop());

   }

}

/*

Sample run:

1

2

7

32

55

*/