Java Java Objects Harnessing the Power of Objects Overload Methods

Bolatito Seriki
Bolatito Seriki
466 Points

I need the correct answer to this question.

Create a new method named drive that accepts no arguments. It should call the newer drive method passing in a 1 for the default.

GoKart.java
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; 
}

public void drive(int lapsDriven) { 
   laps += lapsDriven; 
  barCount -= laps; 

} 
public void drive()
  {
   drive(1);
  }

2 Answers

Bolatito Seriki
Bolatito Seriki
466 Points

Could I see how you did it?

Clayton Perszyk
Clayton Perszyk
Treehouse Moderator 46,473 Points

Bolatito Seriki

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; 
}

public void drive(int lapsDriven) { 
   laps += lapsDriven; 
  barCount -= laps; 

} 
public void drive()
  {
   drive(1);
  }

}
Pratham Mishra
Pratham Mishra
10,675 Points

You have to add another constructor for the drive method which doesn't return anything (void). In that method using the increment and decrement operator, increment the lapsDriven and decrement the barCount.

I hope these hints may help you to solve the problem.