Forums Register Login

beginner assignment

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Maybe you're dividing by zero, I'm not sure though. Can you post some of code where you think the problem is ?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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");
}
}
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
Well done
Please show us the changes.
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1291 times.
Similar Threads
$10 via Paypal to whoever can code this for me !! :)
Java Constructor
can we cast an array ?
Desperately need help writing Tester
Challange to write a program
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:51:09.