programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Math question (very beginner)

Greenhorn
Posts: 13
• Number of slices to send:
Optional 'thank-you' note:
I'm very new to Java (and programming in general) and am working on a series of tutorials.

The problem is...I'm not so good at the math parts. If anyone could just point me in the right direction on how to solve this problem, it would be greatly appreciated.

---Taken from the tutorial---

Exercise 3 --- Last Chance Gas
Al's Last Chance Gas station sits on Route 190 on the edge of Death Valley. There is no other gas station for 200 miles. You are to write a program to help drivers decide if they need gas. The program asks for:

The capacity of the gas tank, in gallons.
The indication of the gas gauge in percent (full= 100, three quarters full = 75, and so on).
The miles per gallon of the car.
The program then writes out "Get Gas" or "Safe to Proceed" depending on if the car can cross the 200 miles with the gas remaining in the tank.

Tank capacity:
12
50
Miles per gallon:
30
Get Gas!

Use integers for all input and all arithmetic.

This is what I've come up with so far:

I have to use only integers for the arithmetic and can only use if-else statements.

Thank You,
[ May 08, 2008: Message edited by: Tod Novak ]

Ranch Hand
Posts: 961
• Number of slices to send:
Optional 'thank-you' note:
Well, you will need first to find out how many gallons you actually have. You said the gauge reading is a percentage.

So, the current gallons formula must be somewhat like this:

Then you will need to determine how far your current gallons can take you.

If maximumDistance is smaller than 200, then it is time to get gas.

Also, you could avoid all this calculations by determining, at the begining, if the car's tank capacity and miles per gallon can actually take the driver that far.

Somewhat like this

I hope I am not wrong on these calculations. I am not very good at math myself.

I hope that helps.

Marshal
Posts: 79273
377
• Number of slices to send:
Optional 'thank-you' note:
Looks correct to me

Tod Novak
Greenhorn
Posts: 13
• Number of slices to send:
Optional 'thank-you' note:
Thank you Edwin!!

That's exactly the nudge I needed.