educative.io

Sample Solution Code for Exercise: charge! is wrong

Sample Solution Code for Exercise: charge! is wrong

class ChargingRobotDemo {

  public static void charge(SimpleRobot r, int maxDist) {
    int i = 0;
    while (i < maxDist && !r.blocked() ) {
      r.forward();
    }
  }
  
  public static void main(String args[]) {
    SimpleRobot r = new SimpleRobot();  
    r.forward();
    r.right();
    charge(r, 3);
    r.left();
    charge(r, 20);
  }
  
}

in the charge method variable i is declared and initialized, but never user.
for meeting the maxDist condition the i should be incremented or madDist needs to be decremented

public static void charge(SimpleRobot r, int maxDist) {
    int i = 0;
    while (i < maxDist && !r.blocked() ) {
      r.forward();
      i++;
    }
  }

.

public static void charge(SimpleRobot r, int maxDist) {
    while (maxDist-- > 0 && !r.blocked() )
        r.forward();
}
2 Likes

Hi @Prashant_Kumar_Meena, thank you so much for pointing this out. We have fixed this issue, and please keep letting us know if we find such things. Thank you.

this has still not been fixed. Thanks.


Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers

1 Like

Hi @gabbagobby

I am extremely sorry to getting to you so late. The links you have shared is not working. Can you please share the working links. I will check it out and let you know. Thanks :slight_smile:

3 and a half years after this bug was initially reported… it is still broken. :unamused: Please fix the example solution.

Hello @Ian
Apologies for the delay. Thank you for pointing that out. We have updated it accordingly.

Happy learning.