Lab 8: Bob's SortedAisle¶
The goal of this lab is to modify the simulation for Bob's Grocery Mart in two ways: (1) add a SortedAisle
class, and (2) allow the user to select the aisle type at run time.
Background¶
The Java Library includes many algorithms for manipulating data. Some of these algorithms are implemented in utility classes including:
- java.util.Arrays – manipulate data in an array
- java.util.Collections – manipulate data in a collection (such as an
ArrayList
)
Arrays Example¶
The following code sorts an array of random numbers using the Arrays.sort()
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Comparable<T>¶
In today's lab, you will use Collections.sort()
to sort the Customer
objects in an ArrayList
.
For this to work, the Customer
class must implement the Comparable interface:
public class Customer implements Comparable<Customer>
In other words, a Customer
is comparable to another Customer
.
The Collections.sort()
will call the compareTo()
method that you implement for the Customer
class.
Instructions¶
-
Create a
src/labs/lab08
folder for today's lab. -
Download the following files into the project:
- Aisle.java – same as before (except for
package
) - Customer.java – same as before (except for
package
) - LimitedAisle.java – same as before (except for
package
) - MartSimulation.java – now using
ArrayList
- MartDriver.java – longer
main()
method
- Aisle.java – same as before (except for
-
Create a new class
SortedAisle
that extendsAisle
. This type of aisle will model the effect of friendly customers that let other customers "cut in line" if they have fewer items. (So customers with fewer items should come before others in the line!)- Implement the constructor, which should be only one line of code.
- In Aisle.java, make the
line
attribute visible toSortedAisle
. (Hint: Don't make the attributeprivate
. But don't make the attributepublic
either.) - In SortedAisle.java, override the
addCustomer()
method so that every time a new customer is added, the line of customers is sorted. Use theCollections.sort()
method to sort yourArrayList
. - Since the sort method works only when the items in the list implement the
Comparable
interface, you will need to modify theCustomer
class to implementComparable
.- What does it mean to compare two Customers? Which customer should come before another? Why?
-
Modify
MartDriver
to useSortedAisle
instead ofLimitedAisle
. (In thefor
loop, replacenew LimitedAisle
withnew SortedAisle
.)- Notice the run-time error in
MartSimulation.getTotalLeft()
. In the previous lab, thegetLeftStore()
method was added toLimitedIsle
to get the number of customers who left the store.SortedAisle
doesn't have such a method. - Implement a new
getLeftStore()
method inAisle
that simply returns 0. (By default, no customers leave the store.)SortedAisle
will inherit that method, andLimitedAisle
will override it. - Remove the typecast from the
getTotalLeft()
method inMartSimulation
. Now thatAisle
definesgetLeftStore()
, thegetTotalLeft()
method should compile without the typecast.
- Notice the run-time error in
-
Run the simulation a few times, and confirm that your code is working.
- Using
SortedAisle
should result in relatively short average waits, but very long longest waits. - People with lots of items will tend to get stuck at the end of the line while others continually cut in front of them.
- Using
-
The current version of
MartDriver
requires the code to be modified and recompiled when we want to change the aisle type used in the simulation.- Uncomment the code in
main()
to allow the user to select any of the three aisle types at run-time. - Inside the loop, write an if statement to create the type of aisle selected by the user.
- Then run the simulation several times to see the effect of changing aisle types.
- Uncomment the code in
Submission¶
Submit Customer
, MartDriver
, and SortedAisle
to Gradescope.