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

Quinton Rivera
Quinton Rivera
5,177 Points

Challenge not sure what I am doing wrong.

Ive tried numerous ways Ive spent more than an hour.

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() {
    drive(1);
  }

  public void drive(int lapsDriven) 
  {
      try
      {
          if((barCount - lapsDriven) < 0)
              throw new IllegalArgumentException();
          else
              lapsDriven += lapsDriven;
              barCount -= lapsDriven;
      }
      catch (IllegalArgumentException iae)
      {
          System.out.println("Overload");
      }

  }



}

2 Answers

Mathieu HAYS
Mathieu HAYS
9,623 Points

I think you probably have an issue with the if/else statement in your drive method. You should used braces because you have two statements in your else block.

You also have an issue with your lapsDriven variable. You should use this.lapsDriven += lapsDriven

You should use the try/catch statement in the class that handles the display functionality

Quinton Rivera
Quinton Rivera
5,177 Points

No i figured it out i went to much in detail, this part did not want me to actually throw an exception. I moved to fast.

My logic worked and the code worked:

My logic was that if you drove 5, and you added another 4 to it, laps driven would be 9

Since you can only store 8 for battery it would tell the driver that he would run out of battery before destination

If we say got another 2 added to the original 5 we would be fine because we could travel 7 and not run out of battery

So i subtracted the 7 from the battery value of 8 so the value would be 1 battery awaiting next method call.