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

Step 1 -- Create an Order class. The Order class represents a purchase order. Th

ID: 3583886 • Letter: S

Question

Step 1 -- Create an Order class.

The Order class represents a purchase order. The details are elsewhere and the only thing an Order object contains is the alphanumeric order identifier code and a number that is the priority of that order.

The Order class should have the following members:

A private instance variable for the order code (which is alphanumeric).

A private instance variable for the priority of the order (the acceptable values are 1, 2, or 3).

A constructor that sets the two instance variables.

Get methods for the order code and priority. No set methods are required.

A toString() method that returns a String that is a report on the data in the Order object.

Step 2 -- Create a PriorityStack class.

Pages 120 to 122 of your text has the code for a basic stack class. Modify that code as follows:

Rename the class PriorityStack.

Instead of having an array of longs, it should have an array of Order objects.

Modify all members to account for the fact you are working with Order objects -- not long primitive values.

Then add the following members to the class:

A method called numberOfOrders(). It returns an integer value that is the number of Order obects that are actually contained within your stack (i.e. the encapsulated array). Remember, that is not the same as the length of your array! You could have a mix of objects and nulls in your stack.

A prioritySort() method that sorts your stack by priority values. A value of 1 is the highest priority, and a value of 3 is the lowest. After sorting, the highest priority orders should be at the top of the stack.

Step 3 -- Create a PriorityStackDriver class

This class contains your main method. In your main method, do the following:

Instantiate a PriorityStack object.

Use your push() method to add Order objects into the stack. Go ahead and hard code the Order objects -- you don't need to use a Scanner to get user input.

While loading your Order objects onto the stack, demonstrate your peek(), isEmpty(), isFull(), and numberOfOrders() methods.

After the stack is full, sort it with your prioritySort() method. You may use any sort algorithm that you like.

Then pop() and display your Order objects from the stack. Make sure you display the information about each object as you pop them.

The concept here is that you load your stack and then sort it by priority. Then, when you begin processing the orders, the highest priority items are immediately available at the top of the stack.

Explanation / Answer

import java.io.*;

class Order
{
String orderCode;
int priority;

//parameter constuctor
Order(String myCode, int myPriority)
{
this.orderCode = myCode;
this.priority = myPriority;   
}
//getter methods
public String getorderCode ()
{
return this.orderCode;
}   
public int getpriority ()
{
return this.priority;
}   
public String toString()
{//overriding the toString() method
return "Order code : "+orderCode+" --- Priority : "+priority;
}
}

class PriorityStack
{
private int maxSize;
private Order[] stackArray;
private int top;
public PriorityStack(int s) // constructor
{
maxSize = s;
stackArray = new Order[maxSize];
top = -1;
}
public void push(Order j) // put item on top of stack
{
stackArray[++top] = new Order(j.getorderCode(), j.getpriority());
}
public Order pop() // take item from top of stack
{
return stackArray[top--];
}
public Order peek() // peek at top of stack
{
return stackArray[top];
}
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
public boolean isFull() // true if stack is Full
{
return (top+1 == maxSize);
}
public int size() // return size
{
return top + 1;
}
public Order peekN(int n) // return item at index n
{
return stackArray[n];
}
public void displayStack()
{
System.out.print(" Stack (bottom-->top): ");
for (int j = 0; j < size(); j++)
System.out.println(peekN(j).toString());
}
public int numberOfOrders()
{
int n=0;
for (int j = 0; j < size(); j++)
if(((stackArray[j].getorderCode())!=" ") && ((stackArray[j].getorderCode())!=null))
n++;
return n;
}
public PriorityStack prioritySort()
{
PriorityStack tmpStack = new PriorityStack(5);
while(!this.isEmpty())
{
Order tmp = this.pop();
while(!tmpStack.isEmpty() && tmpStack.peek().getpriority() < tmp.getpriority())
this.push(tmpStack.pop());
tmpStack.push(tmp);
}
return tmpStack;
}
}

class PriorityStackDriver
{
public static void main (String args[])
{
Order in1 = new Order("hello",1);
Order in2 = new Order("are",2);
Order in3 = new Order("how",1);
Order in4 = new Order("today",3);
Order in5 = new Order("you",2);

PriorityStack myStack = new PriorityStack(5);
myStack.push(in1);
myStack.push(in2);
myStack.push(in3);
myStack.push(in4);
myStack.push(in5);
System.out.print(" Original purchase order stack has following...");
myStack.displayStack();

if(myStack.isFull())
myStack = myStack.prioritySort();
System.out.print(" Sorted purchase order stack has following...");
myStack.displayStack();
  
System.out.println("");
for(int i=0;i<5;i++)
System.out.println(" Poping... " + myStack.pop().toString());
  
  

}
}

/*
Sample Output:

Original purchase order stack has following...
Stack (bottom-->top):
Order code : hello --- Priority : 1
Order code : are --- Priority : 2
Order code : how --- Priority : 1
Order code : today --- Priority : 3
Order code : you --- Priority : 2

Sorted purchase order stack has following...
Stack (bottom-->top):
Order code : today --- Priority : 3
Order code : you --- Priority : 2
Order code : are --- Priority : 2
Order code : how --- Priority : 1
Order code : hello --- Priority : 1

Poping...
Order code : hello --- Priority : 1
Poping...
Order code : how --- Priority : 1
Poping...
Order code : are --- Priority : 2
Poping...
Order code : you --- Priority : 2
Poping...
Order code : today --- Priority : 3
*/

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