Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4601210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:47:35+00:00 2026-05-21T23:47:35+00:00

I’m having trouble with printing out the variable priceUsed After the method getPriceAfterUse() get

  • 0

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();


 }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:47:35+00:00Added an answer on May 21, 2026 at 11:47 pm

    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.

      public void outputDetails() {
        //Calculate the used price before outputting the value
        getPriceAfterUse()
        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 );
    }
    

    or in your test script you could do this.

    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();
    //UPDATE THE USED PRICE 
    a.getPriceAfterUse();
    
    a.outputDetails();
    System.out.println();
    

    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\:

    a.brandName = "something"
    a.color = "some color"
    a.priceNew = "some value"
    

    So now all of these values have been set… except for odometer and priceUsed

    you set the odometer with

    a.updateMilage();
    

    and then you called:

    a.outputDetails(); 
    

    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

    priceUsed = priceNew*(1-(odometer/6000000));
    

    Until that line us run the value of priceUsed in the Car object will print out 0.0

    hmmm… TLDR 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.