Prerequisite Knowledge
This lab assumes that you have the knowledge to properly write methods and use control statements, as well as the knowledge needed to control the Turtle's movements.
The relationship between a method call and the method itself is a kind of interface, a word that in English means the surface where two things meet. Computer Scientists have adopted the word "interface" to talk about how two things can communicate or interact.
In Java an interface is a collection of variable and method definitions. Classes are said to implement a particular interface if they define each of the variables and methods within it. Implementing an interface is like agreeing to a contract - the class which does the implementing agrees to provide a certain set of services. Different classes may implement these items in different ways.
For example, if we declare an interface with three methods (method1, method2, and method3) each class that implements this interface must include three methods (method1, method2, and method3). In addition, the parameters and return types must be the same between the interface and the class that implements it.
Although we are required to implement each method that is in the interface in our implementing class, we are not restricted to these methods. Our implementing classes can have additional methods that are not listed in our interface. Here is an example of what our code might look like for an interface and an implementing class:
interface Example{
public void method1 ();
public boolean method2 ();
public int method3 (boolean x);
}
class Filler implements Example{
public void method1 (){
code...
}
public boolean method2 (){
code...
}
public int method3 (boolean x){
code...
}
}
This lab assumes that you have the knowledge to properly write methods and use control statements, as well as the knowledge needed to control the Turtle's movements.
To start, download the BlueJ project Lab8Task1, unzipping it if required. You will see four files:
Of particular interest is the Raceable interface, which contains the following:
public interface Raceable {
// calibrate the Turtle for turning public void prepare(); // run the race, recording time required public void run(); // report if the last race was completed in less // than m milliseconds public boolean report(int m); }
implement the Raceable interface in the Straight class. In your implementation
You will also need a constructor for each implementation. Then add code in the main method in the Task1 class to create the object and run the race.
In order to record the time that it took to run the raceable object, you will have to use the Turtle's
Turtle.timeInterval(x, y);method, which takes in two long's (x and y) and returns their difference as an int. This is due to the fact that the Turtle's firmware does not allow us to do computations on long's.
Take your code from Task 1 and create another class (Wander) that implements the Raceable interface. This implementation should be identical to the Straight implementation, except that in the run() method, rather than just driving forward until hitting a wall, have the Turtle wander around by randomly moving forward and turning. When an obstacle is hit, stop and record the time that it took.
Modify your code in the main method so that it creates two objects (one of each implementation of Raceable), and runs each one in a race against the clock. Store the results from each race report, and use a series of if statements to produce different outcomes (play different tones) corresponding to the various outcomes.
Lastly, modify your program so that a race does not begin until the PRGM button is pressed. Where is the best place to make this modification?
In Task 3, you need to add a method to both of your implementation classes that returns the amount of time that it took to run each run() method. You do not need to add anything to the Raceable interface. Next, modify your main method so that rather than playing outcome tones as in Task Two, it obtains and stores the times of the two races, compares them, and plays different tones depending on which one finished first.
In this lab, you learned the following: