CSCI A202 - Introduction to Programming (II)

Lecture 3: Objects, Classes, Instance and Class Variables Tutorial (last updated Tue Jan 19)

Objects are entities that can have memory and specific behaviour. Their memory is represented by variables that they have inside and their behaviour is defined by actions that they know how to perform (methods that are associated with those objects). All objects of the same kind, that have the exact same structure, make up a class. In fact, in programming it's always the other way around: one first defines a class, which describes how that particular class of objects will look and behave (what methods they have,) then one creates as many objects (of that kind) as needed and lets them loose, thus running the program.

We will introduce the following concepts in this tutorial:

We will do this with the help of six examples. Each one of them will resemble a short play (as in a stage representation of an action or a story). Our dramatic compositions will be simple, since we will abstract away all the unwanted details. The titles of the plays will be: Sports, Hoosiers, Babies, ATP, Maryland, and with the next tutorial, Candyland.

Here's the first one.

1. Sports: Objects and Their Methods (I)

A Hoosier basketball fan's simple to describe: (s)he cheers, by shouting 'Go Hoosiers!' when (s)he feels like cheering for Bob Knight's team. Write a short program (a play) that presents three Hoosier fans cheering for the IU Hoosiers, each fan cheering once, and in no particular order.

Here's how the program should behave:

tucotuco.cs.indiana.edu% javac Sports.java
tucotuco.cs.indiana.edu% java Sports
Go Hoosiers!
Go Hoosiers!
Go Hoosiers! 
tucotuco.cs.indiana.edu%
The output of the program is in italics.

At a basketball game the noise is so loud that you don't know who is cheering and when. The crowd is anonymous, more or less. Here's the object oriented implementation of this play:

tucotuco.cs.indiana.edu% cat Sports.java
public class Sports { public static void main(String[] args) { Hoosier a = new Hoosier(); Hoosier b = new Hoosier(); Hoosier c = new Hoosier(); a.cheer(); b.cheer(); c.cheer(); } } class Hoosier { void cheer() { System.out.println("Go Hoosiers!"); } }
tucotuco.cs.indiana.edu%
We see that a Hoosier is an object that knows only one thing: to cheer, and in only one way. The objects' behaviour is defined by their methods, and since each object is an instance of a class the methods themselves are called instance methods.

Similar explanations will accompany the next 4 plays, but for now we will only list them in order. We will explain each one of them in class and I will update these notes with what we discuss in lecture.

2. Hoosiers: Objects and Their Methods (II)

The play:

tucotuco.cs.indiana.edu% javac Hoosiers.java
tucotuco.cs.indiana.edu% java Hoosiers
Hoosiers! Hoosiers!
Hoosiers! Hoosiers!
Hoosiers! Hoosiers!
I-N-D-I-A-N-A!
Hoosiers! Hoosiers!
Hoosiers! Hoosiers!
Go IU Beat Purdue!
Hoosiers! Hoosiers!
Hoosiers! Hoosiers! 
tucotuco.cs.indiana.edu%
The screenplay and the cast:
tucotuco.cs.indiana.edu% cat Hoosiers.java
public class Hoosiers { public static void main(String[] args) { Hoosier a = new Hoosier(); Hoosier b = new Hoosier(); Hoosier c = new Hoosier(); for (int i = 0; i < 3; i++) { a.cheer(); b.cheer(); c.cheer(); } } } class Hoosier { void cheer() { int dice; dice = (int)Math.round(Math.random() * 5 + 1); switch (dice) { case 1: case 2: System.out.println("I-N-D-I-A-N-A!"); break; case 3: case 4: System.out.println("Go IU Beat Purdue!"); break; case 5: case 6: System.out.println("Hoosiers! Hoosiers!"); break; } } }
tucotuco.cs.indiana.edu%

3. Babies: Instance Variables (I)

The play:

