Tuesday, June 2, 2015
DrivingTest Project
public class Demo {
public static void main(String[] args) {
Car myCar = new Car();
myCar.fuelCapacity = 20;
myCar.amountOfFuel = 10 ;
myCar.addGas(5);
System.out.println(myCar.amountOfFuel);
myCar.mileage = 15;
myCar.drive(100);
}
}
class Car{
// first think of properties which is nown and then actions which is verb
double mileage;
double amountOfFuel;
double fuelCapacity;
public void drive(double distance){
double fuelNeeded = distance/mileage;
if (amountOfFuel >= fuelNeeded) {
amountOfFuel = amountOfFuel - fuelNeeded;
System.out.println("You travelled "+ distance+" KM");
} else {
System.out.println("You dont have sufficient amount of fuel for the trip");
}
}
public void addGas(double amt){
double emptySpace = fuelCapacity - amountOfFuel;
if (amt < emptySpace) {
amountOfFuel = amountOfFuel + amt;
System.out.println(amt + " liters is added successfully");
} else {
amountOfFuel = fuelCapacity;
System.out.println("Tank is full");
}
}
public double getFuelLevel(){
System.out.println(amountOfFuel);
return amountOfFuel;
}
}
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment