Sophie, Sally and Jack have decided to open a used car lot. The first thing they'll need
Question:
Sophie, Sally and Jack have decided to open a used car lot. The first thing they'll need is a Car class.
Write a class Car. The purpose of a Car object is to represent a single automobile in an inventory of a used car lot. The Car class has the following specification:
- instance variable of type String for the car's identifier
- instance variable of type int for the car's total miles
- instance variable of type doublefor the car's miles per gallon (we'll consider only planet-killing fossil fuel consuming evil cars for this exercise)
- instance variable of type int for the cost to Sophie, Sally and Jack to acquire the car
- instance variable of type int for the price Sophie, Sally and Jack will sell the car
- instance variable of type boolean indicating if the car has been sold or not
Three Constructors
- a no-argument constructor
- a constructor that takes a single String parameter representing the car's identifier
- a constructor that takes five parameters, one for the identifier, a second parameter for the car's initial mileage, and a third parameter for the car's Miles per Gallon, a fourth for the car's cost and the fifth for the car's price.
Accessors
getters for all instance variables:
- getCost
- getIdentifier
- getMilage
- getMPG
- getPrice
- isSold (note that for boolean values, we usually use boolean isSold() instead of boolean getSold())
- int getProfit(this method returns the value price - cost if the car is sold, and 0 for unsold cars).
- int compareMPG(Car otherCar)returns
- a negative value (exact value doesn't matter--only that it be less than zero) if the current car gets fewer miles per gallon than otherCar
- a positive number (exact value doesn't matter--only that it be greater than zero) if the current car gets more miles per gallon than otherCar
- 0 if the two cars have exactly the same MPG (given that MPG is a double number, this will probably occur relatively infrequently)
- int compareMiles(Car otherCar) return
- a negative value (exact value doesn't matter--only that it be less than zero) if the current car has fewer miles than otherCar
- a positive number (exact value doesn't matter--only that it be greater than zero) if the current car has more miles than otherCar
- and 0 if the two cars have exactly the same miles
int comparePrice(Car otherCar)returns
- a negative value (exact value doesn't matter--only that it be less than zero) if the current car's price is less than the price of otherCar
- a positive number (exact value doesn't matter--only that it be greater than zero) if the current car's price is more than the price ofotherCar
- 0 if the two cars have exactly the same price
- toString that returns something similar to "Car id=Ford1950, milage=63000, mpg=13.0, cost=60, price=210, is not sold" or "Car id=Ford1950, milage=63000, mpg=13.0, cost=60, price=210, is sold, profit=150"
Mutators
setters for all instance variables
- setCost
- setIdentifier
- setMiles
- setMPG
- setPrice
- setSold
- void addMiles(int milesToAdd)adds the given value to the car's miles value
Tester
Wri-te a class with a main method to test Car. Do not put your main method in the Car class. Your tester should crea-te at least two Car objects, set different values for the Identifier, Cost, Miles, etc. The tester should especially that the compare methods, getProfit and addMilesmethods work as expected.
Grading Elements
- class name, all constructors, all method names, all method signatures, visibility and return types are correct
- constructors, getters and setters perform correct operation, return correct values
- profit calculated correctly
- accessors do not modify object state
- addMiles performs correct operation
- compareMPG, compareId, comparePrice, getProfitreturn proper values
- separate tester class with main method tests at least two Cars, tests compare methods, addMiles, getProfit
************************
The Car Lot Project Step 2:
The CarLot Class
In step 1 of the project, you created a class to represent a Car that is for sale. In step 2 of the project, you will crea-te a CarLot class that manages all of the vehicles that are for sale. The collection of vehicles that are for sale represent the CarLot's inventory.
Note: The CarLot class requires successfully implementing the Car class developed in Step 1 of the project.
The CarLot Class
Define a class CarLot. Your CarLot will have a single attribute, inventory, of the typ-e ArrayList. Provide default accessor and mutator methods for the inventory attribute.
Accessors
Add the following accessor methods:
- Car findCarByIdentifier(String identifier). Find the car with the specified identifer in the inventory. Return null if the Car is not found
- ArrayList getCarsInOrderOfEntry(). Return an ArrayList of all Cars in the inventory, ordered by their entry into the inventory. This method should return a copy of the inventory, not the inventory itself
- ArrayList getCardsSortedByMPG(). Return an ArrayList of all Cars in the inventory, sorted by MPG. This method should not sort the inventory, but should instead make a copy of the inventory and sort the copy
- Car getCarWithBestMPG(). Return the Car in the inventory with the highest MPG
- Car getCarWithHighestMileage(). Return the Car in the inventory with the highest mileage
- double getAverageMpg(). Return the average MPG of all Cars in the inventory
- double getTotalProfit(). Return the total profit of all cars in the inventory that have been sold
Mutators
Add the following mutator methods:
- void addCar(String id, int mileage, int mpg, double cost, double salesPrice). Add a new Car with the specified id, mileage, mpg, cost, and salesPrice to the inventory
- void sellCar(String identifier, double priceSold ) throws IllegalArgumentException. Sell the Car identified by identirier for the priceSold. If the Car does not exist in the identifier, throw an IllegalArgument Exception with an appropriate error message
JavaDoc
The JavaDoc supplied in Step 1 of the project also contains documentation for the CarLot class. Your implementation of the CarLot class should match the JavaDoc exactly.
To view the JavaDoc, you must extract all of the files from the zip archive. After extracting the files, click on the index.html file to view the JavaDoc.
CarLotTesterClass
Wri-te a tester class CarLotTester that verifies that your CarLot class is working properly. CarLotTester should contain a main method. Specifically, your tester class should:
- Test that the addCar() and sellCar() methods work as expected
- Verify that the accessor methods work as expected by creating a CarLot with an inventory of at least 2 Cars. Your code should call the appropriate accessor and if the correct Car is not returned, print an error message on the Console
Hints
- If you did not complete successfully complete all parts of the Car class from Step 1, finish that before starting Step 2
- In Step 1 of the project, you implement several "compareTo" methods. Use those methods when implementing some of the accessor methods
- The getCarsSortedByMPG() will require sorting the ArrayList. Modify the selection sort from Unit 7 to work with Cars and use the appropriate "compareTo" from Step 1
Grading Elements
- The submission contains Car, CarLot, and CarLotTester. CarTester is ideally provided but is not required
- CarLot contains a single ArrayList attribute named inventory
- All CarLot accessor and mutator methods are implemented exactly as defined in the JavaDoc
- CarLotTester runs successfully
- CarLotTester prints an error message in the event a test fails