Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Objects and Classes

Eric Roberts CS 106A April 20, 2012

Once upon a time . . .

The Smalltalk Language


In the history of programming languages I outlined earlier, the language most responsible for bringing object-oriented ideas into the United States was Smalltalk, which was developed by a team at Xerox PARC that included a computer scientist named Adele Goldberg, whose Smalltalk-80 book became the major reference work on the language.

A Tale of Two Companies


The emergence of modern computing has a lot to do with the history of two companies in Silicon Valley: Xerox and Apple.

In 1970, Xerox established the Palo Alto Research Center, which attracted many of the top computer scientists of the day. That lab pioneered many of the fundamental technologies of modern computing such as graphical user interfaces and objectoriented programming long before they became commonplace.
When those technologies did appear, it was not from Xerox. . . .

Smalltalk and Steve Jobs

Objects and Classes

Review: Objects and Classes


An object is a conceptually integrated data collection that encapsulates state and behavior. A class is a template that defines the common structure for all objects of that class. Each object is created as an instance of one particular class, but a class can serve as a template for many instances. Classes form a hierarchy in which subclasses can inherit the behavior of their superclasses. A constructor is a specialized method that acts as a factory for making new instances of a particular class. In Java, a constructor always has the same name as the class and is invoked by using the new keyword.

Thinking About Objects


acm.graphics GRect class location GRect size GOval color GLine fill status GLabel fill color
. . .

I need a bunch of GRects.

acm.graphics
GRect GOval GLine GLabel
. . .

client

abstraction boundary
with thanks to Randall Monroe at xkcd.com

implementation

Exercise: Random Circles


Write a GraphicsProgram that draws a set of ten circles with randomly chosen sizes, positions, and colors, subject to the condition that the entire circle must fit inside the window.
RandomCircles

Section Problem: Animating Circles


One of your problems for next weeks section is to modify this code so that the circles expand gradually into their final positions, one at a time.
GrowingCircles

Defining Your Own Classes


The standard form of a class definition in Java looks like this:
public class name extends superclass { class body }

Each public class must be stored in a separate file whose name is the name of the class followed by .java. The extends clause on the header line specifies the name of the superclass. If the extends clause is missing, the new class becomes a direct subclass of Object, which is the root of Javas class hierarchy.

The body of a class consists of a set of Java definitions that are generically called entries. The most common entries are constructors, methods, instance variables, and constants.

Representing Student Information


Understanding the structure of a class is easiest in the context of a specific example. The next four slides walk through the definition of a class called Student, which is used to keep track of the following information about a student:
The name of the student The students six-digit identification number The number of units the student has earned A flag indicating whether the student has paid all university fees

Each of these values is stored in an instance variable of the appropriate type.

In keeping with the modern object-oriented convention used throughout both the book and the ACM Java Libraries, these instance variables are declared as private. All access to these values is therefore mediated by methods exported by the Student class.

The Student Class


This comment describes the class as a whole.