tucotuco.cs.indiana.edu% javac Babies.java
tucotuco.cs.indiana.edu% java Babies
Alice: Hello, my name is Alice
Susan: Hello, my name is Susan
Jimmy: Hello, my name is Jimmy
tucotuco.cs.indiana.edu% 
The screenplay and the cast:
tucotuco.cs.indiana.edu% cat Babies.java
public class Babies { public static void main(String[] args) { Baby a = new Baby("Alice"); Baby b = new Baby("Susan"); Baby c = new Baby("Jimmy"); a.talk(); b.talk(); c.talk(); } } class Baby { String name; // instance variable Baby(String givenName) { // constructor name = givenName; } void talk() { // instance method System.out.println(name + ": Hello, my name is " + name); } }
tucotuco.cs.indiana.edu%

4. ATP: Instance Variables (II)

The play:

tucotuco.cs.indiana.edu% javac ATP.java
tucotuco.cs.indiana.edu% java ATP
Sampras: I won this one, and I need 2 more to win.
Agassi: I won this one, and I need 2 more to win.
Sampras: I won this one, and I need 1 more to win.
Sampras: Great! I won the match. 
tucotuco.cs.indiana.edu% 
The screenplay and the cast:
tucotuco.cs.indiana.edu% cat ATP.java
public class ATP { public static void main(String[] args) { Player sampras = new Player("Sampras"); Player agassi = new Player("Agassi"); sampras.wins(); // uncomment next statement for special effects: agassi's memory is shakey to // protect against this we need to define setsWon and name as private fields (in Player) // agassi.setsWon += 2; agassi.wins(); sampras.wins(); sampras.wins(); } } class Player { String playerName; // instance variable int setsWon; // another instance variable Player (String name) { playerName = name; } void wins() { setsWon += 1; if (setsWon < 3) { System.out.println(playerName + ": I won this one, and I need " + (3 - setsWon) + " more to win."); } else { System.out.println(playerName + ": Great! I won the match."); } } }
tucotuco.cs.indiana.edu%

5. Maryland: Instance and Class Variables.

The play:

tucotuco.cs.indiana.edu% javac Maryland.java
tucotuco.cs.indiana.edu% java Maryland
Clinton: Gentlemen, today we will speak in English
   Arafat: Here are my views expressed in English
   Netanyahu: Here are my views expressed in English
   Clinton: Here are my views expressed in English
Clinton: Gentlemen, today we have spoken in English
----------
Arafat: Gentlemen, today we will speak in French
   Netanyahu: Here are my views expressed in French
   Clinton: Here are my views expressed in French
   Arafat: Here are my views expressed in French
Arafat: Gentlemen, today we have spoken in French
----------
Netanyahu: Gentlemen, today we will speak in Russian
   Clinton: Here are my views expressed in Russian
   Arafat: Here are my views expressed in Russian
   Netanyahu: Here are my views expressed in Russian
Netanyahu: Gentlemen, today we have spoken in Russian
----------
tucotuco.cs.indiana.edu% 
The screenplay and the cast:
tucotuco.cs.indiana.edu% cat Maryland.java
public class Maryland { public static void main(String[] args) { Negotiator Clinton = new Negotiator("Clinton", "English"); Negotiator Arafat = new Negotiator("Arafat", "French"); Negotiator Netanyahu = new Negotiator("Netanyahu", "Russian"); // day one - Clinton presiding Clinton.startsMeeting(); Arafat.explains(); Netanyahu.explains(); Clinton.explains(); Clinton.drawsConclusions(); // day two - Arafat presiding Arafat.startsMeeting(); Netanyahu.explains(); Clinton.explains(); Arafat.explains(); Arafat.drawsConclusions(); // day three - Netanyahu presiding Netanyahu.startsMeeting(); Clinton.explains(); Arafat.explains(); Netanyahu.explains(); Netanyahu.drawsConclusions(); } } class Negotiator { static String officialLanguage; String preferredOfficialLanguage; String negotiatorName; Negotiator(String name, String language) { preferredOfficialLanguage = language; negotiatorName = name; } void startsMeeting () { officialLanguage = preferredOfficialLanguage; System.out.println(negotiatorName + ": Gentlemen, today we will speak in " + officialLanguage); } void explains() { System.out.println(" " + negotiatorName + ": Here are my views expressed in " + officialLanguage); } void drawsConclusions() { System.out.println(negotiatorName + ": Gentlemen, today we have spoken in " + officialLanguage + "\n----------"); } } tucotuco.cs.indiana.edu%
As soon as we review arrays we will be ready for the last of the plays in this tutorial entitled Candyland - objects at play.