Labs
We will be changing ParkingLot to improve encapsulation and implement some best practices we have learned here.
- Copy the
CarandParkingLotclasses fromcom.example.objs2.referenceintocom.example.objs2.labs; you must create this package if you have not already. - One of the fields in
ParkingLotshould be constant. Change it. - Add a four-parameter
Carconstructor to theCarclass that includes all fields butpurchasePrice. It should call the five-parameter constructor and pass0.0as the extra parameter. - Remove the no-arg constructor from
Car. - Build a
CarDealershipclass. This class will have to interact with aParkingLotinstance, so it will have a field for one of these. Use an instance initializer to assign this field aParkingLotobject.
Its public interface is as follows.
* void addCarToInventory(Car car) - adds a car to the car inventory (the parking lot) using the parking lot's method.
* Car[] findCarsByModel(String model) - returns an array of cars whose model matches the input model. It must look at each of the parking lot's cars.
This method must return an array of the correct size, so you may want to determine the number of cars with the matching model first.
Car[] getAllCars()- return an array of all cars in the inventory (from theParkingLot).
(Should this class return references to the ParkingLot's array, or a different array?)
- (Optional) Write a
CarDealershipTesterclass. It will test all the methods to see that they work as expected.
For example, it would add several Cars, then get all cars to see if they were added correctly. Then it could find cars by model and compare the resulting array to what was expected.
- Note that this is not a JUnit test. It is a class with a
mainmethod.