Search⌘ K
AI Features

Solution Review: Car Parking Robot

Explore how to build a car parking robot in Java by using nested loops to navigate a parking lot grid. Learn to control iterations, apply conditional checks for parking spots, and track parked cars. This lesson helps you grasp core iteration patterns and practical problem-solving for programming projects.

Rubric criteria

Solution

...
Java
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 cars
for (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 car
counter++;
}
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 ...