University of Chicago, CS 102, Spring 2006, Notes 2

previous  |  toc  |  next


Object-oriented programming languages, including Java, model the world with objects. Objects are grouped into classes.

When an object-oriented programmer sets out to write a program that models some interesting real-world scenario, she thinks of the objects that are somehow fundamental to that situation. These objects loosely correspond to the people, places and things involved in that scenario. Objects that are in some fundamental way similar -- different items of the same kind of thing -- are grouped into classes.

For example, let's say an object-oriented ("oo" for short) programmer wants to write a chess program. A physical chess game consists of the following physical objects: the pieces, the board, perhaps a timer, and let's not forget the two players. An oo programmer would create a class for each different kind of object. And so, the programmer might end up making the following classes: Piece, Board, Timer, and Player. In a particular game scenario, there would be 32 Piece objects belonging to the Piece class, one Board object in the Board class, one Timer object in the Timer class, and two Player objects in the Player class. Each of these objects corresponds to a thing physically present in a real-world chess game, and each class corresponds to some natural category present in the whole batch of such things. An oo programmer might also make a class corresponding to something intangible: a Game. You can't wrap your hands around a chess game, but it's nonetheless a solid conceptual item and something an oo programmer would be likely to objectify.

Let's step back from chess, and consider a simple example: that of a point in 2-dimensional space. A point is completely specified by two numbers. These numbers are typically known as x and y, where x specifies the left-to-right location of a point in 2-space, and y identifies its up-and-down location.

It is very likely one would create a Point class in Java. Note that the Point class corresponds to no particular point in space -- it specifies the whole abstract category of points and defines what it is that points have in common. Any particular point will be represented by an object.

Here is a definition in Java of the Point class.

class Point {
  double x;
  double y;
}
The first line says, I'm defining a class, and it's called Point. Class names in Java are capitalized by convention. If you wrote class point, with a lowercase p, it would not be technically wrong, but it would be stylistically wrong, and you shouldn't do it.

The second line says double x;. This line says, my Point class contains a value called x, which is a double. A double is simply a number that is not necessarily an integer; more on this soon.

The third line says double y;, which means, my Point class contains another value y, which is also a double.

The x and y are collectively known as the instance variables of the Point class.

That's simple enough, but in practice Java class definitions are more complicated than this. Namely, we add public and private modifiers to our class definitions like so:

public class Point {
  private double x;
  private double y;
}
It's too soon to talk about what public and private actually mean; take the need for their presence on faith for now, and I'll explain them later.

We can also add comments to our class definitions. Comments are notes in plain English that you can put in your code anywhere you like. The machine ignores them. They are there expressly to facilitate the reading of your code of by actual people.

There are several comment styles -- several ways to write comments, that is -- in Java. The simplest is to simply type two slashes. Everything that follows two slashes on a given line is a comment. For example, the following is entirely legal Java code:

// I'm feeling a little sleepy today...
public class Point {
  // maybe I need a snack...
  private double x;
  // but then again, maybe that's not necessary.
  // Maybe I'll just step in the bathroom and splash
  // some cold water on my face. 'Scuse me...
  private double y;
  // Yeah, that did the trick.
}
No need for those kinds of comment in your code.

It is more likely your comments will be simple statements of fact about the code you're writing, such as the following:

public class Point {
  // private instance variables 
  private double x;
  private double y;
}
Not especially interesting, but useful. As we write more and more complicated pieces of code throughout the quarter, comments will become more and more important, and if you program enough you will eventually find them indispensible.

Another kind of comment is the javadoc style comment. This is a comment that spans many lines and is easier shown than described:

/**
 * The Point class represents a point in 2-dimensional space.
 *
 * @author Adam Shaw
 */
public class Point {
  // private instance variables 
  private double x;
  private double y;
}
Note that a javadoc comment starts with /** and ends with */, and each intermediate line starts with *. There is a freely available program, itself called javadoc, that automatically builds documentation websites for Java programs. Let me make that clear: the javadoc program will take the Java programs you write and build nice, informative websites out of them, if and only if you have decorated your programs with lots of nice, informative javadoc style comments.

So now you have seen the basic skeleton for creating a Point class. Let's turn our attention to another class: Fraction. Here is a sketch of the class definition for Fraction:

/**
 * The Fraction class represents a fraction where
 * both the numerator and the denominator are integers.
 *
 * @author Adam Shaw
 */
public class Fraction {
  // private instance variables 
  private int numerator;
  private int denominator;
}
Note that the javadoc comment is telling us something really important about the Fraction class: namely, that both top and bottom are integers. There are certainly fractions of interest that don't have this property: for example, π/2 or 1/e. If you need to represent such a fraction, then this class is not for you.

Anyway, the most salient difference between Point and Fraction is the fact that the private instance variables of the Fraction class are labeled int rather than double.

For practice, based on the Java snippets you've seen thus far, write out class definitions for some or all the following number-themed classes. They can all be made out of various combinations of int and double private instance variables.

You might wonder what sorts of values other than ints and doubles you might be able to use when you build classes. The answer follows in the next set of notes.

previous  |  toc  |  next