Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Apr 12: Interfaces Lab CS 159: Advanced Programming James Madison University, Spring 2022 Apr 12: Interfaces Lab Lesson Outline 10 min PA5 Q&A * UML Diagrams * Rubric for Part A * Stub out the code! 15 min Mini Lecture * Lesson: Algorithms (sorting, searching, ...) * Java utility classes: Arrays and Collections * Example: Algorithms.java and Card.java 50 min Sorted Aisles * See instructions below; submit by 11:00pm Before Thursday Homework: Keep working on Project 5 * At a minimum, run with flowers.txt Textbook: Read the following sections: 10.3 The Comparable Interface 10.4 Using Interfaces for Callbacks Sorted Aisles in Bob's Grocery Mart Your goal for this lab is to modify your previous simulation of Bob's Grocery Mart in two ways: by adding a SortedAisle class, and by making it possible for the user to select the aisle type at run time. Part 1: Interfaces Download the following files: Aisle.java -- same as before Customer.java -- same as before LimitedAisle.java -- same as before MartSimulation.java -- now using ArrayList MartDriver.java -- longer main method 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. After sorting the line, the customer with the fewest items is always moved to the front. Implement the constructor. It should be only one line of code. 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.) 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 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. That means you can use it like this: public class MyClass implements Comparable or like this (not recommended): public class MyClass implements Comparable In the former example, your compareTo method must take an argument of type MyClass. In the latter example, it must take an argument of type Object. (So make your Customer comparable to other Customers.) Once you have completed your SortedAisle class, modify MartSimulation so that it uses SortedAisle instead of LimitedAisle. (Simply find and replace LimitedAisle with SortedAisle.) Notice the compiler error when calling getLeftStore(). That 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. 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. Part 2: Polymporphism It's 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 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 only Customer.java, MartDriver.java, and SortedAisle.java to Gradescope. Make sure you pass all the autograder tests. You may resubmit if you get less than 10/10 points.