Question
This class is a type of SimpleQueue specifically designed to order a group of people (it extends SimpleQueue ) and it requires some additional features.
This class is a type of SimpleQueue specifically designed to order a group of people (it extends SimpleQueue
- Grocery lines have ID numbers. Supporting methods:
- GroceryLine (int id)-Constructor for a grocery line with a given ID.
- int getId()-Returns the ID of the grocery line.
- People are in the line and those people have items to be checked out. Additionally, a line can be compared to another line based on the number of items which have not been checked out. Supporting methods:
- Int itemsInLine()-Sums up all items for all people in the line. For example, if there are two people in line, one with 16 items and one with 54 items, this method will return 70.
- Int compareTo (GroceryLine otherLine)-Compares one grocery line to another based on the number of items each line. If the two lines are tied, it compares them by their id.
- Clerks can process items from the front of the line. Supporting method:
- Void processItem()-removes one item from the first person in the line. If the person at the front has no more items, they are removed from the line.
- Lines can be represented as a string. Supporting method:
- String toString()-Converts the line to a string in the following format:
[id]: Person([#]) = [#] shopper(s) with [#] item(s)
import java.util.Queue;
//A data structure (based on a queue) to represent a grocery
//store line.
//Grocery lines can only contain people and can be compared
//to eachother based on the number of items left to process
//in the line. The lines also have id numbers.
class GroceryLine extends SimpleQueue
//Creates a grocery store line with a given ID.
public GroceryLine(int id) {
//O(1)
}
//Returns the ID of the grocery line.
public int getId() {
//O(1)
return -1;
}
//Sums up all items for all people in the line.
public int itemsInLine() {
//O(n), where n = the number of people in line
return -1;
}
//Compare one grocery line to another based on
//the number of items in the line and then, if
//the two lines are tied, by their id.
public int compareTo(GroceryLine otherLine) {
//O(n+m), where n = the number of people in the
//first line and m = the number of people in the
//second line
return 0;
}
//Processes (removes) one item from the first
//person in line. If the person has no more
//items they are removed from the line.
public void processItem() {
//O(1)
}
//Converts the line to a string.
public String toString() {
//O(n), where n = the number of people in line
return null;
}
//-------------------------------------------------------------
// Main Method For Your Testing -- Edit all you want
//-------------------------------------------------------------
public static void main(String[] args) {
GroceryLine line = new GroceryLine(0);
Person mason = new Person(10);
Person george = new Person(20);
line.offer(mason);
line.offer(george);
if (line.getId()==0 && line.itemsInLine() == 30){
System.out.println("Yay 1");
}
line.processItem();
if (line.itemsInLine() == 29 && line.peek().getNumItems()==9){
System.out.println("Yay 2");
}
GroceryLine line2 = new GroceryLine(1);
Person washington = new Person(40);
line2.offer(washington);
if (line.compareTo(line2)<0){
System.out.println("Yay 3");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started