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 3: Methods

small logo

Writing Methods

A method is a logically coherent chunk of code that is executed when the method is "invoked" or "called" by another method. Typically, the call is traced back to the main method, which is the the first to be called.

Imagine that we define the following method:

public static void fred() {
  	Stuff to be executed...
}

This method is called fred, and could be called by another method by including the line

fred();

in that other method.

Generally speaking, method names are important because other programmers will want use the method name to guess what the method does. Your methods should be well-named: succinct and descriptive.

Note that fred is a void method, meaning that it returns no value. The caller invokes fred in order to do something, and so we say that fred is called "for effect," rather than to compute some value.

Grading Requirements

For each of the tasks in this lab:

  • Demonstrate to the instructor or lab TA that your robot successfully completes its mission, and have them initial the lab activity sheet.
  • Print off the code for that task, making sure that you have commented that code appropriately.
  • Turn in the initialed checkoff sheet and the code for each of the tasks by 5 pm on the Friday following lab.

Prerequisite Knowledge

This lab assumes that you have a basic understanding of how to code and run programs for the Turtle robot, such as basic controls to make the Turtle move, turn, stop, etc. One specific requirement is the knowledge of how to calibrate turns with the Turtle, so that we can simply enter in the degree of rotation when it is needed. In addition to the knowledge of basic controls, you should also be able to declare variables and manipulate them accordingly.

Materials

To complete this lab, you will need the basic Turtle robot with its set of double bump sensors.

Task One: The Sentry

Task 1 is to code the Turtle, so that it runs in a never-ending 'sentry' duty. The Turtle should play a 'beep' [ Turtle.beep() ] every time it completes a cycle. A simple, infinite loop has been put in place so that all you have to do is enter the code to make the Turtle:

  1. move forward a distance
  2. turn around
  3. go back to its starting position
  4. turn around again
  5. play a beep

In your solution, use variables for telling the robot how far forward to move and how much to turn. Use the following java skeleton to begin:

public class Task1 {
   public static void main(String[] args) {

      Turtle.calibrateTurn( , ); // Enter your calibration settings here

      while(true) { // The infinite loop, **DO NOT CHANGE**

         // Enter code for the sentry duty here

      }
   }

Task Two: Method-based Sentry

Take the code that you have written within the loop to get the Turtle to perform its sentry duty and place it in a separate method called sentry. This way, each time you want the Turtle to perform a sentry cycle, you should only have to write one line of code (a call to the sentry method) in the main method. Place this new method directly after the main method. Here's a skeleton for the sentry method:

public static void sentry () {

  //Enter code for the sentry duty here

}

You will also need to move the variable declarations into the new method, otherwise the operations in the sentry method would not know what is meant by calls to the variable.

Demonstrate that your Turtle will still perform its sentry duty correctly.

Task Three: Random Sentry

Task 3 is to cause the Turtle to rotate a random number of degrees (between 0 and 359), then it will head out and complete a sentry cycle. Once the Turtle has returned to the base point, it should randomly rotate again, and complete a sentry cycle again, etc. This rotation should be done in the main method.

LeJOS, the version of Java we use to program the Turtle, does not support all of the usual methods for the Random class. In particular, it does not support the constructor with no argument, nor the nextInt method which takes the int argument (the range). So in you solution you must create the Random object using the constructor that uses the seed (as discussed in class), and use the plain nextInt() method to get a random integer. Your problem then is to change that random integer value into one in the range [0 .. 359].

Note: You do not need to worry about turning left or right, just make it turn right each time.

Task Four: Triangle Sentry

Rather than a straight back and forth sentry, modify your program so that the sentry moves in a triangle shape. Call your new method triSentry.

You will also need to change your code so that you don't call the normal sentry method in the infinite loop, but instead, you call the triSentry method. As before, the Turtle should rotate randomly before going on its sentry duty.

Task Five: Hourglass Sentry

Write another triangle method that has the Turtle rotate in the opposite direction of your existing triangle method . So, if in the first triangle method you turn to the right then make the second method so that you turn to the left

Then create another method, called hourGlass, which calls one of the triangle methods then the other.

Lastly, have the main method call hourGlass (which then calls each of your triangle sentry methods).

As before, after each hourglass completion the Turtle should rotate a random number of degrees and then draw out another hourglass.

Conclusion

In this lab, you exercised the following skills:

  • Writing methods and calling them from other methods.
  • Generating random numbers using the Random class.