Car class in Java 2 private instance variables miles per gallon gallons of gas i
ID: 3667871 • Letter: C
Question
Car class in Java
2 private instance variables
miles per gallon
gallons of gas in tank (initially equals 3.5)
4 public methods
constructor() method
- initializes all instance variables to required starting values
- takes one parameter (miles per gallon) from carTester
addGas() method
- increases the amount of gas
- takes one parameter from carTester, the amount of gas to add to the tank
- update instance variable
drive() method
- decreases the amount of gas in the tank
- takes one parameter from carTester, the distance in miles that was driven
- update instance variable
range() method
- calculates range, the number of miles the car can travel until the gas is empty
- no parameters
- does not update instance variables
- returns the calculated value to carTester
carTester:
public class CarTester
{
/**
* main() method
*/
public static void main(String[] args)
{
Car honda = new Car(30.0); // 30 miles per gallon
honda.addGas(9.0); // add 9 more gallons
honda.drive(210.0); // drive 210 miles
// print range remaining
System.out.println("Honda range remaining: " + honda.range());
Car toyota = new Car(26.0); // 26 miles per gallon
toyota.addGas(4.5); // add 4.5 more gallons
toyota.drive(150.0); // drive 150 miles
// print range remaining
System.out.println("Toyota range remaining: " + toyota.range());
}
}
Explanation / Answer
//Car.java
public class Car
{
//private instance variables
private double milesPergallon;
private double gas;
//Set default values for milesPergallon
//and gas
public Car()
{
milesPergallon=0;
//set gas to 3.5
gas=3.5;
}
//Parameter constructor to set milespergallon
public Car(double milesPergallon)
{
this.milesPergallon=milesPergallon;
gas=3.5;
}
//Add gas to instance variable
public void addGas(double gas)
{
this.gas=this.gas+gas;
}
//calculate the remainging gas
public void drive(double miles)
{
//Calculate the remainingmiles
double remainingMiles
=gas*milesPergallon-miles;
//calculate gas in terms remaiingmiles
gas=remainingMiles%milesPergallon;
}
//calculatea the remainingmiles before car gas is empty
public double range()
{
double remainingMiles
=gas*milesPergallon;
return remainingMiles;
}
}//end of the Car
-----------------------------------------------------------------------------------------------------------------------------
/**The java program CarTester that creats two instances of Car
* with miles per gallon , and calls addGas and then calls drive
* method then calls the range method to print the number of
* miles the car can move before the gas is empty.*/
//CarTester.java
public class CarTester
{
/**
* main() method
*/
public static void main(String[] args)
{
Car honda = new Car(30.0); // 30 miles per gallon
honda.addGas(9.0); // add 9 more gallons
honda.drive(210.0); // drive 210 miles
// print range remaining
System.out.println("Honda range remaining: " + honda.range());
Car toyota = new Car(26.0); // 26 miles per gallon
toyota.addGas(4.5); // add 4.5 more gallons
toyota.drive(150.0); // drive 150 miles
// print range remaining
System.out.println("Toyota range remaining: " + toyota.range());
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Honda range remaining: 450.0
Toyota range remaining: 156.0