Assignment 7

COMP102/112 2022 Tri 1: Assignment 7

days Due 12 May 10am


The FloorCleaner program will give you experience in defining classes for objects with fields and constructors. This program involves objects that move around in a simple world. Unlike the previous assignment, you have to complete not only the Robot class describing Robot objects, but also the method in the FloorCleaner class that runs the simulation.


Download the zip file and extract it to your home folder. It should contain template for the Java program you are to complete. Read through the whole assignment to see what you need to do.

Later when the model answer to assignment 6 becomes available, make sure you understand all the components of the programs.


  • Floor Cleaner:
    right Write a program to implement a simulated robot vacuum cleaner that moves around a rectangular floor area, "cleaning" the floor as it goes.

To Submit

You should submit your versions of, and by the due date.

Floor Cleaner

The Roomba is a robot vacuum cleaner made by the iRobot company which will autonomously run around room cleaning the floor. It is a low round wheeled robot that goes in straight lines until it hits something and then changes direction.

One of the problems with designing such a robot is working out a good control strategy to enable it to cover an entire floor area fairly efficiently without requiring fancy sensors such as cameras or range finders. The simplest strategy is to make it go in a random new direction whenever it hits something.

You are to write a program that provides a visual simulation of a roomba cleaning a rectangular region with no obstacles in it (the simplest version of the task), to allow a user to see how efficient it would be.

Run the demo or watch the video before you start.


Your program design should consist of two classes:

  • FloorCleaner which runs the simulation, and
  • Robot which represents the robot.

The Robot class should have fields to represent the state of the robot (position and direction), and other fields to store other values it needs.

It should have a constructor, and six methods:
  • draw() which should draw the robot.
  • erase() which should erases the robot, cleaning the floor under it.
  • getX() which should return the x coordinate of its current position.
  • getY() which should return the y coordinate of its current position.
  • step() which should make the robot move one unit in its current direction, erasing the "floor" it was on.
  • changeDirection() which should make the robot move backwards one unit and then change to a new direction.

The method cleanFloor() in the FloorCleaner class should first draw a grey floor area and create a new Robot object at a random position. It will then loop, repeatedly making the robot take one step, then checking whether the robot's position is off the floor. If so, it will make the robot step back onto the floor and change its direction.


  • Modify the cleanFloor() method to have two robots cleaning the floor. Your program will need to check whether they have bumped into each other, in which case, they should both change direction.
  • Draw the robots in a way that shows the direction the robots are currently moving (eg a small circle towards the "front").


Extend the program in two ways:
  • Add a couple of "pieces of furniture" - rectangular or circular regions that the robots should not be allowed to go into.
  • Changing directions randomly does not clean the whole floor very efficiently. Give the robot a more sophisticated movement strategy to clean the floor faster.

To go further

It is important to understand how Java works, as well as writing programs in Java.

  1. For the Robot class, you had to choose fields to store the state of the robot. There are several reasonable choices for this. What choice did you make? What other choices could you have made? What were the consequences of your choice when you came to write the methods - what was easier your way, and what was harder your way?