|
-
October 25th, 2006, 08:24 AM
#1
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();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|