2 Replies - 1810 Views - Last Post: 04 October 2012 - 11:01 AM Rate Topic: -----

#1 dasrhino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-September 12

Gas Mileage Calculator

Posted 03 October 2012 - 09:59 PM

OK so i know this program is way more complicated than it should be but we are practicing sentinel control. Not sure if i spelled that right but any how, I can get it all to compile fine but it doesn't display anything. I am creating an object that uses a class which calculates mileage until the user enters in a negative amount. Any tips much appreciated!


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




Is This A Good Question/Topic? 0
  • +

Replies To: Gas Mileage Calculator

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Gas Mileage Calculator

Posted 03 October 2012 - 10:04 PM

In the 28th line of the first sample you have an incorrect escape character.
Line 14 in the second sample calls displayWelcome but shouldn't it be printing what is returned since the displayWelcome just returns something instead of calling a printing method.
Was This Post Helpful? 0
  • +
  • -

#3 dasrhino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-September 12

Re: Gas Mileage Calculator

Posted 04 October 2012 - 11:01 AM

View PostDimitriV, on 03 October 2012 - 10:04 PM, said:

In the 28th line of the first sample you have an incorrect escape character.
Line 14 in the second sample calls displayWelcome but shouldn't it be printing what is returned since the displayWelcome just returns something instead of calling a printing method.


Many Thanks! Got it working with a few other errors corrected as well!

Colin
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1