• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

beginner assignment

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you're dividing by zero, I'm not sure though. Can you post some of code where you think the problem is ?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}
}
 
Ranch Hand
Posts: 117
Mac Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
Please show us the changes.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic