Using Methods with Parameters and Utilizing Scope
Methods are all well and good as you know them now but they can't do much if they can't communicate with the functions that utilize them. To this end there are two ways that a method can directly communicate with other methods. One is to take in parameters as information to use inside the method and the other is to send back information to the method that called it as a return.
Another valuable piece of information is to consider what variables can be used by different methods in a class. It would be nice to have variables that can be utilized by multiple methods in a class. To do this we can just pass the variable around like described above but we can also create a variable that is global as opposed to the local variables that you have worked with thus far.
Grading Requirements
For each of the tasks in this lab:
- Demonstrate to the instructor or lab TA that you 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
Basic understanding of Java's Math class, how to create methods which
return values, and the Turtle commands.
Materials
The standard Turtle robot.
The Math Class
The java.lang.Math class is a set of functions provided by
the java structure to allow for more advanced mathematical functions.
Some of these functions include power (pow), absolute value (abs), square
root (sqrt), etc. For a complete list of functions provided by the Math class
see the Sun
Java 1.5 API which contains a complete list of the different classes
provided by the java framework and what methods they contain. To find Math just
scroll in the bottom left hand frame for the Math class and
then click it to bring up its specifics. Click
here to go directly to the Math class API.
Task One: Global Variables
Begin by creating a new project and copying into it you solution to
Task 5 from Lab 3 (the "Hour Glass Patrol"). Then modify it as follows.
Make the Random variable global: To make the Random variable global, we need to declare and initialize it outside of all of our methods, but still inside our class. This allows all the methods within the class to use this variable. Hence, we no longer need to pass it when we call methods, so remove those parameters from the method calls and method headings where necessary. Also, we no longer need the variable to be declared in the main method.
Now run your program to verify that it still works as before.
Task Two: Method Parameters
Now we want the size of the hourglass patrol to vary from one patrol
to the next. Modify your hourglass method to compute a random distance
(i.e. travel time) between 1000 and 4000. Pass this value to each of
the triangle methods called by hourglass and use this travel time in
those method's forward commands. Verify that your program is working correctly.
Task Three: A Function Method
We are now computing random values in a few different places in our
program: a random turn time in the main method and a random travel time
in our hourglass method. Rather than duplicate this code over and over,
create a function method called randRange which
is passed two ints, the lower end of the range and the higher end of
the range, and which returns a random integer in that range.
Now modify your program so that the main method and the hourglass method
call the randRange method to compute their need values. Run your program
on the Turtle robot to ensure that everything still works as before.
Task Four: Using the Math Class
Now let's incorporate some more function
methods and employ some of the builtin methods of the java.lang.Math class.
Create a new copy of Task Three and call it Task Four. Now modify
the code to go in a pattern that takes the Turtle straight out
a random distance from its random turn, turn 90°, go half
the distance it went out forward, turn back towards base and go
the proper distance back to the base. Have the hourglass method
compute this random distance and pass it to the triangle methods
(a different distance for each). The Turtle's path won't look
much like an hourglass anymore.
In your solution, define another function method which is passed
the lengths of the two sides of a right triangle and computes
the length of the hypotenuse. Call it hypoLength.
This method should be called by your triangle methods.
The
Pythagorean
Theorem
|
Hopefully you have seen and remember the Pythagorean
Theorem, which states that the length of the hypotenuse is the
square root of the sum of the squares of the lengths of the sides!
|
Task Five: Another Function Method
Lastly something a little more complicated.
You are to redesign Task Four into Task Five, which has both sides
of the right triangle be random distances. To do this you will
need to compute the return angle, which is labeled a in the diagram
to the right. In your solution define a function method returnAngle which
is passed the lengths of the two sides and return the angle the
Turtle needs to turn to return home. Call this method from your
triangle methods.
A
Little
Trigonometry
|
In your Trigonometry or Precalulus class you no doubt
studied the trigonometric functions such as sine, cosine, and tangent.
One such function that comes in handy in robotics is arctangent,
which can be used to compute the value of the angle a in
the diagram above. a is equal to the arctangent
of x divided by y (x / y). In Java's Math
class this function is called Math.atan().
However, It is important to consider the case when y is 0 (x / 0). This is illegal because as we all know, we cannot divide by 0. But, in looking at the picture above, if y is 0, then the return angle would be 180°.
Note that the atan method returns a value which is assumed to be in radians. In order for this code to work easily with our calibrated turtle, we would like this to be in degrees. The math class provides us with a simple method, Math.toDegrees(), which receives in a double (radians) and returns a double (degrees). You will of course have to cast this returned double as an int.
|
Some Additional Problems
Write any other methods that you think may simplify your code, such
as methods that work with the mathematical computations in Tasks 4 and
5.
Conclusion
In this lab, you learned the following:
- How to implement a global variable and when it is appropriate to use.
- What issues are involved with scope in a program.
- How to implement methods that take and return values in their call.
- How to effectively use the Math 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.