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 Increment and Decrement

First, create a new private int field named lapsDriven.

I cant get it hen i try public void drive(){ lapsDriven++; }

GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int barCount;

  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;
  }
}
Muhammad Asif
Muhammad Asif
Courses Plus Student 557 Points
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;
  }
}

[MOD: edited this code block too -srh]

5 Answers

Hi Dominic,

You are creating a new member variable, not a method.

So, you want a private variable called lapsDriven that's an int. Try:

private int lapsDriven;

Put that at the top of the class with the other member variables.

I hope that helps,

Steve.

Your code worked fine for me. Make sure you have declared the variable, perhaps?

class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int barCount;
  private int lapsDriven; //<-- make sure this variable is declared as shown

  public GoKart(String color) {
    this.color = color;
  }

  public void drive() { // this is your code that you provided with this question
    lapsDriven++; 
  }
Nourez Rawji
Nourez Rawji
3,303 Points

Did you remember to add the private int lapsDriven; at the top of the class? You can't increment a variable before you declare it.

My apologies! I saw your topic showing that you're at the start of the challenge where you define the lapsDriven member variable. The second piece of the challenge requires the drive() method to be created.

If you're at the second part, your method is correct as long as you have declared the member variable as in my first answer. Nourez called that right.

Steve.

Muhammad Asif
PLUS
Muhammad Asif
Courses Plus Student 557 Points
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;
  }
}

[MOD: edited code block - srh]

Hi Muhammad,

Is this a question? What are you needing help with? Let me know and I'll try to assist.

Steve.

Muhammad Asif
PLUS
Muhammad Asif
Courses Plus Student 557 Points

hi steve i am stucked solving this code Protect the call to kart.drive by handling the IllegalArgumentException that is thrown when not enough battery remains. Print out the message from the exception to the screen as you catch the exception.

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); // existing line
    } catch(IllegalArgumentException e) {
      System.out.println(e);
    }    
  }

Hi Muhammed,

Can you get me a link to the challenge - I want to test my answer as its likely to need tweaking.

I think your code looks OK but you might need to call getMessage() on e to make it printable?

So, System.out.println(e.getMessage());