• Post Reply Bookmark Topic Watch Topic
  • New Topic
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

Method misunderstanding

 
Greenhorn
Posts: 2
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone!

This is my first post here (I hope I'm doing it right). I've just recently stumbled upon JavaRanch and it seems like a great place. I'm always a bit hesitant to post questions about programming because I like to try my best before having to ask. However, I have been working on this assignment for a long time now and have really hit a wall. Below I will list the outline of the assignment and then the code. The PreTestDrive.java (main) was given to me from my Professor, I wrote the Car.java (class).

The issues that I'm having is I'm not understanding (exactly) how to develop the drive(). I've been trying so many things and cannot get my head around it. Maybe I need to go to bed.. IDK.. I'm not asking for the answer specifically but maybe something that can make my brain spark. That way I can get that "OH, YEA!" moment in my head. Also, there may be problems with something else in my code which could cause me from being able to complete this method, I'm not sure.

Assignment Outline/Description via Professor:
Implement a class Car. A Car object should have three instance variables, one for fuel efficiency (representing miles per gallon), one for fuel level (representing gallons), and a variable that acts as an odometer (representing miles). The fuel efficiency of a car should be specified as a parameter in the Car constructor and the constructor should set the fuel level and the odometer to zero. There should be getFuelEfficiency(), getOdometer(), and getFuelLevel() methods. There should also be a method addFuel(double gallons) which adds a specified amount to the fuel level and returns the new fuel level, and there should be a method drive(double miles) which simulates driving the car a specified distance. The drive() method should adjust the fuel level by the amount of fuel used, adjust the odometer by the amount of miles driven, and it should return the number of miles driven, which may be less than the number of miles specified if there is not enough fuel. (Notice that there are no setFuelEfficiency(), setOdometer(), and setFuelLevel() methods. The fuel efficiency field is immutable; once it is set by the constructor, it cannot be changed. The odometer should only be changeable by driving the car, as in a real car. The fuel level should only be changed by driving the car or by adding fuel.)


Main:


Car Class:
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James. Welcome to the Ranch!

OK, I'd suggest stepping away from the code for a moment, and making sure you understand what's required.

there should be a method drive(double miles) which simulates driving the car a specified distance. The drive() method should adjust the fuel level by the amount of fuel used, adjust the odometer by the amount of miles driven, and it should return the number of miles driven, which may be less than the number of miles specified if there is not enough fuel.



The car has got two properties that you're keeping track of: the distance travelled and the fuel level. You also know the fuel efficiency (miles per gallon).

So, if your current distance travelled is x, your current fuel level is y, and try to drive z miles:
- how much fuel will you use?
- how much fuel will you have left?
- what will be your total distance travelled?
- and if you try and drive too far, what distance will you travel before you run out of fuel?

Make sure you know how to calculate these using a pencil and paper. Once you completely understand that, then try and write the equivalent code.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you not sure if your addFuel() method is correct?

Why are you declaring gallons as an instance variable (Line 6) and write the comment after it? Can you explain what you're thinking here? Did you do anything with this gallons variable? Just so you know, the gallons parameter declared on Line 36 shadows (partially hides) the instance-level gallons on Line 6.
 
James Conley
Greenhorn
Posts: 2
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help, both of you!

Long story short I used to do some Java programming but haven't touched it since my last programming course, which was two years ago. I'm brushing up and this assignment really helped me put things back together. I need to learn to walk away for a few minutes when the code starts getting at me. After I went to class yesterday I sat down and re-worked some of my issues ending up with the desired result. Below I have attached the complete code to the project I was working on.

Revised Car Class


Thanks again!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic