This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Interfaces Lab

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.

  1. Implement the constructor. It should be only one line of code.
  2. In Aisle.java, make the line attribute visible to SortedAisle. (Hint: Don’t make line private to Aisle. But don’t make it public either.
  3. In SortedAisle.java, override the addCustomer method so that every time a new customer is added, the line of customers is sorted.
  4. 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 implement Comparable.

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 SortedAisles instead of LimitedAisles. (Simply find and replace LimitedAisle with SortedAisle.)

  • Notice the compiler error when calling getLeftStore. That method was added to LimitedAisle to get the number of customers who left the store. SortedAisle doesn’t have such a method.
  • Implement a new getLeftStore method in Aisle that simply returns 0. (By default, no customers leave the store.) SortedAisle will inherit that method, and LimitedAisle 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 .