Skip to content

Lab 12: Bob’s SortedAisle

The goal of this lab is to modify the most recent version of Bob’s Grocery Mart in two ways:

  1. Add a SortedAisle class that implements an interface.
  2. Allow the user to select the desired 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:

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
20
21
22
23
24
25
26
27
28
29
30
31
32
package lab12;

import java.util.Arrays;
import java.util.Random;

/**
 * Example program that uses Arrays utility class.
 *
 * @author CS159 Faculty
 * @version 03/25/2024
 */
public class Demo {

    /**
     * Create and sort an array of 100 random integers.
     *
     * @param args command-line arguments
     */
    public static void main(String[] args) {
        Random rand = new Random();
        int[] nums = new int[10];

        for (int i = 0; i < nums.length; i++) {
            nums[i] = rand.nextInt(100);
        }
        System.out.println("Random: " + Arrays.toString(nums));

        Arrays.sort(nums);
        System.out.println("Sorted: " + Arrays.toString(nums));
    }

}

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

  1. Create a CS159/labs/lab12 folder for today’s lab.

  2. Download the following files into the project:

  3. Create a new class SortedAisle that extends Aisle. 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 to SortedAisle. (Hint: Don’t make the attribute private. But don’t make the attribute public either.)
    • In SortedAisle.java, override the addCustomer() 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 the sort method works only when the items in the list implement the Comparable interface, you will need to modify the Customer class to implement Comparable.
      • What does it mean to compare two Customers? Which customer should come before another? Why?
  4. Modify MartDriver to use SortedAisle instead of LimitedAisle. (In the for loop, replace new LimitedAisle with new SortedAisle.)

    • Notice the run-time error in MartSimulation.getTotalLeft(). In the previous lab, the getLeftStore() method was added to LimitedIsle 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.
    • Remove the typecast from the getTotalLeft() method in MartSimulation. Now that Aisle defines getLeftStore(), the getTotalLeft() method should compile without the typecast.
  5. 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.
  6. 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.

Submission

Submit Customer, MartDriver, and SortedAisle to Gradescope.