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 trialMatt George
2,507 PointsOkay, so let's throw an IllegalArgumentException from the drive method if the requested number of laps cannot be complet
Okay, so let's throw an IllegalArgumentException from the drive method if the requested number of laps cannot be completed on the current battery level. Make the exception message "Not enough battery remains".
Please help me https://teamtreehouse.com/library/java-objects/harnessing-the-power-of-objects/throwing-exceptions
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
int newAmount = mBarsCount + laps {
if(laps > mBarsCount) {
throw new IllegalArugmentException("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;
}
}
5 Answers
Michael Hess
24,512 PointsHi Matt,
You're really close! But, get rid of:
int newAmount = mBarsCount + laps
and set the number of bars minus the laps :
mBarsCount -= laps;
You should end up with:
if (laps > mBarsCount){
throw new IllegalArgumentException ("Not enough battery remains");
}
mBarsCount -= laps;
If you have any other questions feel free to ask! Hope this helps!
James Pask
13,600 PointsI used this too and found the explanation quite helpful :)
Matt George
2,507 Pointspublic 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 > mBarsCount){
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;
}
}
Matt George
2,507 Pointslike that???
Michael Hess
24,512 PointsAdd a curly brace to close drive() and you've got it!
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 > mBarsCount){
throw new IllegalArgumentException ("Not enough battery remains");
}
mBarsCount -= laps;
} //add a curly brace here and you've got it!
public void charge() {
while (!isFullyCharged()) {
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
}
Matt George
2,507 Pointswhen i clicked preview i show me this
./GoKart.java:25: error: class, interface, or enum expected
public void charge() {
^
./GoKart.java:28: error: class, interface, or enum expected
}
^
./GoKart.java:31: error: class, interface, or enum expected
public boolean isBatteryEmpty() {
^
./GoKart.java:33: error: class, interface, or enum expected
}
^
./GoKart.java:35: error: class, interface, or enum expected
public boolean isFullyCharged() {
^
./GoKart.java:37: error: class, interface, or enum expected
}
^
JavaTester.java:69: error: cannot find symbol
if (!kart.isBatteryEmpty()) {
^
symbol: method isBatteryEmpty()
location: variable kart of type GoKart
7 errors
Matt George
2,507 Pointswait... i just copied your code and it works now
Thanks Alot!!!
Michael Hess
24,512 PointsMy Pleasure!
Matt George
2,507 PointsMatt George
2,507 PointsThanks Michael, but this is what i got and it still doesnt work:
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); }
} mBarsCount -= laps;
public void charge() { while (!isFullyCharged()) { mBarsCount++; } }
public boolean isBatteryEmpty() { return mBarsCount == 0; }
public boolean isFullyCharged() { return mBarsCount == MAX_BARS; }
}
Michael Hess
24,512 PointsMichael Hess
24,512 PointsTry this:
Jordan Bester
4,752 PointsJordan Bester
4,752 PointsHey micheal why would we get rid of :
int newAmount = mBarsCount + laps ?
I got the code but Just need some clarity on why we are not using this code?