James Conley

Greenhorn
+ Follow
since Aug 26, 2012
James likes ...
Mac Java Linux
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by James Conley

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!
11 years ago
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:
11 years ago