Java Java Objects Harnessing the Power of Objects Overload Methods

Sandy Woods, Jr
PLUS
Sandy Woods, Jr
Courses Plus Student 1,930 Points

Method Overloading in Java

Some users of our GoKart class wrote and asked that our drive method accept a parameter to specify how many laps to go, instead of just one. It happens, well hey at least you can practice your addition and subtraction shortcuts.

Modify the drive method to define a parameter of how many laps should be driven. Update the method body to handle the new parameter.

The drive method works and is correct but the logic doesn't make sense: barCount = barCount -laps(laps should be driven?!?). Why not barCount-= lapsDriven? Wouldn't that decrement one bar per lap?

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 laps) {
    lapsDriven += laps;
    barCount-= laps;
  }
}

2 Answers

Ezekiel dela Peña
Ezekiel dela Peña
6,228 Points

Simply because lapsDriven is the total of all the laps that you've done, while the laps corresponds to the current number laps you will be doing.

Visual example:

assuming I have editted the GoKart class, so that I can have a getters for barCount and lapDriven

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;
        this.lapsDriven = 0;
    }

    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 int getBarCount() {
        return this.barCount;
    }

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

    public int getLapDriven() {
        return this.lapsDriven;
    }
}
        GoKart goKart = new GoKart("red");
        goKart.getLapDriven(); // will return  0
        goKart.charge(); // barCount now will be 8
        goKart.drive(3); // barCount will be 5
        System.out.println(goKart.getBarCount());
        goKart.getLapDriven(); // will return 3
        goKart.drive(2); // barCount will be 3
        System.out.println(goKart.getBarCount());

but if we implement the code to the one you're saying

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

Then

        GoKart goKart = new GoKart("red");
        goKart.getLapDriven(); // will return  0
        goKart.charge(); // barCount now will be 8
        goKart.drive(3); 
        System.out.println(goKart.getBarCount()); // barCount will be 5
        goKart.getLapDriven(); // will return 3
        goKart.drive(2);  
        System.out.println(goKart.getBarCount()); // barCount = 0, which is logically incorrect
Sandy Woods, Jr
PLUS
Sandy Woods, Jr
Courses Plus Student 1,930 Points

This code was helpful. Please explain this statement: System.out.println(goKart.getBarCount()); // barCount = 0, which is logically incorrect

I got 3 for barCount and 5 for lapsDriven