Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# beginner assignment

Dg Mcinnis
Greenhorn
Posts: 3
Hi, I having trouble with an introductory java assignment. We are required to create class Car and class CarTester. My issue is with the method returning the amount of fuel remaining in the tank.
If I use / to divide miles travelled by efficiency I get an error in output which returns the value of NaN. I cannot multiply by the fraction without making class Car specific to the values provided in CarTester. I'm having trouble coming up with a solution for this. Any help or guidance would be appreciated.

Bund De
Greenhorn
Posts: 13
Maybe you're dividing by zero, I'm not sure though. Can you post some of code where you think the problem is ?

Campbell Ritchie
Sheriff
Posts: 48968
60
Welcome to the Ranch

Agree with the suggestion. You get NaN only from floating‑point arithmetic, by dividing 0.0 by 0.0 or similar. The details are in the Java Language Specification, but that isn’t easy to read. Suggest you test for zeros:-NaN means not‑a‑number and it has some peculiar behaviour. NaNs can be dangerous, because they spread; any arithmetic with NaN as a term or factor is liable to give NaN as a result.

Campbell Ritchie
Sheriff
Posts: 48968
60
There are several ways of getting NaN in that Java Language Specification (JLS) link I quoted earlier, some of which I didn’t know about. You might get NaNs from ±∞ - ±∞. Check in the JLS.

Dg Mcinnis
Greenhorn
Posts: 3
Thanks for the responses. I took out where i has the variable initialized to 0, the class still compiled but I still got NaN. Here is the code I have so far.

Car Class

public class Car
{
private double gas;
private double efficiency;
public double drive;

/**
Constructs a car with a given fuel efficiency.
@param anEfficiency the fuel efficiency of the car
*/

public Car(double anEfficiency)
{
gas = 0;
efficiency = 0;
drive = 0;
}

/** Adds gas to the tank.
@param amount the amount of fuel to add
*/

{
double total = gas + amount;
gas = total;
}

/**
Drives a certain amount, consuming gas.
@param distance the distance driven
*/

public void drive(double distance)
{
double drive = distance;
}

/**
Gets the amount of gas left in the tank.
@return the amount of gas
*/

public double getGasInTank()
{
double gasInTank = gas - (drive / efficiency);
return gasInTank;
}
}

and the Tester Class

public class CarTester
{
public static void main(String [] args)
{
Car myHybrid = new Car(50); // 50 miles per gallon

myHybrid.drive(100); // consumes 2 gallons
double gasLeft = myHybrid.getGasInTank();

// TODO: Print actual and expected gas level
System.out.print("Gas Level: ");
System.out.println(myHybrid.getGasInTank());
System.out.println("Expected: 18");
}
}

Paul Mrozik
Ranch Hand
Posts: 117
You've gotten great answers from the forum members here.

Look at this line of code:

and this one:

Campbell Ritchie
Sheriff
Posts: 48968
60
Paul Mrozik, welcome to the Ranch
What is that about initialising to 0? all number fields are given default values of 0, and you should initialise them to real values before using them.

Dg Mcinnis
Greenhorn
Posts: 3
I was able to work it out after I stopped staring at it for a while and went back, i had a few things wrong. Thank you so much for your help. Its nice to have a forum that provides guidance instead of giving answers, you learn more that way.

Greatly appreciate it, thanks again!

Campbell Ritchie
Sheriff
Posts: 48968
60
Well done