The key concepts for this recitation are those of elementary object-based programming (no inheritance): objects, classes, typed instance variables, methods, local variables. Also students need to begin to recognise Java syntax, especially spotting a type definition, and noticing the nesting of syntactic features (ie matching braces). A sample program has been written to illustrate much of this.
Begin by thinking of the overall structure: there is a real-world system of balls bouncing on a table, and a computer system that contains a model of this. The real-world system has a table, with coordinates. The top-left corner is at (0, 20), for example. There are balls: at any time each ball is somewhere on the table, and moving around. If the ball reaches a corner, it is stopped there; if it reaches a wall, it bounces elastically. The real-world system starts with two balls, and more are added at regular intervals.
The program source is found in various files, and it is executed to give a computation and produce output.
As the program runs, there are a (changing) collection of objects that exist; each object is an instance of one class; each object has instance variables (fields), each of which may have a primitive value (like double or boolean) or may refer to some other object. Also, at a time while a method is active, its local variables also have values.
The program has separate files for each class, so the code for class Ball is in file Ball.java, etc. The same name can be used for instance variable and method (they are distinguished by context) but it is unwise to make duplicate use of a name for local variable, argument, and/or instance variable. Note that Java is case-sensitive. Conventions are: variable names and method names start with lower case; class names start with upper case.
Look at the code of Ball. Think about the three Ball objects in the diagram handed out (which shows the state of the program just before the first call to the step method of field). Note that each has its own values for the instance variables. Look at the code that declares these instance variables (lines 17 to 19). Notice that each declaration gives the type of the variable (it restricts the possible values that variable can take on).
Look at the code for horizontalBounce (lines 80-93). Notice how the code is found after the method signature, which is line 84. Notice the local variable timeToBounce which is declared and given a value (line 85). Notice on line 86 the weird syntax for boolean operators (|| for "or", also there are && for "and" and ! for "not" and "==" for equality). Notice how the braces match (line 86 with 89) to see what code is executed when the if condition is true (see the "return" which causes the method to return rather than carry on after the "if" as usual).
When a method is called on a ball, the code uses (and may alter) the values of the instance variables of that instance. Predict what would change if we called the stop() method on the a ball balls.elementAt(1) shown on the diagram (ans: its xVelocity and yVelocity would become 0.0 instead of 3.0 and 1.0 respectively, and no other instance variables in it or any other object would be altered). Notice the constructor (line 21-30): its name is the same as the class, and it has no return type (as distinct from a method like stop, where the return is void). Notice that a method like predictLocation is called by b.predictLocation(0.1), or by b.predictLocation(t) where t is a variable whose value is passed as the argument; in contrast the constructor is called by new Ball(position, a, b).
Now look at the code of Field. Find where in the code we find the description of the instance variables in a Field object (they are balls (a Vector object) and four corners). (ans: lines 13-17) The Vector class is part of the Java libraries; for the present students can accept that a Vector object somehow is a container which has references in it to Ball objects.
Look specifically at the step method on lines 37 to 74. This has several local variables (b defined on line 45), current (line 41), next (line 42), i (line 44)), and also that its structure has nested control aspects: there is a loop (lines 44 to 73), which takes us to each ball in turn, and in the body we have many boolean tests involving that ball.
Work on the following two little problems. We suggest that you first try to work out what you want to do, (eg decide what algorithmic idea to use, whether intance variables or local variables are needed, what the signature will be if a method is being designed) then try to express that in Java (you can usually just imitate the existing code for most aspects of Java syntax; if you have a concept and don't know the syntax, you can look in reference books).
In answering question B you will need to go through the vector balls, checking each one. This question is just one of many similar "code cliches" which can all be done in the same style, as a loop through the Vector. It's useful to have a consistent idiom which you use whenever you need to traverse a vector. (A good discussion is in "The Practice of Programming" by Kernighan and Pike, section 1.3) For example, similar code can be used to determine whether all balls have stopped
boolean allStopped = true;
for (int i=0; i< balls.size(); i++) {
Ball b = (Ball)(balls.elementAt(i));
if (!(b.isStopped())) {
allStopped = false;
break;
}
}
or to set furthestBall to be the ball with largest x coordinate
(the variable should be null if there are no balls at all)
Ball furthestBall = null;
for (int i=0; i< balls.size(); i++) {
Ball b = (Ball)(balls.elementAt(i));
if (furthestBall == null) {
furthestBall = b;
} else if ( furthestBall.getLocation().x() < b.getLocation().x() ) {
furthestBall = b;
}
}
As a further exercise, how can one similarly write code to