JavaJava: Lesson 1 – Speaking with the Finch


Resources available for teachers.   

Required Java Skills: Creating and running a program; declaring a variable; printing text to the screen; accepting user input and storing it in a variable

This Finch is a small robot designed for students learning computer science. You can write programs to move and turn the Finch, light up its beak, and collect information with its sensors. As you write programs, you will be able to test your programs with the Finch in the real world!

The very first thing you will do with the Finch is to make it talk! First, create a new Finch file. You should copy, give it a new name, and add it to the your Finch project or workspace. The details of how you do this will vary slightly based on the Java environment that you are using. Do not create a new project or workspace. If you do this, the libraries that the Finch uses will not be configured correctly.

Open your new file. You will see that it already includes some code for you. This code imports the Finch library and declares a Finch object. You will replace the comment that says “// Write some code here!” with your own lines of code. Don’t remove any of the other lines.

To make the Finch do different things, you will use different commands called methods. You will start with two commands: saySomething() and sleep(). First, try out the example shown below. You will only need to put in the commands that are outlined. All of the other code is already in your file.

Notice that every Finch command must be used after the name myFinch and a period (myFinch.). This tells the computer that you are using a Finch command.

The first command that you used was saySomething(). To use this command, you type myFinch.saySomething(). Inside the parentheses, you add a string in quotation marks. This command plays the string over your computer speakers. If you can’t hear it, make sure the volume is turned up.

The saySomething() command is followed by the sleep() command. The sleep() command pauses the program for a period of time. Inside the parentheses, you add a positive integer. This integer is the number of milliseconds that the program should pause. The program above pauses for 3000 ms, or 3 s.

You need to use a sleep() command every time you use a saySomething() command. The sleep() command gives the computer time to play the string. Otherwise, the program may end before the computer has finished playing it. If you want the computer to say something long, you will need to increase the time that the sleep() command pauses the program.

Exercise 1:

Make the Finch say a string with at least 15 words. How do you need to change the sleep() command to give the program enough time to say all of the words?

Exercise 2:

Instead of placing an integer inside the parentheses of the sleep() command, you can use an integer variable instead. Declare an integer variable that holds a number of milliseconds. Then use that variable in the sleep() command.

Exercise 3:

Instead of placing a string inside the parentheses of the saySomething() command, you can use a String variable instead. Write a program that asks the user to enter what the Finch should say and then makes the Finch say it.

Exercise 4:

Write a program that asks the user their name and then has the Finch greet them by name.