I’m having trouble with printing out the variable priceUsed
After the method getPriceAfterUse() get the value for me, the method OutputDetails should print all the information. But the variable priceUsed is printed 0.0. I don’t know why.
Here is my code:
import java.util.Scanner;
class Car
{
public String brandName;
public String color;
public double priceNew, priceUsed;
public double odometer;
public double getPriceAfterUse()
{
priceUsed = priceNew*(1-(odometer/6000000));
return priceUsed;
}
public double updateMilage()
{
Scanner keyboard = new Scanner(System.in);
odometer = keyboard.nextDouble();
return odometer;
}
public void outputDetails()
{
System.out.println("The car brand name is : " + brandName);
System.out.println("The car new price: " + priceNew);
System.out.println("The car used price: " + priceUsed);
System.out.println("The car color: " + color);
System.out.println("The car Odemeter: " + odometer );
}
}
public class CarTest{
public static void main(String args[])
{
Scanner keyboard = new Scanner (System.in);
Car a = new Car();
System.out.println("Enter you car Brand Name: ");
a.brandName = keyboard.next();
System.out.println("Enter your car color: ");
a.color = keyboard.next();
System.out.println("Enter your new price: ");
a.priceNew = keyboard.nextDouble();
System.out.println("Enter your Odometer:");
a.updateMilage();
System.out.println();
a.outputDetails();
System.out.println();
}
}
You are calling OutputDetails before you are setting the value of PricedUsed.
If you look at the output on the last line you have
System.out.println(a.getPriceAfterUse());
This works correctly because it it the first time it is called. Maybe you could add getPriceAfterUse() call to the OutputDetials method so that the used price is updated each time you display the values.
EDIT:
To solve this problem you could do this.
or in your test script you could do this.
EDIT #2:
Here is where I think your problem lies.
You have a class Cars, where you have the variables brandName, color, priceNew, priceUsed, and odometer.
I believe you are confusing the return statement of the method with setting the value of the variable.
When you call Car a = new Car(); you have created a new Car Object. Now assume that inside of this car element all of the variables (in this case brandName, color, priceUsed, etc) are empty.
You are setting their values by calling\:
So now all of these values have been set… except for odometer and priceUsed
you set the odometer with
and then you called:
But where along this chain of commands have you changed or even set the value of priceUsed? you have not done this. So when you call outputDetails it will print out all of the values in your Car Object. and since you have not set the value of priceUsed it will print out 0.0
remember that the return in a function does not set the value it just sends a value back to whatever called it… that is how the value gets passed back to System.out.println for example
System.out.println(a.getPriceAfterUse()) will print out the calculated price after use and the value that is printed is that is returned by the public double getPriceAfterUse() method.
BUT the value of the variable priceUsed which is inside your object is SET when you run the line
Until that line us run the value of priceUsed in the Car object will print out 0.0
hmmm… TLDR 🙂