Question: java code:: Car We're going to work with a motor pool in the next few lessons, and we'll start with a Car class. Write a
java code:: Car
We're going to work with a motor pool in the next few lessons, and we'll start with aCarclass.
Write a classCar. The purpose of aCarobject is to represent a single automobile in a motor pool, such as a taxi service. TheCarclass has the following specification:
instance variable of typeStringfor the car's identifier
instance variable of typeintfor the car's total miles
instance variable of typeintfor the car's total fuel consumption (we'll consider only planet-killing fossil fuel consuming evil cars for this exercise)
three constructors
a no-argument constructor
a constructor that takes a singleStringparameter representing the car's identifier
a constructor that takes three parameters, one for the identifier, a second parameter for the car's initial mileage, and a third parameter for the car's initial fuel consumption
accessors
getters for all instance variables:
getMiles
getFuelUsed
getIdentifier
double getMPG()that returns the current lifetime MPG for the car
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 thanotherCar
a positive number (exact value doesn't matter--only that it be greater than zero) if the current car gets more miles per gallon thanotherCar
0 if the two cars have exactly the same MPG (given that MPG is a double number, this will probably occur relatively infrequently)
toStringthat returns something similar to "Car [identifier=Ford1950, miles=160000, fuelUsed=16000]"
mutators
setters for all instance variables
setMiles
setFuelUsed
setIdentifier
void addFuel(int fuelAmount)
void addMiles(int milesTraveled)
Tester
Write a classCarTesterhaving just amainmethod that
creates 3Carobjects (let's call themcar1andcar2aandcar2b)
eachCarobject should be created with a different constructor
each method should be tested at least once with one car
addFuelandaddMilesshould be used forcar2aso that is gets the same miles per gallon ascar2b
to make sure that all the possible relations of two car MPGs are calculated correctly, testcompareMPGfor
(car1, car2a),
(car2b, car1)
(car2a, car2b)
Grading Elements
class name, all constructors, all method names, all method signatures and return types are correct
constructors, getters and setters perform correct operation, return correct values
MPG calculated correctly
accessors do not modify object state
addFuelUsed, addMiles perform correct operation
compareMPG returns proper values
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
