Introducing Concepts of Object and Classes

Objects and a simple design of a class have to be taught together. This lesson shows that a class is like a template and an object is, in a sense, the result of a class.

The concept of objects can be introduced through this lesson during the first few weeks of school. This will allow you to continue to refer back to this experience as you build students’ knowledge and understanding of objects and classes.

A Teacher's Guide to Teaching Objects and Classes

This activity is an introduction to objects using PLAY-DOH® and a cookie cutter (given in Activity 1). For example, using a dog and rabbit cookie cutter is useful because developing an application (Client program) like "an animal farm", "a preschool animal game", "a simulation" is much easier. However, any cookie cutter should work. As a teacher, it is very easy to go back at various points during the school year and say, "remember the PLAY-DOH? The difference between the object and the class?" This should be done typically in one or two class periods (55 minutes in length). By the end of these two days, the students should have at least a surface level understanding of what an object is, as well as the difference between an object and a class. Then write out a sample code (given in the presentation) designing a class and a client program which instantiates an object of the class. Besides teaching objects and classes, you can expand this lesson to include other topics such as:

  • Concept of public and private variables.
  • Concept of public and private methods.(make sure the label points out public class and what that implies)
  • Types of methods (constructor, accessors, modifiers and destructors).
  • Method Headers
  • The data type that each method returns.
  • Passing Parameters.
  • Concept of formal and actual parameters
  • Usage of the dot operator

Distribute a handout of a sample program with all these parts labeled and explained. Once your students have designed a class and a client during lab time, it’s helpful to go over these concepts again. It will probably take two class periods to go over the design of a class before the students are ready for lab activity.

Lab Setup: The computers in the lab are MS Windows XP machines. These computers should have the java sdk/jre (1.4.1), JCreator (the latest free version available on the website - https://jcreator.en.softonic.com/download) and the latest version of BlueJ (www.bluej.org).

Labwork: Ask students to open up BlueJ and pull up one of the sample programs. The Shapes Project is a good one to use. (c:\BlueJ\examples\Shapes).

Given below is the set of steps to be taken as a lab activity.

  1. Right-click on each one of the rectangles: Circle, Square and Triangle
  2. Click on compile.
  3. Click on new Circle( )
  4. Click OK on the "new Instance" dialog box.
  5. The red box with label "circle1" comes up at the bottom of the application.
  6. Immediately go around the room explaining that the rectangle with the label circle is the class and the red box is the object.
  7. Right-click on the object and all the methods that show up are the methods available for that object.

At this point you also need to add info on how to make it run so that it shows up on the applet viewer.

Note: This visual feedback on the difference between a class and an object makes it very clear to the students who did not quite get it during the lecture.

Now students can open up JCreator and type up the sample code (Class and Client programs.) Students are expected at this point to successfully compile and execute the programs.

End of Topic activity:

After students have done at least three mini-projects (lab activities) designing classes and clients, done a few homework exercises (Multiple choice, True or False, etc.), we need to find out what they have successfully understood. Activity 2 is adapted from the concept of using CRC (Class-Responsibility-Collaborator) cards. Typical CRC cards activity seem to have less success in a high school setting because students find it boring. Putting it out as a competition has better chance of success. Every group gets only one turn. Make sure you have given students ample time to think out various possibilities.

Hint: Mix your groups up so that students who were struggling with the lab exercises have been paired with the ones who have a clear understanding.

Activity 1: Using PLAY-DOH and Cookie Cutters

  • Necessary Supplies
    • PLAY-DOH (at least two colors)
    • Cookie Cutters—2 or 3 (any cookie cutter would work. This example uses a rabbit and dog)
    • Rolling Pin
  • Activity
    • Take some of the red PLAY-DOH and roll it out on the table.
    • Using the rabbit cookie cutter, cut out the rabbit.
    • Pull the rabbit out of the cookie cutter.
    • Repeat last 3 steps two more times.
    • Now you should have 3 red rabbits.
      • State: "The rabbit is the object, the cookie cutter is the class."
      • State: "You can make multiple objects out of the same class."
    • Roll out the blue PLAY-DOH.
    • Repeat the steps given in the previous slide.
    • You should have 3 blue rabbits.
      • State: "The rabbit is the object, the cookie cutter is the class."
    • Now repeat the above exercise with the dog cookie cutter.
      • Roll out the red PLAY-DOH, apply the dog cookie cutter 3 times. State: "Different classes result in different objects that possess the characteristics of the class."
      • Note: You can add levity in the classroom by letting students play around with the PLAY-DOH and identify the specific features of the object. (i.e long ears for the rabbit, four legs, short, stubby tail).
      • Now take either the Dog or the Rabbit PLAY-DOH and start to design the class.
public class Rabbit{  // be creative here.    private int ears;    private int legs;     private String favoriteFood;     private String rabbitColor;//add constructor (perhaps passing in value for color)//add accessor methods //public void hop()//public void nibble()}
  • Now write a short client program to test your class.
public static void main(...){Rabbit bugsBunny = new Rabbit("blue");// more code goes here}

 

Activity 2: Application of the CRC Cards Idea

  • This activity can be done with multiple groups.
  • Be sure to mix the groups with various learning capabilities.
  • Using typical CRC (Class-Responsibility-Collaborator) cards, students may not get very excited. Setting it up as a game or competition works much better.
  • Example 1:
    • List out the variables and methods that can be designed for a Teacher in a School Setting (Think of a system being for registering students at a High School)
      • Have the groups first write it out on paper. Give at least 5 to 10 minutes for design.
      • Group 1 puts their design on the board.
      • Now Group 2 comes up adds or removes methods.
  • Example 2:
    • Design a class for the various courses being offered. (Think of a system being designed for registering students at a High School)
      • Group 2 puts their solution on the board.
      • Group 1 suggests modifications.
  • Teacher decides which team won or if it is a draw.
  • Advantages of this Activity
    • Teacher knows if the students can design a class.
    • Teacher can one more time show how an object can be instantiated.
    • Students get a review of the components of a class.
    • Students get a chance to discuss with their classmates their ideas and possibly clarify any misconceptions.