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 a Car class.
Write a class Car. The purpose of a Car object is to represent a single automobile in a motor pool, such as a taxi service. 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 int for 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 single String parameter 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 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)
toString that 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 class CarTester having just a main method that
creates 3 Car objects (let's call them car1 and car2a and car2b)
each Car object should be created with a different constructor
each method should be tested at least once with one car
addFuel and addMiles should be used for car2a so that is gets the same miles per gallon as car2b
to make sure that all the possible relations of two car MPGs are calculated correctly, test compareMPG for
(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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started