Write a class called Car that models the amount of gas required by the car to dr
ID: 3782251 • Letter: W
Question
Write a class called Car that models the amount of gas required by the car to drive a certain distance. The attributes of the Car object are: • tank capacity (in liters) (a double) • amount of gas in tank (in liters) (a double) • fuel consumption rate (in liters/km) ( a double)
FULL QUESTION IN IMAGES BELOW
---------------
Exercise 5: [10 points] Write a class called Car that models the amount of gas required by the car to drive a certain distance. The attributes of the Car object are: tank capacity (in liters (a double) amount of gas in tank (in liters (a double) fuel consumption rate (in liters km a double) It has the following methods: a constructor public Car(double c, double f that sets the tank capacity to c, fuel consumption to f. It should also set the amount of gas in tank to 0 Get and set methods for all the attributes.Explanation / Answer
// Car.java
public class Car {
//constructor
public Car(double c, double f) {
this.tankCapacity = c;
this.fuelConsumtionRate = f;
this.gas = 0;
}
// required private fields
private double tankCapacity;
private double gas;
private double fuelConsumtionRate;
// method to fill gas in tank
public void fill(double g)
{
if ((g + getGas()) > getTankCapacity())
{
System.out.println("Cannot fill. Exceeds capacity.");
}
else
{
setGas(g+getGas());
}
}
// method to drive
public void drive(double d)
{
// computing gas required to drive d distance.
double requiredGas = d*getFuelConsumtionRate();
if (requiredGas > getGas())
{
System.out.println("Cannot drive. Not enough gas.");
}
else
{
setGas(getGas() - requiredGas);
}
}
//getters and setters
public double getTankCapacity() {
return tankCapacity;
}
public void setTankCapacity(double tankCapacity) {
this.tankCapacity = tankCapacity;
}
public double getGas() {
return gas;
}
public void setGas(double gas) {
this.gas = gas;
}
public double getFuelConsumtionRate() {
return fuelConsumtionRate;
}
public void setFuelConsumtionRate(double fuelConsumtionRate) {
this.fuelConsumtionRate = fuelConsumtionRate;
}
}
// CarTest.java
import java.util.*;
public class CarTest {
public static void main(String[] args) {
// scanner is used to get user input
Scanner sc = new Scanner(System.in);
System.out.print("Enter the capacity (in litres): ");
double capacity = sc.nextDouble();
System.out.print("Enter the fuel consumption rate (in lt/km): ");
double rate = sc.nextDouble();
Car c = new Car(capacity, rate);
System.out.print("Enter the amount of gas to fill: ");
double gas = sc.nextDouble();
System.out.print("Enter the disatnce to drive: ");
double d = sc.nextDouble();
c.fill(gas);
c.drive(d);
System.out.println();
System.out.println("Gas remaining in tank: " + c.getGas());
}
}
/*
Sample run
Enter the capacity (in litres): 20
Enter the fuel consumption rate (in lt/km): 0.1
Enter the amount of gas to fill: 30.0
Enter the disatnce to drive: 100.0
Cannot fill. Exceeds capacity.
Cannot drive. Not enough gas.
Gas remaining in tank: 0.0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.