Interfaces Lab
Categories:
3 minute read
Introduction
Your goal for this assignment is to modify the Bob’s simulation in two ways: by adding a SortedAisle class, and by making it possible for the user to select the aisle type at run time.
Instructions
Interface
Download the following files:
Create a new class SortedAisle
that extends Aisle. Your new class should override the addCustomer
method so that every time a new customer is added, the entire list of customers is sorted. This way, the customer with the fewest items is always moved to the front. This type of aisle will model the effect of friendly customers that let other customers “cut” if they have fewer items.
- Implement the constructor. It should be only one line of code.
- In
Aisle.java
, make theline
attribute visible to SortedAisle. (Hint: Don’t makeline
private to Aisle. But don’t make it public either. - In
SortedAisle.java
, override theaddCustomer
method so that every time a new customer is added, the line of customers is sorted. - Use the Collections.sort method to sort your ArrayList. Since this method only works when the items in the list implement the Comparable interface, you will also need to modify the
Customer
class to implementComparable
.
Comparable
is a generic interface. This means you can use it like this:
public class MyClass implements Comparable<MyClass>
or like this (not recommended):
public class MyClass implements Comparable
In the former class your compareTo
method must take an argument of type MyClass
in the latter it must take an argument of type Object
.
Once you have completed your SortedAisle
class modify MartSimulation
so that it uses SortedAisle
s instead of LimitedAisle
s. (Simply find and replace LimitedAisle with SortedAisle.)
- Notice the compiler error when calling
getLeftStore
. That method was added toLimitedAisle
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.
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.
Polymporphism
It is unfortunate that the current version of the simulation requires the code to be modified and recompiled when we want to change the aisle type used in the simulation. Modify the file MartDriver
and MartSimulation
classes to allow the user to select any of the three aisle types at run time. Execute the simulation several times to see the effect of changing aisle types.
Submit Customer.java
, MartDriver.java
, and SortedAisle.java
to Gradescope .