Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Harnessing the Power of Objects Overload Methods

Jordan Bartholomew
Jordan Bartholomew
5,926 Points

barCount

Modify the drive method to define a parameter of how many laps should be driven. Update the method body to handle the new parameter.

public void drive(int lapsDriven) { barCount -= lapsDriven; lapsDriven += barCount; }

this is correct, but why shouldn't it be "public void drive(int barCount)? wouldn't that make it so it can only go as long as the battery life?

1 Answer

Acutally, the variable name you use as the parameter in the function is meaningless. You could write the function as:

public void drive(int someFunkyName) {
    lapsDriven += someFunkyName;
    barCount -= someFunkyName;
}

and it would be perfectly valid. The value being passed in represents the number of laps driven, so it makes sense to use that as the variable name. To address your point, this function does not check to ensure that the number of laps driven is not greater than the total of the barCount variable, so it is possible as it is currently written, to end up with a negative value for the barCount after this function executes. However, just changing the name of the parameter variable to barCount would have no effect as the parameter variable is a local variable and does not reference the barCount variable created earlier in the program.

In order to ensure that the value entered into the drive function does not cause the barCount to end up being lower than zero you would need to include some additional code inside the drive function before you modified the lapsDriven and barCount variables.