TIK2O JUNE EXAM-Robots: Part 2 (Practical)

Wednesday, June 9th, 2004. 1:00 pm.

INSTRUCTIONS TO STUDENTS.
  1. Students will do one question from PART A (either Question 1 or 2) and one question from PART B (either Question 3 or 4).
  2. Questions 2 and 4 have an increased DD (Degree of Difficulty). Your raw mark on these questions will be multiplied by the indicated factor to arrive at your adjusted score.
  3. PART A questions do not use secondary class files. All code is to be included in the driver file (the file containing the main method). You are free to use whatever supporting classes are built into the becker library.
  4. Evidence of Stepwise Refinement strategies will be sought when I examine your code.
  5. Be sure to comment your code as necessary.
  6. It is imperative that you name your files exactly as requested. Attach all (and only) the requested files to your email to handin at the end of the exam.
  7. Good luck.
PART A. Question 1. (DD:1.0)

Detour.java (Sample Run)

Preamble. As is the case every summer, road crews are out repairing potholes created by the freezing and thawing of our Canadian winters. Street 1 was particularly hard hit and, as a result, barriers (Walls) have been placed at various intersections along Street 1. To navigate Street 1, it is necessary for Karel to take a short detour around the barrier to Street 0 and then return immediately back to continue his route.

Task. Karel is responsible for delivering flyers from (0,1) to (9,1). Write and submit the single source file, Detour.java, that uses a configuration of barriers in the text file, Detour.txt, to accomplish the task in a manner similar to this example.

PART A. Question 2. (DD:1.1)

PlantSeeds.java (Sample Run)

Preamble. Each spring, Karel the farmer, must plant seeds in his field. The dimensions of his field are 10 by 10, and extend from (0,0) to (9,9). Unfortunately, with the price of seeds skyrocketing, Karel can't always afford to buy enough seeds to plant the entire area, but he does his best with the seeds (Things) he has, using a row major strategy as can be seen in this example.

Task. Write and submit the file, PlantSeeds.java, that creates a Robot object to plant the field as in the example. Randomly allocate the robot an initial allotment of between 70 and 100 seeds (Things). Your robot plants one seed per intersection until s/he has no more seeds left and then simply stops.

PART B. Question 3. (DD:1.0)

FollowTheLeader.java (Sample Run)

Preamble: A popular playground game is 'Follow the Leader'. Typically, one kid marches around making moves and everyone behind him is expected to follow his or her move. To make things more interesting, Walls are constructed in the simulated playground and the leader turns left or right depending on the coordinates of the intersection he is on, as follows. If the sum of the Avenue and Street coordinates is even, the leader turns left. If the sum of the Avenue and Street coordinates is odd, he turns right. The game continues until the kids are all standing on a Thing.

Task. Develop and write the driver file, FollowTheLeader.java, that constructs a 7 by 7 City from the text file, Obstacles.txt, instantiates 5 Kid objects named as in the UML diagram above (only the Leader is labeled), mounts the objects on separate threads, and plays Follow the Leader until coming to rest on the intersection containing a Thing. Submit only FollowTheLeader.java and Kid.java.
PART B. Question 4. (DD:1.2)

Traffic.java (Sample Run)

Preamble. A traffic light can be placed on a Thread and operate independently in a bid to control traffic flow. In this example three such traffic lights are placed at random locations on Streets 1 and 2. The duration of the green, yellow, and red lights have been set to (sleep()) for 8000, 3000, and 8000 ms, respectively. A taxicab (RobotSE) starts out at (0,1) and sticks to his circuit of the City from (0,1) to (10,2). As all good drivers do, cabbies obey the traffic lights, coming to a stop when on an intersection where the light is RED, proceeding when on an intersection when the light is GREEN, and speeding up (slightly), when the light is YELLOW.

Task. Duplicate the example using three classes. The main driver class file (Traffic.java) sets up the City and launches four threads; three for each of the traffic lights (TrafficLight.java) and one for the taxicab (Taxi.java). Run the simulation indefinitely.