Solution Review: Car Parking Robot
The solution to the 'Car Parking Robot' challenge.
We'll cover the following...
Rubric criteria
Solution
Press + to interact
class CarParking{public static void main(String args[]){robot(10, 10, 19);}public static void robot(int width, int length, int cars){int counter = 0; // Number of parked carsfor (int i = 0; i < length; i++){for (int j = 0; j < width; j++) // Filling row-wise{if (j % 4 == 0) // Leaving three columns{if (counter < cars) // If any car left{System.out.print("đźš—"); // Parking the carcounter++;}else // No car left{System.out.print("đźš«"); // Marking space as available}}else // distance less than 3 meters{System.out.print(" "); // Adding the space}}System.out.print("\n"); // Row completed, move to next row}}}
Rubric-wise explanation
Point 1:
Look at the header of the robot()
function at line 8. It takes the width
and the length
of the car parking lot. Additionally, it also accepts the number of cars needed to be parked: cars
.
Point 2, 3:
We need to keep track of cars that are parked by the robot. We made the int
variable, counter
, for this purpose. According to the challenge, the robot needs to advance width-wise. We make a nested loop. The outer loop (at line 11) controls the vertical propagation (movement along the rows) of the robot, whereas, the inner-loop (at line 13) controls the horizontal propagation (movement ...