Lab 8: Manipulating Arrays: a Robot Scheduler

Copyright RIT 2005
$Id: writeup.xml,v 1.5 2009/10/22 23:55:49 vcss231 Exp $


Team Setup

You are to work on this lab in teams of size 2. Students who do not abide by this rule will not get full points for in-class participation.

Overview

Ask Dr. Tiger

Objectives

  1. Write code that manipulates arrays.
  2. Learn how to remove an element from an array.
  3. Gain more experience with Javadoc documentation.
  4. See how a program can be developed in a stepwise fashion.

Background

In this lab, you will write a class that contains schedule information for a robot (simply a list of points to visit). This class will also provide several methods for examining and modifying the schedule. The purpose of this lab is to give you practice with reading Javadocs and implementing a class from them, and to write code that makes (extensive) use of arrays.

For the first activity, you will write stubs for the RobotSchedule class, and implement methods to create and print the schedule as well as the total distance traveled.

For the second activity, you will write methods that allow you to swap points in the schedule and delete points from the schedule.

Pre - Lab Work

  1. Read this entire document.

  2. Review your course notes and textbook information on arrays.

  3. Read about creating random numbers in Java (RandomNumbers.html)

In-Lab Activities

Activity #1 : Creating and printing the schedule

To get started in this lab, you will need to understand what your code is supposed to do. Read the documentation for RobotSchedule. This shows all of the methods that you are required to provide, though you may choose to implement additional methods in your program. Note that if you choose to implement additional methods, they must be declared "private" in order to not violate the interface defined in the Javadoc. You should also download the jar file lab8.jar (http://www.cs.rit.edu/~vcss231/pub/lab08/lab8.jar) which contains two additional classes. ScheduleManager provides a simple text interface to the schedule, asking the user questions about what to do to the schedule and then calling the appropriate RobotSchedule methods. Point is the type of object that your schedule will contain. You will not be modifying either of these classes, so it is not critical to understand the details of how they are implemented.

The next step will be to create a skeleton for your RobotSchedule. Write stubs for all the required methods and compile and run the ScheduleManager. This may give you a better idea for how the two classes are meant to interact.

The last part of this activity is to write the methods RobotSchedule.generateSchedule(int), RobotSchedule.printSchedule() and RobotSchedule.totalDistance(). Note the named constants to be used in these methods - also you should pay attention to the difference between the maximum possible schedule length (MAX_POINTS) and the actual length of the current schedule, which may be smaller (and therefore should be kept in a separate non-constant instance variable)!

When you print the schedule, it should be in the format:


1. (2,3)
2. (5,7)
3. (0,9)
...
Total distance traveled: 37.23548774

(Of course your actual numbers will vary.) The distance will be computed in RobotSchedule.totalDistance(), using the standard distance formula d = sqrt((x2-x1)^2+(y2-y1)^2). The method should compute and return the total distance of the path, summing the distance between each consecutive pair of points.

To generate points for the schedule, you should use an object from the Random class to generate integers between 0 and MAX_PVALUE inclusive. You will need to create new Points when you build the schedule, so check the constructor for that class as well.

How To Submit

When you have compiled your code and tested it to your satisfaction submit it using the following command:

try grd-231 lab8-1 RobotSchedule.java

Since you should not be modifying the other files, there is no need to submit them.

Activity #2 : Modifying the schedule

After generating a schedule, a robot programmer might decide that the distance looks to be more than his or her robot is capable of. In this activity, you will support deleting and reordering of points within the schedule.

The RobotSchedule.swapPoints(int, int) method should do just as it says, swap the two points given by the two numbers. HOWEVER, something sneaky is going on here! Java numbers array elements starting at zero ("zero-indexed"), but when you printed the schedule, you started at 1 ("one-indexed"). Since these latter numbers are the ones the user will see, the numbers given to swapPoints are one-indexed, so make sure you are swapping the right elements. You should also check that the given indices are valid, so that you aren't swapping a point with a nonexistent element.

Finally, RobotSchedule.deletePoint(int) will remove a point from the schedule, but also tidy things up so that there is no hole left behind. (The argument to this method is also one-indexed, so be careful.) The diagram below gives a good explanation of how to accomplish this.


Remove Element from an Array

In order to delete an element and leave no gap in the array, you have to start at the element that you wish to delete (in the figure, it is the third element of the array), and copy the next element over the one you want to delete. Continue walking down the array, copying each element into the previous spot, until you get to the end. At the last element, you should replace with a null value. Also, don't forget to update your count of the number of points.

How To Submit

When you have compiled your code and tested it to your satisfaction submit it using the following command:

try grd-231 lab8-2 RobotSchedule.java


Grade Computation

Grade Breakdown: