Need Serious Help on Odometer exercise
First off, yes, this is homework so I am not asking anyone to do it for me. I am taking an online Java class (not recommended!) where the only resource is the Savitch "Absolute Java" book. I have read the book and reread it 50 times now and cannot figure this out. Can someone point me in the right direction?
Here is the problem - you've probably seen it before.
"Define a class called Odometer that will be used to track fuel and mileage for an auto- mobile. The class should have member variables to track the miles driven and the fuel efficiency of the vehicle in miles per gallon. Include a mutator function to reset the odometer to zero miles, a mutator function to set the fuel efficiency, a mutator function that accepts miles driven for a trip and adds it to the odometer's total, and an accessor method that returns the number of gallons of gasoline that the vehicle has consumed since the odometer was last reset."
A few stupid questions:
1) Am I writing a separate program to create the class called Odometer and then a second program that accesses the class? I ask because the book video series goes into hiding information by using private and public classes.
2) Can someone simply explain the concept of Mutator and Accessor?
Thanks for any help you can give me.
still stuck after over 18 hours
Guys, I think my problem with this problem is that if requires me to know things that we have not covered yet. For example, it says in the instructions to use mutators and accessors but that isn't covered until next week - ahhh, the joys of online classes. Anyway, I am stuck on the basics on how to even put this together. Could I walk through this in simple terms and have you give me advice?
Part 1: Define the class
Here is where I will define all the variables private class, such as milesDriven and milesPerGallon. Do I set their values to 0 here as well?
Part 2: Do I need a constructor? I read that this would be where I would store the formulas? So one formula I need is (gallonsUsed = milesDriven / milesPerGallon) - speaking of which, does that mean I need define gallonsUsed in the first part?
Part 3: Mutator - to change the value? So if the original value of miles is O, the mutator allows me to change that value? This is the "setter"?
Part 4: Accesor: This is the "return" piece? Is that always formatted like that?
Part 5: Create a new class that will access the Odometer class.
Now here is where I get really stuck. I need to:
a. Ask user to enter miles
b. Allow them to reset the odometer to 0 (not even sure why they want this)
c. Ask user for mpg
d. Compute gallons used
If anyone can walk me through this and first tell me if I am going at this the right way and then give me hints on how to write this code, I would be very appreciative.
PS: Please be patient - I've spoken with other students who are equally stuck on this. The teacher is gone on Spring Break but this is due during the break and all the tutors are gone so we are left stranded. I have watched tons of OOP videos and read lots of website tutorials but it's left me more confused than ever.:eek: