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.
-
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.
-
-
Explain how one piece of Rail determines which other piece of
Rail is ``connected'' to it.
-
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.)
-
-
What instance variable in which class determines the speed of a train?
-
Describe the relationship between the value of this variable and the speed
of the train.
-
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:
- Make the SwitchRail a different color from other
Rails.
-
You must not alter the signatures of any of the public methods in the
class.
-
A train is allowed to enter a SwitchRail from either of the
switchable directions, even if the it is not switched that way.
For example, an SNERail toggles between a straight rail going south
and north and a corner rail going south and east.
Even if this rail is switched as a straight rail, a train can enter from
the east and exit to the south.
For a SNERail, the switch merely determines which way the train leaves
if it enters from the south.
What to hand in:
-
A printout of your SwitchRail class
as well as a copy of your SwitchRail.java file on disk
(see section on ``How to hand in your assignment'').
-
A written report on your testing strategy, how it was carried out, and your
test results.
-
A written description of any remaining bugs or missing features in your
SwitchRail class.
Label it ``Bugs and Missing Features in SwitchRail''.
If you are convinced that there are no bugs or missing features,
say so.
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:
-
We have not specified how you should implement Crashes.
This is up to you.
-
You need to think carefully about:
- which method in which class should detect a crash.
- how to tell the Car that has been
crashed into that it has been hit and should stop.
- how to stop all the Cars behind the crash scene.
- what information, and hence, instance variable(s), is needed
to record the fact that a train has crashed, so that it does not move
again.
-
If you design your changes as we have envisioned it, it should only involve
the modification of two files, Car.java and Rail.java, and only
require the addition of about 30 lines of code, including comments!
What to hand in:
-
Printouts of all the classes that you have modified for the Crash feature
as well as a copy of each of these .java files on disk
(see section on ``How to hand in your assignment'').
On your printouts, you must clearly identify
each place where you have made modifications.
We suggest you use a pen or a highlighter.
-
A report (no more than a page) explaining your design of the Crash
feature.
Use good writing style, as you will be graded on clarity and readability
as well as the contents of your report.
-
A written description of any remaining bugs or missing features in your
Crash modifications.
Label it ``Bugs and Missing Features in Crash''.
If you are convinced that there are no bugs or missing features,
say so.
Even though you are not required to hand in a report on testing for the
Crash feature, you should, of course, thoroughly test your modifications.
Otherwise, how would you know what bugs, if any, are in your code?
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.
-
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.
-
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.
- the number of array accesses.
- the number of assignments.
- the number of comparisons.
- the number of multiplications and divisions.
- 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