wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes beginner assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "beginner assignment" Watch "beginner assignment" New topic
Author

beginner assignment

Dg Mcinnis
Greenhorn

Joined: Feb 09, 2013
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

Joined: Feb 09, 2013
Posts: 12
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

Joined: Oct 13, 2005
Posts: 38508
    
  23
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

Joined: Oct 13, 2005
Posts: 38508
    
  23
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

Joined: Feb 09, 2013
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
*/

public void addGas(double amount)
{
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.addGas(20);
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

Joined: Feb 10, 2013
Posts: 117

You've gotten great answers from the forum members here.

Look at this line of code:



and this one:



Then read Campbell's post again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
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

Joined: Feb 09, 2013
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

Joined: Oct 13, 2005
Posts: 38508
    
  23
Well done
Please show us the changes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: beginner assignment