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 (Retired) Harnessing the Power of Objects Method Signatures

drive is already there?!!

I understand there need to be other methods with different signatures. when I typed public void drive(int mBarsCount) {} it didn't work because it has the same number of types in the signature? int..as other int laps caused error.

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 charge() {
    while (!isFullyCharged()) {
      mBarsCount = mBarsCount -1;
    }
  }

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

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

}

9 Answers

It's basically wanting you to add another drive() method that takes no arguments and makes 1 lap; so to do that you call the existing drive() method inside the new drive() method and passing the required argument (1 in this case) and that's it. If you still are not sure on how to do it here is what the code should look like:

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

ok thanks.. quick response!! Im marking You for best answer.

You're welcome. Thank you as well :)

how do you post your answer like that Jeremy? i am failing to respond to the questions coming in a helpful way. i am posting my answers as text.

add the accent mark that is above the tab key 3 times followed by the language you are coding, java in this instance and hit enter, then type or paste your code then hit enter again and type 3 more accent marks (above the tab key) by themselves then hit enter once more. That is how I do it.

you add the ''' ```

so print

print

``` before and after

if you add the name of the language you are coding (e.g., java, html, etc.) after the first three accent marks before hitting enter it will automatically format the code for that language- FYI.

you are awesome

i was putting this''' thank you so much

Modified from Java in 21 Days

//Throw an exception

public void getPoint(int x, int y) throws NumberFormatException {
     //center of method
}

//throwing multiple exceptions

public void storingPoint(int x, int y)
     throws NumberFormatException, EOFexception {
}

// passing exceptions using constructor notice no return void

public WebRetriever() throws MalformedURLException{
    //center of constructor
}

//makes it so any class works with WebRetriever objects deals with exception

*Rather than try and catch clauses you can use this technique it is up to your method that calls this method to deal with that exception,, hmm interesting they don’t have examples for that */

public void readFloat (String input) throws NumberFormatException {
    float in = Float.parseFloat(input);
}
//ie. you want to print a message.