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

Jad Michael Saber
Jad Michael Saber
526 Points

I don't know what to do ...

I don't know what to write help please

GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int barCount;
  private int lapsDriven;

  public GoKart(String color) {
    this.color = color;
  }

  public String getColor() {
    return color;
  }
  public void charge() {
    barCount = MAX_BARS;
  }

  public boolean isBatteryEmpty() {
    return barCount == 0;
  }

  public boolean isFullyCharged() {
    return MAX_BARS == barCount;
  }

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

  public void drive() {
     }
}

1 Answer

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Jad

The first task asks you to change the way user asks for lap to be driven. Rather than give them one lap per request they want to ask more than one essentially. Thus you should accept variable lapRequest as argument to the drive method. You can just modify the available drive() method to be like this:

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

the next (second task) : Of course, another user of the code just wrote and asked "Where'd that drive method go! I loved that method, can you put it back please?"

Basically it ask you to return the old way to ask for lap. 1 lap each time. Thus you just need to insert lapRequest = 1 to turn the current drive(int lapRequest) to act like the old drive() method right?

Moreover the users that request old style drive method may still use drive() to call the drive method. You can facilitate this using method overloading which allows you to make different methods that share the same name but accept different variable as argument. As you can see the difference between the way old user call drive() and new users call drive(int lapRequest) is the different argument it passes. Thus you just modify the above code to add:

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

Add: another drive() method but without any argument

I hope this can help a little.