BIG JAVA P8.6 Implement a class Car with the following properties. A car has a c
ID: 3670567 • Letter: B
Question
BIG JAVA P8.6 Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Supply a function drive that simulates driving the car for a certain distance, reducing the fuel level in the gas tank, and functions get_gas, to return the current fuel level, and add_gas, to tank up. Sample usage: Car my_beemer(29); // 29 miles per gallon my_beemer.add_gas(20); // Tank 20 gallons my_beemer.drive(100); // Drive 100 miles cout << my_beemer.get_gas() << " "; // Print fuel remaining You will implement TWO public classes: the Car class, and the CarTester class. The CarTester class contains the main method. Thanks for the help!
Explanation / Answer
Program plan:Here we need to implement the Car class and CarTester class which has the following methods.
void drive(float distance):drive is the method in Car class.it accepts the distance as the parameter which is used to reduce the fuel level in the car by simple calculation.fuel can be reduced based on the distance travel by the car and mileage or fuel efficiency of the car.
float get_gas():method in the car class returns the current fuel level in the car.
void add_gas(float extrafuel):it accepts the extrafuel argument add it to the fuel level in the car.
CarTester class contains the main method where the car class object can be created and all methods can be tested.
Program:
//importing packages
import java.lang.*;
class Car
{
//defining the class members
float mileage; //nothing but fuel efficiency can be measured in miles/gallon or litre/km
float fuel=0;//fuel level in the car initially assigned to 0
Car(float efficiency) /*Car constructor which accepts the efficiency as the argument and increases the fuel level in the car*/
{
mileage=efficiency;
}
void add_gas(float extrafuel) //methods which adds fuel to the car
{
fuel+=extrafuel;
}
float get_gas()//method returns the fuel level in the car
{
return fuel;
}
void drive(float distance)
{
fuel-=distance/mileage;
/*distance/mileage gives the fuel consumed by travelling the specified distance hence it is subtracted from current fuel level*/
}
}//Car class completed
//CarTester class definition
class CarTester
{
public static void main(String arg[])
{
Car my_beemer=new car(29);
//creating the car class object.the cnstructor implicitly called and assign the fuel value to 29
System.out.println("Fuel level in the car is "+my_beemer.get_gas());
my_beemer.add_gas(20);//which increases the fuel level by 20
System.out.println("Fuel level in the car is "+my_beemer.get_gas());
my_beemer.drive(100); //calculates the fuel level after travelling 100km
System.out.println("Current fuel level in the car is "+my_beemer.get_gas());
}
}
Sample output:
Fuel level in the car is 0
Fuel level in the car is 20
Current fuel level in the car is 16.5517
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.