public class Mileage
{
int milesDriven = 0;
int gallonsUsed = 0;
public void setMilesDriven( int miles )
{
milesDriven = milesDriven + miles;
}
public int getMilesDriven()
{
return milesDriven;
}
public void setGallonsUsed( int gallons )
{
gallonsUsed = gallonsUsed + gallons;
}
public int getGallonsUsed()
{
return gallonsUsed;
}
public String displayWelcome()
{
return "Welcome to the gas mileage calulator!/n Enter a positive number of miles to calculate MPG \n or a negative value to calculate average MPG:\n";
}
}
import java.util.Scanner;
public class MileageTest
{
public static void main( String[] args )
{
Mileage mileage1 = new Mileage();
Scanner input = new Scanner( System.in );
int miles;
int gallons;
float mPG;
mileage1.displayWelcome();
miles = input.nextInt();
while (miles >= 0)
{
mileage1.displayWelcome();
miles = input.nextInt();
System.out.println(" How many gallons did you use?:\n ");
gallons = input.nextInt();
mileage1.setMilesDriven(miles);
mileage1.setGallonsUsed(gallons);
}
mPG = mileage1.getMilesDriven() mileage1.getGallonsUsed();
System.out.printf( " You have gone a total %d miles using %d gallons.\n Your average miles per gallon is:%.02f", mileage1.getMilesDriven(), mileage1.getGallonsUsed(), mPG );
}
}

New Topic/Question
Reply




MultiQuote




|