We\'re going to work with a motor pool in the next few lessons, and we\'ll start
ID: 3600730 • Letter: W
Question
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)
Please do this program in eclipse and put the code at the end so I can copy it into my Java IDE easily.
Explanation / Answer
/* Car Class*/
public class Car {
private String identifier;
private int miles;
private int fuelUsed;
public Car(String identifier, int miles, int fuelUsed) {
super();
this.identifier = identifier;
this.miles = miles;
this.fuelUsed = fuelUsed;
}
public Car(String identifier) {
super();
this.identifier = identifier;
}
public Car() {
super();
}
public String getIdentifier() {
return identifier;
}
public int getMiles() {
return miles;
}
public int getFuelUsed() {
return fuelUsed;
}
public double getMPG() {
double currentMPG = 0;
double fuelUsedInGallon=this.getFuelUsed()*3.78541;
currentMPG=(double) this.getMiles()/fuelUsedInGallon;
return currentMPG;
}
public int compareMPG(Car otherCar){
if(this.getMPG()<otherCar.getMPG()){
return -1;
}else if(this.getMPG()>otherCar.getMPG()){
return 1;
}
return 0;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public void setMiles(int miles) {
this.miles = miles;
}
public void setFuelUsed(int fuelUsed) {
this.fuelUsed = fuelUsed;
}
public String toString(){
return "Car [identifier="+this.getIdentifier()+",miles="+this.getMiles()+",fuelUsed="+this.getFuelUsed()+"]";
}
public void addFuel(int fuelAmount){
this.setFuelUsed(this.getFuelUsed()+fuelAmount);
}
public void addMiles(int milesTraveled) {
this.setMiles(this.getMiles()+milesTraveled);
}
}
/*Class CarTester*/
public class CarTester {
public static void main(String[] args) {
Car car1=new Car();
Car car2a=new Car("Ferrari458");
Car car2b=new Car("Ford1950",160000,16000);
/* Test all setters*/
car1.setIdentifier("BMWi8");
car1.setFuelUsed(7000);
car1.setMiles(56000);
/* Test toString method*/
System.out.println(car2b.toString());
/*Test addFuel and addMiles*/
car2a.addFuel(16000);
car2a.addMiles(160000);
/*Test getters*/
System.out.println(car2a.getIdentifier());
System.out.println(car2a.getMiles());
System.out.println(car2a.getFuelUsed());
System.out.println(car2b.getMiles());
System.out.println(car2b.getFuelUsed());
/*Test compareMPG (car1, car2a)*/
System.out.println(car1.compareMPG(car2a));
/*Test compareMPG (car2b, car1)*/
System.out.println(car2b.compareMPG(car1));
/*Test compareMPG (car2a, car2b)*/
System.out.println(car2a.compareMPG(car2b));;
}
}
If you find my code useful please give a thumbs-up and keep in touch with chegg.Thank you :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.