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:
- move forward a distance
- turn around
- go back to its starting position
- turn around again
- 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.
© 2001, 2004 by Scott Anderson, Frank Klassner, Pam Lawhead,
and Myles McNally. This work is supported by NSF grants 0088884 and 0306096. Permission
to use, copy, adapt and modify this lab for instruction purposes is granted.
These materials can be obtained from our web site
www.mcs.alma.edu/LMICSE. If you have suggestions
for improvement, please contact us via the web site; we would really appreciate
it. This file was last modified on
June 4, 2005.