Teacher Note: As an alternative to having students write the entire class for Exercises 2-6, you could instead provide students with the sample code and have them explore object-oriented programming by adding animals. A similar modification would also work for the Extension. Special thanks to Nina Lurie from William Floyd High School in New York for this suggestion!
Lesson 15 – Object-Oriented Programming with the Finch
Required Java Concepts
Principles of object-oriented programming and creation of custom classes
Throughout these lessons, you have been using the Finch class. This class encapsulates all of the methods that you use to control the Finch’s wheels, lights, and buzzer, as well as the methods that you use to get data from the Finch’s sensors. Since the Finch is an actual object, this class is a great example of object-oriented programming!
You can use the Finch class to create two independent Finch objects! Work with a partner or another group to create a program that uses two Finches. The first Finch will be a controller for driving the second Finch. Tilting the beak of the first Finch down should make the the second Finch drive forward, while tilting the Finch’s left or right wheel down should cause the second Finch to turn left or right. When should the Finch back up? Hint: It might be helpful to mark which Finch is which by lighting their beaks different colors.
In the last few lessons, you have been writing custom methods. In this lesson, you will design methods to create an entire custom class. This class will enable the Finch to mimic different animals.
Your class should have at least five private methods that enable the Finch to mimic different animals. For example, one method might make the Finch mimic a cow by mooing, moving slowly, and having a white nose. To make sounds, you can use the Finch saySomething() method, or you can use the playClip() method. The latter method can play short WAV files over the computer speakers. This website has some WAV files that you can use. In most Java environments, you will need to place the WAV files you want to use in the main Finch project folder. Remember to test your methods as you write them!
Your class should contain private variables for Finch and the animal that the Finch is currently mimicking, as well as the list of possible animals. Write public methods to get and set the variable that represents the current animal. Remember to check that the animal is valid before setting the variable! The set method should return true if the variable was successfully set to a valid animal, and false otherwise.
Your class should also contain a public method to mimic the current animal. This method will call the private methods that you wrote in Exercise 2. Do you need to implement any error checking for this method?
Finally, your class should contain a public method to set the current animal to a random valid value, as well as a public method to close the Finch connection at the end of the program.
Now that you have completed your class, you need to write a driver class to test the whole thing (though hopefully you tested the methods as you wrote them). The main part of this test class should ask the user for an animal. If the user enters a valid animal, the Finch should mimic that animal. Otherwise, the Finch should inform the user of their error and mimic a random animal.
Extension: Moody Finch
Would we rather create R2D2 or the Terminator? That’s the central question behind the notion of emotional robots – if we give our robots the ability to feel, they’re much less likely to annihilate the human race with nuclear weapons1 or to enslave us so that they can use us as batteries2.
Right now, the Finch doesn’t have any emotions, and a world dominated by small (but coldly brutal) USB-tethered robots is only a few iterations of Moore’s law away3. Save us all from this fate by giving Finch some emotions; you will create a new class, called MoodyFinch, which has an internal emotional state. This class should contain the following methods:
public boolean setEmotion(String setting) – Sets the internal emotion variable. Returns true if an acceptable emotion (angry, sad, happy, or blah) was used, false otherwise
public void playEmotion( ) – Plays the sequence of the current emotion
public String getEmotion( ) – Returns the Finch’s current emotional state
private void angry( ) – The Finch shows that it’s angry
private void happy( ) – The Finch runs a happy routine
private void sad( ) – The Finch runs a sad routine
private void blah( ) – The Finch should act apathetic and uncaring
The angry(), happy(), sad(), and blah() methods are private, and so can’t be called by any methods outside the MoodyFinch class. Instead, people using the MoodyFinch class will call playEmotion( ) to play whichever of these four routines the Finch is feeling right now. The routines should involve use of the beak LED and the motors. The Finch should say something or make a sound whenever one is called.
Remember that you will also need to write a client class to fully test the functionality of your MoodyFinch class.
1 See Terminator . And Battlestar Galactica (though arguably those robots had emotions, but they should’ve been programmed with happier ones)
2 See The Matrix