Java

TOPIC 09 – INTRODUCING OBJECTS

 

 

LESSON NOTE

 

 

ABOUT THIS LESSON

 

This lesson introduces you to the use of objects in programming.  It doesn't come close to talking about all of the concepts related to objects.  We will actually do a unit to cover that later called Object Oriented Programming (OOP).

 

But for now, this is a very basic introduction to objects.

 

We have already seen how to create different variables to store different types of information.  And we also created some built-in objects such as Scanners and Strings.

 

REASON FOR USING OBJECTS

 

When programs get complex enough, it is often useful to group related variables together inside a single entity.  We can do this by placing the related variables inside an object.

 

CLASSES

 

To define what types of information is stored inside an object, we create a class.  It is in the class that we place all of the details about how the object will be used.

 

Once our class is created, we can create many objects of that class.

 

 

EXAMPLE 1 – PERSON CLASS


Let's consider a program that has to deal with a lot of information about people.  It deals with their first name, last name, birth year and location of birth.  So four pieces of information per person. We could simply create all four variables each time we have a new person in the program.  Or, we could create a class that has all the information about a Person in it.  This will lower the number of variables in the program for the programmer to have to keep track of.  It will also help with organization.

 

Here is the code to define a Person class with four variables (called datafields) in it.

 

public class Person

{

     public String first;

     public String last;

     public int birthYear;

     public String birthLocation;

}

 

The above code starts simply with the class name.  Inside the class, all we put is the list of variables (datafields) that will be associated with Person objects.  The word public simply means that the datafields can be access from outside the class.  For now, we will always use public datafields.

 

Below is the code that will make use of the Person class to create an object called p.  Note that both files have to be in the same folder for this program to work.

 

public class Tester

{

     public static void main(String[] args)

     {

         Person p = new Person();

         p.first = "Patrick";

         p.last = "Campeau";

         p.birthYear = 1977;

         p.birthLocation = "Sudbury";

        

         System.out.println(p.first + " " + p.last);

         System.out.println("Born in " + p.birthYear + " in " + p.birthLocation);

     }

}

 

Like all programs, this includes a main function.  Inside, we create a Person object called me.  From that point on, we can access the datafields (defined in the Person class) by using p.datafieldName.

 

The example only shows the one Person object but we could create many more if needed.

 

 

NAMING CONVENTIONS FOR CLASSES

 

You should already know that class names should start with a capital letter followed by lower case letters.  If there are inner words, then their first letter should be capitalized as well.

 

NAMING CONVENTIONS FOR OBJECTS

 

The accepted rules for naming objects are the same as for variables.  Notably, they should start with a lower case letter.

 

 

EXAMPLE 2 – TIME TABLE CLASS

 

Let's consider an application that has to keep track of time tables for students.  Each time table simply has information for each course that a specific student is taking.  So this would be an implementation:

 

public class Timetable

{

     public String courseCode1;

     public String courseCode2;

     public String courseCode3;

     public String courseCode4;

     public String courseTitle1;

     public String courseTitle2;

     public String courseTitle3;

     public String courseTitle4;

     public int classroom1;

     public int classroom2;

     public int classroom3;

     public int classroom4;

     public String teacher1;

     public String teacher2;

     public String teacher3;

     public String teacher4;

}

 

We can then write a Java program to create our dream TimeTable.

 

public class TimeTableTester

{

     public static void main(String[] args)

     {

         Timetable dream = new Timetable();

        

         dream.courseTitle1="Calculus and Vectors";

         dream.courseCode1="MCV4U";

         dream.teacher1="Bernhard Riemann";

         dream.classroom1=231;

        

         dream.courseTitle2="Physics";

         dream.courseCode2="SPH4U";

         dream.teacher2="Albert Einstein";

         dream.classroom2=114;

        

         dream.courseTitle3="Chemistry";

         dream.courseCode3="SCH4U";

         dream.teacher3="John Dalton";

         dream.classroom3=109;

        

         dream.courseTitle4="Computer Science";

         dream.courseCode4="ICS4U";

         dream.teacher4="Bill Gates";

         dream.classroom4=101;

 

          //Note:  This program doesn't output anything.

          //It just shows you how to use the Timetable class.

     }

}