Lesson 10 – Writing Methods for Finch

Required Java Concepts

Writing functions, For loops

Teaching Materials

Get Access

In the previous lessons, you have learned to use the existing Finch methods to write your programs. 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.

You can then declare methods that use the Finch. For example, the method below was designed to make the Finch turn 90°. This method uses only the static Finch variable, so the method is static as well.

Exercise 1:

Try out the method above for your robot. Adjust the wait time to make your robot turn 90°. Notice that the Finch is moving at a low speed so that it will make an accurate turn.

You can also create Finch methods that take parameters. So far in this course, you have adjusted wait time and speed to change how far the Finch moves, but moving a specific distance has required some trial and error. Now you can write a function that accepts a distance in centimeters and makes the Finch move that distance! To do this, you will measure how far the Finch moves for different time periods, and then you will use this information to figure out how to calculate the wait time needed to move a particular distance.

Exercise 2:

For this activity, you will need tape and a measuring device, such as a yardstick or a tape measure. Write a program to make the Finch move forward with a wheel speed of 125 for 0.5 seconds. Then measure how far the Finch moves. You may find it helpful to mark the Finch’s starting point with tape.

Record the measured distance under “Trial 1” in the data table below. Repeat this process twice to record two more trials for a time of 0.5 seconds. Continue to change the movement time and measure distance to complete the data table.

Use your data to write an equation for distance in terms of speed and time. Based on your data, what does a Finch speed of 0.5 correspond to in cm/s?

Solve your equation for time. Now you can take a distance as input and calculate the time required to move that distance.

Exercise 3:

Use your equation from Exercise 2 to write a method that takes a distance in centimeters as a parameter. The Finch should move that distance at a speed of 125.

Exercise 4:

Use the two Finch methods you have written in this lesson to create a third method named drawSquare(). This function should accept a distance in centimeters and have the Finch move in a square with a side length equal to the input distance.

You can also write Finch methods that return a value! For example, the method below returns the larger of the two light values measured by the Finch.

Exercise 5:

Write a method that returns the temperature measured by the Finch in Fahrenheit.