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

Michel Bourgeois
Michel Bourgeois
731 Points

Add logic to the drive method so that it throws an IllegalArgumentException if there aren't enough bars .....

have no clue what to do???? here are the errors i have

./GoKart.java:29: error: illegal start of type try{ ^ ./GoKart.java:29: error: ';' expected try{ ^ ./GoKart.java:35: error: class, interface, or enum expected }catch(IllegalArgumentException){ ^ ./GoKart.java:37: error: class, interface, or enum expected } ^ 4 errors

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(42);
  }

  if(laps > barCount){
      throw new IllegalArgumentException("Not enough Energy bars!!!");

    }
       lapsDriven += laps
       barCount -= laps;

       }

3 Answers

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

Hi there, Michel! First, your code should go in the drive method that accepts a number of laps. This should happen in the GoKart.java file as no other file is available in this challenge. A try/catch is not required by the challenge. Rather, the challenge is asking you to throw an IllegalArgumentException if the number of laps requested is larger than the number of energy bars currently available.

Let's say for instance that we're requesting the kart make 4 laps, but it only has 2 energy bars remaining. We want to check if the number of laps requested is greater than the number of energy bars we have. If it is, we throw the exception.

Hope this helps, but let me know if you're still stuck! :sparkles:

Michel Bourgeois
Michel Bourgeois
731 Points

Good afternoon Jennifer!!! here is my code i had and if statement and then throw an IllegalArgumentException! Still not working

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

public void drive(int laps) { lapsDriven += laps barCount -= laps; if(lapsDriven > barCount>{ throw new IllegalArgumentException("Not enough Energy bars!!!");

}

}

i corrected the code on the exercises!

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

Hi there! Oh you're soooo close here. First we want to check the condition if laps are greater than barCount. Then throw the exception. If the exception is not thrown then increment lapsDriven and decrement barCount. Also, your if statment ends with a > sign instead of a closed parenthesis which is causing a syntax error. Take a look at how I did it:

  public void drive(int laps) {
    if(laps > barCount) {
      throw new IllegalArgumentException("Not enough energy bars!");
    }
    lapsDriven += laps;
    barCount -= laps;
  }

In our drive method we accept the number of laps coming in. If the number of laps we want to drive (let's say 5) is greater than our current bar count (let's say it's at 3 now) then we throw an exception that says we don't have enough energy. But if that's not the case, then we increase the number of laps driver and decrease the the bar count by the number of laps.

Hope this clarifies things! :sparkles:

Michel Bourgeois
Michel Bourgeois
731 Points

Thanks for your help Jennifer! I do understand the if condition with the exception but it is still does not work!!! so if i m not mistaken the number of laps have to be in the drive method?????

public void drive() { drive(42);

i get a compiler error

./GoKart.java:30: error: illegal start of expression public void drive(int laps) { ^ ./GoKart.java:30: error: illegal start of expression public void drive(int laps) { ^ ./GoKart.java:30: error: ';' expected public void drive(int laps) { ^ ./GoKart.java:30: error: ';' expected public void drive(int laps) { ^ ./GoKart.java:36: error: ';' expected lapsDriven += laps ^ ./GoKart.java:39: error: reached end of file while parsing } ^ JavaTester.java:63: error: method drive in class GoKart cannot be applied to given types; kart.drive(1); ^ required: no arguments found: int reason: actual and formal argument lists differ in length ./GoKart.java:27: error: method drive in class GoKart cannot be applied to given types; drive(42); ^ required: no arguments found: int reason: actual and formal argument lists differ in length 8 errors

thanks!

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

Hi again, Michel! No, you're not meant to change the first drive method at all. It's still only supposed to run one lap at a time. Furthermore, you've now introduced a syntax error. Your first drive method is missing a closed curly brace. Take a look at both of the drive functions together.

public void drive() {  //This function make the cart run one lap
    drive(1);  
  }

  public void drive(int laps) {
    if(laps > barCount) { 
      throw new IllegalArgumentException("Not enough energy bars!");
    }
    lapsDriven += laps;
    barCount -= laps;
  }
Michel Bourgeois
Michel Bourgeois
731 Points

Got it finally!!!! Thank you for your patience Jennifer!

Muhammad Asif
PLUS
Muhammad Asif
Courses Plus Student 557 Points

class Example {

public static void main(String[] args) {

GoKart kart = new GoKart("purple");
if (kart.isBatteryEmpty()) {
  System.out.println("The battery is empty");
}
try {
   kart.drive(42);
} catch(IllegalArgumentException iae){
    System.out.println(iae);
}

} }

xiaoluke
xiaoluke
6,387 Points

you have placed your try catch in the incorrect place. You are meant to use the try, catch in Example.java not GoKart.Java.

try{
  kart.drive(42);
}

etc etc