Need help on this program
Hi all,
The purpose of this program is listed in here:
// Inputs the miles driven and gallons used (both integers) for each tankful.
// Calculates and displays miles per gallon obtained for each tankful and print
// the combined miles per gallon obtained for all tankfuls up to this point.
// All results should be floating point. Use Scanner and sentinel-controlled
// repetition to obtain data from the user.
// Exercise 4.17 MPG.java
import java.util.Scanner;
public class MPG
{
private int milesDriven;
private int gallonsUsed;
private float result;
public void setMilesDriven( int miles )
{
milesDriven = miles;
}
public void setGallonsUsed( int gallons )
{
gallonsUsed = gallons;
}
public String getMilesDriven( int miles )
{
return milesDriven;// error occurs here
}
public String getGallonsUsed( int gallons )
{
return gallonsUsed;
}
public MPG( float result )
{
result = gallons * miles;
System.out.printf( result);
}
}
I try to compile this program, but the error I get is "incompatible types-found int but expected java.lang.String." How do I fix this, and how do I
use sentinel control? If you can help me with these two things, I'd appreciate it. The sooner you can help me, the better.
When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
> I try to compile this program, but the error I get is
> "incompatible types-found int but expected
> java.lang.String."
That means you provided an int where the program expected a string. Just like it says.
> How do I fix this,
Either provide a String instead of the int, or change the code that's expecting a String to operate on an int instead.
> and how do I
> use sentinel control? I
I assume the instructor just wants a flag that indicates whether you're done or not. If the user enters "quit" or whatever, set that flag to true. The loop control ( while(whatever), for example) checks the flag, and either enters the loop or not.
> The sooner you can
> help me, the better.
I'd advise you to keep comments like this out of your future posts. Nobody here cares about your timetable. We answer questions on our own schedule, and comments like that, and "urgent" and "asap" just annoy people.
jverda at 2007-7-13 18:14:16 >

import java.util.Scanner;
public class MPG
{
private int milesDriven;
private int gallonsUsed;
private float result;
public void setMilesDriven( int miles )
{
milesDriven = miles;
}
public void setGallonsUsed( int gallons )
{
gallonsUsed = gallons;
}
public String getMilesDriven( int miles )
{
return milesDriven; // error occurs here
}
public String getGallonsUsed( int gallons )
{
return gallonsUsed;
}
public MPG( float result )
{
result = gallons * miles;
System.out.printf( result);
}
}
When posting code use code tags. There is a code button on the page
when you post a message.
Your getMilesDriven() method has a return type of String but you are returning milesDriven which is an int.
Also your constructor has a parameter result but inside the constructor you
immediately calculate a new value for result overwriting whatever value you
pass in.
Hey,
Thank you for your helpful replies. I was able to fix the errors and while the program did compile, I ran into this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.awt.Win32GraphicsEnvironment.displayChanged(Win32GraphicsEnvironment.java:109)
at sun.awt.windows.WToolkit$4.run(WToolkit.java:698)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
I still can't figure out how to do the sentinel-controlled repetition yet. Any idea what this means?
How are you trying to run this program. Looks like your trying to use it in an applet but the MPG class prints to standard output.Why dont you try to use this class within a console application (a class with a main method)..J
I understand that I would have to add this line within the code:public static void main ( String args [ ] ) {but how do I code the whole application with the main method?
One way to do this would be to write a seperate driver program.
class DriverProgram {
public static void main(String[] args) {
declare an object of type MPG
loop {
get user input
call appropriate methods of your MPG object
prompt user if they need to input more data (this is your sentinel)
if user enters no more data
exit program
}
}
}
Alternatively the sentinel could be when the user enters 0 for distance driven
and 0 for gallons used.
flounder,You write great psuedo code :-)Ajay
Awww schucks you pseudo flatterer you.
> Awww schucks you pseudo flatterer you.No, really, I meant it sincerely. I wish i could write 1% as good as you.Superb way to communicate to someone.
I would do what flounder posted. I usually either use a scratch made driver class, as flounder shows, or I use JUnit tests.J