Required Java Skills: Writing functions, for loops
Sample code available for educators. 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.
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.
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.
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.
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.
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.
Write a method that returns the temperature measured by the Finch in Fahrenheit.