import java.util.*;
/*
*Car class implements the car functionalities like drive
*Add gas and get gas
* @author yourname
* @version 1.0
*/
class Car
{
private int gas; //Gas varaible
private final int MILES_PER_GALLON = 50; //Constant that indicates miles per a gallon
//Class constructor with gas parameter
public Car(int gas)
{
this.gas = gas;
}
//Drive method that takes distance as parameter
public void drive(int distance)
{
//Calculating gas required for given distance to be travelled
int gasReuired = distance/MILES_PER_GALLON;
//Check if enough gas is there in tank to drive
if(gas >= gasReuired)
{
//Subtract the gasrequired from gas
gas = gas - gasReuired;
//Print successful drive message
System.
out.
println("Successfully driven distance " + distance
+ " miles"); }
else
{
//Print unsuccessful drive message
System.
out.
println("There is not enough gas to drive " + distance
+ " miles"); }
}
//addGas method to add gas to the tank
public void addGas(int gas)
{
//Adding gas
this.gas = this.gas + gas;
//Print success message
System.
out.
println("Successfully added "+ gas
+" gallons of gas"); }
//This method gives the gas in the tank
public int getGasLevel()
{
return this.gas;
}
}
/*
Test class to test the Car class features
*/
class Test
{
public static void main
(String [] args
) {
Car myHybrid = new Car(50); //50 miles per gallon
myHybrid.addGas(20); //Tank up 20 gallons
myHybrid.drive(100); //Drive 100 miles
System.
out.
println("The fuel remaining is "+ myHybrid.
getGasLevel()+ " gallons"); }
}