/** * The Student class keeps track of the following pieces of data * about a student: the student's name, ID number, the number of * units the student has earned toward graduation, and whether * the student is paid up with respect to university bills. * All of this information is entirely private to the class. * Clients can obtain this information only by using the various * methods defined by the class. */ public class Student {
The class header defines Student as a direct subclass of Object.

/** * Creates a new Student object with the specified name and ID. * @param name The student's name as a String * @param id The student's ID number as an int */ public Student(String name, int id) { This comment describes the constructor. studentName = name; studentID = id; } The constructor sets the instance variables.
page 1 of 4 skip code

The Student Class


/** * Gets the name of this student. * @return The name of this student These methods retrieve the value of /** */ an instance variable and data * The Student class keeps track of the following pieces of are called getters. Because the student name public String getName() { * about a student: the student's name, ID number, the are fixed, there are and ID number number of return studentName; * units the student has earned toward graduation, and whether no corresponding setters. } * the student is paid up with respect to university bills. * All of this information is entirely private to the class. /** * Clients can obtain this information only by using the various * Gets the ID number of this student. * methods defined by the class. * @return The ID number of this student */ */ This method changes the value of public int getID() { an instance variable and is called a public class Student { return studentID; setter. } /** * Creates a new Student object with the specified name and ID. /** * @param name The student's name as a String * Sets the number of units earned. * @param id The student's ID number as an int * @param units The new number of units earned */ */ public Student(String name, int id) { public void setUnits(int units) { studentName = name; unitsEarned = units; studentID = id; } }
page 2 of 4 skip code

The Student Class


/** * Gets the name ofof units earned. number this student. * @return The name ofof units this student has earned number this student */ public String getName() { int getUnits() { return studentName; unitsEarned; } /** * Gets the ID number of this student. Sets whether the student is paid up. * @returnflag ID number of this student @param The The value true or false indicating paid-up status */ public int getID() { void setPaidUp(boolean flag) { paidUp = flag; return studentID; }
Names for getter methods usually begin with the prefix get. The only /** exception is for getter methods that * Sets thewhether of units earned. Returns number the student is paid up. return a boolean, in which case * @param units Thethe student of paid up @return Whether new number is units earned name typically begins with is. the

*/
public void setUnits(int units) { boolean isPaidUp() { return paidUp; unitsEarned = units; }
page 3 of 4 skip code

The Student Class


/** * Gets theanumber of units earned. student. Creates string identifying this The toString method * @return The number of unitsdisplay this student this student has earned Java how to display tells string used to a value of this class. All */ of your classes should public int getUnits() { { String toString() override toString. return unitsEarned; " (#" + studentID + ")"; studentName + }

/** Classes often export named constants. /* Public constants */ * Sets whether the student is paid up. * The number The value true or false indicating /**@param flag of units required for graduation */paid-up status */public static final int UNITS_TO_GRADUATE = 180; public void setPaidUp(boolean flag) { paidUp = flag; /* Private instance variables */ } private String studentName; /* The student's name */ /* The student's ID number */ /**private int studentID; private int unitsEarned; * Returns whether the student is/* Theup. paid number of units earned */ private boolean paidUp; * @return Whether the student is/* Whether student is paid up */ paid up */ } public boolean isPaidUp() { These declarations define the instance return paidUp; variables that maintain the internal state of the class. All instance variables }
used in the text are private.
page 4 of 4 skip code

Using the Student Class


Once you have defined the Student class, you can then use its constructor to create instances of that class. For example, you could use the following code to create two Student objects:
Student chosenOne = new Student("Harry Potter", 123456); Student topStudent = new Student("Hermione Granger", 314159);

You can then use the standard receiver syntax to call methods on these objects. For example, you could set Hermiones number-of-units field to 263 by writing
topStudent.setUnits(263);

or get Harrys full name by calling


chosenOne.getName();

Rational Numbers
As a more elaborate example of class definition, section 6.4 defines a class called Rational that represents rational numbers, which are simply the quotient of two integers. Rational numbers can be useful in cases in which you need exact calculation with fractions. Even if you use a double, the floating-point number 0.1 is represented internally as an approximation. The rational number 1 / 10 is exact. Rational numbers support the standard arithmetic operations:
Addition: a c + = b d Subtraction: a c = b d ad + bc bd Multiplication: a x c = b d Division: a . c . = b d ac bd

ad bc bd

ad bc

Implementing the Rational Class


The next five slides show the code for the Rational class along with some brief annotations. As you read through the code, the following features are worth special attention:
The constructors for the class are overloaded. Calling the constructor with no argument creates a Rational initialized to 0, calling it with one argument creates a Rational equal to that integer, and calling it with two arguments creates a fraction. The constructor makes sure that the number is reduced to lowest terms. Moreover, since these values never change once a new Rational is created, this property will remain in force. Operations are specified using the receiver syntax. When you apply an operator to two Rational values, one of the operands is the receiver and the other is passed as an argument, as in
r1.add(r2)

The this Keyword


The implementation of the Rational class makes use of the Java keyword this, which always refers to the current object. The code, however, uses it in two distinct ways:
1. In the first two versions of the constructor, the keyword this is used to invoke one of the other constructors for this class. 2. In the methods that implement the arithmetic operators, this is used to emphasize that a named field comes from this object rather than from one of the other values of that class.

The keyword this is also often used to distinguish between an instance variable and a local variable with the same name. If you include this before the variable name, you indicate to the compiler that you mean the instance variable.

The Rational Class


/** * The Rational class is used to represent rational numbers, which * are defined to be the quotient of two integers. */ public class Rational { /** Creates a new Rational initialized to zero. */ public Rational() { this(0); } /** * Creates a new Rational from the integer argument. * @param n The initial value */ public Rational(int n) { this(n, 1); }
These constructors are overloaded so that there is more than one way to create a Rational value. These two versions invoke the primary constructor by using the keyword this.
page 1 of 5 skip code

The Rational Class


/** The primary constructor /** creates a new Rational * Creates a new Rational with the value x / y. * The Rational class is used to represent rational numbers, which from the numerator and * @param x The numerator of the rational number denominator. The call * are defined to be the quotient of two integers. * @param y The denominator of the rational number to gcd ensures that the */ fraction is reduced to */ public class Rational { lowest terms. public Rational(int x, int y) { int g = gcd(Math.abs(x), Math.abs(y)); /** Creates a new Rational initialized to zero. */ The add method creates num = x / g; public Rational() { a new Rational object den = Math.abs(y) / g; this(0); using the addition rule. if (y < 0) num = -num; The two rational values } appear in this and r. } /** /** * Creates a new Rational from the integer argument. * Adds the rational number r to this one and returns the sum. * @param n The initial value * @param r The rational number to be added */ * @return The sum of the current number and r public Rational(int n) { */ this(n, 1); public Rational add(Rational r) { } return new Rational(this.num * r.den + r.num * this.den, this.den * r.den); }
page 2 of 5 skip code

The Rational Class


/** * Creates a new Rational with the valuethis y. Subtracts the rational number r from x / one. * @param x The numeratornumber to be subtracted r rational of the rational number * @param yThe result of subtracting r from number @return The denominator of the rational the current number */ public Rational(int x, int y) { Rational subtract(Rational r) { return gcd(Math.abs(x), Math.abs(y)); int g =new Rational(this.num * r.den - r.num * this.den, num = x / g; this.den * r.den); } den = Math.abs(y) / g; if (y < 0) num = -num; /**} * Multiplies this number by the rational number r. /**@param r The rational number used as a multiplier * * Adds theThe resultnumber r to thisthe current number bysum. @return rational of multiplying one and returns the r */ * @param r The rational number to be added * @returnRational of the current number{and r public The sum multiply(Rational r) */ return new Rational(this.num * r.num, this.den * r.den); } public Rational add(Rational r) { return new Rational(this.num * r.den + r.num * this.den, this.den * r.den); These methods (along with divide on the next page) work just like the add method but use } a different formula. Note that these methods do have access to the components of r.
page 3 of 5 skip code

The Rational Class


/** * Subtracts thenumber by number r fromnumberone. Divides this rational the rational this r. * @param r The rational number to beas a divisor used subtracted * @return The result of subtracting rcurrent number by number dividing the from the current r */ public Rational subtract(Rational r) { divide(Rational r) { return new Rational(this.num * r.den -this.den * r.num); r.den, r.num * this.den, } this.den * r.den); } /** /**Creates a string representation of this rational number. * * Multiplies this number by the rational number r. @return The string representation of this rational number */ * @param r The rational number used as a multiplier * @returnString toString() { public The result of multiplying the current number by r */ if (den == 1) { publicreturn "" multiply(Rational r) This method converts the Rational + num; { } else { return new Rational(this.num * r.num, this.den to its string Rational number * r.den); form. If the denominator is 1, the } return num + "/" + den; number is displayed as an integer. } }

page 4 of 5

skip code

The Rational Class


/** * Divides this number by the rational number r. Calculates the greatest common divisor using Euclid's algorithm. * @param r The rational number used as a divisor x First integer * @returnyThe result of dividing the current number by r @param Second integer * */@return The greatest common divisor of x and y */public Rational divide(Rational r) { private int gcd(int x, int y) { * r.den, this.den * r.num); return new Rational(this.num } int r = x % y; while (r != 0) { Euclids gcd method is declared to /** x = y; be private because it is part of the implementation of number. * Creates a string representation of this rational this class and is y = r; * @return The string representation of never used outside of it. number r = x % y; this rational */ } public String toString() { return y; } if (den == 1) { return "" + num; /* Private instance variables */ } else { private int num; + "/" + den; return num /* The numerator of this Rational */ private int den; } /* The denominator of this Rational */ } }
As always, the instance variables are private to this class.
page 5 of 5 skip code

Simulating Rational Calculation


The next slide works through all the steps in the calculation of a simple program that adds three rational numbers.

1 2

1 3

1 6

With rational arithmetic, the computation is exact. If you write this same program using variables of type double, the result looks like this:
RoundoffExample

1.0/2.0 + 1.0/3.0 + 1.0/6.0 = 0.999999999999999

Adding Three Rational Values


public void run() { Rational a = new Rational(1, 2); public Rational(int x, int y)r) { Rational add(Rational { 36 5 Rational b = new Rational(1, 3); int g = gcd(Math.abs(x), Math.abs(y)); Rational c = new Rational(1, 6); public= Rational(int x, this.num * r.den + r.num * this.den , returnxnewg; num / Rational( int y) { Rational =sum = a.add(b).add(c); int gcd(Math.abs(x), Math.abs(y)); den = gMath.abs(y) / g; this.den * r.den ); println(a + " + " + b + " + " + c + " = " + sum); num =< x0) num = -num; / g; if (y }} den = Math.abs(y) / g; 36 6 temporary } a b c sum result if (y < 0) num = -num; } 5 1 1 1 this r 1 6 2 x 3 y 6 g 1 1 num 1 num this5 den 2 den 1 3 6 3 6 6 x 1 y 2 g num 3 5 1 den 6 36 5 36 6 36 1 1
TestRational

1/2 + 1/3 + 1/6 = 1

skip simulation

The End

You might also like