Lesson 16 – Classes with Finch

Java Concepts

Classes

Teacher Materials

Get Access

Throughout these lessons, you have been writing classes in Java, but you have primarily just been using main() to call Java functions, Finch methods, or your own methods. This lesson will focus on designing classes in an object-oriented way. A class defines the methods and variables for a programming object. A good object-oriented class will encapsulate a set of variables and methods that logically belong together.

Declaring and Testing a Class

To demonstrate, suppose that we want to improve the Finch’s dancing ability. We can create a custom class called DancingFinch that will contain the variables and methods that we need to turn the Finch into a great dancer. The first thing that the class contains is a variable finch of type Finch. This variable is private, meaning that it can only be accessed within this class.

Next is the class constructor, which initializes the finch variable. This constructor is called when a DancingFinch object is created, and it is public because it can be called outside this class.

Finally, the DancingFinch class contains one method, spin(), which makes the robot turn in a circle to the right or left. Like the constructor, this method is public because it can be called outside this class.

To test our new class, we have to create a second class called DancingFinchTest. Inside the main() method of DancingFinchTest, we create a DancingFinch object called bird and then test the spin() method.

Exercise 1

Add two more public methods to the DancingFinch class. What dance moves can you create? Don’t forget to add documentation as you add to your class!

Exercise 2

Add a private method called stepBack() to the DancingFinch class. This method should take two parameters. The first parameter, wheel, should indicate whether the right wheel or the left wheel should move. The wheel should move slowly backward for a period of time given by the second parameter. What happens when you try to call stepBack() from DancingFinchTest?

Exercise 3

Add a public method called moonwalk() to the DancingFinch class. This method should use the private stepBack() method to make the Finch move backward in a series of steps. Can you create any other public methods that use the stepBack() method?

Inheritance

We can only call the DancingFinch methods on an object of the DancingFinch class. We cannot call the original Finch functions. For example, if we try to call setTurn(), we will get an error.

If we want to be able to call both the original Finch methods and our new methods, we can use inheritance. The concept of inheritance in Java enables us to create a new subclass that has all the functionality of the original class (the superclass) plus added functionality. 

For example, we can create a new class, DancingFinch2, that uses the keyword extends to inherit the functionality of the original Finch class. Notice that the constructor has to include a call to super() to ensure that the constructor for the superclass is called.

Now DancingFinchTest can create a DancingFinch2 object that can use both the original Finch methods and our added custom methods.

In the remainder of this lesson, you will create a new class called MoodyFinch that inherits the functionality of the Finch class. MoodyFinch will give the Finch an emotional state. Remember that you will also need to write a client class to fully test the functionality of your MoodyFinch class!

Exercise 4

Use inheritance to create the MoodyFinch class to give the Finch an internal emotional state. This class should also include an array variable that contains at least four acceptable emotion states, such as angry, sad, happy, or blah (you don’t have to use these!). This variable should be preceded by the keyword static, which means that the variable cannot be changed and is the same for every instance of the class. We are using a static variable here because all objects of type MoodyFinch must have the same emotions.

Exercise 5

Internal variables in a class are often associated with mutator and accessor methods. A mutator method, or setter, is used to change the value of the internal variable. An accessor method, or getter, returns the value of the internal variable. 

Write a mutator method called setEmotion() for the MoodyFinch class that sets the internal emotion variable to a value passed to the method. What should the mutator method do if the parameter value is not a valid emotion? Test your mutator method using an accessor method that returns the current emotion.

Exercise 6

Write a private method to demonstrate each of the valid Finch emotional states. For example, the Finch could turn its lights red and make a high-pitched sound when it is angry. Then write a public playEmotion() method that calls the private function corresponding to the Finch’s current emotional state.