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

Jackie Wang
Jackie Wang
479 Points

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

Create a new method named drive that accepts no arguments. It should call the newer drive method passing in a 1 for the default. correct it help me i don't know what's wrong with my program

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() {
    lapsDriven++;
    barCount--;
  }
  public void drive(int laps) {
  lapsDriven += lapsDriven;
   barCount -= barCount;
    public void drive() {
      drive(1);
    //this is the newer  drive method,but it's uncorrect.
    }

}
}
Presley Cobb
Presley Cobb
971 Points

You are defining a method inside of another method definition. Close the first method then try again.

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Jackie! What Presley means is that you've done something like this:

public void myFunction() {
    public void myFunction2() {
      // some code here
  }
}

This is the attempt to declare another function inside a function which is not what they're asking for and actually invalid, I believe. But even if we fix that there are still errors in your code. It seems like you may have changed the original code that passed step 1.

In the drive function that takes a number of laps, we want to add the number of laps we're going to drive to the number of laps already driven. Let's say our GoKart has driven 10 laps. Your code adds the number of laps already driven to itself. It will double every time it's run.

lapsDriven += lapsDriven;  //This adds the current count 10 to the current count resulting in 20

But let's say we want to only drive the GoKart 3 more laps. Then we need to call the drive function and send in 3 laps. So we need to add the number of laps we want to drive to the number already driven.

lapsDriven += laps; //If we send in 3 as the number of laps and the lapsDriven is currently 10 the new lapsDriven will be 13

Also you have the line drive(1);. And this is correct, but it should be the only line within the drive function that has no parameters.

I think you can get it with these tips, but let me know if you're still stuck! :sparkles:

hie im still stuck can you please send a snippet of the code?

Jackie Wang
Jackie Wang
479 Points

Can you show me what you mean

Jackie Wang
Jackie Wang
479 Points

You guys are all the best answer, thank you for all of you!!!! I forgot the " } " and I stick there for three days ......: (((((((((