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 trialDominic Cruz-Pena
546 PointsFirst, create a new private int field named lapsDriven.
I cant get it hen i try public void drive(){ lapsDriven++; }
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;
}
}
5 Answers
Steve Hunter
57,712 PointsHi 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.
Joseph Wasden
20,407 PointsYour 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
3,303 PointsDid you remember to add the private int lapsDriven;
at the top of the class? You can't increment a variable before you declare it.
Steve Hunter
57,712 PointsMy 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
Courses Plus Student 557 Pointsclass 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]
Steve Hunter
57,712 PointsHi Muhammad,
Is this a question? What are you needing help with? Let me know and I'll try to assist.
Steve.
Muhammad Asif
Courses Plus Student 557 Pointshi 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);
}
}
Steve Hunter
57,712 PointsHi 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());
Muhammad Asif
Courses Plus Student 557 PointsMuhammad Asif
Courses Plus Student 557 Points[MOD: edited this code block too -srh]