University of Toronto
CSC148F--Introduction to Computer Science, Fall 1997
Assignment 4: Train Simulation

Due:  Thursday 4 December, 6 p.m.
This assignment is worth 10 marks towards your grade in this course.

For this assignment, you will work with a Java program which simulates an electric train set. You will find the source code for this program on the course web page. The assignment consists of several parts.  


Part A

To begin with, you should run the TrainSimulation program and experiment with the various features of the program. Answer the following questions, which are designed to help you understand how the program works. Questions 2 and 3, together, should take no more than a page to answer.
  1. Draw an inheritance diagram which includes all the classes defined in the program. If class B is derived from class A, then show an arrow pointing from B to A. If a class in the program explicitly inherits from a Java API class (e.g., Thread), then show this relationship as well in your diagram. To distinguish the Java API classes from the ones in our program, draw a rectangle around each Java API class name and an oval around each class name from our program.
    1. Explain how one piece of Rail determines which other piece of Rail is ``connected'' to it.
    2. Explain how, when building a Track, you ``connect'' one piece of Rail to another. (Try designing a new track configuration by changing the constructor of the Track class.)
    1. What instance variable in which class determines the speed of a train?
    2. Describe the relationship between the value of this variable and the speed of the train.
    3. Explain what sequence of code is executed when someone clicks on the ``Accelerate'' button which causes the train(s) to speed up.

What to hand in: The answers to the questions.  


Part B

There are only two kinds of Cars in our program, Engines and Cabooses. Create a new class called CoalCar by extending it from the Car class. The CoalCar should be black in color but otherwise have the same behaviour as the Caboose. Test your new class by instantiating some CoalCars and having them run on the track. You must use the exact class name CoalCar.  

What to hand in: A printout of your CoalCar class as well as a copy of your CoalCar.java file on disk (see section on ``How to hand in your assignment'').  


Part C

The addToTrack method of the Train class puts all the cars of a train onto a track. This method is written iteratively. Modify addToTrack so that it calls a recursive helper method to accomplish the same thing recursively. You need to design the interface of this method. The signature of the addToTrack must not be altered.  

What to hand in: Hand in a printout of your modified addToTrack method and of your recursive helper method. Also, hand in a copy of your revised Train.java file on disk (see section on ``How to hand in your assignment''). There is no need to hand in any test runs, although you should verify that your modifications do not alter the outwardly perceivable behaviour of the program in any way.  


Part D

A SwitchRail is a piece of Rail which can ``switch'' between a StraightRail and a CornerRail. A description of the SwitchRail is given at the top of the SwitchRail class. To ``switch'' (or toggle) a SwitchRail, a user should click on it. However, this does not work in our program because the implementation of the SwitchRail class is incorrect and incomplete. Your task is to correct this.  

We have given you the declarations of all the instance variables of the class (along with comments describing how they are used) and the signatures of all the public methods. Also, we have given you all the completed subclasses of SwitchRail (for example, WESRail). Three SwitchRail methods (handleEvent, draw, toString) have already been implemented. However, the remaining eight SwitchRail methods (exitOK, validDir, register, unRegister, exit, nextRail, and the two versions of the constructor) have bodies that resemble their counterparts in the TwoEndRail class. So, for now, a SwitchRail behaves more or less as a StraightRail does. You need to correct the bodies of the eight methods mentioned above so that the SwitchRail behaves as it should.  

Come up with a test strategy for testing your corrected SwitchRail class and carry out this testing. Describe the test cases, how you set them up, and the results of your testing.  

Important notes:

What to hand in:


Part E

You should notice that, in our program, trains do not run into each other but rather through each other. This, of course, is not realistic. For this part, you are to implement the ``Crash'' feature.  

Here is how Crashes should work. Suppose Car X of Train B is on Rail R and Train A enters R. Then we say that ``Train A has crashed into car X of train B at rail R''. In this case, what should happen is that all of Train A as well as every Car from X to the end of Train B (including X) should stop and never move again. All the Cars of Train B which are in front of X (excluding X) should keep going. In other words, B gets decoupled at X and both A and the tail of B stop.  

Hints and notes:

What to hand in:


Part F

[This part is not related to trains.] Consider the Java method invertible (on the next page), which can be used to determine whether or not an n by n matrix is nonsingular (has an inverse). You do not need to understand how this method works -- it's not a very good algorithm anyway. We just want you to analyse the efficiency of it. Answer these questions.
  1. Give a careful analysis, showing all of your steps, of the worst case number of array accesses for a call of invertible(A,n). Express your answer in terms of the parameter n and using ``big-Oh'' notation.
  2. State whether or not each of the following list is a good measure of the efficiency of the method invertible. You do not need to justify your answers.
    1. the number of array accesses.
    2. the number of assignments.
    3. the number of comparisons.
    4. the number of multiplications and divisions.
    5. the number of subtractions.

   public static boolean invertible( double[][] A, int n) {

      // Preconditions: n>0 and A has dimensions n by n.
      // Postconditions: returns true if A is a nonsingular n by n matrix,
      //                 returns false otherwise.
      // Note that A maybe modified.

      for (int k=0; k<(n-1); k++) {

         int p = -1;

         for (int c=k; c<n; c++) {
            if (A[c][k] != 0) p=c;
         } // c-loop

         if (p == -1) return false;
         else {
            double temp = A[k][k];
            A[k][k] = A[p][k];
            A[p][k] = temp;
         }

         for (int i=(k+1); i<n; i++) {

            double M = A[i][k] / A[k][k];

            for (int j=k; j<n; j++) {
               A[i][j] = A[i][j] - M * A[k][j];
            } // j-loop

         } // i-loop

      } // k-loop

      return true;
   } // invertible


How to hand in your assignment

You must clearly label each part of your work for this assignment, arrange them in order (Parts A,B,C,D,E,F) and put them all in an 8 1/2 by 11 inch envelope, As well, you must also hand in all the .java files that you have modified or created on a 3 1/2 inch floppy disk. This should include CoalCar.java, Train.java, SwitchRail.java, any files you modified for the Crash feature. You must include only those modified files, and there must be absolutely nothing else on the disk. The files must be in the top directory. Be sure that your name and student number is on that disk and put the disk in the envelope as well. Finally, don't forget to fill out a cover page and tape it to the front of the envelope.

Marks will be deducted if you do not follow these instructions.  

That's all, folks!


                           TENTATIVE GRADING SCHEME  
                           ========================
                           
                            15 - Part A
                             5 - Part B
                            10 - Part C
                            20 - Part D
                            25 - Part E
                            15 - Part F
                            10 - Programming style, overall
                           ----
                           100 - total

Diane Horton
Tue Nov 18 10:43:34 EST 1997