LMICSE: Lego Mindstorms in Computer Science Education

Site Map | Contact Us
Project Overview | Staff | Grant Information
Short Workshops | Primary Workshops
CS 1 | Data Str. & Algo. | Prog. Languages | Architecture | Intelligent Sys. | Operating Sys. | Net-centric
Ada | C | C++ | Java | Lisp

Lab 8: Using Interfaces

small logo

Interfaces

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...
		}
}

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.

Materials

Basic RCX Turtle with bump sensors.

Task One

To start, download the BlueJ project Lab8Task1, unzipping it if required. You will see four files:

  1. Task1: the main class for this task
  2. Turtle: the usual Turtle class
  3. Raceable: an interface
  4. Straight: the very beginnings of an implementation of Raceable

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

  • prepare(): calibrates the Turtle for turning.
  • run(): drives the Turtle forward until it runs into an obstacle, and then stops it and records the amount of time in milliseconds that it took to hit the wall by setting the value of an instance variable.
  • report: receives a time limit in milliseconds, and returns whether or not the Turtle found the wall in less time than the time limit it received. Play a beep at the end of the method.

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.

Task Two

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?

Task Three

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.

Some Additional Problems

  • Add any more useful methods that you can think of to your implementations
  • Create a third class that implements Raceable and run it against the first two implementations

Conclusion

In this lab, you learned the following:

  • How to implement an Interface using a class
  • The need for a contract between interfaces and the objects that implement them
  • That there are multiple ways to implement one interface