Jump to page content Jump to navigation

College Board

AP Central

AP Exam Reader
Siemens Awards for Advanced Placement

APAC 2010
Print Page
Home > AP Courses and Exams > Course Home Pages > Introducing Concepts of Object and Classes

Introducing Concepts of Object and Classes

by Deepa Muralidhar
CSTA member
Northview High School
Atlanta, Georgia

Objects and a simple design of a class have to be taught together. The aim of this lesson is to show that a class is like a template and an object is in a sense the result of a class.

I usually like to have the students familiar with the concept of variables, assignment of variables, loops and conditionals. However, it is not necessary that students understand loops and conditionals while introducing the concept of objects.

In my classroom when I bring in the concept of objects, students have been in the classroom for about a month and half. I assume absolute no prior knowledge of programming and start with the fundamental concepts of what is a variable during the first week of school. They have learnt to create simple programs using variables, conditionals and loops. They have used Strings and have been told that a String is an object but at this point they do not have any idea what an 'object' is. It is at this point that I introduce objects.

While this is how I do it, I do believe that students can be taught what an object is early in the school year. After understanding the concept of basic variables, a teacher could introduce the topic of classes and objects. There is really no need for a student to have a knowledge of conditionals and loops to learn how to design classes.

A Teacher's Guide to Teaching Objects and Classes
The entire concept of objects and classes, which includes, couple of labs designing a class/client as well as a review activity should be taught for about 4 to 6 weeks before moving on to the next topic. The activity that I have suggested here is an introduction to objects using PLAY-DOH®. and a cookie cutter (given in Activity 1). I have suggested using a Dog and Rabbit cookie cutter. The reason I chose these examples is because, later on to develop an application (Client program) like "an animal farm", "a preschool animal game", "a simulation" is much easier. Students are able to relate to this very well. However, any cookie cutter should work. When I first tried this out, I used a Gingerbread Boy with scented PLAY-DOH. It worked very well! Students were trying to guess the various scents and what kinds of objects would be perfect to make with these PLAH-DOH. I have had a lot of success with this activity. Students talk about this throughout the entire year. 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, a couple of other topics will also need to be touched.

  • 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

Obviously, the students are not going to grasp all these concepts simultaneously. I recommend distributing a handout of a sample program with all these parts labeled and explained. Once they have designed a class and a client during lab time, going over these concepts again will help. I expect it will take 2 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. On these computers I have our technology specialist install the java sdk/jre (1.4.1), JCreator (the latest free version available on the website- www.jcreator.com ) and the latest version of BlueJ (www.bluej.org ).

Labwork: Students at this point will be extremely eager to go to lab and type out their own code or the sample code provided to them. Kindly hold them off. Ask students to open up BlueJ and pull up one of the sample programs. The one I like most is the Shapes Project. (c:\BlueJ\examples\Shapes).

Given below is the set of steps to be taken as a lab activity.
   i) Right-click on each one of the rectangles: Circle, Square and Triangle
   ii) Click on compile.
   iii) Click on new Circle( )
   iv) Click OK on the "new Instance" dialog box.
   v) The red box with label "circle1" comes up at the bottom of the application.
   vi) Immediately go around the room explaining that the rectangle with the label circle is the class and the red box is the object.
   vii) 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 3 mini-projects (lab activities) designing classes and clients, done a few homework exercises (Multiple choice, True or False etc.) prefereably from the textbook that is being used in the classroom, as teachers we need to find out what they have understood and make them realize what they know and what they do not know. Activity 2 is adapted from the concept of using CRC cards. Typcial CRC cards activity seem to have less success in a high school setting. Students indicate that it is quite 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.

The class is now ready for an end of topic project!

Activity 1: Using PLAY-DOH and Cookie Cutters

  • Necessary Supplies
    • PLAY-DOH (at least two colors-e.g. blue, red--A scented variety adds more fun in the classroom)
    • Cookie Cutters - 2 or 3 (any cookie cutter would work. For this example I am going to use 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 in my classroom has only two groups. It 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. In a high school setting the usual CRC activity may fail. Setting it up as a game/competition seems to work much better. I have done this with a lot of success.
  • 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)
      • Group 1 comes up to the designed board and lists what they have.
        • (Have the groups first write it out on paper and then copy it on the board. Give at least 5 to 10 minutes for design)
      • 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.

Deepa Muralidhar teaches AP Computer Science A & AB at Northview High School, in North Fulton County, Atlanta, GA. Deepa has taught AP Computer Science in both C++ and JAVA. Deepa has been programming since she was 17, in high school. She majored in Computer Science, getting her Bachelors and Masters in the subject. Logic development and algorithmic analysis are her favorite topics. Formerly a computer programmer she loves the challenge of teaching computer science to high school students. Deepa is currently working on a study guide for AP Computer Science A. She is a member of the Computer Science Teachers Association.

  ABOUT MY AP CENTRAL
    Course and Email Newsletter Preferences
  AP COURSES AND EXAMS
    Course Home Pages
    Course Descriptions
    The Course Audit
    Sample Syllabi
    Teachers' Resources
    Exam Calendar and Fees
    Exam Questions
    FAQs
  PRE-AP
    Teachers' Corner
    Workshops
  AP COMMUNITY
    About Electronic Discussion Groups
    Become an AP Exam Reader

Back to top