Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Harnessing the Power of Objects Method Signatures

Sohaib Rashid
Sohaib Rashid
627 Points

If someone can help me with this assignment, I would highly appreciate it!

The task as :I have added a method drive that defines a parameter laps. These high-powered GoKarts use 1 energy bar off their battery per lap. Using method signatures, add a new default method called drive that performs just 1 lap and takes no arguments.

Happy Coding!

GoKart.java
public class GoKart {
  public static final int MAX_BARS = 8;
  private String mColor;
  private int mBarsCount;

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0;
  }

  public String getColor() {
    return mColor;
  }

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    mBarsCount -= laps;
  }

  public void drive() {
    int laps = 1;

  public void charge() {
    while (!isFullyCharged()) {
      mBarsCount++;
    }
  }

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

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

}

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Sohaib;

Exciting to see you asking questions like this early on. Method signatures and method overloading (using the same method name for different functions) are a powerful feature so understanding them is important. From the Oracle documentation on methods:

Overloading Methods

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance").

Let's take a look at this as it relates to this challenge specifically.

Challenge

I have added a method drive that defines a parameter laps. These high-powered GoKarts use 1 energy bar off their battery per lap. Using method signatures, add a new default method called drive that performs just 1 lap and takes no arguments.

In looking at the prompting code we see that the current drive() method takes a number of laps as an argument and inside that method it decreases the battery level by one bar for each lap. If, for example, we wanted to send a kart around the track three times, we would tell it to drive(3) and it's battery would be depleted by three bars. Hopefully that much makes some sense.

Now, what if in our program we simply want to have our kart go around the track once. We need to do drive(1), right? The challenge is asking us to create another method called drive, thereby overloading that method name. In the end we need to be able to do drive() and have it be the same as drive(1), with me?

To start we can do:

public void drive() {
    mBarsCount -= 1;
}

That would do exactly what the challenge is asking, correct? We have overloaded our drive() method and created a new method that handles one lap while taking no parameters. Great! But wait a minute, what happens if at some point down the road we want to do something else beyond reducing the battery charge level. What if the tires also need to be decreased in some way? The way we have things coded now we would have to change mTireThickness in both drive() methods. Not ideal. Perhaps that is what is in the // Other driving code omitted for clarity purposes portion of the original drive() method.

What to do then? Well, since we already have a drive() method that takes parameters, and our new drive method is basically using the same signature, we should be able to do something like:

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

Now we're loggin'! We have two distinct drive() methods that accomplish our necessary tasks, but really only have to maintain the code in one of them. Now if we add a consideration for tire thickness in drive(int laps) when we call the drive() method it will take that into consideration as well.

I hope that made some sense, please post back if it did not.

Happy coding, Ken

Thank you Ken !

Sohaib Rashid
Sohaib Rashid
627 Points

Thank you Ken! I understand how our new drive method basically uses the same parameter list or signature as the original drive method! Happy Coding and thank you for your help!