In: Computer Science
Part 1: Create a Car class in accordance with the following specifications.
Some of the other fields:
The Car class must implement the following methods.
public fillTank(double): double
public toString():String
public equals(Car):boolean
public driveCar():boolean
public getTripOdometer():double
public clearTripOdometer():void
public getOdometer():double
public getFuelLevel():double
public getFuelTankSize():double
public setUpTrip(double, double): void
/**
* Car Class Implementation
*/
public class Car {
private String make;
private String model;
private String color;
private int year;
private double tankSize;
private double fuelEconomy;
private double optimalSpeed;
private double fuelLevel;
private double odometer;
private double tripOdometer;
//default constructor
public Car() {
make="";
model="";
color="";
year=9999;
tankSize=0.0;
fuelEconomy=0.0;
optimalSpeed=0.0;
}
//parameterised constructor
public Car(String make, String model, String color,
int year, double tankSize, double fuelEconomy,
double
optimalSpeed) {
super();
this.make = make;
this.model = model;
this.color = color;
this.year = year;
this.tankSize = tankSize;
this.fuelEconomy =
fuelEconomy;
this.optimalSpeed =
optimalSpeed;
}
//copy constructor
public Car(Car car) {
make = car.make;
model = car.model;
color = car.color;
year = car.year;
tankSize = car.tankSize;
fuelEconomy =
car.fuelEconomy;
optimalSpeed =
car.optimalSpeed;
}
//fill the tank will fuel
public double fillTank(double fuel) {
double remainingCapacity = tankSize
- fuelLevel;
if (fuel > remainingCapacity)
{
System.out.println("Current capacity of the tank is: " +
remainingCapacity);
System.out.println(remainingCapacity + " fuel is sufficient for the
tank to fill");
fuelLevel =
tankSize;
}
else if (fuel <
remainingCapacity){
System.out.println("Fuel is less than the tank current Capacity: "
+ remainingCapacity);
fuelLevel =
fuelLevel + fuel;
}
else if (fuel == remainingCapacity)
{
System.out.println("Tank is full with the fuel");
fuelLevel =
tankSize;
}
return fuelLevel;
}
@Override
public String toString() {
return "Car [make=" + make + ",
model=" + model + ", color=" + color + ", year=" + year + ",
tankSize="
+ tankSize + ", fuelEconomy=" + fuelEconomy + ",
optimalSpeed=" + optimalSpeed + ", fuelLevel="
+ fuelLevel + "]";
}
//compare the two car objects
public boolean equals(Car obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Car other = (Car) obj;
if (color == null) {
if (other.color
!= null)
return false;
} else if
(!color.equals(other.color))
return
false;
if
(Double.doubleToLongBits(fuelEconomy) !=
Double.doubleToLongBits(other.fuelEconomy))
return
false;
if (make == null) {
if (other.make
!= null)
return false;
} else if
(!make.equals(other.make))
return
false;
if (model == null) {
if (other.model
!= null)
return false;
} else if
(!model.equals(other.model))
return
false;
if
(Double.doubleToLongBits(optimalSpeed) !=
Double.doubleToLongBits(other.optimalSpeed))
return
false;
if
(Double.doubleToLongBits(tankSize) !=
Double.doubleToLongBits(other.tankSize))
return
false;
if (year != other.year)
return
false;
return true;
}
//check if the car is started
public boolean driveCar() {
if (this.tripOdometer > 0)
{
return
true;
}
return false;
}
//return the tripOdometer
public double getTripOdometer() {
return this.tripOdometer;
}
//clearing the tripOdometer after the ride or before ride
public void clearTripOdometer() {
this.tripOdometer = 0.0;
}
//get the odometer reading
public double getOdometer() {
return this.odometer;
}
//get the fuel level
public double getFuelLevel() {
return this.fuelLevel;
}
//get the tank size
public double getFuelTankSize() {
return this.tankSize;
}
//start the trip
public void setUpTrip(double fuel, double
optimalSpeed) {
this.clearTripOdometer();
this.fillTank(fuel);
this.optimalSpeed =
optimalSpeed;
}
}