Throughout these lessons, you have used a wide variety of Finch methods. In this lesson, you will write your own methods to make the Finch do new things. To write a method that uses the Finch, you will often want to declare the Finch as a private static variable. You should create the Finch object inside the class constructor. This is shown below for a class called FinchMethods.
Lesson 10 – Methods with Finch
Declaring and Calling a Method
After you create a private static variable for the Finch, you can then declare methods that use that variable. To declare a method, you must give Java some information about what kind of method you are creating. In this lesson, we will create methods that are public and static. You will learn more about these keywords when you study classes. We will start by writing methods that do not pass a value back to the program. These methods have the keyword void in the declaration.
As an example, this code defines a method named drawSquare() that moves the Finch in a square. The name of a method should not contain any spaces or punctuation. In the method declaration, the name is followed by a pair of parentheses and then a pair of curly braces that contain the code that makes up the method.
After you define a method, you can use the name to call that method. For example, the code above calls the method drawSquare() in main(). Methods enable you to easily reuse code. Methods are also a tool for abstraction. Abstraction is the idea that you can make programs easier to use and understand by placing self-contained groups of actions within methods. A programmer can then use the methods without understanding the details of the code within them. For example, someone could use the drawSquare() method without needing to know what is inside it. Good documentation is essential to ensure that a method can be used correctly by another programmer, so be sure to comment your method!
Define the drawSquare() method. Then use a for loop to draw 6 squares. Turn left 60° between each square. What other patterns can you draw with the drawSquare() method?
Write a drawCircle() method, and then use it to create interesting patterns.
Create a “wall following” method. To follow a wall, your method should include these three steps:
- Move forward in a curved path (curving to the left or right) until an obstacle is detected.
- Move straight back a short distance.
- Rotate in the opposite direction (45° to 90°).
To test your method, write a program that calls it repeatedly. You will need to adjust the distances and angles in your method to make your robot perform well. Your program should be able to handle corners as well as straight sections of wall. Test your program on your classroom wall, or construct a small maze out of cardboard!
Methods with Parameters
When you call the method, you provide a value for the parameter (5 in the example below), and the method is executed with the parameter variable equal to that value. Notice that it is okay that we have two methods that have the name drawSquare(). Java can tell them apart because one requires a parameter and one doesn’t.
Define the drawSquare() method that takes a parameter. Then use a for loop to draw 5 squares with random sizes between 5 and 20 cm. What other patterns can you draw with this method?
Write a method named blinkAll() that takes three parameters named red, green, and blue. This method should blink all of the Finch LEDs (beak and tail) in the color given by the parameters. The lights should blink on and off just once when you call the method. Test your method by using it to blink the lights until button A is pressed.
Methods that Return Values
Write a method named isLevel() that returns a Boolean value. The method should return True when the Finch is in a level position and False otherwise. Use this method and your blinkAll() method to blink the Finch lights until the Finch is moved out of the level position.