# Gas Mileage Calculator

Page 1 of 1

## 2 Replies - 6562 Views - Last Post: 04 October 2012 - 11:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294195&amp;s=ae10109de8f59a8cc77c2b3420515830&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dasrhino

• New D.I.C Head

Reputation: 0
• 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

• vexing conundrum

Reputation: 587
• Posts: 2,746
• 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.

### #3 dasrhino

• New D.I.C Head

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

## Re: Gas Mileage Calculator

Posted 04 October 2012 - 11:01 AM

DimitriV, 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

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}