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 trialGem Knight
4,779 PointsNeed help
I am not sure why my code isn't working. Can someone please help.
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) {
// Other driving code omitted for clarity purposes
if (laps > MAX_BARS) {
throw new IllegalArgumentException("Not enough battery remains");
}
mBarsCount -= laps;
}
public void charge() {
while (!isFullyCharged()) {
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
}
2 Answers
Derek Markman
16,291 PointsIn your overloaded 'drive' method (The drive method that takes an int as an argument). You need to check if mBarsCount is less than the number of laps passed into the drive method. If the number of laps is greater than mBarsCount, then you need to throw an IllegalArgumentException that says "Not enough battery remains."
Since you're trying to throw an IllegalArgumentException from your drive method if mBarsCount is less than the number of laps, you need to specify on your method that it throws an IllegalArgumentException.
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);
}
//modify the overloaded drive method
public void drive(int laps) throws IllegalArgumentException { //specifies that this method could throw an IllegalArgumentException
// Other driving code omitted for clarity purposes
if(mBarsCount < laps) { //checks if mBarsCount is less than the number of laps
throw new IllegalArgumentException("Not enough battery remains."); //throws an IllegalArgumentException, with the message you specified.
}
mBarsCount -= laps; //this specific line of code will only happen if mBarsCount is greater than the number of laps specified.
}
public void charge() {
while (!isFullyCharged()) {
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
}
Let me know if you have any other questions.
Gem Knight
4,779 PointsOk
Gem Knight
4,779 PointsGem Knight
4,779 PointsThanks for your help. I did not copy that code, I just experimented with it and found a better way... I think.
Derek Markman
16,291 PointsDerek Markman
16,291 PointsIf you don't mind, could I see the way that you completed it? Because often with the code challenges here, they accept and pass code that otherwise if used in an actual program wouldn't compile.