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 Throwing Exceptions

Stage 2 Harnessing the power of objects

Hie, can somebody help me understand where i am getting it wrong

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() {
    drive(1);
  }
  public void drive (int laps){
    int newCharge =mBarsCount-laps;
    if (newCharge<0){
      throw new IllegalArgumentException ("Not enough battery remains");
    }
    mBarsCount=newCharge;
  }
  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    mBarsCount -= laps;
  }

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

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

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

}

5 Answers

William Li
PLUS
William Li
Courses Plus Student 26,867 Points

I don't see anything wrong with your code. Are you getting error or sth?

OK, i see the problem now, you have duplicated drive method.

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

delete this block of code, and you're good to go.

/GoKart.java:25: error: illegal start of expression public void drive(int laps) { ^ ./GoKart.java:25: error: illegal start of expression public void drive(int laps) { ^ ./GoKart.java:25: error: ';' expected public void drive(int laps) { ^ ./GoKart.java:25: error: ';' expected public void drive(int laps) { ^ ./GoKart.java:30: error: illegal start of expression public void charge() { ^ ./GoKart.java:30: error: illegal start of expression public void charge() { ^ ./GoKart.java:30: error: ';' expected public void charge() { ^ ./GoKart.java:36: error: illegal start of expression public boolean isBatteryEmpty() { ^ ./GoKart.java:36: error: ';' expected public boolean isBatteryEmpty() { ^ ./GoKart.java:40: error: illegal start of expression public boolean isFullyCharged() { ^ ./GoKart.java:40: error: ';' expected public boolean isFullyCharged() { ^ ./GoKart.java:44: error: reached end of file while parsing } ^ JavaTester.java:54: error: cannot find symbol if (!kart.isBatteryEmpty()) { ^ symbol: method isBatteryEmpty() location: variable kart of type GoKart ./GoKart.java:25: error: variable laps is already defined in method drive(int) public void drive(int laps) { ^ ./GoKart.java:31: error: cannot find symbol while (!isFullyCharged()) { ^ symbol: method isFullyCharged() location: class GoKart ./GoKart.java:37: error: cannot return a value from method whose result type is void return mBarsCount == 0; ^ ./GoKart.java:41: error: cannot return a value from method whose result type is void return mBarsCount == MAX_BARS; ^ 17 errors

William Li
PLUS
William Li
Courses Plus Student 26,867 Points

I don't know what you have done wrong then.

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() {
    drive(1);
  }
  public void drive (int laps){
    int newCharge =mBarsCount-laps;
    if (newCharge<0){
      throw new IllegalArgumentException ("Not enough battery remains");
    }
    mBarsCount=newCharge;
  }

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

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

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

}

Copy this code over and replace the code template provided by the Challenge, you'll pass. This code is your original code with the duplicated method removed.

that is what the java editor is saying but at first it was only saying "variable drive is already defined" so now i think i messed up something else which i i am failing to figure out now

okay william, let me try to start afresh, i think i have messed up something else, thank you so much. will keep you posted