Lab Exercise for Assignment 7

COMP102/112 2022 Tri 1: Lab Exercise 7


This assignment will give you experience in defining classes for objects with fields and constructors.

It is specifically designed to help you with the FloorCleaner program you have to write for Assignment 7.


Download the zip file for lab exercise 7 and extract it to the COMP-102-112-2022T1-Lab7 folder in your home folder. It should contain templates for the exercise program. Read through the exercise and run the demos so that you know 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.


Shunting Yard Exercise

Run the demo or watch the video before you start.

ShuntingYardExercise is a very simple program that runs a simulation of a train in a shunting yard:
  • The train runs on a short track which has a buffer at each end.
  • The train starts at the left buffer, then moves forward to the right buffer,
  • It then reverses direction and moves back to the left buffer, and reverses again
  • It keeps doing this forever.
You have to complete the simulateTrain() method.

The Train object has fields that remember
  • its current x-position,
  • its current speed (positive to the right, negative to the left) and
  • the size of the train

The Train object needs a constructor and 5 methods which you must complete:
  • The parameters of the constructor are the initial x-position, and its size.
  • Two methods to step and reverse:
    • step() makes it go forward one step in its current direction.
    • reverse() makes it go backward one step, and then take the opposite direction.
  • Two methods to report its current position:
    • getFront() returns the x coordinate of the front of the train.
    • getBack() returns the x coordinate of the back of the train.
  • A draw() method