CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2006
    Posts
    2

    Help with Java Program

    I'm currently writing a program that will calculate the miles per gallon for a car. Though i have ran into some problems. The first problem is when it output the mpg and cumulative mpg i get a run-on number after the decimal. The second problem is i need to the loop to continue unless the odometer is 0. This is what i have so far. BTW i new to programming so any help would be greatly appreciated.

    import java.io.*;

    public class CalculateMPG
    {
    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

    //your variables here
    int odometer;
    int startingOdometer;
    int currentOdometer;
    int miles;
    double MPG;
    double gallons;
    double totalGas;
    double totalMiles;
    double cumMPG;

    public void displayHeader()
    {
    System.out.println("**** Automobile Miles Per Gallon ****");
    System.out.println("");
    }

    public void getFirstReading()
    throws java.io.IOException
    {
    System.out.print("Starting odometer reading at fill-up: ");
    startingOdometer = new Integer (keyboard.readLine()).intValue();
    System.out.println("");
    }

    public boolean fillUp()
    throws java.io.IOException
    {
    System.out.print("Odometer reating at next fill-up (0 to stop): ");
    currentOdometer = new Integer (keyboard.readLine()).intValue();

    if (currentOdometer==0)
    return true;

    else
    {
    System.out.print("Gallons of gas purchased: ");
    gallons = new Double (keyboard.readLine()).doubleValue();
    }
    return false;

    }

    public void findMPG()
    {
    miles = currentOdometer - startingOdometer;
    MPG = miles/gallons;
    totalGas = (totalGas+gallons);
    totalMiles = currentOdometer - startingOdometer;
    cumMPG = totalMiles/totalGas;
    System.out.println("Miles per gallon for this tank is " +MPG);
    System.out.println("Cumulative miles per gallon is " +cumMPG);
    System.out.println("");
    System.out.println("Odometer reating at next fill-up (0 to stop): ");
    }

    public static void main(String[]args)
    throws java.io.IOException
    {
    boolean done = false;
    CalculateMPG car = new CalculateMPG();
    car.displayHeader();
    car.getFirstReading();
    car.fillUp();
    car.findMPG();



    }
    }

  2. #2
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: Help with Java Program

    Two words "Code Tags"
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  3. #3
    Join Date
    Apr 2006
    Location
    India
    Posts
    214

    Re: Help with Java Program

    Whe you use CODE tags it improves code readability. Your code does not seem to follow OO design practice, you should do some reading on the same.
    Naming conventions are incorrect, class attributes have not been explicitly specified as private/public why ??

    Your first query about run-on decimal , show a sample out put that you get and also tell us what you actually expect.

    Your second query about loop, can be handled pretty easily though it isnt really neat handling with current code, lets try solving problems in given sequence. Hint for the second is you need to add a loop for reading the values and the terminating condition being when odometer reading is entered as 0.
    "It is a capital mistake to theorize before one has data. Insensibly one begins to twist facts to suit theories, instead of theories to suit facts." Quote from Sherlock Holmes

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with Java Program

    Quote Originally Posted by Logo
    when it output the mpg and cumulative mpg i get a run-on number after the decimal.
    That's because you are outputting a floating point value. If you want to limit the number of decimal places that are displayed you could use the printf(..) method. For example to limit it to 2 decimal places:
    Code:
    System.out.printf("Cumulative miles per gallon is %.2f", new Double(cumMPG));

  5. #5
    Join Date
    Oct 2006
    Posts
    2

    Re: Help with Java Program

    sorry about the program not being in code tags...i will try to convert that later tonight....Thanks for the help so far with the numbers, currently i'm getting like 24.984738474847 for mpg and cumMpg and wish to get 25.0. I will try that code you (keang) provided. As for the the other problem, somebody told me to insert a while loop so that i will be asked for the odometer, gas, and it will out put the mpg and cumMpg as long as the milleage is not 0. I having trouble finding out how to go about that, i have tried to insert while loops but i can't get any of them to work.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help with Java Program

    Quote Originally Posted by Logo
    i have tried to insert while loops but i can't get any of them to work.
    We can't help you fix your loop unless you post it up here - formatted and commented.

    Doing more things faster is no substitute for doing the right things...
    S. R. Covey
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured