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 Method Overloading

Sean Loke
Sean Loke
14,659 Points

VERY CONFUSED WITH - fill(MAX_PEZ); and Challenge Task (GoKart)

Q1. I dont understand why Craig changed pezCount = MAX_PEZ; into fill(MAX_PEZ); and it still produces the same result.

public void fill() { fill(MAX_PEZ); }

public void fill(int pezAmount) { pezCount += pezAmount; }

Q2. (a) 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 lapsLeft) {
lapsDriven += lapsLeft;
barCount -= lapsLeft;

}

got passed through this through guessing game. Could someone explain for me this part of the code?

Q2. (b) Create a new method named drive that accepts no arguments. It should call the newer drive method passing in a 1 for the default.

Not quite sure what to do here..... :(. Someone please help

Thank you!

2 Answers

Jeremiah Shore
Jeremiah Shore
31,168 Points

Q1: I think you may have confused the intended use of the fill() method from the example with what is needed in the code challenge. In the example, we are filling the PezDispenser up to its maximum capacity, which is defined in the constant MAX_PEZ.

edit: I think I actually misunderstood your first question too lol. Looking back at this—was reviewing with my gf, she was having an issue with the same challenge last night—it looks like you are actually asking "Why are we defining and using a function instead of just changing the variable." This is understandable, and basically, it's just a better defined and more flexible way of doing things. You can understand the intention behind the variable name fill() and you also get the benefit of being able to call that whenever you want instead of re-typing the same code. DRY: don't repeat yourself! Imagine if you also had to increment a refill counter variable to keep track of how many times the dispenser was refilled? The more complexity and features you add, the more you will want to define your objects by their behaviors through functions.

Q2a: In the code challenge they are asking you to modify your drive method to accept an int argument for the number of laps to drive, and then to increase/decrease your member variables—lapsDriven & barCount—accordingly. The difference, and the source of your confusion, is more likely in the understanding of the problem and its translation into code. To aid your understanding of the terminology, think of parameter as meaning the things the method requires to run and arguments as the actual values/variables/objects that are passed to a method when it is called.

Q2b: This is asking to create another drive method that accepts no parameters. Essentially, you are creating what is called an "overloaded" method. You can have as many methods named drive as you might want, so long as they define different types for their parameters, or the types of their listed parameters are in a unique order.

In this case, they are basically asking for a much simpler version of your new drive method—much like you had it before, that also utilizes the newly added method for optimal maintainability. One thing to also consider is that we imposed the restriction that only one lap could be driven at a time when calling drive(). In reality, this is technically a constant, and not some arbitrary number; in my example below I've defined it as a constant that more clearly demonstrates its intention in its variable name.

example of an overloaded method:

public class Dog {
  private static final int BARK_COUNT_DEFAULT = 1;  

  public void bark() {
    bark(BARK_COUNT_DEFAULT);
  }

  public void bark(int numOfTimesToBark) {
    //your code defining WHAT SPECIFICALLY to do when barking goes here...
    //and because bark() calls this version, you only have to write that code once
  }

}
Hans Roseboom
Hans Roseboom
1,409 Points

When making the assignments I already had an solution like you're suggesting. Only this wasn't the right solution for the problem. To solve the assignmet they are looking for a much simpler solution. Eventually I found that the solution for question 2b was:

  public void drive() {
    drive(0);
}
Julian Rios
Julian Rios
3,458 Points

Would this work as a recursive call for this example?

Pseudo code:

if (#ofTimesToBark == 1) System.out.println(“Bark”); else Bark(#ofTimesToBark - 1) // if 5 calls bark each time until it reaches 1.

Ben Schreiber
Ben Schreiber
1,273 Points

think it is:

public void drive() { drive(1); }

lap Nguyen
lap Nguyen
715 Points

thx Ben for the short answer and Jeremiah for the explained one. both helped me equally.