• Post Reply Bookmark Topic Watch Topic
  • New Topic

Errors trying to get random number to driver  RSS feed

 
Sam Kluender
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use a setter method to pick a random integer to be the MPG for a car. However, I'm having major issues in my driver when trying to use that random number in an instance. Any help? I'm not finished with the driver yet because I keep getting "cannot find symbol errors" Thanks!





 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're receiving the errors because getMpg() is not defined. Are you trying to set the MPG (you tell the car what its MPG is) or get the MPG (the car tells you what its MPG is)?
 
Sam Kluender
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are my directions

=============================================================================================
B. Design and implement a class called Car.java. It should have instance variables for the make, model, year, mpg, and odometer reading. Include getter and setter methods for all instance data. The method setmpg should have an int parameter top, and should set the mpg to a random integer from 10 to top, inclusive. Include a toString method that provides a one line statement of the car's year, make, and model, as in the first three columns in the table below.

Design a PriceComparison.java class that creates 4 instances of cars, asks a user for the per gallon price of gas, and reports for each car, in a table, the car information (from toString) together with the odometer reading, the mpg (using getter methods) and the total price of gas consumed, based on the odometer value and the mpg. Money should look like money.


Sample screen from PriceComparison.java:
Enter current price of gas > 2.78

YEAR MAKE MODEL MILES MPG COST at $2.78 PER GALLON

2006 Toyota Prius 100000 45 $6,177.16
2008 Saab 95 90000 25 $10,008.00
2012 Bugatti Veyron 30000 10 $8,340.00
2003 GM Cadillac 70000 14 $13,900.00
============================================================================================

Basically I believe I want to randomly generate an integer and have that integer be the MPG for a car
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First
Include getter and setter methods for all instance data.
Then update setmpg to
have an int parameter top, and should set the mpg to a random integer from 10 to top
Remember that set methods set values of the instance variables, get methods return the values of the instance variables.
 
Sam Kluender
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that what I did?

Isn't

mpg = generator.nextInt(top+1) +10;
return mpg;

giving it the parameter top and setting the mpg to a random integer from 10 top 10?


 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setters assign values to instance variables but do not return them. Getters return the values of instance variables but do not assign them.

So correct setMpg() and add all the other setters and getters.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to be asking two things. Don't. Do one thing, get it working and then consider the other thing. I suggest you set the mpg first and consider the random numbers later. I suggest you try something like thisThat way you can try different numbers from the keyboard. Once you have got the set method working, then consider the random thing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!