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 trialAaron Williams
1,702 PointsIm so confused
.
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(MAX_BARS);
}
public void drive(int laps) {
// Other driving code omitted for clarity purposes
mBarsCount -= laps;
int newDriveAmount = mBarsCount + laps;
if (newDriveAmount > MAX_BARS) {
throw new IllegalArgumentException("Not enough battery remains");
}
mBarsCount = newDriveAmount;
}
public void charge() {
while (!isFullyCharged()) {
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
}
5 Answers
Patrick Tse
10,682 PointsLet me clarify. I was saying to throw an iae if (mBarsCount<laps). Please see comments:
public void drive(int laps) {
//If number of battery bars is less than the number of laps, throw an iae
if(mBarsCount < laps){
throw new IllegalArgumentException("Not enough battery remains.");
}
// run the GoKart and deplete the battery for the number of laps.
mBarsCount -= laps;
}
Patrick Tse
10,682 PointsBackground information:
- mBarsCount is the battery remaining in the GoKart. (Maximum 8 bars)
- laps is the number of laps the GoKart is needed to run.
- assuming 1 lap will need 1 bar of battery. It is needed to prevent the GoKart from running more laps than it can handle. (mBarsCount should be more or equal to laps)
So before driving (mBarsCount -= laps), you need to throw the IllegalArgumentException if (mBarsCount < laps).
Aaron Williams
1,702 PointsI'm now getting "Bummer! Are you sure you threw the IllegalArgumentException if the requested amount of laps would make the battery less than zero?" 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(MAX_BARS); }
public void drive(int laps) { // Other driving code omitted for clarity purposes mBarsCount -= laps; int newDriveAmount = mBarsCount + laps; if (mBarsCount >= laps) { throw new IllegalArgumentException("Not enough battery remains"); } mBarsCount = newDriveAmount; }
public void charge() { while (!isFullyCharged()) { mBarsCount++; } }
public boolean isBatteryEmpty() { return mBarsCount == 0; }
public boolean isFullyCharged() { return mBarsCount == MAX_BARS; }
}
Aaron Williams
1,702 PointsOk thanks for that.
ISAIAH S
1,409 Points- You don't need
int newDriveAmount = mBarsCount + laps;
, and -
if (mBarsCount >= laps) {
should beif (laps > mBarsCount) {
.
Ricky Catron
13,023 PointsRicky Catron
13,023 PointsHey Aaron what exactly are you confused about? The only thing I see a problem with at a glance is the indentation in your drive function.
mBarsCount -= laps; int newDriveAmount = mBarsCount + laps;
^ Why is this indented more here?
--Ricky