Olá colegas.
Sou iniciante em java e fiz um exercício do livro Big Java Binder do Cay Horstsman que não está funcionando como esperado. Acredito que seja por que não estou conseguindo utilizar o parâmetro
totalAddGas que é etornado no método addingGas. Ou pose ser outra coisa que não estou vendo, alguém pode me ajudar? Muito grato
Segue meu código:
/*
* 17.
Consider a Car class that simulates fuel consumption in a car. We will assume a
fixed efficiency (in miles per gallon) that is supplied in the constructor. There are
methods for adding gas, driving a given distance, and checking the amount of gas
left in the tank. Make a card for a Car object, choosing suitable instance variables
tockphoto.
and showing their values after the object was constructed.
* @author salatiel
*/
/**
* Car class that simulates fuel consumption in a car
*/
public class Car_17 {
/**
* Construct of Car_17 without parameters.
* @param efficience
*/
public Car_17(){
}
private double efficience;
private double addGas;
private double totalAddGas;
private double distance;
/**
* set an efficience to the car
* @param efficience
*/
public void setEfficience(double efficience) {
this.efficience = efficience;
}
/**
* Driving a given distance.
* @param amount that is the distance drived by the car
*/
public void driveDistance(double amount){
distance = amount;
}
/**
* Adding gas in the tank of car.
* @param amount
*/
public double addingGas(double amount){
double totalAddGas = addGas + amount ;
addGas = totalAddGas;
return totalAddGas;
}
/**
* Checking the amount of gas left in the tank of car
* @return
*/
public double CalcFuel(){
double leftGas = totalAddGas - (distance/efficience);
return leftGas;
}
}
minha classe teste:
package chapter03_ImplementingClasses.Check_Self;
public class Car_17Tester {
public static void main(String[] args) {
Car_17 c1 = new Car_17();
c1.setEfficience(10);
c1.driveDistance(80);
c1.addingGas(50);
c1.CalcFuel();
System.out.println(c1.CalcFuel());
System.out.println("expected 42");
}
}
o resultado:
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp
-8.0
expected 42