192
Object Oriented Programming David J. Eck Bradley P. Kjell Anban W. Pillay

Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

  • Upload
    others

  • View
    42

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Object Oriented Programming

David J. EckBradley P. Kjell

Anban W. Pillay

Page 2: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Object Oriented Programming in Java

David J. Eck et.al

2018

i

Page 3: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming
Page 4: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Contents

Contents i

1 Introduction to Objects 11.1 What is Object Oriented Programming? 1

Object Orientation as a New Paradigm: The Big Picture 21.2 Fundamentals of Objects and Classes 4

Objects and Classes 4Class Members and Instance Members 8Access Control 15Creating and Destroying Objects 16Garbage Collection 23Everything is NOT an object 24

1.3 Introduction to Error Handling 261.4 Javadoc 291.5 Creating Jar Files 32

APIs and Packages 341.6 Mixing Static and Non-static 39

Static Import 41

2 Object Oriented Analysis and Design 432.1 Software Engineering 43

Software Engineering Life-Cycles 44Object Oriented Software Engineering 45Abstraction and Information Hiding 46

2.2 A Simple Methodology 49

i

Page 5: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Contents

Discovering Classes: Class-Responsibility-Collaboration cards 492.3 Documenting Designs: The Unified Modelling Language 512.4 A Case Study: Card Games 62

Designing the classes 622.5 Example: A Simple Card Game 70

3 Inheritance 753.1 Introduction to Inheritance 753.2 Constructors in Sub-classes 803.3 Overriding Methods 83

Overloaded vs Overridden Methods 853.4 Another Example subclass 863.5 Inheritance and Types 88

Type Checking 90Typecasting 90

3.6 Controlling Access to Members of a Class 92Example 93

4 Abstract Classes, Interfaces and Inner Classes 994.1 Abstract Classes 994.2 Interfaces 106

Implementing an Interface 108Example Problem 109Type Casts 115default methods in interfaces 117Additional Facts about Interfaces 118

4.3 Nested Classes 119Static Nested Classes 120Inner Classes 121Anonymous Inner Classes 122Java 8 Lambda Expressions 123

5 Graphical User Interface Programming 1255.1 The Basic GUI Application 125

JFrame and JPanel 126Components and Layout 129Events and Listeners 130

5.2 Graphics and Painting 132Coordinates 134Colors 135Fonts 136Shapes 137

ii

Page 6: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Contents

Graphics2D 139An Example 140

5.3 Mouse Events 143Event Handling 144MouseEvent and MouseListener 146MouseEvent Data 149Anonymous Event Handlers 153Timers 155Timers and Animation 156Keyboard Events 158

5.4 Basic Components 162JButton 164JLabel 165JCheckBox 166JTextField and JTextArea 167

5.5 Basic Layout 169Basic Layout Managers 171

5.6 A Simple Calculator 174Exercises for Chapter 5 177Quiz on Chapter 5 184

iii

Page 7: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming
Page 8: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Chapter

1Introduction to Objects

Object-oriented programming (OOP) represents an attempt to make pro-Object-orientedprogramming is oneof severalprogrammingparadigms

grams more closely model the way people think about and deal with theworld. In the older styles of programming, a programmer who is faced withsome problem must identify a computing task that needs to be performed in or-der to solve the problem. Programming then consists of finding a sequence ofinstructions that will accomplish that task. But at the heart of object-orientedprogramming, instead of tasks we find objects – entities that have behaviors, thathold information, and that can interact with one another. Programming consistsof designing a set of objects that model the problem at hand. Software objects inthe program can represent real or abstract entities in the problem domain. Thisis supposed to make the design of the program more natural and hence easier toget right and easier to understand.

An object-oriented programming language such as Java includes a numberof features that make it very different from a standard language. In order to makeeffective use of those features, you have to “orient” your thinking correctly.

1.1 What is Object Oriented Programming?

Object-orientation 1 is a set of tools and methods that enable softwareObject OrientedProgrammingengineers to build reliable, user friendly, maintainable, well documented,

reusable software systems that fulfills the requirements of its users. It is claimedthat object-orientation provides software developers with new mind tools to use

1This discussion is based on Chapter 2 of “An Introduction to Object-Oriented Programming”by Timothy Budd.

1

Page 9: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

in solving a wide variety of problems. Object-orientation provides a new view ofcomputation. A software system is seen as a community of objects that cooperatewith each other by passing messages in solving a problem.

An object-oriented programming laguage provides support for the followingobject-oriented concepts:

Objects and Classes

Inheritance

Polymophism and Dynamic binding

Object Orientation as a New Paradigm: The Big Picture

It is claimed that the problem-solving techniques used in object-oriented pro-gramming more closely models the way humans solve day-to-day problems.2

So lets consider how we solve an everyday problem: Suppose you wantedto send flowers to a friend named Robin who lives in another city.To solve thisproblem you simply walk to your nearest florist run by, lets say, Fred. You tell Fredthe kinds of flowers to send and the address to which they should be delivered.You can be assured that the flowers will be delivered.

Now, lets examine the mechanisms used to solve your problem.

• You first found an appropriate agent (Fred, in this case) and you passed tothis agent a message containing a request.

• It is the responsibility of Fred to satisfy the request.

• There is some method (an algorithm or set of operations) used by Fred todo this.

• You do not need to know the particular methods used to satisfy the request—such information is hidden from view.

Off course, you do not want to know the details, but on investigation youmay find that Fred delivered a slightly different message to another florist inthe city where your friend Robin lives. That florist then passes another messageto a subordinate who makes the floral arrangement.The flowers, along with yetanother message, is passed onto a delivery person and so on. The florists also hasinteractions with wholesalers who, in turn, had interactions with flower growersand so on.

This leads to our first conceptual picture of object-oriented programming:2This discussion is based on Chapter 2 of An Introduction to Object-Oriented Programming

by Timothy Budd.

2

Page 10: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. What is Object Oriented Programming?

An object-oriented program is structured as community of interactingagents called objects. Each object has a role to play. Each object providesa service or performs an action that is used by other members of thecommunity.

Messages and Responsibilities

Members of an object-oriented community make requests of each other. Thenext important principle explains the use of messages to initiate action:

Action is initiated in object-oriented programming by the transmission ofamessage to an agent (an object) responsible for the actions. The messageencodes the request for an action and is accompanied by any additionalinformation (arguments/parameters) needed to carry out the request. Thereceiver is the object to whom the message is sent. If the receiver acceptsthe message, it accepts responsibility to carry out the indicated action.In response to a message, the receiver will perform somemethod to satisfythe request.

There are some important issues to point out here:

• The client sending the request need not know the means by which therequest is carried out. In this we see the principle of information hiding.

• Another principle implicit in message passing is the idea of finding some-one else to do the work i.e. reusing components that may have been writtenby someone else.

• The interpretation of the message is determined by the receiver and canvary with different receivers. For example, if you sent the message “deliverflowers” to a friend, she will probably have understood what was requiredand flowers would still have been delivered but the method she used wouldhave been very different from that used by the florist.

• In object-oriented programming, behaviour is described in terms of respon-sibilities.

• Client’s requests for actions only indicates the desired outcome. The re-ceivers are free to pursue any technique that achieves the desired outcomes.

• Thinking in this way allows greater independence between objects.

• Thus, objects have responsibilities that they are willing to fulfill on request.The collection of reponsibilities associated with an object is often called aprotocol.

3

Page 11: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

Classes and Instances (Objects)

The next important principle of object-oriented programming is

All objects are instances of a class. The method invoked by an object inresponse to a message is determined by the class of the receiver. All objectsof a given class use the same method in response to similar messages.

Fred is an instance of a category or class of people i.e. Fred is an instance of aclass of florists. The term florist represents a class or category of all florists. Fredis an object or instance of a class.

We interact with instances of a class but the class determines the behaviourof instances. We can tell a lot about how Fred will behave by understanding howFlorists behave. We know, for example, that Fred, like all florists can arrange anddeliver flowers.

In the real world there is this distinction between classes and objects. Real-world objects share two characteristics: They all have state and behavior. Forexample, dogs have state (name, color, breed, hungry) and behavior (barking,fetching, wagging tail). Students have state (name, student number, courses theyare registered for, gender) and behavior (take tests, attend courses, write tests,party).

1.2 Fundamentals of Objects and Classes

We move now from the conceptual picture of objects and classes to a discussionof software classes and objects.3

Objects are closely related to classes. A class can contain variables and meth-ods. If an object is also a collection of variables and methods, how do they differfrom classes?

Objects and Classes

Objects

In object-oriented programming we create software objects that model real worldobjects. Software objects are modeled after real-world objects in that they toohave state and behavior. A software object maintains its state in one or morevariables. A variable is an item of data named by an identifier. A software objectimplements its behavior with methods. A method is a function associated withan object.

3This discussion is based on the “Object-oriented Programming Concepts” section of the JavaTutorial by Sun MicroSystems.

4

Page 12: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

Staterepresented by variables

Behavourrepresented by methods

Figure .: An Object

Definition: An object is a software bundle of variables and related meth-ods.

An object is also known as an instance. An instance refers to a particular object.For e.g. Karuna’s bicycle is an instance of a bicycle—It refers to a particularbicycle. Sandile Zuma is an instance of a Student.

The variables of an object are formally known as instance variables becausethey contain the state for a particular object or instance. In a running program,there may be many instances of an object. For e.g. there may be many Studentobjects. Each of these objects will have their own instance variables and eachobject may have different values stored in their instance variables. For e.g. eachStudent object will have a different number stored in its studentNumber variable.

Encapsulation

Object diagrams show that an object’s variables make up the center, or nucleus,of the object. Methods surround and hide the object’s nucleus of variables fromother objects in the program. Packaging an object’s variables within the protec-tive custody of its methods is called encapsulation.

Encapsulating related variables and methods into a neat software bundle is asimple yet powerful idea that provides two benefits to software developers:

• Modularity: The source code for an object can be written and maintainedindependently of the source code for other objects. Also, an object can beeasily passed around in the system. You can give your bicycle to someoneelse, and it will still work.

• Information-hiding: An object has a public interface that other objects canuse to communicate with it. The object can maintain private informationand methods that can be changed at any time without affecting other ob-jects that depend on it.

5

Page 13: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

message

object A

object B

Figure .: A Message

Messages

Software objects interact and communicate with each other by sending messagesto each other. When object A wants object B to perform one of B’s methods,object A sends a message to object B

There are three parts of a message: The three parts for the messageSystem.out.println("Hello World"); are:

• The object to which the message is addressed (System.out)

• The name of the method to perform (println)

• Any parameters needed by the method (”Hello World!”)

Classes

A class is a software blueprint for objects. A class is used to manufacture orcreate objects. The class specifies the state of its objects by defining the instance

A class is a kind offactory for

constructing objects.The non-static

parts of the classspecify, or describe,what variables andmethods the objects

will contain.

variables necessary to contain the state of every object. The class specifies thebehaviour of objects by declaring and providing implementations for the instancemethods necessary to operate on the state of the object.

Definition: A class is a blueprint that defines the variables and themethods common to all objects of a certain kind.

After you’ve created the class, you can create any number of objects from thatclass. All the objects created from the class will have the same characteristics.Objects are created and destroyed as the program runs, and there can be manyobjects with the same structure, if they are created using the same class.

Types

Java, like most programming languages classifies values and expressions into types.For e.g. String’s and int’s are types. A type basically specifies the allowed valuesand allowed operations on values of that type.

6

Page 14: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

Definition: A type is a set of values together with one or more operationsthat can be applied uniformly to all these values.

A type system basically gives meaning to collections of bits. Because any valuesimply consists of a set of bits in a computer, the hardware makes no distinctionbetween memory addresses, instruction code, characters, integers and floating-point numbers. Types inform programs and programmers how they should treatthose bits.

For example the integers are a type with values in the range−2,147,483,648 to +2,147,483,647 and various allowed operations that include addition, subtraction,modulus etc.

The use of types by a programming language has several advantages:

• Safety. Use of types may allow a compiler to detect meaningless or invalidcode. For example, we can identify an expression ”Hello, World” / 3 asinvalid because one cannot divide a string literal by an integer. Strongtyping offers more safety.

• Optimization. Static type-checking may provide useful information to acompiler. The compiler may then be able to generate more efficient code.

• Documentation. Types can serve as a form of documentation, since theycan illustrate the intent of the programmer. For instance, timestamps maybe a subtype of integers – but if a programmer declares a method as return-ing a timestamp rather than merely an integer, this documents part of themeaning of the method.

• Abstraction. Types allow programmers to think about programs at a higherlevel, not bothering with low-level implementation. For example, program-mers can think of strings as values instead of as a mere array of bytes.

There are fundamentally two kinds of types in Java: primitive types and refer-ence types i.e. any variable you declare is either declared to be one of the primitivetypes or a reference type. int, double and char are the built-in, primitive typesin Java.The primitive types can be used in various combinations to create other,composite types. Every time we define a class, we are actually defining a newtype. For example, the Student class defined above introduces a new type. Wecan now use this type like any other type: we can declare variables to be of thistype and we can use it as a type for parameters of methods.

Before a variable can be used, it must be declared. A declaration gives avariable a name, a type and an initial value for e.g. int x = 8 declares x to beof type int. All objects that we declare also have to be of a specified type–thetype of an object is the class from which it is created. Thus, when we declareobjects we state the type like so: Student st = new Student();. This statement

7

Page 15: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

declares the variable st to be of type Student. This statement creates a new objectof the specified type and runs the Student constructor. The constructor’s job isto properly initialize the object.

The String type is another example of an object type. Student and String

are composite types and give us the same advantages as the built-in types. Theability to create our own types is a very powerful idea in modern languages.

When declaring variables, we can assign initial values. If you do not spec-ify initial values, the compiler automatically assigns one: Instance variables ofnumerical type (int, double, etc.) are automatically initialized to zero; booleanvariables are initialized to false; and char variables, to the Unicode characterwith code number zero. The default initial value of object types is null.

Class Members and Instance Members

A class definition is made of members or components. A class can define variables(or fields) and methods. Variables and methods can be static or non-static i.e. theyare defined with or without the keyword static.

e.g.static double lastStudentNumber; //a static

member/variable/field

double studentNumber; //a non-static variable

static void printLastNumber() {...} //a static member/method

void printNumber() {...} //a non-static method

The non-static members of a class (variables and methods) are also knownas instance variables and methods while the static members are also known asclass variables and class methods. Each instance of a class (each object) gets itsown copy of all the instance variables defined in the class. When you create aninstance of a class, the system allocates enough memory for the object and all itsinstance variables.

In addition to instance variables, classes can declare class variables (or staticvariables). A class variable contains information that is shared by all instances(objects) of the class. If one object changes the variable, it changes for all otherobjects of that type. e.g. A Student number generator in a NewStudent class.

You can invoke a class method directly from the class, whereas you mustinvoke instance methods on a particular instance. e.g. The methods in the Math

class are static and can be invoked without creating an instance of the Math classfor e.g. we can say Math.sqrt(x).

Consider a simple class whose job is to group together a few static membervariables for example a class could be used to store information about the personwho is using the program:

class UserData { static String name; static int age; }

8

Page 16: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

In programs that use this class, there is one copy each of the variables UserData.nameand UserData.age. There can only be one “user,” since we only have memoryspace to store data about one user. The class, UserData, and the variables it con-tains exist as long as the program runs. Now, consider a similar class that includesnon-static variables:

class PlayerData { String name; int age; }

In this case, there is no such variable as PlayerData.name or PlayerData.age,since name and age are not static members of PlayerData. There is nothing muchin the class except the potential to create objects. But, it’s a lot of potential, sinceit can be used to create any number of objects! Each object will have its ownvariables called name and age. There can be many “players” because we can makenew objects to represent new players on demand. A program might use thisclass to store information about multiple players in a game. Each player has aname and an age. When a player joins the game, a new PlayerData object can becreated to represent that player. If a player leaves the game, the PlayerData objectthat represents that player can be destroyed. A system of objects in the programis being used to dynamically model what is happening in the game. You can’t dothis with “static” variables!

An object that belongs to a class is said to be an instance of that class and thevariables that the object contains are called instance variables. The methodsthat the object contains are called instance methods.

For example, if the PlayerData class, is used to create an object, then that ob-ject is an instance of the PlayerData class, and name and age are instance variablesin the object. It is important to remember that the class of an object determinesthe types of the instance variables; however, the actual data is contained insidethe individual objects, not the class. Thus, each object has its own set of data.

The source code for methods are defined in the class yet it is better to thinkof the instance methods as belonging to the object, not to the class. The non-static methods in the class merely specify the instance methods that every objectcreated from the class will contain. For example a draw() method in two differentobjects do the same thing in the sense that they both draw something. But thereis a real difference between the two methods—the things that they draw can bedifferent. You might say that the method definition in the class specifies whattype of behavior the objects will have, but the specific behavior can vary fromobject to object, depending on the values of their instance variables.

The static and the non-static portions of a class are very different things andserve very different purposes. Many classes contain only static members, or onlynon-static. However, it is possible to mix static and non-static members in asingle class. The “static” definitions in the source code specify the things that arepart of the class itself, whereas the non-static definitions in the source code specifythings that will become part of every instance object that is created from the class.

9

Page 17: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

Static member variables and static member methods in a class are sometimescalled class variables and class methods, since they belong to the class itself,rather than to instances of that class.

So far, we’ve been talking mostly in generalities. Let’s now look at a specificexample to see how classes and objects work. Consider this extremely simpli-fied version of a Student class, which could be used to store information aboutstudents taking a course:

public class Student {

public String name; // Student's name. public double test1,

test2, test3; // Grades on three tests.

public double getAverage() { // compute average test grade

return

(test1 + test2 + test3) / 3; }

}// end of class Student

None of the members of this class are declared to be static, so the class ex-ists only for creating objects. This class definition says that any object that is aninstance of the Student class will include instance variables named name, test1,test2, and test3, and it will include an instance method named getAverage().The names and tests in different objects will generally have different values. Whencalled for a particular student, the method getAverage() will compute an averageusing that student’s test grades. Different students can have different averages.(Again, this is what it means to say that an instance method belongs to an indi-vidual object, not to the class.)

In Java, a class is a type, similar to the built-in types such as int and boolean.So, a class name can be used to specify the type of a variable in a declarationstatement, the type of a formal parameter, or the return type of a method. Forexample, a program could define a variable named std of type Student with thestatement

Student std;

However, declaring a variable does not create an object! This is an importantpoint, which is related to this Very Important Fact:

In Java, no variable can ever hold an object. A variable can only hold areference to an object.

You should think of objects as floating around independently in the com-puter’s memory. In fact, there is a special portion of memory called the heapwhere objects live. Instead of holding an object itself, a variable holds the in-formation necessary to find the object in memory. This information is called a

10

Page 18: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

reference or pointer to the object. In effect, a reference to an object is the addressof the memory location where the object is stored. When you use a variableof class type, the computer uses the reference in the variable to find the actualobject.

In a program, objects are created using an operator called new, which createsan object and returns a reference to that object. For example, assuming that stdis a variable of type Student, declared as above, the assignment statement

std = new Student();

would create a new object which is an instance of the class Student, and it wouldstore a reference to that object in the variable std. The value of the variable is areference to the object, not the object itself. It is not quite true to say that theobject is the “value of the variable std”. It is certainly not at all true to say thatthe object is “stored in the variable std.” The proper terminology is that “thevariable std refers to the object,”.

So, suppose that the variable std refers to an object belonging to the classStudent. That object has instance variables name, test1, test2, and test3. Theseinstance variables can be referred to as std.name, std.test1, std.test2, andstd.test3. This follows the usual naming convention that when B is part of A,then the full name of B is A.B. For example, a program might include the lines

System.out.println("Hello, " + std.name + ". Your test grades

are:");

System.out.println(std.test1);

System.out.println(std.test2);

System.out.println(std.test3);

This would output the name and test grades from the object to which std

refers. Similarly, std can be used to call the getAverage() instance method inthe object by saying std.getAverage(). To print out the student’s average, youcould say:

System.out.println( "Your average is " + std.getAverage() );

More generally, you could use std.name any place where a variable of typeString is legal. You can use it in expressions. You can assign a value to it. Youcan pass it as a parameter to method. You can even use it to call methods fromthe String class. For example, std.name.length() is the number of charactersin the student’s name.

It is possible for a variable like std, whose type is given by a class, to referto no object at all. We say in this case that std holds a null reference. Thenull reference is written in Java as “null”. You can store a null reference in thevariable std by saying “std = null;” and you could test whether the value of“std” is null by testing “if (std == null). . .”.

11

Page 19: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

If the value of a variable is null, then it is, of course, illegal to refer to instancevariables or instance methods through that variable–since there is no object, andhence no instance variables to refer to. For example, if the value of the variable stis null, then it would be illegal to refer to std.test1. If your program attemptsto use a null reference illegally like this, the result is an error called a null pointerexception.

Let’s look at a sequence of statements that work with objects:

Student std, std1, // Declare four variables of

std2, std3; // type Student.

std = new Student(); // Create a new object belonging

// to the class Student, and

// store a reference to that

// object in the variable std.

std1 = new Student(); // Create a second Student object

// and store a reference to

// it in the variable std1.

std2 = std1; // Copy the reference value in std1

// into the variable std2.

std3 = null; // Store a null reference in the

// variable std3.

std.name = "John Smith"; // Set values of some instance

variables.

std1.name = "Mary Jones";

// (Other instance variables have default

// initial values of zero.)

After the computer executes these statements, the situation in the computer’smemory looks like this:

12

Page 20: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

This picture shows variables as little boxes, labeled with the names of thevariables. Objects are shown as boxes with round corners. When a variablecontains a reference to an object, the value of that variable is shown as an arrowpointing to the object. The variable std3, with a value of null, doesn’t pointanywhere. The arrows from std1 and std2 both point to the same object. Thisillustrates a Very Important Point:

When one object variable is assigned to another, only a reference is copied.The object referred to is not copied.

When the assignment “std2 = std1;” was executed, no new object was cre-ated. Instead, std2 was set to refer to the very same object that std1 refers to.This has some consequences that might be surprising. For example, std1.nameand std2.name are two different names for the same variable, namely the instancevariable in the object that both std1 and std2 refer to. After the string “MaryJones” is assigned to the variable std1.name, it is also be true that the value ofstd2.name is “Mary Jones”. There is a potential for a lot of confusion here, butyou can help protect yourself from it if you keep telling yourself, “The object isnot in the variable. The variable just holds a pointer to the object.”

You can test objects for equality and inequality using the operators == and !=,but here again, the semantics are different from what you are used to. The test“if (std1 == std2)”, tests whether the values stored in std1 and std2 are thesame. But the values are references to objects, not objects. So, you are testing

13

Page 21: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

whether std1 and std2 refer to the same object, that is, whether they point to thesame location in memory. This is fine, if its what you want to do. But sometimes,what you want to check is whether the instance variables in the objects have thesame values. To do that, you would need to ask whether

std1.test1 == std2.test1 && std1.test2 == std2.test2 &&

std1.test3

== std2.test3 && std1.name.equals(std2.name)}

I’ve remarked previously that Strings are objects, and I’ve shown the strings“Mary Jones” and “John Smith” as objects in the above illustration. A variableof type String can only hold a reference to a string, not the string itself. It couldalso hold the value null, meaning that it does not refer to any string at all. Thisexplains why using the == operator to test strings for equality is not a good idea.

The fact that variables hold references to objects, not objects themselves, has acouple of other consequences that you should be aware of. They follow logically,if you just keep in mind the basic fact that the object is not stored in the variable.The object is somewhere else; the variable points to it.

Suppose that a variable that refers to an object is declared to be final. Thismeans that the value stored in the variable can never be changed, once the variablehas been initialized. The value stored in the variable is a reference to the object.So the variable will continue to refer to the same object as long as the variableexists. However, this does not prevent the data in the object from changing. Thevariable is final, not the object. It’s perfectly legal to say

final Student stu = new Student();

stu.name = "John Doe"; // Change data in the object;

// The value stored in stu is not

changed!

// It still refers to the same object.

Next, suppose that obj is a variable that refers to an object. Let’s considerwhat happens when obj is passed as an actual parameter to a method. The value ofobj is assigned to a formal parameter in the method, and the method is executed.The method has no power to change the value stored in the variable, obj. It onlyhas a copy of that value. However, that value is a reference to an object. Since themethod has a reference to the object, it can change the data stored in the object.After the method ends, obj still points to the same object, but the data stored inthe object might have changed. Suppose x is a variable of type int and stu is avariable of type Student. Compare:

void dontChange(int z) { void change(Student s) {

z = 42; s.name = "Fred";

} }

14

Page 22: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

The lines: The lines:

x = 17; stu.name = "Jane";

dontChange(x); change(stu);

System.out.println(x); System.out.println(stu.name);

outputs the value 17. outputs the value "Fred".

The value of x is not The value of stu is not

changed ,

changed by the method, but stu.name is.

which is equivalent to This is equivalent to

z = x; s = stu;

z = 42; s.name = "Fred";

Access Control

When writing new classes, it’s a good idea to pay attention to the issue of accesscontrol. Recall that making a member of a class public makes it accessible fromanywhere, including from other classes. On the other hand, a private membercan only be used in the class where it is defined.

In the opinion of many programmers, almost all member variables should bedeclared private. This gives you complete control over what can be done withthe variable. Even if the variable itself is private, you can allow other classes tofind out what its value is by providing a public accessor method that returnsthe value of the variable. For example, if your class contains a private membervariable, title, of type String, you can provide a method

public String getTitle() { return title; }

that returns the value of title. By convention, the name of an accessor methodfor a variable is obtained by capitalizing the name of variable and adding “get”in front of the name. So, for the variable title, we get an accessor methodnamed “get” + “Title”, or getTitle(). Because of this naming convention, ac-cessor methods are more often referred to as getter methods. A getter methodprovides “read access” to a variable.

You might also want to allow “write access” to a private variable. That is,you might want to make it possible for other classes to specify a new value forthe variable. This is done with a setter method. (If you don’t like simple, Anglo-Saxon words, you can use the fancier term mutator method.) The name of a settermethod should consist of “set” followed by a capitalized copy of the variable’sname, and it should have a parameter with the same type as the variable. A settermethod for the variable title could be written

15

Page 23: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

public void setTitle( String newTitle ) { title = newTitle; }

It is actually very common to provide both a getter and a setter method fora private member variable. Since this allows other classes both to see and tochange the value of the variable, you might wonder why not just make the vari-able public? The reason is that getters and setters are not restricted to simplyreading and writing the variable’s value. In fact, they can take any action at all.For example, a getter method might keep track of the number of times that thevariable has been accessed:

public String getTitle() {

titleAccessCount++; //Increment member variable

titleAccessCount.

return title;

}

and a setter method might check that the value that is being assigned to thevariable is legal:

public void setTitle( String newTitle ) {

if ( newTitle == null ) //Don't allow null strings as titles!

title = "(Untitled)"; // Use an appropriate default value

instead.

else

title = newTitle; }

Even if you can’t think of any extra chores to do in a getter or setter method,you might change your mind in the future when you redesign and improve yourclass. If you’ve used a getter and setter from the beginning, you can make themodification to your class without affecting any of the classes that use your class.The private member variable is not part of the public interface of your class;only the public getter and setter methods are. If you haven’t used get and setfrom the beginning, you’ll have to contact everyone who uses your class and tellthem, “Sorry guys, you’ll have to track down every use that you’ve made of thisvariable and change your code.”

Creating and Destroying Objects

Object types in Java are very different from the primitive types. Simply declaringa variable whose type is given as a class does not automatically create an objectof that class. Objects must be explicitly constructed. For the computer, theprocess of constructing an object means, first, finding some unused memory inthe heap that can be used to hold the object and, second, filling in the object’sinstance variables. As a programmer, you don’t care where in memory the objectis stored, but you will usually want to exercise some control over what initialvalues are stored in a new object’s instance variables. In many cases, you will also

16

Page 24: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

want to do more complicated initialization or bookkeeping every time an objectis created.

Initializing Instance Variables

An instance variable can be assigned an initial value in its declaration, just likeany other variable. For example, consider a class named PairOfDice. An objectof this class will represent a pair of dice. It will contain two instance variables torepresent the numbers showing on the dice and an instance method for rollingthe dice:

public class PairOfDice {

public int die1 = 3; // Number showing on the first die.

public int die2 = 4; // Number showing on the second die.

public void roll() {

// Roll the dice by setting each of the dice to be

// a random number between 1 and 6.

die1 = (int)(Math.random()*6) + 1;

die2 = (int)(Math.random()*6) + 1;

}

} // end class PairOfDice

The instance variables die1 and die2 are initialized to the values 3 and 4 re-spectively. These initializations are executed whenever a PairOfDice object isconstructed. It is important to understand when and how this happens. ManyPairOfDice objects may exist. Each time one is created, it gets its own instancevariables, and the assignments “die1 = 3” and “die2 = 4” are executed to fill inthe values of those variables. To make this clearer, consider a variation of thePairOfDice class:

public class PairOfDice {

public int die1 = (int)(Math.random()*6) + 1;

public int die2 = (int)(Math.random()*6) + 1;

public void roll() {

die1 = (int)(Math.random()*6) + 1;

die2 = (int)(Math.random()*6) + 1;

}

} // end class PairOfDice

Here, the dice are initialized to random values, as if a new pair of dice were beingthrown onto the gaming table. Since the initialization is executed for each new

17

Page 25: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

object, a set of random initial values will be computed for each new pair of dice.Different pairs of dice can have different initial values. For initialization of staticmember variables, of course, the situation is quite different. There is only onecopy of a static variable, and initialization of that variable is executed just once,when the class is first loaded.

If you don’t provide any initial value for an instance variable, a default ini-tial value is provided automatically. Instance variables of numerical type (int,double, etc.) are automatically initialized to zero if you provide no other values;boolean variables are initialized to false; and char variables, to the Unicodecharacter with code number zero. An instance variable can also be a variable ofobject type. For such variables, the default initial value is null. (In particular,since Strings are objects, the default initial value for String variables is null.)

Constructors

Objects are created with the operator, new. For example, a program that wantsto use a PairOfDice object could say:

PairOfDice dice; // Declare a variable of type PairOfDice.

dice = new PairOfDice(); // Construct a new object and store a

// reference to it in the variable.

In this example, “new PairOfDice()” is an expression that allocates memoryfor the object, initializes the object’s instance variables, and then returns a refer-ence to the object. This reference is the value of the expression, and that value isstored by the assignment statement in the variable, dice, so that after the assign-ment statement is executed, dice refers to the newly created object. Part of thisexpression, “PairOfDice()”, looks like a method call, and that is no accident. Itis, in fact, a call to a special type of method called a constructor. This mightpuzzle you, since there is no such method in the class definition. However, everyclass has at least one constructor. If the programmer doesn’t write a construc-tor definition in a class, then the system will provide a default constructor

for that class. This default constructor does nothing beyond the basics: allocatememory and initialize instance variables. If you want more than that to happenwhen an object is created, you can include one or more constructors in the classdefinition.

The definition of a constructor looks much like the definition of any othermethod, with three differences.

1. A constructor does not have any return type (not even void).

2. The name of the constructor must be the same as the name of the class inwhich it is defined.

18

Page 26: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

3. The only modifiers that can be used on a constructor definition are theaccess modifiers public, private, and protected. (In particular, a con-structor can’t be declared static.)

However, a constructor does have a method body of the usual form, a blockof statements. There are no restrictions on what statements can be used. And itcan have a list of formal parameters. In fact, the ability to include parameters isone of the main reasons for using constructors. The parameters can provide datato be used in the construction of the object. For example, a constructor for thePairOfDice class could provide the values that are initially showing on the dice.Here is what the class would look like in that case:

The constructor is declared as “public PairOfDice(int val1, int val2)...”,with no return type and with the same name as the name of the class. This is howthe Java compiler recognizes a constructor. The constructor has two parameters,and values for these parameters must be provided when the constructor is called.For example, the expression “new PairOfDice(3,4)” would create a PairOfDice

object in which the values of the instance variables die1 and die2 are initially 3and 4. Of course, in a program, the value returned by the constructor should beused in some way, as in

PairOfDice dice; // Declare a variable of type PairOfDice.

dice = new PairOfDice(1,1); // Let dice refer to a new

PairOfDice

// object that initially shows 1,

1.

Now that we’ve added a constructor to the PairOfDice class, we can no longercreate an object by saying “new PairOfDice()”! The system provides a defaultconstructor for a class only if the class definition does not already include a con-structor, so there is only one constructor in the class, and it requires two actualparameters. However, this is not a big problem, since we can add a second con-structor to the class, one that has no parameters. In fact, you can have as manydifferent constructors as you want, as long as their signatures are different, thatis, as long as they have different numbers or types of formal parameters. In thePairOfDice class, we might have a constructor with no parameters which pro-duces a pair of dice showing random numbers:

public class PairOfDice {

public int die1; // Number showing on the first die.

public int die2; // Number showing on the second die.

public PairOfDice() {

// Constructor. Rolls the dice, so that they

initially

19

Page 27: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

// show some random values.

roll(); // Call the roll() method to roll the dice.

}

public PairOfDice(int val1, int val2) {

// Constructor. Creates a pair of dice that

// are initially showing the values val1 and val2.

die1 = val1; // Assign specified values

die2 = val2; // to the instance variables.

}

public void roll() {

// Roll the dice by setting each of the dice to be

// a random number between 1 and 6.

die1 = (int)(Math.random()*6) + 1;

die2 = (int)(Math.random()*6) + 1;

}

} // end class PairOfDice

Now we have the option of constructing a PairOfDice object with “newPairOfDice()” or with “new PairOfDice(x,y)”, where x and y are int-valuedexpressions.

This class, once it is written, can be used in any program that needs to workwith one or more pairs of dice. None of those programs will ever have to usethe obscure incantation “(int)(Math.random()*6)+1”, because it’s done insidethe PairOfDice class. And the programmer, having once gotten the dice-rollingthing straight will never have to worry about it again. Here, for example, is amain program that uses the PairOfDice class to count how many times two pairsof dice are rolled before the two pairs come up showing the same value. Thisillustrates once again that you can create several instances of the same class:

public class RollTwoPairs {

public static void main(String[] args) {

PairOfDice firstDice; // Refers to the first pair of

dice.

firstDice = new PairOfDice();

PairOfDice secondDice; // Refers to the second pair of

dice.

secondDice = new PairOfDice();

int countRolls; // Counts how many times the two pairs

of

// dice have been rolled.

20

Page 28: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

int total1; // Total showing on first pair of dice.

int total2; // Total showing on second pair of dice.

countRolls = 0;

do { // Roll the two pairs of dice until totals are the

same.

firstDice.roll(); // Roll the first pair of dice.

total1 = firstDice.die1 + firstDice.die2; // Get

total.

System.out.println("First pair comes up " + total1);

secondDice.roll(); // Roll the second pair of

dice.

total2 = secondDice.die1 + secondDice.die2; // Get

total.

System.out.println("Second pair comes up " + total2);

countRolls++; // Count this roll.

System.out.println(); // Blank line.

} while (total1 != total2);

System.out.println("It took " + countRolls

+ " rolls until the totals were the

same.");

} // end main()

} // end class RollTwoPairs

Constructors are methods, but they are methods of a special type. They arecertainly not instance methods, since they don’t belong to objects. Since they areresponsible for creating objects, they exist before any objects have been created.They are more like static member methods, but they are not and cannot bedeclared to be static. In fact, according to the Java language specification, theyare technically not members of the class at all! In particular, constructors are notreferred to as “methods”.

Unlike other methods, a constructor can only be called using the new operator,in an expression that has the form

new class-name{parameter-list}

where the parameter-list is possibly empty. I call this an expression because itcomputes and returns a value, namely a reference to the object that is constructed.

21

Page 29: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

Most often, you will store the returned reference in a variable, but it is also legalto use a constructor call in other ways, for example as a parameter in a methodcall or as part of a more complex expression. Of course, if you don’t save thereference in a variable, you won’t have any way of referring to the object that wasjust created.

A constructor call is more complicated than an ordinary method call. It ishelpful to understand the exact steps that the computer goes through to executea constructor call:

1. First, the computer gets a block of unused memory in the heap, largeenough to hold an object of the specified type.

2. It initializes the instance variables of the object. If the declaration of aninstance variable specifies an initial value, then that value is computed andstored in the instance variable. Otherwise, the default initial value is used.

3. The actual parameters in the constructor, if any, are evaluated, and thevalues are assigned to the formal parameters of the constructor.

4. The statements in the body of the constructor, if any, are executed.

5. A reference to the object is returned as the value of the constructor call.

The end result of this is that you have a reference to a newly constructed object.You can use this reference to get at the instance variables in that object or to callits instance methods.

For another example, let’s rewrite the Student class. I’ll add a constructor,and I’ll also take the opportunity to make the instance variable, name, private.

public class Student {

private String name; // Student's name.

public double test1, test2, test3; // Grades on three tests.

// Constructor for Student objects-provides a name for the

Student.

Student(String theName) {

name = theName;

}

// Getter method for the private instance variable, name.

public String getName() {

return name;

}

// Compute average test grade.

public double getAverage() {

22

Page 30: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

return (test1 + test2 + test3) / 3;

}

}// end of class Student

An object of type Student contains information about some particular stu-dent. The constructor in this class has a parameter of type String, which specifiesthe name of that student. Objects of type Student can be created with statementssuch as:

std = new Student("John Smith");

std1 = new Student("Mary Jones");

In the original version of this class, the value of name had to be assigned by aprogram after it created the object of type Student. There was no guarantee thatthe programmer would always remember to set the name properly. In the newversion of the class, there is no way to create a Student object except by calling theconstructor, and that constructor automatically sets the name. The programmer’slife is made easier, and whole hordes of frustrating bugs are squashed before theyeven have a chance to be born.

Another type of guarantee is provided by the private modifier. Since theinstance variable, name, is private, there is no way for any part of the programoutside the Student class to get at the name directly. The program sets the value ofname, indirectly, when it calls the constructor. I’ve provided a method, getName(),that can be used from outside the class to find out the name of the student. ButI haven’t provided any setter method or other way to change the name. Once astudent object is created, it keeps the same name as long as it exists.

Garbage Collection

So far, this section has been about creating objects. What about destroying them?In Java, the destruction of objects takes place automatically.

An object exists in the heap, and it can be accessed only through variablesthat hold references to the object. What should be done with an object if thereare no variables that refer to it? Such things can happen. Consider the followingtwo statements (though in reality, you’d never do anything like this):

Student std = new Student("John Smith"); std = null;

In the first line, a reference to a newly created Student object is stored in thevariable std. But in the next line, the value of std is changed, and the referenceto the Student object is gone. In fact, there are now no references whatsoever tothat object stored in any variable. So there is no way for the program ever to usethe object again. It might as well not exist. In fact, the memory occupied by theobject should be reclaimed to be used for another purpose.

Java uses a procedure called garbage collection to reclaim memory occupiedby objects that are no longer accessible to a program. It is the responsibility of

23

Page 31: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

the system, not the programmer, to keep track of which objects are “garbage”. Inthe above example, it was very easy to see that the Student object had becomegarbage. Usually, it’s much harder. If an object has been used for a while, theremight be several references to the object stored in several variables. The objectdoesn’t become garbage until all those references have been dropped.

In many other programming languages, it’s the programmer’s responsibilityto delete the garbage. Unfortunately, keeping track of memory usage is very error-prone, and many serious program bugs are caused by such errors. A programmermight accidently delete an object even though there are still references to thatobject. This is called a dangling pointer error, and it leads to problems whenthe program tries to access an object that is no longer there. Another type oferror is a memory leak, where a programmer neglects to delete objects that are nolonger in use. This can lead to filling memory with objects that are completelyinaccessible, and the program might run out of memory even though, in fact,large amounts of memory are being wasted.

Because Java uses garbage collection, such errors are simply impossible. Garbagecollection is an old idea and has been used in some programming languages sincethe 1960s. You might wonder why all languages don’t use garbage collection. Inthe past, it was considered too slow and wasteful. However, research into garbagecollection techniques combined with the incredible speed of modern computershave combined to make garbage collection feasible. Programmers should rejoice.

Everything is NOT an object

Wrapper Classes and Autoboxing

Recall that there are two kinds of types in Java: primitive types and object types(Classes). In some object-oriented languages, everything is an object. Howeverin Java and in C++, the primitive types like int and double are not objects. Thisdecision was made for memory and processing efficiency—it takes less memoryto store an int than it is to store an object.

Sometimes, however, it is necessary to manipulate the primitive types as ifthey were objects. To make this possible, you can define wrapper classes whosesole aim is to contain one of the primitive types. They are used for creatingobjects that represent primitive type values.

For example the Java API contains the classes Double (that wraps a singledouble) and Integer that wraps a single integer. These classes contain vari-ous static methods including Double.parseDouble and Integer.parseInteger

that are used to convert strings to numerical values. The Character class wrapsa single char type. There is a similar class for each of the other primitive types,Long, Short, Byte, Float, and Boolean.

24

Page 32: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Fundamentals of Objects and Classes

Remember that the primitive types are not classes, and values of primitivetype are not objects. However, sometimes it’s useful to treat a primitive value asif it were an object. You can’t do that literally, but you can “wrap” the primitivetype value in an object belonging to one of the wrapper classes.

For example, an object of type Double contains a single instance variable, oftype double. The object is a wrapper for the double value. For example, you cancreate an object that wraps the double value 6.0221415e23 with

Double d = new Double(6.0221415e23);

The value of d contains the same information as the value of type double, butit is an object. If you want to retrieve the double value that is wrapped in theobject, you can call the method d.doubleValue(). Similarly, you can wrap anint in an object of type Integer, a boolean value in an object of type Boolean,and so on. (As an example of where this would be useful, the collection classesthat will be studied in Chapter 10 can only hold objects. If you want to add aprimitive type value to a collection, it has to be put into a wrapper object first.)

In Java 5.0, wrapper classes have become easier to use. Java 5.0 introducedautomatic conversion between a primitive type and the corresponding wrapperclass. For example, if you use a value of type int in a context that requires anobject of type Integer, the int will automatically be wrapped in an Integer

object. For example, you can say Integer answer = 42; and the computer willsilently read this as if it were Integer answer = new Integer(42);.

This is called autoboxing. It works in the other direction, too. For example,if d refers to an object of type Double, you can use d in a numerical expressionsuch as 2*d. The double value inside d is automatically unboxed and multipliedby 2. Autoboxing and unboxing also apply to method calls. For example, youcan pass an actual parameter of type int to a method that has a formal parameterof type Integer. In fact, autoboxing and unboxing make it possible in manycircumstances to ignore the difference between primitive types and objects.

The wrapper classes contain a few other things that deserve to be mentioned.Integer contains constants Integer.MIN_VALUE and Integer.MAX_VALUE, whichare equal to the largest and smallest possible values of type int, that is, to −2147483648and 2147483647 respectively. It’s certainly easier to remember the names thanthe numerical values. There are similar named constants in Long, Short, andByte. Double and Float also have constants named MIN_VALUE and MAX_VALUE.MAX_VALUE still gives the largest number that can be represented in the given type,but MIN_VALUE represents the smallest possible positive value. For type double,Double.MIN_VALUE is 4.9×10−324. Since double values have only a finite accuracy,they can’t get arbitrarily close to zero. This is the closest they can get without ac-tually being equal to zero.

The class Double deserves special mention, since doubles are so much morecomplicated than integers. The encoding of real numbers into values of type

25

Page 33: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

double has room for a few special values that are not real numbers at all in themathematical sense. These values named constants in the class: Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY, and Double.NaN. The infinite values can occur asvalues of certain mathematical expressions. For example, dividing a positivenumber by zero will give Double.POSITIVE_INFINITY. (It’s even more compli-cated than this, actually, because the double type includes a value called “neg-ative zero”, written -0.0. Dividing a positive number by negative zero givesDouble.NEGATIVE_INFINITY.) You also get Double.POSITIVE_INFINITY when-ever the mathematical value of an expression is greater than Double.MAX_VALUE.For example, 1e200*1e200 is considered to be infinite. The value Double.NaN iseven more interesting. “NaN” stands for Not a Number, and it represents an un-defined value such as the square root of a negative number or the result of dividingzero by zero. Because of the existence of Double.NaN, no mathematical operationon real numbers will ever throw an exception; it simply gives Double.NaN as theresult.

You can test whether a value, x, of type double is infinite or undefined by call-ing the boolean-valued static methods Double.isInfinite(x) and Double.isNaN().(It’s especially important to use Double.isNaN() to test for undefined values, be-cause Double.NaN has really weird behavior when used with relational operatorssuch as ==. In fact, the values of x == Double.NaN and x != Double.NaN are bothfalse, no matter what the value of x, so you really can’t use these expressions totest whether x is Double.NaN.)

1.3 Introduction to Error Handling

In addition to the control structures that determine the normal flow ofcontrol in a program, Java has a way to deal with “exceptional” cases that

throw the flow of control off its normal track. When an error occurs during theexecution of a program, the default behavior is to terminate the program and toprint an error message. However, Java makes it possible to “catch” such errorsand program a response different from simply letting the program crash. Thisis done with the try..catch statement. In this section, we will take a preliminary,incomplete look at using try..catch to handle errors.

Exceptions

The term exception is used to refer to the type of error that one might want tohandle with a try..catch. An exception is an exception to the normal flow ofcontrol in the program. The term is used in preference to “error” because insome cases, an exception might not be considered to be an error at all. You cansometimes think of an exception as just another way to organize a program.

26

Page 34: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Introduction to Error Handling

Exceptions in Java are represented as objects of type Exception. Actual ex-ceptions are defined by subclasses of Exception. Different subclasses representdifferent types of exceptions We will look at only two types of exception in thissection: NumberFormatException and IllegalArgumentException.

A NumberFormatException can occur when an attempt is made to convert astring into a number. Such conversions are done, for example, by Integer.parseIntand Integer.parseDouble . Consider the method call Integer.parseInt(str)where str is a variable of type String. If the value of str is the string “42”, thenthe method call will correctly convert the string into the int 42. However, ifthe value of str is, say, “fred”, the method call will fail because “fred” is nota legal string representation of an int value. In this case, an exception of typeNumberFormatException occurs. If nothing is done to handle the exception, theprogram will crash.

An IllegalArgumentException can occur when an illegal value is passed asa parameter to a method. For example, if a method requires that a parameter begreater than or equal to zero, an IllegalArgumentException might occur whena negative value is passed to the method. How to respond to the illegal value isup to the person who wrote the method, so we can’t simply say that every illegalparameter value will result in an IllegalArgumentException. However, it is acommon response.

One case where an IllegalArgumentException can occur is in the valueOf

method of an enumerated type. Recall that this method tries to convert a stringinto one of the values of the enumerated type. If the string that is passed as aparameter to valueOf is not the name of one of the enumerated type’s value, thenan IllegalArgumentException occurs. For example, given the enumerated type

enum Toss { HEADS, TAILS };

Toss.valueOf("HEADS") correctly returns Toss.HEADS, but Toss.valueOf(``FEET'')results in an IllegalArgumentException.

try …catch

When an exception occurs, we say that the exception is “thrown”. For example,we say that Integer.parseInt(str) throws an exception of type NumberFormatExceptionwhen the value of str is illegal. When an exception is thrown, it is possible to“catch” the exception and prevent it from crashing the program. This is donewith a try..catch statement. In somewhat simplified form, the syntax for atry..catch is:

27

Page 35: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

try {

statements-1

}

catch ( exception-class-name variable-name ) {

statements-2

}

The exception-class-name in the catch clause could be NumberFormatException,IllegalArgumentException, or some other exception class. When the computerexecutes this statement, it executes the statements in the try part. If no erroroccurs during the execution of statements-1, then the computer just skips overthe catch part and proceeds with the rest of the program. However, if an excep-tion of type exception-class-name occurs during the execution of statements-1,the computer immediately jumps to the catch part and executes statements-2,skipping any remaining statements in statements-1. During the execution ofstatements-2, the variable-name represents the exception object, so that you can,for example, print it out. At the end of the catch part, the computer proceedswith the rest of the program; the exception has been caught and handled anddoes not crash the program. Note that only one type of exception is caught; ifsome other type of exception occurs during the execution of statements-1, itwill crash the program as usual.

(By the way, note that the braces, are part of the syntax of the try..catchstatement. They are required even if there is only one statement between thebraces. This is different from the other statements we have seen, where the bracesaround a single statement are optional.)

As an example, suppose that str is a variable of type String whose valuemight or might not represent a legal real number. Then we could say:

try {

double x;

x = Double.parseDouble(str);

System.out.println( "The number is " + x );

}

catch ( NumberFormatException e ) {

System.out.println( "Not a legal number." );

}

If an error is thrown by the call to Double.parseDouble(str), then the out-put statement in the try part is skipped, and the statement in the catch part isexecuted.

It’s not always a good idea to catch exceptions and continue with the program.Often that can just lead to an even bigger mess later on, and it might be betterjust to let the exception crash the program at the point where it occurs. However,sometimes it’s possible to recover from an error. For example, suppose that wehave the enumerated type

28

Page 36: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Javadoc

enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,

SATURDAY, SUNDAY}

and we want the user to input a value belonging to this type. TextIO does notknow about this type, so we can only read the user’s response as a string. Themethod Day.valueOf can be used to convert the user’s response to a value of typeDay. This will throw an exception of type IllegalArgumentException if the user’sresponse is not the name of one of the values of type Day, but we can respondto the error easily enough by asking the user to enter another response. Here isa code segment that does this. (Converting the user’s response to upper case willallow responses such as “Monday” or “monday” in addition to “MONDAY”.)

Scanner keyboard = new Scanner(System.in);

Day weekday; // User's response as a value of type Day.

while ( true ) {

String response; // User's response as a String.

keyboard.put("Please enter a day of the week: ");

response = keyboard.nextLinen();

response = response.toUpperCase();

try {

weekday = Day.valueOf(response);

break;

}

catch ( IllegalArgumentException e ) {

System.out.println( response +

" is not the name of a day of the week." );

}

}

The break statement will be reached only if the user’s response is acceptable,and so the loop will end only when a legal value has been assigned to weekday.

1.4 Javadoc

Good programming means extensive comments and documentation. At the veryleast, explain the method of each instance variable, and for each method explainits purpose, parameters, returns, where applicable. You should also strive for aconsistent layout and for expressive variable names.

A program that is well-documented is much more valuable than the sameprogram without the documentation. Java comes with a tool called javadoc thatcan make it easier to produce the documentation is a readable and organizedformat. JavaDoc is a program that will automatically extract/generate an HTMLhelp-page from code that is properly commented. In particular, it is designedproduce a help file that, for a class, lists the methods, constructors and publicfields, and for each method explains what it does together with pre-conditions,

29

Page 37: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

post-conditions, the meaning of the parameters, exceptions that may be thrownand other things.

Javadoc is especially useful for documenting classes and packages of classesthat are meant to be used by other programmers. A programmer who wants touse pre-written classes shouldn’t need to search through the source code to findout how to use them. If the documentation in the source code is in the correctformat, javadoc can separate out the documentation and make it into a set ofweb pages. The web pages are automatically formatted and linked into an easilybrowseable Web site. Sun Microsystem’s documentation for the standard JavaAPI was produced using javadoc.

Javadoc documentation is prepared from special comments that are placed inthe Java source code file. Recall that one type of Java comment begins with /*and ends with */. A Javadoc comment takes the same form, but it begins with/** rather than simply /*.

/**

* This method prints a 3N+1 sequence to standard output, using

* startingValue as the initial value of N. It also prints the

number

* of terms in the sequence. The value of the parameter,

startingValue,

* must be a positive integer.

*/

static void print3NSequence(int startingValue) { ...

You can have Javadoc comments for methods, member variables, and forclasses. The Javadoc comment always immediately precedes the thing it is com-menting on. Like any comment, a Javadoc comment is ignored by the computerwhen the file is compiled. But there is a tool called javadoc that reads Javasource code files, extracts any Javadoc comments that it finds, and creates a setof Web pages containing the comments in a nicely formatted, interlinked form.By default, javadoc will only collect information about public classes, methods,and member variables, but it allows the option of creating documentation fornon-public things as well. If javadoc doesn’t find any Javadoc comment forsomething, it will construct one, but the comment will contain only basic infor-mation such as the name and type of a member variable or the name, return type,and parameter list of a method. This is syntactic information. To add informa-tion about semantics and pragmatics, you have to write a Javadoc comment.

In addition to normal text, the comment can contain certain special codes.For one thing, the comment can contain HTML mark-up commands. (HTMLis the language that is used to create web pages, and Javadoc comments are meantto be shown on web pages.) The javadoc tool will copy any HTML commandsin the comments to the web pages that it creates. As an example, you can add <p>

30

Page 38: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Javadoc

to indicate the start of a new paragraph. (Generally, in the absence of HTMLcommands, blank lines and extra spaces in the comment are ignored.)

In addition to HTML commands, Javadoc comments can include doc tags,which are processed as commands by the javadoc tool. A doc tag has a namethat begins with the character . I will only discuss three tags: @param, @return,and @throws. These tags are used in Javadoc comments for methods to provideinformation about its parameters, its return value, and the exceptions that itmight throw. These tags are always placed at the end of the comment, after anydescription of the method itself. The syntax for using them is:

@param parameter-name description-of-parameter

@return description-of-return-value

@throws exception-class-name description-of-exception

The descriptions can extend over several lines. The description ends at thenext tag or at the end of the comment. You can include a @param tag for everyparameter of the method and a @throws for as many types of exception as youwant to document. You should have a @return tag only for a non-void method.These tags do not have to be given in any particular order. Here is an examplethat doesn’t do anything exciting but that does use all three types of doc tag:

If you want to create Web-page documentation, you need to run the javadoctool. You can use javadoc in a command line interface similarly to the waythat the javac and java commands are used. Javadoc can also be applied in theEclipse integrated development environment: Just right-click the class or pack-age that you want to document in the Package Explorer, select ”Export,” andselect ”Javadoc” in the window that pops up. Consult the documentation formore details.

31

Page 39: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

/**

* This method computes the area of a rectangle, given its width

* and its height. The length and the width should be positive

numbers.

* @param width the length of one side of the rectangle

* @param height the length the second side of the rectangle

* @return the area of the rectangle

* @throws IllegalArgumentException if either the width or the

height

* is a negative number.

*/

public static double areaOfRectangle( double length, double

width ) {

if ( width < 0 || height < 0 )

throw new IllegalArgumentException("Sides must have

positive length.");

double area;

area = width * height;

return area;

}

1.5 Creating Jar Files

As the final topic for this chapter, we look again at jar files. Recall that a jar file is a“java archive” that can contain a number of class files. When creating a programthat uses more than one class, it’s usually a good idea to place all the classes thatare required by the program into a jar file, since then a user will only need thatone file to run the program. Jar files can also be used for stand-alone applications.In fact, it is possible to make a so-called executable jar file. A user can runan executable jar file in much the same way as any other application, usually bydouble-clicking the icon of the jar file. (The user’s computer must have a correctversion of Java installed, and the computer must be configured correctly for thisto work. The configuration is usually done automatically when Java is installed,at least on Windows and Mac OS.)

The question, then, is how to create a jar file. The answer depends on whatprogramming environment you are using. There are two basic types of program-ming environment – command line and IDE. Any IDE (Integrated Program-ming Environment) for Java should have a command for creating jar files. Inthe Eclipse IDE, for example, it’s done as follows: In the Package Explorer pane,select the programming project (or just all the individual source code files thatyou need). Right-click on the selection, and choose “Export” from the menuthat pops up. In the window that appears, select “JAR file” and click “Next”. Inthe window that appears next, enter a name for the jar file in the box labeled

32

Page 40: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Creating Jar Files

“JAR file”. (Click the “Browse” button next to this box to select the file nameusing a file dialog box.) The name of the file should end with “.jar”. If you arecreating a regular jar file, not an executable one, you can hit “Finish” at this point,and the jar file will be created. You could do this, for example, if the jar file con-tains an applet but no main program. To create an executable file, hit the “Next”button twice to get to the “Jar Manifest Specification” screen. At the bottom ofthis screen is an input box labeled “Main class”. You have to enter the name ofthe class that contains the main() method that will be run when the jar file isexecuted. If you hit the “Browse” button next to the “Main class” box, you canselect the class from a list of classes that contain main() methods. Once you’veselected the main class, you can click the “Finish” button to create the executablejar file.

It is also possible to create jar files on the command line. The Java Develop-ment Kit includes a command-line program named jar that can be used to createjar files. If all your classes are in the default package (like the examples in thisbook), then the jar command is easy to use. To create a non-executable jar fileon the command line, change to the directory that contains the class files that youwant to include in the jar. Then give the command jar cf JarFileName.jar

*.class where JarFileName can be any name that you want to use for the jar file.The “*“ in “*.class” is a wildcard that makes *.class match every class file inthe current directory. This means that all the class files in the directory will beincluded in the jar file. If you want to include only certain class files, you canname them individually, separated by spaces. (Things get more complicated ifyour classes are not in the default package. In that case, the class files must be insubdirectories of the directory in which you issue the jar file.)

Making an executable jar file on the command line is a little more compli-cated. There has to be some way of specifying which class contains the main()

method. This is done by creating a manifest file. The manifest file can be aplain text file containing a single line of the form Main-Class: ClassName whereClassName should be replaced by the name of the class that contains the main()

method. For example, if the main()method is in the class MosaicDrawFrame, thenthe manifest file should read “Main-Class: MosaicDrawFrame”. You can give themanifest file any name you like. Put it in the same directory where you will issuethe jar command, and use a command of the form jar cmf ManifestFileName

JarFileName.jar *.class to create the jar file. (The jar command is capable ofperforming a variety of different operations. The first parameter to the command,such as “cf” or “cmf”, tells it which operation to perform.)

By the way, if you have successfully created an executable jar file, you can runit on the command line using the command “java -jar”. For example:java -jar JarFileName.jar

33

Page 41: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

APIs and Packages

One of the important advantages of object-oriented programming is that it pro-motes reuse. When writing any piece of software, a programmer can use a largeand growing body of pre-written software. The Java SDK (software develop-ment kit) consists of thousands of classes that can be used by programmers. So,learning the Java language means also being able to use this vast library of classes.

Toolboxes

Someone who wants to program for Macintosh computers – and to produceprograms that look and behave the way users expect them to – must deal with theMacintosh Toolbox, a collection of well over a thousand different methods. Thereare methods for opening and closing windows, for drawing geometric figuresand text to windows, for adding buttons to windows, and for responding tomouse clicks on the window. There are other methods for creating menus andfor reacting to user selections from menus. Aside from the user interface, there aremethods for opening files and reading data from them, for communicating overa network, for sending output to a printer, for handling communication betweenprograms, and in general for doing all the standard things that a computer hasto do. Microsoft Windows provides its own set of methods for programmers touse, and they are quite a bit different from the methods used on the Mac. Linuxhas several different GUI toolboxes for the programmer to choose from.

The analogy of a “toolbox” is a good one to keep in mind. Every program-ming project involves a mixture of innovation and reuse of existing tools. A pro-grammer is given a set of tools to work with, starting with the set of basic toolsthat are built into the language: things like variables, assignment statements, ifstatements, and loops. To these, the programmer can add existing toolboxes fullof methods that have already been written for performing certain tasks. Thesetools, if they are well-designed, can be used as true black boxes: They can becalled to perform their assigned tasks without worrying about the particular stepsthey go through to accomplish those tasks. The innovative part of programmingis to take all these tools and apply them to some particular project or problem(word-processing, keeping track of bank accounts, processing image data froma space probe, Web browsing, computer games,...). This is called applicationsprogramming.

A software toolbox is a kind of black box, and it presents a certain interfaceto the programmer. This interface is a specification of what methods are in thetoolbox, what parameters they use, and what tasks they perform. This informa-tion constitutes the API, or Applications Programming Interface, associated withthe toolbox. The Macintosh API is a specification of all the methods availablein the Macintosh Toolbox. A company that makes some hardware device – say

34

Page 42: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Creating Jar Files

a card for connecting a computer to a network – might publish an API for thatdevice consisting of a list of methods that programmers can call in order to com-municate with and control the device. Scientists who write a set of methods fordoing some kind of complex computation – such as solving “differential equa-tions”, say – would provide an API to allow others to use those methods withoutunderstanding the details of the computations they perform.

The Java programming language is supplemented by a large, standard API.You’ve seen part of this API already, in the form of mathematical methods such asMath.sqrt(), the String data type and its associated methods, and theSystem.out.print() methods. The standard Java API includes methods forworking with graphical user interfaces, for network communication, for read-ing and writing files, and more. It’s tempting to think of these methods as beingbuilt into the Java language, but they are technically methods that have beenwritten and made available for use in Java programs.

Java is platform-independent. That is, the same program can run on plat-forms as diverse as Macintosh, Windows, Linux, and others. The same JavaAPI must work on all these platforms. But notice that it is the interface that isplatform-independent; the implementation varies from one platform to another.A Java system on a particular computer includes implementations of all the stan-dard API methods. A Java program includes only calls to those methods. Whenthe Java interpreter executes a program and encounters a call to one of the stan-dard methods, it will pull up and execute the implementation of that methodwhich is appropriate for the particular platform on which it is running. This is avery powerful idea. It means that you only need to learn one API to program fora wide variety of platforms.

Javaʼs Standard Packages

Like all methods in Java, the methods in the standard API are grouped intoclasses. To provide larger-scale organization, classes in Java can be grouped intopackages, which were introduced briefly in Subection2.6.4. You can have evenhigher levels of grouping, since packages can also contain other packages. In fact,the entire standard Java API is implemented in several packages. One of these,which is named “java”, contains several non-GUI packages as well as the originalAWT graphics user interface classes. Another package, “javax”, was added in Javaversion 1.2 and contains the classes used by the Swing graphical user interfaceand other additions to the API.

A package can contain both classes and other packages. A package that iscontained in another package is sometimes called a “sub-package.” Both the javapackage and the javax package contain sub-packages. One of the sub-packages ofjava, for example, is called “awt”. Since awt is contained within java, its full nameis actually java.awt. This package contains classes that represent GUI components

35

Page 43: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

such as buttons and menus in the AWT, the older of the two Java GUI toolboxes,which is no longer widely used. However, java.awt also contains a number ofclasses that form the foundation for all GUI programming, such as the Graphicsclass which provides methods for drawing on the screen, the Color class whichrepresents colors, and the Font class which represents the fonts that are used todisplay characters on the screen. Since these classes are contained in the pack-age java.awt, their full names are actually java.awt.Graphics, java.awt.Colorand java.awt.Font. (I hope that by now you’ve gotten the hang of how thisnaming thing works in Java.) Similarly, javax contains a sub-package namedjavax.swing, which includes such classes as javax.swing.JButton, javax.swing.JMenu,and javax.swing.JFrame. The GUI classes in javax.swing, together with thefoundational classes in java.awt are all part of the API that makes it possible toprogram graphical user interfaces in Java.

The java package includes several other sub-packages, such as java.io, whichprovides facilities for input/output, java.net, which deals with network com-munication, and java.util, which provides a variety of “utility” classes. The mostbasic package is called java.lang. This package contains fundamental classessuch as String, Math, Integer, and Double.

It might be helpful to look at a graphical representation of the levels of nestingin the java package, its sub-packages, the classes in those sub-packages, and themethods in those classes. This is not a complete picture, since it shows only avery few of the many items in each element:

The official documentation for the standard Java 5.0 API lists 165 differ-ent packages, including sub-packages, and it lists 3278 classes in these packages.Many of these are rather obscure or very specialized, but you might want tobrowse through the documentation to see what is available.

36

Page 44: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Creating Jar Files

Even an expert programmer won’t be familiar with the entire API, or evena majority of it. In this book, you’ll only encounter several dozen classes, andthose will be sufficient for writing a wide variety of programs.

Using Classes from Packages

Let’s say that you want to use the class java.awt.Color in a program that youare writing. Like any class, java.awt.Color is a type, which means that you canuse it declare variables and parameters and to specify the return type of a method.One way to do this is to use the full name of the class as the name of the type. Forexample, suppose that you want to declare a variable named rectColor of typejava.awt.Color. You could say:

java.awt.Color rectColor;

This is just an ordinary variable declaration of the form “type-name variable-name;”. Of course, using the full name of every class can get tiresome, so Javamakes it possible to avoid using the full names of a class by importing the class.If you put

import java.awt.Color;

at the beginning of a Java source code file, then, in the rest of the file, youcan abbreviate the full name java.awt.Color to just the simple name of the class,Color. Note that the import line comes at the start of a file and is not inside anyclass. Although it is sometimes referred to as as a statement, it is more properlycalled an import directive since it is not a statement in the usual sense. Usingthis import directive would allow you to say

Color rectColor;

to declare the variable. Note that the only effect of the import directive is to allowyou to use simple class names instead of full “package.class” names; you aren’treally importing anything substantial. If you leave out the import directive, youcan still access the class – you just have to use its full name. There is a shortcutfor importing all the classes from a given package. You can import all the classesfrom java.awt by saying

import java.awt.*;

The “*” is a wildcard that matches every class in the package. (However, it doesnot match sub-packages; you cannot import the entire contents of all the sub-packages of the java packages by saying importjava.*.)

Some programmers think that using a wildcard in an import statement is badstyle, since it can make a large number of class names available that you are notgoing to use and might not even know about. They think it is better to explicitlyimport each individual class that you want to use. In my own programming,

37

Page 45: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

I often use wildcards to import all the classes from the most relevant packages,and use individual imports when I am using just one or two classes from a givenpackage.

In fact, any Java program that uses a graphical user interface is likely to usemany classes from the java.awt and java.swing packages as well as from anotherpackage named java.awt.event, and I usually begin such programs with

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

A program that works with networking might include: “import java.net.*;”,while one that reads or writes files might use “import java.io.*;”. (But whenyou start importing lots of packages in this way, you have to be careful aboutone thing: It’s possible for two classes that are in different packages to have thesame name. For example, both the java.awt package and the java.util packagecontain classes named List. If you import both java.awt.* and java.util.*,the simple name List will be ambiguous. If you try to declare a variable oftype List, you will get a compiler error message about an ambiguous class name.The solution is simple: use the full name of the class, either java.awt.List orjava.util.List. Another solution, of course, is to use import to import theindividual classes you need, instead of importing entire packages.)

Because the package java.lang is so fundamental, all the classes in java.langare automatically imported into every program. It’s as if every program beganwith the statement “import java.lang.*;”. This is why we have been able to usethe class name String instead of java.lang.String, and Math.sqrt() instead ofjava.lang.Math.sqrt(). It would still, however, be perfectly legal to use thelonger forms of the names.

Programmers can create new packages. Suppose that you want some classesthat you are writing to be in a package named utilities. Then the source code filethat defines those classes must begin with the line

package utilities;

This would come even before any import directive in that file. Furthermore,the source code file would be placed in a folder with the same name as the package.A class that is in a package automatically has access to other classes in the samepackage; that is, a class doesn’t have to import the package in which it is defined.

In projects that define large numbers of classes, it makes sense to organizethose classes into packages. It also makes sense for programmers to create newpackages as toolboxes that provide functionality and API’s for dealing with areasnot covered in the standard Java API. (And in fact such “toolmaking” program-mers often have more prestige than the applications programmers who use theirtools.)

38

Page 46: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mixing Static and Non-static

However, I will not be creating any packages in this textbook. For the pur-poses of this book, you need to know about packages mainly so that you willbe able to import the standard packages. These packages are always available tothe programs that you write. You might wonder where the standard classes areactually located. Again, that can depend to some extent on the version of Javathat you are using, but in the standard Java 5.0, they are stored in jar files ina subdirectory of the main Java installation directory. A jar (or “Java archive”)file is a single file that can contain many classes. Most of the standard classescan be found in a jar file named classes.jar. In fact, Java programs are generallydistributed in the form of jar files, instead of as individual class files.

Although we won’t be creating packages explicitly, every class is actually partof a package. If a class is not specifically placed in a package, then it is put insomething called the default package, which has no name. All the examples thatyou see in this book are in the default package.

1.6 Mixing Static and Non-static

Classes, as I’ve said, have two very distinct purposes. A class can be used to grouptogether a set of static member variables and static member methods. Or it canbe used as a factory for making objects. The non-static variables and methods inthe class definition specify the instance variables and methods of the objects. Inmost cases, a class performs one or the other of these roles, not both.

Sometimes, however, static and non-static members are mixed in a single class.In this case, the class plays a dual role. Sometimes, these roles are completelyseparate. It is also possible for the static and non-static parts of a class to interact.This happens when instance methods use static member variables or call staticmember methods. An instance method belongs to an object, not to the class itself,and there can be many objects with their own versions of the instance method.But there is only one copy of a static member variable. So, effectively, we havemany objects sharing that one variable.

Suppose, for example, that we want to write a PairOfDice class that uses theRandom class for rolling the dice. To do this, a PairOfDice object needs accessto an object of type Random. But there is no need for each PairOfDice object tohave a separate Randomobject. (In fact, it would not even be a good idea: Becauseof the way ran dom number generators work, a program should, in general, useonly one source of random numbers.) A nice solution is to have a single Random

variable as a static member of the PairOfDice class, so that it can be shared byall PairOfDice objects. For example:

import java.util.Random;

public class PairOfDice {

39

Page 47: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

\code{private static Random randGen = new Random();}

public int die1; // Number showing on the first die.

public int die2; // Number showing on the second die.

public PairOfDice() {

// Constructor. Creates a pair of dice that

// initially shows random values.

roll();

}

public void roll() {

// Roll the dice by setting each of the dice to be

// a random number between 1 and 6.

die1 = randGen.nextInt(6) + 1;

die2 = randGen.nextInt(6) + 1;

}

} // end class PairOfDice

As another example, let’s rewrite the Student class. I’ve added an ID for eachstudent and a static member called nextUniqueID . Although there is an ID

variable in each student object, there is only one nextUniqueID variable.public class Student {

private String name; // Student's name.

private int ID; // Unique ID number for this student.

public double test1, test2, test3; // Grades on three tests.

private static int nextUniqueID = 0;

// keep track of next available unique ID number

Student(String theName) {

// Constructor for Student objects; provides a name for

the Student,

// and assigns the student a unique ID number.

name = theName;

nextUniqueID++;

ID = nextUniqueID;

}

public String getName() {

// Accessor method for reading value of private

// instance variable, name.

return name;

}

public int getID() {

// Accessor method for reading value of ID.

40

Page 48: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mixing Static and Non-static

return ID;

}

public double getAverage() {

// Compute average test grade.

return (test1 + test2 + test3) / 3;

}

} // end of class Student

The initialization “nextUniqueID = 0” is done once, when the class is firstloaded. Whenever a Student object is constructed and the constructor says“nextUniqueID++;”, it’s always the same static member variable that is beingincremented. When the very first Student object is created, nextUniqueID be-comes 1. When the second object is created, nextUniqueID becomes 2. After thethird object, it becomes 3. And so on. The constructor stores the new value ofnextUniqueID in the ID variable of the object that is being created. Of course,ID is an instance variable, so every object has its own individual ID variable. Theclass is constructed so that each student will automatically get a different valuefor its IDvariable. Furthermore, the ID variable is private, so there is no wayfor this variable to be tampered with after the object has been created. You areguaranteed, just by the way the class is designed, that every student object willhave its own permanent, unique identification number. Which is kind of cool ifyou think about it.

Static Import

The import directive makes it possible to refer to a class such as java.awt.Colorusing its simple name, Color. All you have to do is say import java.awt.Color

or import java.awt.*. Uou still have to use compound names to refer to staticmember variables such as System.out and to static methods such as Math.sqrt.

Java 5.0 introduced a new form of the import directive that can be used toimport static members of a class in the same way that the ordinary import

directive imports classes from a package. The new form of the directive is calleda static import, and it has syntax

import static package-name class-name static-member-name;

to import one static member name from a class, orimport static package-name class-name.*;

to import all the public static members from a class. For example, if you prefacea class definition with

import static java.lang.System.out;

then you can use the simple name out instead of the compound name System.out.This means you can use out.println instead of System.out.println. If you are

41

Page 49: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Introduction to Objects

going to work extensively with the Mathclass, you can preface your class definitionwith

import static java.lang.Math.*;

This would allow to say sqrtinstead of Math.sqrt, log instead of Math.log,PI instead of Math.PI, and so on.

Note that the static import directive requires a package-name, even for classesin the standard package java.lang. One consequence of this is that you can’t doa static import from a class in the default package.

42

Page 50: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Chapter

2Object OrientedAnalysis and Design

2.1 Software Engineering

The difficulties inherent with the development of software has led manycomputer scientists to suggest that software development should be treated

as an engineering activity. They argue for a disciplined approach where the soft-ware engineer uses carefully thought out methods and processes.

Large, complex programming projects are only likely to succeed if a careful,systematic approach is adopted during all stages of the software life cycle. Thesystematic approach to programming, using accepted principles of good design,is called software engineering. The software engineer tries to efficiently constructprograms that verifyably meet their specifications and that are easy to modifyif necessary. There is a wide range of “methodologies” that can be applied tohelp in the systematic design of programs. (Most of these methodologies seemto involve drawing little boxes to represent program components, with labeledarrows to represent relationships among the boxes.)

The term software engineering has several meanings (from wikipedia):

† As the broad term for all aspects of the practice of computer programming,as opposed to the theory of computer programming, which is called com-puter science;

† As the term embodying the advocacy of a specific approach to computerprogramming, one that urges that it be treated as an engineering profes-sion rather than an art or a craft, and advocates the codification of recom-mended practices in the form of software engineering methodologies.

43

Page 51: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

† Software engineering is

(1) “the application of a systematic, disciplined, quantifiable approach tothe development, operation, and maintenance of software, that is, theapplication of engineering to software,” and

(2) “the study of approaches as in (1).” IEEE Standard 610.12

Software Engineering Life-Cycles

A decades-long goal has been to find repeatable, predictable processes or method-ologies that improve productivity and quality of software. Some try to system-atize or formalize the seemingly unruly task of writing software. Others applyproject management techniques to writing software. Without project manage-ment, software projects can easily be delivered late or over budget. With largenumbers of software projects not meeting their expectations in terms of function-ality, cost, or delivery schedule, effective project management is proving difficult.

A large programming project goes through a number of stages, starting withspecification of the problem to be solved, followed by analysis of the problemand design of a program to solve it. Then comes coding, in which the program’sdesign is expressed in some actual programming language. This is followed bytesting and debugging of the program. After that comes a long period of mainte-nance, which means fixing any new problems that are found in the program andmodifying it to adapt it to changing requirements. Together, these stages formwhat is called the software life cycle. (In the real world, the ideal of consecutivestages is seldom if ever achieved. During the analysis stage, it might turn outthat the specifications are incomplete or inconsistent. A problem found duringtesting requires at least a brief return to the coding stage. If the problem is seri-ous enough, it might even require a new design. Maintenance usually involvesredoing some of the work from previous stages.)

Software engineering requires performing many tasks, notably the following,some of which may not seem to directly produce software.

• Requirements Analysis Extracting the requirements of a desired softwareproduct is the first task in creating it. While customers probably believethey know what the software is to do, it may require skill and experience insoftware engineering to recognize incomplete, ambiguous or contradictoryrequirements.

• Specification Specification is the task of precisely describing the softwareto be written, usually in a mathematically rigorous way. In reality, mostsuccessful specifications are written to understand and fine-tune applica-tions that were already well-developed. Specifications are most importantfor external interfaces, that must remain stable.

44

Page 52: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Software Engineering

• Design and Architecture Design and architecture refer to determining howsoftware is to function in a general way without being involved in details.Usually this phase is divided into two sub-phases.

• Coding Reducing a design to code may be the most obvious part of thesoftware engineering job, but it is not necessarily the largest portion.

• Testing Testing of parts of software, especially where code by two differentengineers must work together, falls to the software engineer.

• Documentation An important (and often overlooked) task is document-ing the internal design of software for the purpose of future maintenanceand enhancement. Documentation is most important for external inter-faces.

• Maintenance Maintaining and enhancing software to cope with newly dis-covered problems or new requirements can take far more time than theinitial development of the software. Not only may it be necessary to addcode that does not fit the original design but just determining how softwareworks at some point after it is completed may require significant effort bya software engineer. About 2/3 of all software engineering work is main-tenance, but this statistic can be misleading. A small part of that is fixingbugs. Most maintenance is extending systems to do new things, which inmany ways can be considered new work. In comparison, about 2/3 of allcivil engineering, architecture, and construction work is maintenance in asimilar way.

Object Oriented Software Engineering

Object Oriented Software Engineering rests on three principles:

• Abstraction: Ignore the details. In philosophical terminology, abstractionis the thought process wherein ideas are distanced from objects. In com-puter science, abstraction is a mechanism and practice to reduce and factorout details so that one can focus on few concepts at a time.Abstraction uses a strategy of simplification, wherein formerly concrete de-tails are left ambiguous, vague, or undefined. [wikipedia:Abstraction]

• Modularization: Break into pieces. A module can be defined variously,but generally must be a component of a larger system, and operate withinthat system independently from the operations of the other components.Modularity is the property of computer programs that measures the extentto which they have been composed out of separate parts called modules.

45

Page 53: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

Programs that have many direct interrelationships between any two ran-dom parts of the program code are less modular than programs where thoserelationships occur mainly at well-defined interfaces between modules.

• Information hiding: separate the implementation and the function. Theprinciple of information hiding is the hiding of design decisions in a com-puter program that are most likely to change, thus protecting other partsof the program from change if the design decision is changed. Protectinga design decision involves providing a stable interface which shields theremainder of the program from the implementation (the details that aremost likely to change).

Abstraction and Information Hiding

Methods as Abstraction Mechanisms

A good example of abstraction and information hiding in practice is the method.A method is an abstraction mechanism and consists of instructions for per-

forming some task, chunked together and given a name. “Chunking” allowsyou to deal with a potentially very complicated task as a single concept. In-stead of worrying about the many, many steps that the computer might have togo through to perform that task, you just need to remember the name of themethod. Whenever you want your program to perform the task, you just callthe method. Methods are a major tool for dealing with complexity.

A method is a “black box” because you do not have to see what’s “inside” itbefore you use it–the complexity of how it works is hidden. A black box needssome kind of interface with the rest of the world, which allows some interactionbetween what’s inside the box and what’s outside. A physical black box mighthave buttons on the outside that you can push, dials that you can set, and slotsthat can be used for passing information back and forth. Since we are trying tohide complexity, not create it, we have the first rule of black boxes:

The interface of a black box should be fairly straightforward, well-defined,and easy to understand.

Your television, your car, your VCR, your refrigerator... are all examples ofblack boxes in the real world. You can turn your television on and off, changechannels, and set the volume by using elements of the television’s interface –dials and remote control – without understanding anything about how the thingactually works.

A black box does have an inside – the code in a method that actually performsthe task, all the electronics inside your television set. The inside of a black box iscalled its implementation. The second rule of black boxes is that:

46

Page 54: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Software Engineering

To use a black box, you shouldn’t need to know anything about its im-plementation; all you need to know is its interface.

In fact, it should be possible to change the implementation, as long as the be-havior of the box, as seen from the outside, remains unchanged. For example,when the insides of TV sets went from using vacuum tubes to using transistors,the users of the sets didn’t even need to know about it – or even know what itmeans. Similarly, it should be possible to rewrite the inside of a method, to usemore efficient code, for example, without affecting the programs that use thatmethod.

The implementor of the black box doesn’t need to know about the many waysin which the box will be used–she just needs to make sure that the box performsits assigned task and interfaces correctly with the rest of the world. This is thethird rule of black boxes:

The implementor of a black box should not need to know anything aboutthe larger systems in which the box will be used. In a way, a black boxdivides the world into two parts: the inside (implementation) and theoutside. The interface is at the boundary, connecting those two parts.

You should not think of an interface as just the physical connection between thebox and the rest of the world. The interface also includes a specification of whatthe box does and how it can be controlled by using the elements of the physicalinterface. It’s not enough to say that a TV set has a power switch; you need tospecify that the power switch is used to turn the TV on and off!

The interface of a method has a semantic as well as a syntactic component.The syntactic part of the interface tells you just what you have to type in orderto call the method. The semantic component specifies the task the method willaccomplish. To write a legal program, you need to know the syntactic specifica-tion of the method. You need to know the method’s semantic specification tounderstand the purpose of the method and to use it effectively. We refer to bothparts of the interface – syntactic and semantic – collectively as the contract ofthe method.

The contract of a method says, essentially, “Here is what you have to do to useme, and here is what I will do for you, guaranteed.” When you write a method,the comments that you write for the method should make the contract very clear.

Preconditions and Postconditions

When working with methods as building blocks, it is important to be clear abouthow a method interacts with the rest of the program. A convenient way to expressthe contract of a method is in terms of preconditions and postconditions.

47

Page 55: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

The precondition of a method is something that must be true when themethod is called, if the method is to work correctly.

For example, for the built-in method Math.sqrt(x), a precondition is that theparameter, x, is greater than or equal to zero, since it is not possible to take thesquare root of a negative number. In terms of a contract, a precondition repre-sents an obligation of the caller of the method. If you call a method withoutmeeting its precondition, then there is no reason to expect it to work properly.The program might crash or give incorrect results, but you can only blame your-self, not the method.

A postcondition of a method represents the other side of the contract. Itis something that will be true after the method has run. The postcondition ofthe method Math.sqrt() is that the square of the value that is returned by thismethod is equal to the parameter that is provided when the method is called.Of course, this will only be true if the precondition – that the parameter isgreater than or equal to zero – is met. A postcondition of the built-in methodSystem.out.print() is that the value of the parameter has been displayed on thescreen.

Preconditions most often give restrictions on the acceptable values of param-eters, as in the example of Math.sqrt(x). However, they can also refer to globalvariables that are used in the method. The postcondition of a method specifiesthe task that it performs. For a method, the postcondition should specify thevalue that the method returns. When you are given a pre-written method, astatement of its preconditions and postconditions tells you how to use it andwhat it does. When you write a method, the preconditions and postconditionsgive you an exact specification of what the method is expected to do. It is a goodidea to write preconditions and postconditions as comments.

Classes as Abstraction Mechanisms

Methods are not the only example of black boxes in programming. A class isalso a black box. It has a “public” part, representing its interface, and a “private”part that is entirely inside its hidden implementation. All the principles of blackboxes apply to classes as well as to methods.

The interface to a class consists of all the members of the class that are acces-sible. Usually, this will include all the public methods as we generally hide thefields. When a method is made accessible, one needs only to know its signature:i.e. the return type, name and arguments. As the user programmer, then, wemust know WHAT the method does, and not HOW it works.

48

Page 56: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. A Simple Methodology

Figure .: A Simple Methodology

2.2 A Simple Methodology

The following is a simple methodology that we will use:

Discovering Classes: Class-Responsibility-Collaboration cards

The concepts of object orientation does not only apply to the coding stage of pro-gram development. But there are also object-oriented methodologies for analysisand design. The question in this stage of the software life cycle is, “How can

49

Page 57: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

one discover or invent the overall structure of a program?” As an example of arather simple object-oriented approach to analysis and design, consider this ad-vice: Write down a description of the problem. Underline all the nouns in thatdescription. The nouns should be considered as candidates for becoming classesor objects in the program design. Similarly, underline all the verbs. These arecandidates for methods. This is your starting point. Further analysis might un-cover the need for more classes and methods, and it might reveal that subclassingcan be used to take advantage of similarities among classes.

This is perhaps a bit simple-minded, but the idea is clear and the generalapproach can be effective: Analyze the problem to discover the concepts that areinvolved, and create classes to represent those concepts. The design should arisefrom the problem itself, and you should end up with a program whose structurereflects the structure of the problem in a natural way.

We strive for responsibility-driven design: each class should be responsiblefor its own data. We strive for loose coupling: each class is largely independentand communicates with other classes via a small well-defined interface. We strivefor cohesion: each class performs one and only one task (for readability, reuse).

Class-Responsibility-Collaboration cards (CRC cards) are a brainstormingtool used in the design of object-oriented software. They were proposed by WardCunningham. They are typically used when first determining which classes areneeded and how they will interact.

CRC cards are usually created from index cards on which are written:

1. The class name.

2. The package name (if applicable).

3. The responsibilities of the class.

4. The names of other classes that the class will collaborate with to fulfill itsresponsibilities.

For example consider the CRC Card for a Playing Card class:

The responsibilities are listed on the left. The classes that the Playing Card

class will collaborate with are listed on the right.

50

Page 58: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Documenting Designs: The Unified Modelling Language

The idea is that class design is undertaken by a team of developers. CRCcards are used as a brainstorming technique. The team attempts to determineall the classes and their responsibilities that will be needed for the application.The team runs through various usage scenarios of the application. For e.g. onesuch scenario for a game of cards may be “the player picks a card from the deckand adds it to his hand”. The team uses the CRC cards to check if this scenariocan be handled by the responsibilites assigned to the classes. In this way, thedesign is refined until the team agrees on a set of classes and has agreed on theirresponsibilities.

Using a small card keeps the complexity of the design at a minimum. Itfocuses the designer on the essentials of the class and prevents him from gettinginto its details and inner workings at a time when such detail is probably counter-productive. It also forces the designer to refrain from giving the class too manyresponsibilities.

2.3 Documenting Designs: The Unified Modelling Language

This section will give you a quick overview of the basics of UML. Keepin mind that this is not a comprehensive tutorial on UML but rather a brief

introduction to UML which can be read as a UML tutorial. If you would like tolearn more about the Unified Modelling Language, or in general about softwareanalysis and design, refer to one of the many books available on the topic. Thereare also a lot of tutorials on the Internet which you can take as a starting point.1

The Unified Modelling Language (UML) is a diagramming language or nota-tion to specify, visualize and document models of Object Oriented software sys-tems. UML is not a development method, that means it does not tell you whatto do first and what to do next or how to design your system, but it helps you tovisualize your design and communicate with others. UML is controlled by theObject Management Group (OMG) and is the industry standard for graphicallydescribing software.

UML is designed for Object Oriented software design and has limited usefor other programming paradigms.

UML is not a method by itself, however it was designed to be compatiblewith the leading object-oriented software development methods of its time (e.g.,OMT, Booch, Objectory). Since UML has evolved, some of these methods havebeen recast to take advantage of the new notation (e.g., OMT) and new meth-ods have been created based on UML. Most well known is the Rational UnifiedProcess (RUP) created by the Rational Software Corporation.

1This discussion is taken from the user documentation of the UML tool Umbrello andwikipedia.

51

Page 59: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

Modelling There are three prominent parts of a system’s model:

• Functional Model

Showcases the functionality of the system from the user’s Point of View.Includes Use Case Diagrams.

• Object Model

Showcases the structure and substructure of the system using objects, at-tributes, operations, and associations. Includes Class Diagrams.

• Dynamic Model

Showcases the internal behavior of the system. Includes Sequence Dia-grams, Activity Diagrams and State Machine Diagrams.

UML is composed of many model elements that represent the different partsof a software system. The UML elements are used to create diagrams, whichrepresent a certain part, or a point of view of the system. In UML 2.0 there are13 types of diagrams. Some of the more important diagrams are:

• Use Case Diagrams show actors (people or other users of the system), usecases (the scenarios when they use the system), and their relationships

• Class Diagrams show classes and the relationships between them

• Sequence Diagrams show objects and a sequence of method calls they maketo other objects.

• Collaboration Diagrams show objects and their relationship, putting em-phasis on the objects that participate in the message exchange

• State Diagrams show states, state changes and events in an object or a partof the system

• Activity Diagrams show activities and the changes from one activity to an-other with the events occurring in some part of the system

• Component Diagrams show the high level programming components (suchas KParts or Java Beans).

• Deployment Diagrams show the instances of the components and their re-lationships.

52

Page 60: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Documenting Designs: The Unified Modelling Language

Figure .: Umbrello UML Modeller showing a Use Case Diagram

Use Case Diagrams Use Case Diagrams describe the relationships and depen-dencies between a group of Use Cases and the Actors participating in the process.

It is important to notice that Use Case Diagrams are not suited to representthe design, and cannot describe the internals of a system. Use Case Diagrams aremeant to facilitate the communication with the future users of the system, andwith the customer, and are specially helpful to determine the required featuresthe system is to have. Use Case Diagrams tell, what the system should do but donot–and cannot–specify how this is to be achieved.

AUse Case describes–from the point of view of the actors–a group of activitiesin a system that produces a concrete, tangible result.

Use Cases are descriptions of the typical interactions between the users of asystem and the system itself. They represent the external interface of the systemand specify a form of requirements of what the system has to do (remember, onlywhat, not how).

When working with Use Cases, it is important to remember some simplerules:

• Each Use Case is related to at least one actor

53

Page 61: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

• Each Use Case has an initiator (i.e. an actor)

• Each Use Case leads to a relevant result (a result with a business value)

An actor is an external entity (outside of the system) that interacts with thesystem by participating (and often initiating) a Use Case. Actors can be in reallife people (for example users of the system), other computer systems or externalevents.

Actors do not represent the physical people or systems, but their role . Thismeans that when a person interacts with the system in different ways (assumingdifferent roles) he will be represented by several actors. For example a person thatgives customer support by the telephone and takes orders from the customer intothe system would be represented by an actor “Support Staff” and an actor “SalesRepresentative”

U se Case Descriptions are textual narratives of the Use Case. They usuallytake the form of a note or a document that is somehow linked to the Use Case,and explains the processes or activities that take place in the Use Case.

Class Diagrams Class Diagrams show the different classes that make up a systemand how they relate to each other. Class Diagrams are said to be ”static” diagramsbecause they show the classes, along with their methods and attributes as well asthe static relationships between them: which classes ”know” about which classesor which classes ”are part” of another class, but do not show the method callsbetween them.

A Class defines the attributes and the methods of a set of objects. All objectsof this class (instances of this class) share the same behavior, and have the sameset of attributes (each object has its own set). The term ”Type” is sometimes usedinstead of Class, but it is important to mention that these two are not the same,and Type is a more general term.

In UML, Classes are represented by rectangles, with the name of the class,and can also show the attributes and operations of the class in two other ”com-partments” inside the rectangle.

In UML, Attributes are shown with at least their name, and can also showtheir type, initial value and other properties. Attributes can also be displayedwith their visibility:

• + Stands for public attributes

• # Stands for protected attributes

• - Stands for private attributes

54

Page 62: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Documenting Designs: The Unified Modelling Language

Figure .: Umbrello UML Modeller showing a Class Diagram

Figure .: Visual representation of a Class in UML

55

Page 63: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

Figure .: Visual representation of a generalization in UML

Operations (methods) are also displayed with at least their name, and canalso show their parameters and return types. Operations can, just as Attributes,display their visibility:

• + Stands for public operations

• # Stands for protected operations

• - Stands for private operations

Class Associations Classes can relate (be associated with) to each other in dif-ferent ways:

Inheritance is one of the fundamental concepts of Object Orientated pro-gramming, in which a class “gains” all of the attributes and operations of theclass it inherits from, and can override/modify some of them, as well as add moreattributes and operations of its own.

• Generalization In UML, a Generalization association between two classesputs them in a hierarchy representing the concept of inheritance of a de-rived class from a base class. In UML, Generalizations are represented by aline connecting the two classes, with an arrow on the side of the base class.

• Association An association represents a relationship between classes, andgives the common semantics and structure for many types of ”connections”between objects.Associations are the mechanism that allows objects to communicate to eachother. It describes the connection between different classes (the connectionbetween the actual objects is called object connection, or link .Associations can have a role that specifies the purpose of the associationand can be uni- or bidirectional (indicates if the two objects participatingin the relationship can send messages to the other, of if only one of them

56

Page 64: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Documenting Designs: The Unified Modelling Language

Figure .: Visual representation of an Association in UML Aggregation

Figure .: Visual representation of an Aggregation relationship in UML

knows about the other). Each end of the association also has a multiplicityvalue, which dictates how many objects on this side of the association canrelate to one object on the other side.In UML, associations are represented as lines connecting the classes partic-ipating in the relationship, and can also show the role and the multiplicityof each of the participants. Multiplicity is displayed as a range [min..max]of non-negative values, with a star (*) on the maximum side representinginfinite.

• Aggregations Aggregations are a special type of associations in which thetwo participating classes don’t have an equal status, but make a “whole-part” relationship. An Aggregation describes how the class that takes therole of the whole, is composed (has) of other classes, which take the roleof the parts. For Aggregations, the class acting as the whole always has amultiplicity of one.In UML, Aggregations are represented by an association that shows a rhom-bus on the side of the whole.

• Composition Compositions are associations that represent very strong ag-gregations. This means, Compositions form whole-part relationships aswell, but the relationship is so strong that the parts cannot exist on its own.They exist only inside the whole, and if the whole is destroyed the parts dietoo.In UML, Compositions are represented by a solid rhomb on the side ofthe whole.

Other Class Diagram Items Class diagrams can contain several other itemsbesides classes.

• Interfaces are abstract classes which means instances can not be directlycreated of them. They can contain operations but no attributes. Classes

57

Page 65: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

Figure .

can inherit from interfaces (through a realisation association) and instancescan then be made of these diagrams.

• Datatypes are primitives which are typically built into a programming lan-guage. Common examples include integers and booleans. They can nothave relationships to classes but classes can have relationships to them.

• Enums are a simple list of values. A typical example is an enum for days ofthe week. The options of an enum are called Enum Literals. Like datatypesthey can not have relationships to classes but classes can have relationshipsto them.

• Packages represent a namespace in a programming language. In a diagramthey are used to represent parts of a system which contain more than oneclass, maybe hundereds of classes.

Sequence Diagrams Sequence Diagrams show the message exchange (i.e. methodcall) between several Objects in a specific time-delimited situation. Objects areinstances of classes. Sequence Diagrams put special emphasis in the order andthe times in which the messages to the objects are sent.

In Sequence Diagrams objects are represented through vertical dashed lines,with the name of the Object on the top. The time axis is also vertical, increasingdownwards, so that messages are sent from one Object to another in the form ofarrows with the operation and parameters name.

Messages can be either synchronous, the normal type of message call wherecontrol is passed to the called object until that method has finished running, orasynchronous where control is passed back directly to the calling object. Syn-chronous messages have a vertical box on the side of the called object to showthe flow of program control.

Collaboration Diagrams Collaboration Diagrams show the interactions occur-ring between the objects participating in a specific situation. This is more or lessthe same information shown by Sequence Diagrams but there the emphasis isput on how the interactions occur in time while the Collaboration Diagrams putthe relationships between the objects and their topology in the foreground.

In Collaboration Diagrams messages sent from one object to another are rep-resented by arrows, showing the message name, parameters, and the sequence of

58

Page 66: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Documenting Designs: The Unified Modelling Language

Figure .: Umbrello UML Modeller showing a Sequence Diagram

the message. Collaboration Diagrams are specially well suited to showing a spe-cific program flow or situation and are one of the best diagram types to quicklydemonstrate or explain one process in the program logic.

State Diagram State Diagrams show the different states of an Object during itslife and the stimuli that cause the Object to change its state.

State Diagrams view Objects as state machines or finite automata that can bein one of a set of finite states and that can change its state via one of a finite setof stimuli. For example an Object of type NetServer can be in one of followingstates during its life:

• Ready

• Listening

• Working

• Stopped

and the events that can cause the Object to change states are

• Object is created

• Object receives message listen

59

Page 67: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

Figure .: Umbrello UML Modeller showing a Collaboration Diagram

• A Client requests a connection over the network

• A Client terminates a request

• The request is executed and terminated

• Object receives message stop ... etc

Activity Diagram Activity Diagrams describe the sequence of activities in a sys-tem with the help of Activities. Activity Diagrams are a special form of StateDiagrams, that only (or mostly) contains Activities.

Activity Diagrams are always associated to a Class , an Operation or a Use Case.

Activity Diagrams support sequential as well as parallel Activities. Parallelexecution is represented via Fork/Wait icons, and for the Activities running inparallel, it is not important the order in which they are carried out (they can beexecuted at the same time or one after the other)

60

Page 68: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Documenting Designs: The Unified Modelling Language

Figure .: Umbrello UML Modeller showing a State Diagram

Figure .: Umbrello UML Modeller showing an Activity Diagram

61

Page 69: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

2.4 A Case Study: Card Games

In this section, we look at some specific examples of object-oriented designin a domain that is simple enough that we have a chance of coming up with

something reasonably reusable. Consider card games that are played with a stan-dard deck of playing cards (a so-called “poker” deck, since it is used in the gameof poker).

Designing the classes

When designing object-oriented software, a crucial first step is to identify theobjects that will make up the application. One approach to do this is to identifythe nouns in the problem description. These become candidates for objects. Nextwe can identify verbs in the description: these suggest methods for the objects.

Consider the following description of a card game:

In a typical card game, each player gets a hand of cards. The deck isshuffled and cards are dealt one at a time from the deck and added tothe players’ hands. In some games, cards can be removed from a hand,and new cards can be added. The game is won or lost depending onthe value (ace, 2, ..., king) and suit (spades, diamonds, clubs, hearts)of the cards that a player receives.

If we look for nouns in this description, there are several candidates for ob-jects: game, player, hand, card, deck, value, and suit. Of these, the value andthe suit of a card are simple values, and they will just be represented as instancevariables in a Card object. In a complete program, the other five nouns mightbe represented by classes. But let’s work on the ones that are most obviouslyreusable: card, hand, and deck.

If we look for verbs in the description of a card game, we see that we canshuffle a deck and deal a card from a deck. This gives use us two candidatesfor instance methods in a Deck class: shuffle() and dealCard(). Cards canbe added to and removed from hands. This gives two candidates for instancemethods in a Hand class: addCard() and removeCard(). Cards are relatively pas-sive things, but we need to be able to determine their suits and values. We willdiscover more instance methods as we go along.

The Deck Class:

First, we’ll design the deck class in detail. When a deck of cards is first created, itcontains 52 cards in some standard order. The Deck class will need a constructorto create a new deck. The constructor needs no parameters because any new deckis the same as any other. There will be an instance method called shuffle() that

62

Page 70: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. A Case Study: Card Games

will rearrange the 52 cards into a random order. The dealCard() instance methodwill get the next card from the deck. This will be a method with a return type ofCard, since the caller needs to know what card is being dealt. It has no parameters– when you deal the next card from the deck, you don’t provide any informationto the deck; you just get the next card, whatever it is. What will happen if thereare no more cards in the deck when its dealCard() method is called? It shouldprobably be considered an error to try to deal a card from an empty deck, sothe deck can throw an exception in that case. But this raises another question:How will the rest of the program know whether the deck is empty? Of course,the program could keep track of how many cards it has used. But the deck itselfshould know how many cards it has left, so the program should just be able toask the deck object. We can make this possible by specifying another instancemethod, cardsLeft(), that returns the number of cards remaining in the deck.This leads to a full specification of all the methods in the Deck class:

/** Constructor. Create a shuffled deck of cards.

* @precondition: None

* @postcondition: A deck of 52, shuffled cards is created.*/

public Deck()

/** Shuffle all cards in the deck into a random order.

* @precondition: None

* @postcondition: The existing deck of cards with the cards

* in random order.*/

public void shuffle()

/** Returns the size of the deck

* @return the number of cards still left in the deck.

* @precondition: None

* @postcondition: The deck is unchanged. */

public int size()

/** Determine if this deck is empty

* @return true if this deck has no cards left.

* @precondition: None

* @postcondition: The deck is unchanged. */

public boolean isEmpty()

/** Deal one card from this deck

* @return a Card from the deck.

* @precondition: The deck is not empty

* @postcondition: The deck has one less card. */

public Card deal()

This is everything you need to know in order to use the Deck class. Of course,it doesn’t tell us how to write the class. This has been an exercise in design, not

63

Page 71: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

in programming. With this information, you can use the class in your programswithout understanding the implementation. The description above is a contractbetween the users of the class and implementors of the class—it is the “publicinterface” of the class.

The Hand Class:

We can do a similar analysis for the Hand class. When a hand object is first created,it has no cards in it. An addCard() instance method will add a card to the hand.This method needs a parameter of type Card to specify which card is being added.For the removeCard() method, a parameter is needed to specify which card toremove. But should we specify the card itself (“Remove the ace of spades”), orshould we specify the card by its position in the hand (“Remove the third cardin the hand”)? Actually, we don’t have to decide, since we can allow for bothoptions. We’ll have two removeCard() instance methods, one with a parameterof type Card specifying the card to be removed and one with a parameter of typeint specifying the position of the card in the hand. (Remember that you canhave two methods in a class with the same name, provided they have differenttypes of parameters.) Since a hand can contain a variable number of cards, it’sconvenient to be able to ask a hand object how many cards it contains. So, weneed an instance method getCardCount() that returns the number of cards inthe hand. When I play cards, I like to arrange the cards in my hand so that cardsof the same value are next to each other. Since this is a generally useful thing tobe able to do, we can provide instance methods for sorting the cards in the hand.Here is a full specification for a reusable Hand class:

/** Create a Hand object that is initially empty.

* @precondition: None

* @postcondition: An empty hand object is created.*/

public Hand() {

/** Discard all cards from the hand, making the hand empty.

* @precondition: None

* @postcondition: The hand object is empty. */

public void clear() {

/** If the specified card is in the hand, it is removed.

* @param c the Card to be removed.

* @precondition: c is a Card object and is non-null.

* @postcondition: The specified card is removed if it

exists.*/

public void removeCard(Card c) {

/** Add the card c to the hand.

* @param The Card to be added.

64

Page 72: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. A Case Study: Card Games

* @precondition: c is a Card object and is non-null.

* @postcondition: The hand object contains the Card c

* and now has one more card.

* @throws NullPointerException is thrown if c is not a

* Card or is null. */

public void addCard(Card c) {

/** Remove the card in the specified position from the hand.

* @param position the position of the card that is to be

* removed, where positions start from zero.

* @precondition: position is valid i.e.

* 0 < position < number cards

* @postcondition: The card in the specified position is

* removed and there is one less card in the hand.

* @throws IllegalArgumentException if the position does

* not exist in the hand. */

public void removeCard(int position) {

/** Return the number of cards in the hand.

* @return int the number of cards in the hand

* @precondition: none

* @postcondition: No change in state of Hand. */

public int getCardCount() {

/** Gets the card in a specified position in the hand.

* (Note that this card is not removed from the hand!)

* @param position the position of the card that

* is to be returned

* @return Card the Card at the specified position.

* @throws IllegalArgumentException if position does not

exist.

* @precondition: position is valid i.e.

* 0 < position < number cards.

* @postcondition: The state of the Hand is unchanged. */

public Card getCard(int position) {

/** Sorts the cards in the hand in suit order and in value

order

* within suits. Note that aces have the lowest value, 1.

* @precondition: none

* @postcondition: Cards of the same

* suit are grouped together, and within a suit the cards

* are sorted by value. */

public void sortBySuit() {

/** Sorts the cards in the hand so that cards are sorted into

* order of increasing value. Cards with the same value

65

Page 73: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

* are sorted by suit. Note that aces are considered

* to have the lowest value.

* @precondition: none

* @postcondition: Cards are sorted in order of increasing

value.*/

public void sortByValue() {

The Card Class

The class will have a constructor that specifies the value and suit of the card that isbeing created. There are four suits, which can be represented by the integers 0, 1,2, and 3. It would be tough to remember which number represents which suit, soI’ve defined named constants in the Card class to represent the four possibilities.For example, Card.SPADES is a constant that represents the suit, spades. (Theseconstants are declared to be public final static ints. It might be better to usean enumerated type, but for now we will stick to integer-valued constants. I’llreturn to the question of using enumerated types in this example at the end ofthe chapter.) The possible values of a card are the numbers 1, 2, ..., 13, with 1standing for an ace, 11 for a jack, 12 for a queen, and 13 for a king. Again, I’vedefined some named constants to represent the values of aces and face cards.

A Card object can be constructed knowing the value and the suit of the card.For example, we can call the constructor with statements such as:

card1 = new Card( Card.ACE, Card.SPADES ); // Construct ace of

spades.

card2 = new Card( 10, Card.DIAMONDS ); // Construct 10 of

diamonds.

card3 = new Card( v, s ); // This is OK, as long as v and s

// are integer expressions.

A Card object needs instance variables to represent its value and suit. I’vemade these private so that they cannot be changed from outside the class, andI’ve provided getter methods getSuit() and getValue() so that it will be possi-ble to discover the suit and value from outside the class. The instance variablesare initialized in the constructor, and are never changed after that. In fact, I’vedeclared the instance variables suit and value to be final, since they are neverchanged after they are initialized. (An instance variable can be declared final

provided it is either given an initial value in its declaration or is initialized inevery constructor in the class.)

Finally, I’ve added a few convenience methods to the class to make it eas-ier to print out cards in a human-readable form. For example, I want to beable to print out the suit of a card as the word “Diamonds”, rather than as themeaningless code number 2, which is used in the class to represent diamonds.Since this is something that I’ll probably have to do in many programs, it makes

66

Page 74: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. A Case Study: Card Games

sense to include support for it in the class. So, I’ve provided instance methodsgetSuitAsString() and getValueAsString() to return string representations ofthe suit and value of a card. Finally, I’ve defined the instance method toString()

to return a string with both the value and suit, such as “Queen of Hearts”. Re-call that this method will be used whenever a Card needs to be converted into aString, such as when the card is concatenated onto a string with the + operator.Thus, the statement

System.out.println( "Your card is the " + card );

is equivalent toSystem.out.println( "Your card is the " + card.toString() );

If the card is the queen of hearts, either of these will print out``Your card is the Queen of Hearts''.

Here is the complete Card class. It is general enough to be highly reusable, sothe work that went into designing, writing, and testing it pays off handsomelyin the long run.

/** An object of type Card represents a playing card from a

* standard Poker deck, including Jokers. The card has a suit,

which

* can be spades, hearts, diamonds, clubs, or joker. A spade,

heart,

* diamond, or club has one of the 13 values: ace, 2, 3, 4, 5,

6, 7,

* 8, 9, 10, jack, queen, or king. Note that "ace" is

considered to be

* the smallest value. A joker can also have an associated

value;

* this value can be anything and can be used to keep track of

several

* different jokers.

*/

public class Card {

public final static int SPADES = 0; // Codes for the 4

suits.

public final static int HEARTS = 1;

public final static int DIAMONDS = 2;

public final static int CLUBS = 3;

public final static int ACE = 1; // Codes for the

non-numeric cards.

public final static int JACK = 11; // Cards 2 through 10

have their

public final static int QUEEN = 12; // numerical values for

their codes.

67

Page 75: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

public final static int KING = 13;

/** This card's suit, one of the constants SPADES, HEARTS,

DIAMONDS,

* CLUBS. The suit cannot be changed after the card is

* constructed. */

private final int suit;

/** The card's value. For a normal cards, this is one of the

values

* 1 through 13, with 1 representing ACE. The value cannot be

changed

* after the card is constructed. */

private final int value;

/** Creates a card with a specified suit and value.

* @param theValue the value of the new card. For a regular

card (non-joker),

* the value must be in the range 1 through 13, with 1

representing an Ace.

* You can use the constants Card.ACE, Card.JACK, Card.QUEEN,

and Card.KING.

* For a Joker, the value can be anything.

* @param theSuit the suit of the new card. This must be one

of the values

* Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or

Card.JOKER.

* @throws IllegalArgumentException if the parameter values

are not in the

* permissible ranges */

public Card(int theValue, int theSuit) {

if (theSuit != SPADES && theSuit != HEARTS && theSuit !=

DIAMONDS &&

theSuit != CLUBS )

throw new IllegalArgumentException("Illegal playing

card suit");

if (theValue < 1 || theValue > 13)

throw new IllegalArgumentException("Illegal playing

card value");

value = theValue;

suit = theSuit;

}

/** Returns the suit of this card.

* @returns the suit, which is one of the constants

Card.SPADES,

68

Page 76: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. A Case Study: Card Games

* Card.HEARTS, Card.DIAMONDS, Card.CLUBS */

public int getSuit() {

return suit;

}

/** Returns the value of this card.

* @return the value, which is one the numbers 1 through 13.

*/

public int getValue() {

return value;

}

/** Returns a String representation of the card's suit.

* @return one of the strings "Spades", "Hearts", "Diamonds",

"Clubs".*/

public String getSuitAsString() {

switch ( suit ) {

case SPADES: return "Spades";

case HEARTS: return "Hearts";

case DIAMONDS: return "Diamonds";

case CLUBS: return "Clubs";

default: return "Null";

}

}

/** Returns a String representation of the card's value.

* @return for a regular card, one of the strings "Ace", "2",

* "3", ..., "10", "Jack", "Queen", or "King". */

public String getValueAsString() {

switch ( value ) {

case 1: return "Ace";

case 2: return "2";

case 3: return "3";

case 4: return "4";

case 5: return "5";

case 6: return "6";

case 7: return "7";

case 8: return "8";

case 9: return "9";

case 10: return "10";

case 11: return "Jack";

case 12: return "Queen";

default: return "King";

}

}

/** Returns a string representation of this card, including

69

Page 77: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

both

* its suit and its value. Sample return values

* are: "Queen of Hearts", "10 of Diamonds", "Ace of Spades",

*/

public String toString() {

return getValueAsString() + " of " + getSuitAsString();

}

} // end class Card

2.5 Example: A Simple Card Game

We will finish this section by presenting a complete program that uses the Card

and Deck classes. The program lets the user play a very simple card game calledHighLow. A deck of cards is shuffled, and one card is dealt from the deck andshown to the user. The user predicts whether the next card from the deck willbe higher or lower than the current card. If the user predicts correctly, then thenext card from the deck becomes the current card, and the user makes anotherprediction. This continues until the user makes an incorrect prediction. Thenumber of correct predictions is the user’s score.

My program has a method that plays one game of HighLow. This method hasa return value that represents the user’s score in the game. The main()method letsthe user play several games of HighLow. At the end, it reports the user’s averagescore.

Note that the method that plays one game of HighLow returns the user’s scorein the game as its return value. This gets the score back to the main program,where it is needed. Here is the program:

import java.util.Scanner;

/**

* This program lets the user play HighLow, a simple card game

* that is described in the output statements at the beginning of

* the main() method. After the user plays several games,

* the user's average score is reported.

*/

public class HighLow {

Scanner keyboard = new Scanner(System.in);

public static void main(String[] args) {

System.out.println("This program lets you play the simple

card game,");

70

Page 78: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Example: A Simple Card Game

System.out.println("HighLow. A card is dealt from a deck

of cards.");

System.out.println("You have to predict whether the next

card will be");

System.out.println("higher or lower. Your score in the

game is the");

System.out.println("number of correct predictions you make

before");

System.out.println("you guess wrong.");

System.out.println();

int gamesPlayed = 0; // Number of games user has

played.

int sumOfScores = 0; // The sum of all the scores from

// all the games played.

double averageScore; // Average score, computed by

dividing

// sumOfScores by

gamesPlayed.

boolean playAgain; // Record user's response when

user is

// asked whether he wants to

play

// another game.

do {

int scoreThisGame; // Score for one game.

scoreThisGame = play(); // Play the game and get the

score.

sumOfScores += scoreThisGame;

gamesPlayed++;

System.out.print("Play again? ");

playAgain = keyboard.nextBoolean();

} while (playAgain);

averageScore = ((double)sumOfScores) / gamesPlayed;

System.out.println();

System.out.println("You played " + gamesPlayed + "

games.");

System.out.printf("Your average score was %1.3f.\n",

averageScore);

} // end main()

/**

71

Page 79: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

* Let's the user play one game of HighLow, and returns the

* user's score on that game. The score is the number of

* correct guesses that the user makes.

*/

private static int play() {

Deck deck = new Deck(); // Get a new deck of cards, and

// store a reference to it in

// the variable, deck.

Card currentCard; // The current card, which the user

sees.

Card nextCard; // The next card in the deck. The user

tries

// to predict whether this is higher

or lower

// than the current card.

int correctGuesses ; // The number of correct predictions

the

// user has made. At the end of

the game,

// this will be the user's score.

char guess; // The user's guess. 'H' if the user

predicts that

// the next card will be higher, 'L' if

the user

// predicts that it will be lower.

deck.shuffle(); // Shuffle the deck into a random order

before

// starting the game.

correctGuesses = 0;

currentCard = deck.dealCard();

System.out.println("The first card is the " + currentCard);

while (true) { // Loop ends when user's prediction is

wrong.

/* Get the user's prediction, 'H' or 'L' (or 'h' or

'l'). */

System.out.print("Will the next card be higher(H) or

lower(L)? ");

72

Page 80: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Example: A Simple Card Game

do {

guess = keyboard.next().charAt(0);

guess = Character.toUpperCase(guess);

if (guess != 'H' && guess != 'L')

System.out.print("Please respond with H or L:

");

} while (guess != 'H' && guess != 'L');

/* Get the next card and show it to the user. */

nextCard = deck.dealCard();

System.out.println("The next card is " + nextCard);

/* Check the user's prediction. */

if (nextCard.getValue() == currentCard.getValue()) {

System.out.println("The value is the same as the

previous card.");

System.out.println("You lose on ties. Sorry!");

break; // End the game.

}

else if (nextCard.getValue() > currentCard.getValue()) {

if (guess == 'H') {

System.out.println("Your prediction was

correct.");

correctGuesses++;

}

else {

System.out.println("Your prediction was

incorrect.");

break; // End the game.

}

}

else { // nextCard is lower

if (guess == 'L') {

System.out.println("Your prediction was

correct.");

correctGuesses++;

}

else {

System.out.println("Your prediction was

73

Page 81: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Object Oriented Analysis and Design

incorrect.");

break; // End the game.

}

}

/* To set up for the next iteration of the loop, the

nextCard

becomes the currentCard, since the currentCard has

to be

the card that the user sees, and the nextCard will be

set to the next card in the deck after the user makes

his prediction. */

currentCard = nextCard;

System.out.println();

System.out.println("The card is " + currentCard);

} // end of while loop

System.out.println();

System.out.println("The game is over.");

System.out.println("You made " + correctGuesses

+ " correct

predictions.");

System.out.println();

return correctGuesses;

} // end play()

} // end class

74

Page 82: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Chapter

3Inheritance

Object oriented languages have a feature called inheritance. Inheritance enablesyou to define a new class based upon an existing class. The new class is similarto the existing class, but has additional member variables and methods. Thismakes programming easier because you can build upon an existing class insteadof starting out from scratch. Inheritance (and other features of object orientedlanguages) is responsible for the enormous success of modern software. Program-mers are able to build upon previous work and to continuously improve and up-grade existing software. For example, graphical user interface programming isdone by using inheritance with the basic graphical classes that are contained ina standard library. The classes needed for a particular application are created bycustomizing these basic classes.

3.1 Introduction to Inheritance

Problems with Hacking Code Instead of creating new classes from old classes byinheritance, you could just copy the source code for the old class and modify itso that it does exactly what you want. But this leads to problems.

If you have the source code for a class, you could copy the code and changeit to do what you wanted. Before object oriented programming that was whatwas done. But there are at least two problems with this:

1. It is hard to stay organized. Say that you already have several dozen classesand that you need additional classes based on the original ones. Also, saythat you need several classes based on the new classes. You will end up with

75

Page 83: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

(a) Inheritance between classes (b) Inheritance: Parent and child

Figure .: Inheritance

dozens of source files which are all versions of other source files that havebeen changed in various ways. Without careful planning you will end upwith an unorganized, inconsistent, buggy mess.

2. You need to study the original code. Say that you have a complicated classthat basically does what you want, but you need a small modification. Ifyou edit the source code, even to make a small change, you risk break-ing something. So you must study the original code to be sure that yourchanges are correct. This may not be easy.

The automatic inheritance mechanism of Java greatly relieves both of theseproblems. In a diagram (see Fig. 4.1) showing inheritance, an arrow points fromthe new class to the class it is based upon. The arrow is sometimes labeled “is a”.

In this chapter, clouds represent classes and rectangles represent objects. Thisstyle of representing classes comes from the book Object-oriented Analysis andDesign, by Grady Booch (Addison-Wesley, 1994). In official UML (UnifiedModeling Language) both classes and objects are represented with rectangles.

Single Inheritance The class that is used to define a new class is called a parentclass (or superclass or base class.) The class based on the parent class is called achild class (or subclass or derived class.)

In Java, (unlike with humans) children inherit characteristics from just oneparent. This is called single inheritance. Some languages allow a child to inheritfrom more than one parent. This is called multiple inheritance. With multipleinheritance, it is sometimes hard to tell which parent contributed what character-istics to the child (as with humans). Java avoids these problems by using singleinheritance.

76

Page 84: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Introduction to Inheritance

Figure .: Inheritance Diagram

Figure .: Is-a Relationship

A parent can have any number of children. But a child can have only oneparent. There are three sets of phrases for describing inheritance relationships:parent/child, base class/derived class, superclass/subclass.

Programmers use all three sets interchangebly.

Is-a Relationship A parent class cannot inherit characteristics from its childclass. Inheritance goes in only one direction.

The picture shows a parent class and a child class. The line between themshows the “is-a” relationship. The arrow points to the parent class from the childclass. The picture can be read as “a Ford is-a automobile.” The phrase “is-a” isin common use in computer science. The arrow between a child and parent issometimes called an “is-a link”. The clouds represent classes. This picture doesnot show objects.

Inheritance is between classes, not between objects.

77

Page 85: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

Figure .: Objects

A parent class is a blueprint that is followed when an object is constructed. Achild class of the parent is another blueprint (that looks much like the original),but with added features. The child class is used to construct objects that look likethe parent’s objects, but with added features.

The picture shows a parent class and a child class, and some objects that havebeen constructed from each. These objects are shown as rectangles (to conveythe idea that they are more real than the classes, which are only designs). In thepicture, “Joe’s car,” “Mary’s Ford,” and “Bob’s Ford” represent actual objects. Thecloudy classes represent designs, not actual objects. Objects are constructed as aprogram runs. An object is constructed by following a description in a class file,the result of compiling a Java source program.

Hierarchies This picture shows a hierarchy of classes. It shows that “Ford is-aautomobile,” “Nissan is-a automobile,” and that “VW is-a automobile.” It alsoshows that “Sentra is-a Nissan.” In a hierarchy, each class has at most one parentbut might have several children classes. The class at the top of the hierarchy hasno parent. This class is called the root of the hierarchy.

A class may be the parent for a child class and may be a child of another class.Just as with human relationships, a person is a child of some humans and a parentto others. The syntax for deriving a child class from a parent class is:

class childClass extends parentClass {

// new characteristics of the child class go here

}

78

Page 86: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Introduction to Inheritance

Figure .: Hierarchies

Video Store Example Programming in Java consists mostly of creating class hi-erarchies and instantiating objects from them. The Java Development Kit givesyou a rich collection of base classes that you can extend to do your work.

Here is a program that uses a class Video to represent videos available at arental store. Inheritance is not explicitly used in this program (so far).

class Video {

String title; // name of the item

int length; // number of minutes

boolean avail; // is the video in the store?

// constructor

public Video( String ttl ) {

title = ttl; length = 90; avail = true;

}

// constructor

public Video( String ttl, int lngth ) {

title = ttl; length = lngth; avail = true;

}

public void show() {

System.out.println( title + ", " + length + " min.

available:" + avail );

}

}

79

Page 87: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

public class VideoStore {

public static void main ( String args[] ) {

Video item1 = new Video("Jaws", 120 );

Video item2 = new Video("Star Wars" );

item1.show();

item2.show();

}

}

The Video class has basic information in it, and could be used for documen-taries and instructional videos. But more information is needed for movie videos.Let us make a class that is similar to Video, but now includes the name of the di-rector and a rating.

class Movie extends Video {

String director; // name of the director

String rating; // G, PG, R, or X

// constructor

public Movie( String title, int length, String dir,

String rate ) {

super( title, length ); //use the super class's constructor

director = dir;

rating = rate; //initialize what's new to Movie

}

}

The class Movie is a subclass of Video. An object of type Movie has the follow-ing members in it:

title inherited from Video

length inherited from Video

avail inherited from Video

show() inherited from Video

director defined in Movie

rating defined in Movie

Both classes are defined: the Video class can be used to construct objects ofthat type, and now the Movie class can be used to construct objects of the Movietype.

A child class inherits both variables and methods e.g. the method show inthe superclass Video is inherited in the subclass Movie.

3.2 Constructors in Sub-classes

The class definition for Video has a constructor that initializes the fields (or at-tributes) of Video objects. The class Movie has a constructor that initializes the

80

Page 88: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Constructors in Sub-classes

data of Movie objects.The constructor for class Movie looks like this:

// constructor

public Movie( String title, int length, String dir,

String rate ){

super(title, length);// use the super class's constuctor

director = dir;//initialize the members new to Movie

rating = rate; //initialize the members new to Movie

}

The statement super(title, length) invokes a constructor of the parent toinitialize some of the data. There are two constructors in the parent. The one thatis invoked is the one that matches the argument list in super(title, length).Then the next two statements initialize the members that only Movie has. Note:super() must be the first statement in the subclass’s constructor.

The statement that invokes the parent’s constructor called is super() becausethe parent of a class is sometimes called its superclass.

A constructor for a child class always starts with an invocation of one of theconstuctors in the parent class. If the parent class has several constructors thenthe one which is invoked is determined by matching argument lists. For example,we could define a second constructor for Movie that does not include an argumentfor length. It starts out by invoking the parent constructor that does not have anargument for length:

// alternate constructor

public Movie( String title, String dir, String rate){

super( title ); // invoke the matching parent class

constructor

director = dir;

rating = rate; // initialize members unique to Movie

}

A child constructor always invokes a parent constructor. Examine the follow-ing proposed constructor for Movie:

// proposed constructor

public Movie( String ttl, int lngth, String dir, String rtng ){

title = ttl; // do what the parent's constuctor does.

length = lngth;

avail = true;

director = dir;

rating = rtng;

}

81

Page 89: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

It looks like there is no need to invoke the parent class’s constructor since allvariables are initialized in this one. However a constructor from the parent classis always invoked even if you don’t explicity ask for it.

The Java compiler automatically (automagically) inserts a call to super in thefirst line of the constructor. The above code can be regarded as “shorthand” forthis:

// proposed constructor

public Movie( String ttl, int lngth, String dir, String rtng ){

super(); // invoke the parent's no-argument

constructor

title = ttl; // do what the parent's constuctor does.

length = lngth;

avail = true;

director = dir; rating = rtng;

}

As always, super() comes first, even if you don’t write it in. If the parentdoes not have a no-argument constructor, then using this “shorthand” causes asyntax error. In our program the class definition for Video (the superclass) lacksa no-argument constructor. The proposed constructor (above) calls for such aconstructor so it would cause a syntax error.

A class has a no-argument constructor when:

1. The programmer can write a no-argument constructor for a class.

2. If the programmer does not write any constructors for a class, then a no-argument constructor (called the default constructor) is automatically sup-plied.

3. If the programmer writes even one constructor for a class then the defaultconstructor is not automatically supplied.

In the example program, the class definition for Video includes a constructor,so the default constructor was not automatically supplied. So the constructorproposed for Movie causes a syntax error.

The Object Class

Remember the rule: every constructor starts out with a super() constructor. Ifyou don’t explicitly put it in, then the Java compiler automatically puts it in foryou. Now look at the definition of Video.

This is correct. All classes have a parent class (a super class) except one. Theclass at the top of the Java class hierarchy is called Object. If a class definition does

82

Page 90: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Overriding Methods

not specify a parent class then it automatically has Object as a parent class. Thecompiler automatically assumes, for example: class Video extends Object {

. . . }. It is not possible to write a class definition that does not have Objectas its ultimate ancestor.

If you define a class that does not explicitly extend another class, then itautomatically extends Object. If you define the class so that it extends a classother than Object, then that class either extends another class or extends Object.Now that class in turn must either extend another class or extend Object. Thereis no way to end the chain except by (ultimately) extending Object. You mightcleverly try to have class A extend class B, and have class B extend class A. Butthis (and other loops) is not allowed. Ultimately, every Java object inherits itsobject-like behavior from the class Object. When an object is constructed a chainof constructors is invoked starting with the one in Object and ending with theone in the requested class. The fundamental parts of the object are put togetherby the constructor in Object, then additional parts are added down the chainuntil the requested class is reached. Construction starts with the constructor inObject because each constructor (except Object’s constructor) starts by invokingits super() constructor (with or without arguments).

The parent class does not have to be recompiled each time a new child classis derived from it.

3.3 Overriding Methods

Here is an example program that makes use of the two classes:

public class VideoStore{

public static void main ( String args[] ) {

Video item1 = new Video("Microcosmos", 90 );

Movie item2 = new Movie("Jaws", 120, "Spielberg", "PG" );

item1.show();

item2.show();

}

}

The program prints:

Microcosmos, 90 min. available:true

Jaws, 120 min. available:true

The statement item2.show() calls the show() method of item2. This methodwas inherited without change from the class Video. This is what it looks like:

public void show() {

System.out.println( title + ", " + length +

" min. available:" + avail ); }

83

Page 91: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

It does not mention the new variables that have been added to objects of typeMovie, so nothing new is printed out. We cannot change show() in Video to in-clude the line System.out.println( "dir: "+ director + rating ); becausethe class Video does not define the instance variables director and rating, so itsshow() method can’t use them.

We need a new show() method in the class Movie://added to class Movie

public void show() {

System.out.println( title + ", " + length +

" min. available:" + avail );

System.out.println("dir: " + director + " " + rating );

}

Now, even though the parent has a show() method the new definition ofshow() in the child class will override the parent’s version.

A child’s method overrides a parent’s method when it has the same signatureas a parent method. Now the parent has its method, and the child has its ownmethod with the same signature. (Remember that the signature of a method isthe name of the method and its parameter list.)

An object of the parent type includes the method given in the parent’s def-inition. An object of the child type includes the method given in the child’sdefinition.

With the change in the class Movie the following program will print out thefull information for both items.

class videoStore{

public static void main ( String args[] ) {

Video item1 = new Video("Microcosmos", 90 );

Movie item2 = new Movie("Jaws", 120, "Spielberg", "PG" );

item1.show();

item2.show();

}

}

The line item1.show() calls the show() method defined in Video, and the lineitem2.show() calls the show() method defined in Movie.

Microcosmos, 90 min. available:true Jaws,

120 min. available:true

dir: Spielberg PG

Notice that the Movie class includes some code that is already written.

Using super in a Childʼs Method Sometimes (as in the example) you want a childclass to have its own method, but that method includes everything the parent’smethod does. You can use the super reference in this situation. For example,here is Video’s method:

84

Page 92: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Overriding Methods

public void show() {

System.out.println( title + ", " + length + "

min. available:" + avail ); }

Here is Movie’s method without using super:public void show() {

System.out.println( title + ", " + length

+ " min. available:" + avail );

System.out.println( "dir: " + director+ " " + rating );

}

Movie’s method would better be written using super:public void show() {

super.show();

System.out.println( "dir: " + director + " " + rating ); }

Unlike the case when super is used in a constructor, inside a method super

does not have to be used in the first statement. Two reasons why using super inthis way is a good thing to do.

1. You should not have to write the same code more than once.

2. A change made to the method in the parent class is inherited by the childclass.

Overloaded vs Overridden Methods

Methods can be overloaded or overridden, but constructors can be only over-loaded. Overloaded methods and constructors let you use the same method name(or constructor) but with different argument lists. Overriding lets you redefine amethod in a subclass, when you need new subclass specific behavior. Overloadedmethods let you reuse the same method name in a class, but with different ar-guments (and optionally, a different return type). Overloading a method oftenmeans you’re being a little nicer to those who call your methods, because yourcode takes on the burden of coping with different argument types rather thanforcing the caller to do conversions prior to invoking your method. The rules foroverloaded methods are simple:

1. Overloaded methods must change the argument list.

2. Overloaded methods can change the return type.

3. Overloaded methods can change the access modifier.

4. A method can be overloaded in the same class or in a subclass.

85

Page 93: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

Overloading Examplepublic void changeSize(int size, String name, float pattern) {

}

The following methods are legal overloads of the changeSize() method:public void changeSize(int size, String name) { }

public int changeSize(int size, float pattern) { }

Deciding which of the matching methods to invoke is based on the argu-ments. Overriding lets you redefine a method in a subclass, when you need newsubclass specific behavior. Rules for overridden methods

1. The argument list must exactly match that of the overridden method.

2. The return type must exactly match that of the overridden method.

3. The access level must not be more restrictive than that of the overriddenmethod.

4. The access level can be less restrictive than that of the overridden method.

public class Animal {

public void eat() { }

}

The following are all illegal overrides.private void eat() { } // Access modifier

// is more restrictive

public void eat(String food) { } //A legal overload,

//not an override, because

//the argument list changed

public String eat() { }//Not an override because of

//the return type, but not an

//overload either because ’theres

//no change in the argument list

3.4 Another Example subclass

To reiterate the ideas discussed thus far, we give an example of another subclassof Video.

86

Page 94: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Another Example subclass

Figure .: Many Subclasses

Music Video Class So far the video rental application has two classes: Videoand Movie. Say that you wanted to create a new class, MusicVideo that will belike Video but will have two new instance variables: artist (the name of the per-former) and category (“R&B”, “Pop”, “Classical”, “Other” ). Both of these willbe Strings.

TheMusicVideo class will need its own constructor and its own show()method.The new class looks like this:

class MusicVideo extends Video{

String artist;

String category;

// The constructor will go here

// The show() method will go here

}

The MusicVideo class is a subclass of Video. The Movie class is not shown, butis also a subclass of Video. Remember that a class can have several sub-classes.

MusicVideo inherits title, length, and avail from its parent and adds artist andcategory. Notice that MusicVideo inherits only from its parent Video. It does notinherit anything from its sibling class Movie. Here is the definition so far:

We need a constructor for MusicVideo. We will use four parameters for title,length, artist and category. Initialize avail to true.

// constructor

public MusicVideo ( String ttl, int len, String art, String

cat ) {

super( ttl, len );

artist = art;

category = cat;

}

87

Page 95: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

Note that the super reference must be in the first statement of the constructor.To finish the MusicVideo class, we write a show() method for it using a superreference to do the things already done by the parent class.

The show() method for MusicVideo can use super.show() where ever it needsto. It is the first statement (below) because that is where it makes the most sense.

public void show() {

System.out.println( title + ", " + length +

" min. available:" + avail );

}

}

You may have noticed a major design flaw in the definitions of these classes— none has a rental price! Say that it was your job to fix this problem. How canthis be fixed?

All videos have a rental price, so the fixes should be made to the parent classVideo. A new variable rent should be added to the parent class. Then modifyits constructor and its show() method. The two child classes will inherit thesechanges. Fixing the parent class fixes all of its children.

3.5 Inheritance and Types

Inheritance defines is-a relationships between objects. For example, a MusicVideois-a Video. This means that an object of type MusicVideo is automatically an ob-ject of type Video. Thus, subclass objects can be assigned to superclass variables(but NOT vice-versa).

A variable that can hold a reference to an object of class A can also holda reference to an object belonging to any subclass of A.

This is the substitution principle: you can substitute a subclass reference fora superclass reference. Thus, given the definitions of the classes above, any of thefollowing are legal:

Movie mymovie = new Movie("The Return of the King", 180,

"Jackson", "PG");

Video myvideo = new Movie("Jaws", 120, "Spielberg", "PG" );

myvideo = mymovie ;

The variable myvideo now refers to a Movie object. This is logical since a Movieis-a Video.

This also works for parameters to methods: i.e. a method requiring an objectof class A as parameter, could be passed any object constructed from a subclass ofA. For example, if a method requires an object of type Video then you can pass itany object whose type is a subclass of Video.

88

Page 96: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Inheritance and Types

public static void printDetails(Video v){

...

}

public static void main(String[] args){

Movie mymovie = new Movie("The Return of the King", 180,

"Jackson", "PG")

printDetails(mymovie)

}

You may also create arrays of type Video and populate it with objects createdfrom any subclass of Video.

Video [] videoArray = new Video[5];

...

v[0] = new Movie("Jaws", 120, "Spielberg", "PG" );

v[1] = new MusicVideo("LOTR Theme", 10, "Hans Zimmer","movie

score");

...

for(int i = 0; i < videoArray.length; i++){

System.out.println(v[i].getTitle());

}

Note carefully:

A subclass reference can be assigned to a superclass variable; BUT a su-perclass reference cannot be assigned to a subclass variable.

i.e. while we can say Video v = new Movie(...); we cannot say Movie m =

new Video(...). This holds even if the superclass variable is referencing a sub-class object: for e.g. this is illegal also:

Video myvideo = new Movie("Jaws", 120, "Spielberg", "PG" );

Movie mymovie = new Movie("The Return of the King", 180,

"Jackson", "PG");

myvideo = mymovie; // is legal

mymovie = myvideo; // is illegal

The last statement is illegal because it is an attempt to assign a superclass referenceto a subclass variable. It is illegal even though myvideo is actually referencinga subclass object. The compiler checks the type of myvideo and because it isdeclared to be of type Video, it cannot be assigned to a subclass variable.

Again this rule is logical because Video variable could potentially refer to othertypes of videos that are not Movies. You are not allowed to assign a value of typeVideo to a variable of type Movie because not every Video is a Movie.

89

Page 97: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

Type Checking

Assigning subclass references to superclass variables has an important consequencethat is due to the fact that Java is strongly typed. In strongly typed languages, thetype of every variable must be declared before its use. This allows the compilerto check that each variable is being correctly used.

Once you declare a variable to be a certain type, the compiler will ensurethat it is only ever assigned values of that type (or values that are sub-typesof that type).

Recall that for primitive types the compiler checks assignments, for e.g. that adouble is not being assigned to an int. For reference types, the compiler checksassignments and for correctness of methods calls i.e. that the method exists inthe class and the type, number and order of the parameters are correct. For e.g.given these statements:

Video item1 = new Video("Microcosmos", 90 );

Movie item2 = new Movie("Jaws", 120, "Spielberg", "PG" );

item1.getTitle();

the compiler checks that the getTitle() method is defined in the Video class.In all cases, when checking for correct use of variables, the compiler checks

the declared type of the variable.

The declared type of a variable is used in type checking.

So, given this:Video item3 = new Movie("Jaws", 120, "Spielberg", "PG" );

item3.getDirector();

the compiler checks the declared type of item3 (which is Video) and a compileerror results if the getDirector() method does not exist in the Video class. Eventhough item3 is actually referencing a Movie object, the compiler checks againstthe declared type of the variable.

Typecasting

Java is strongly typed, but it is possible to subvert the type checking by convert-ing variables from one type to another. Converting a variable from one type toanother is known as typecasting . Typecasting may be implicit (i.e. done with-out the programmer doing anything) or explicit (where the programmer has tospecify a cast. )

Widening conversions are allowed automatically (implicitly), without inter-vention from the programmer: e.g. int are typecast to double automatically.Narrowing conversions are allowed only by explicit type casts. e.g. convertingdouble to int. The code below shows implicit and explicit typecasting.

90

Page 98: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Inheritance and Types

double d = 0.0;

int i = 6;

d = i; //allowed (implicit typecast)

i = d; //error (narrowing conversion)

i = (int)d; //allowed (explicit typecast)

The same ideas applies to classes and objects. You can store sub-class objectsin super-class variables since this is considered a narrowing conversion. So thetypecast is implicit. For all other conversion, an explicit typecast is required.

If you know that myVideo refers to a Movie, you can use the type cast (Movie)myVideoto tell the compiler to treat myVideo as if it were actually of type Movie. So, youcould say myMovie = (Movie)myVideo; and you could even refer to ((Movie)myVideo).director.

Note that for object types, type-casts are checked. If myVideo refers to anobject of type MusicVideo, then the type cast (Movie)myVideo will produce anerror.

The code below shows the use of typecasting for object types.Video myVideo = new Video();

Movie myMovie = new Movie();

//...............................

myVideo = new Movie(); //legal-implicit typecast

myVideo = myMovie; //legal

myMovie.director = "Akira Kurosawa"; //legal

(Movie)myVideo.director = "Gavin Hood" ; //legal

myMovie = (Movie)myVideo; //legal

Note that, as explained in a previous section, the use of all variables arechecked by the compiler. So the explicit typecast in line 7 is required becausethe artist field is not defined in the Video class. If the variable is not typecast,then an error results.

the instanceof operator Java has a special operator, the instanceof operatorthat determines whether or not an object is an instance of a particular class. Forexample, given these definitions:

Video v = new Movie(...);

then v instanceof Movie returns true since v references a Movie object. Notethat this checks the class of the actual object being referenced, not the type ofthe variable. Even though the type of v is Video, the object being referenced is aMovie.

The method below shows how typecasting may be used to deal with differentsubclass objects.

public void printDetails(Video myVideo) {

System.out.println("Title : "

91

Page 99: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

+ myVideo.title);

if (myVideo instanceof Movie) {

System.out.println("Type of Video: Movie");

System.out.println("director: "

+ (Movie)myVideo.director);

}

else if (myVideo instanceof MusicVideo) {

System.out.println("Type of Video: Music Video");

System.out.println("Artist: "

+ (MusicVideo)myVideo.artist);

}

}

3.6 Controlling Access to Members of a Class

Access level modifiers determine whether other classes can use a particular fieldor invoke a particular method. There are two levels of access control:

• A class may be declared with the modifier public, in which case that classis visible to all classes everywhere. If a class has no modifier (the default,also known as package-private), it is visible only within its own package.

• A member(i.e. a field or a method) may be declared public, private, pro-tected, or have no explicit modifier. If the member has not been declaredwith a modifier, its access is known as package-private.

The private modifier specifies that the member can only be accessed in itsown class. The protected modifier specifies that the member can only be accessedwithin its own package (as with package-private) and, in addition, by a subclassof its class even if the subclass is in another package.

The following table shows the access to members permitted by each modifier.

Access Level Modifier Class Package Subclass Worldpublic Y Y Y Y

protected Y Y Y Nno modifier Y Y N N

private Y N N N

Table .: Access Modifiers

The first column indicates whether the class itself has access. As you can see, aclass always has access to its own members. The second column indicates whetherclasses in the same package as the class have access to the member. The third

92

Page 100: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Controlling Access to Members of a Class

column indicates whether subclasses of the class - declared outside this package -have access to the member. The fourth column indicates whether all classes haveaccess to the member.

Access levels affect you in two ways.

1. when you use classes that come from another source, such as the classes inthe Java platform, access levels determine which members of those classesyour own classes can use.

2. when you write a class, you need to decide what access level every membervariable and every method in your class should have.

Example

Let’s look at a collection of classes and see how access levels affect visibility. Thefollowing figure shows the four classes in this example and how they are related.

Figure .: Classes and Packages of the Example Used to Illustrate Access

Access from within the class The first class Alpha shows that all members of aclass have access to all other members within the class, regardless of the accessmodifier. The isEqualTo method demonstrates that instances of the same classhave access to one another’s private members.

package one;

/**

* Access from within the Class

*/

public class Alpha {

//member variables / fields

private int privateVariable = 1;

int packageVariable = 2; //default access

protected int protectedVariable = 3;

public int publicVariable = 4;

93

Page 101: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

//methods

private void privateMethod() {

System.out.printf("privateMethod called%n");

}

void packageMethod() { //default access

System.out.printf("packageMethod called%n");

}

protected void protectedMethod() {

System.out.printf("protectedMethod called%n");

}

public void publicMethod() {

System.out.printf("publicMethod called%n");

}

/**

* A member's access modifier determines which classes have

access to that member,

* not which instances have access. So instances of the same

class

* have access to one another's private members:

*/

public void isEqualTo(Alpha anotherAlpha) {

if (this.privateVariable ==

anotherAlpha.privateVariable) //legal

System.out.printf("equal");

else

System.out.printf("not equal");

}

public static void main(String[] args) {

Alpha a = new Alpha();

Alpha anotherAlpha = new Alpha();

a.privateMethod(); //legal

a.packageMethod(); //legal

a.protectedMethod(); //legal

a.publicMethod(); //legal

System.out.printf("privateVariable: %2d%n",

a.privateVariable); //legal

System.out.printf("packageVariable: %2d%n",

a.packageVariable); //legal

System.out.printf("protectedVariable: %2d%n",

a.protectedVariable);//legal

94

Page 102: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Controlling Access to Members of a Class

System.out.printf("publicVariable: %2d%n",

a.publicVariable); //legal

a.isEqualTo(anotherAlpha);

}

}

A member’s access level determines which classes have access to that member,not which instances have access. So instances of the same class have access to oneanother’s private members:

package one;

public class Alpha {

...

public boolean isEqualTo(Alpha anotherAlpha) {

if ( this.privateVariable

== anotherAlpha.privateVariable) {

//legal

return true;

} else {

return false;

}

}

}

Access from within the same package The class Beta is in the same package asAlpha, so it has access to all Alpha’s members except those that are private.

/**

* Same package as Alpha

*/

package one;

/**

* Access from within the same package

*/

public class Beta {

public static void main(String[] args) {

Alpha a = new Alpha();

//a.privateMethod(); //illegal

a.packageMethod(); //legal

a.protectedMethod(); //legal

a.publicMethod(); //legal

//System.out.printf("privateVariable:%2d%n",

a.privateVariable); //illegal

95

Page 103: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Inheritance

System.out.printf("packageVariable:%2d%n",

a.packageVariable); //legal

System.out.printf("protectedVariable:%2d%n",a.protectedVariable);

//legal

System.out.printf("publicVariable:%2d%n",

a.publicVariable); //legal

}

}

Access from within a subclass The class AlphaSub is in a different package butextends Alpha. It has access to all public and protected members.

package two;

import one.Alpha;

/**

* Access from anywhere else.

* @author anban

*

*/

public class AlphaSub extends Alpha{

public void test(){

//privateVariable=9; //illegal

//privateMethod();

//packageMethod();

//packageVariable = 9 ; //illegal

publicVariable = 0;

publicMethod();

protectedMethod();

protectedVariable =7;

}

public static void main(String[] args) {

AlphaSub a2 = new AlphaSub();

a2.protectedMethod(); //legal

System.out.printf("protectedVariable: %2d%n",

a2.protectedVariable); //legal

}

}

96

Page 104: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Controlling Access to Members of a Class

Access from anywhere else A class anywhere else has access only to public mem-bers.

package two;

import one.*;

public class DeltaTwo {

public static void main(String[] args) {

Alpha a = new Alpha();

//a.privateMethod(); //illegal

//a.packageMethod(); //illegal

//a.protectedMethod(); //illegal

a.publicMethod(); //legal

//System.out.printf("privateVariable: %2d%n",

// a.privateVariable); //illegal

//System.out.printf("packageVariable: %2d%n",

// a.packageVariable); //illegal

//System.out.printf("protectedVariable: %2d%n",

// a.protectedVariable); //illegal

System.out.printf("publicVariable: %2d%n",

a.publicVariable); //legal

}

}

97

Page 105: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming
Page 106: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Chapter

4Abstract Classes,Interfaces and InnerClasses

4.1 Abstract Classes

We will motivate the need for Abstract classes by looking at the Shape hierarchyof classes discussed in the previous chapter. Recall that we used Shape class as asuperclass, and particular shapes, such as rectangles, were defined as subclasses ofShape.

Part of the Shape class is shown below:class Shape {

Color color; // Color of the shape.

void setColor(Color newColor) {

// Method to change the color of the shape.

color = newColor; // change value of instance variable

redraw(); // redraw shape, which will appear in new

color

}

void redraw() {

// method for drawing the shape

? ? ? // what commands should go here?

}

. . . // more instance variables and methods

} // end of class Shape

The subclasses inherited from Shape:

99

Page 107: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

class Rectangle extends Shape {

void redraw() {

...// commands for drawing a rectangle

}

...//possibly, more methods and variables

}

class Oval extends Shape {

void redraw() {

...// commands for drawing an oval

}

...// possibly, more methods and variables

}

class RoundRect extends Shape {

void redraw() {

...// commands for drawing a rounded rectangle

}

...// possibly, more methods and variables

}

The problem we had to deal with is: what does the redraw() method inthe Shape class do? How should it be defined? The fact is that the class Shaperepresents an abstract idea of a shape, and there is no way to draw such a thing.

One might then think that we should leave out the redraw() method fromthe Shape class. This would not work for several reasons:

1. It has to be there, or it would be illegal to call it in the setColor() methodof the Shape class. We wrote the setColor method to automatically callredraw() method everytime we changed the color.

2. Even if the setColor method did call the redraw() method, the biggerproblem is that it would be illegal to write “oneShape.redraw() ;”, whereoneShape is a variable of type Shape. Code such as the following would notcompile:

Shape oneShape = new Rectangle();

oneShape.redraw();

The compiler would complain that oneShape is a variable of type Shape andthere is no redraw() method in the Shape class. We would then be forcedto typecast oneShape.

Another option is have an empty method, i.e. a method with no statements:void redraw() {

}

100

Page 108: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Abstract Classes

This is an untidy solution–we seem to be forced to do something unnatural. Italso does not force us to override the method. We can, legally, create subclassesthat do not have the redraw() method.

You may wonder why we have a Shape class in the first place, as we do notintend to ever call the redraw() method of the Shape class. There is also no reasonto ever construct an actual object of type Shape - we intend only to work with theconcrete subclasses. But, the Shape definition gives us the following importantbenefit: We can declare variables of type Shape that will refer to objects of one ofthe subclasses. This will allow us to write code that manipulates Shape objects ingeneral without having to worry about particular concrete shapes. For example,we could write a method that takes a Shape object as an argument and that willmanipulate any object of any subclass of Shape:

public void shrinkShape(Shape s, int amount){

//code here

}

We could also write generic loops that manipulate collections of shapes:

Shape[] arrayOfShapes = new Shape[4];

...

for (int i = 0; i < arrayOfShapes.length; i++){

arrayOfShapes[i].redraw();

}

A neat solution to the problems discussed above is the notion of an abstractclass. An abstract class is one that is not used to construct objects, but only usedfor making subclasses. An abstract class exists to express the common propertiesof all its subclasses.

An abstract class in Java is a class that is never instantiated. Its purpose isto be a parent to several related classes. The children classes inherit fromthe abstract parent class.

An abstract class is declared abstract in the class header:

abstract class Shape {

...

}

A class that is not abstract is called a concrete class - i.e. all classes we haveseen thus far are concrete classes. Even though it can not be instantiated, anabstract class can define methods and variables that child classes inherit. However,abstract classes will also have abstract methods.

An abstract method has no implementation and is declared to be abstract byusing the abstract modifier.

101

Page 109: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

abstract public void redraw();

// abstract method -- must be defined in

// concrete subclasses

An abstract method definition consists of:

• optional access modifier (public, private, and others),

• the reserved word abstract,

• the class of the return value,

• a method signature,

• a semi-colon.

No curly braces or method body follow the signature.

An abstract method has no body. (It has no statements.) It declares anaccess modifier, return type, and method signature followed by a semi-colon. A non-abstract child class inherits the abstract method and mustdefine a non-abstract method that matches the abstract method.

Note the difference between an abstract method and an empty method:public void redraw() {}

//A method with no statements

An abstract method has no implementation, so it has no braces.A programmer would normally make a method , such as the redraw(), an

abstract method, since it is never meant to be called. It is there only to tell thecomputer that all Shapes understand the redraw message. Thus, code such as thefollowing will compile:

Shape s = new Rectangle();

s.redraw()

The redraw() that runs will be the one defined in the rectangle class. As anabstract method, redraw exists only to specify the common interface of all theactual, concrete versions of redraw() in the subclasses of Shape.

The definition of the Shape class is given below:abstract class Shape {

private Color color; // color of shape.

// method to change the color of the shape

public void setColor(Color newColor) {

color = newColor; // change value of instance variable

102

Page 110: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Abstract Classes

redraw(); // redraw shape, which will appear in new

color

}

// abstract method -- must be defined in

// concrete subclasses

public abstract void redraw();

...// more instance variables and methods

} // end of class Shape

Note that it contains fields and concrete methods in addition to abstract meth-ods.

Abstract classes are extended like any other class. For example the subclassRectangle could be defined as follows:

class Rectangle extends Shape {

private double length;

private double width;

public void redraw() {

...// commands for drawing a rectangle

}

...//possibly, more methods

}

An abstract child of an abstract parent does not have to define concrete (non-abstract) methods for the abstract methods it inherits. This means that there maybe several steps between an abstract base class to a child class that is completelynon-abstract. When extending an abstract class you can:

• leave some or all of the abstract methods undefined. In this case, you mustthen declare this subclass as abstract as well.

• define all methods (ie. provide implementations). In this case the subclassis no longer abstract.

If a class has one or more abstract methods it must be declared to be ab-stract. An abstract class may have methods that are not abstract (the usual sortof method). These methods are inherited by children classes in the usual way.

• A non-abstract child class of an abstract parent class must override each ofthe abstract methods of its parent. A non-abstract child must override eachabstract method inherited from its parent by defining a method with thesame signature and same return type.

– Objects of the child class will include this method.

103

Page 111: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

• A child may define additional methods with signatures different from theparent’s method.

– Child objects will include these methods in addition to the first one.

• It is an error if a child defines a method with the same signature as a parentmethod, but with a different return type.

These rules are not really as terrible as they seem. After working with inheri-tance for a while the rules will seem clear. Here is an abstract class Parent.

abstract class Parent {

public abstract int compute( int x, String j);

}

Here is a child of Parent:

class Child extends Parent {

public int compute( int x, String j )

{ . . . }

}

The child’s compute() method correctly overrides the parent’s abstract method.For e.g. the following does not correctly override the parent’s abstract method:

class Child extends Parent

{

public double compute( int x, String j )

{ . . . }

}

It is an error if a child defines a method with the same signature as a parentmethod, but with a different return type.

Non-abstract children must override the abstract methods of their parents.Here is our situation:

class Parent {

public abstract int compute( int x, String j);

}

class Child extends Parent{

// child's compute method

}

Th child class is not declared to be abstract so it must override the compute

method. The below shows legal methods that may be added to the Child classand some illegal methods.

104

Page 112: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Abstract Classes

Method defined in Child Override, Overload or Error?public int compute(int x,String j){...} Overridepublic int compute(int stuff, String str ){...} Override (Parameter names

don’t matter, just their types.)public int Compute(int x, String j){...} Additional Methodpublic int compute(String j, int x){...} Overload–the parameter order

matters.public double compute(int x, String j){...} Error–if the signature is the

same, the return type mustmatch the parent.

Using Abstract classes Abstract classes cannot be instantiated but can be usedas variable types. The following is a legal use of Shape:

Shape s = new Rectangle();

s.redraw();

But, Shape s = new Shape(); is illegal because abstract classes may not be in-stantiated.

If child class defines just one method, and that method has the same name asthe parent’s abstract method, but with a different signature then the child classmust be abstract. Since the child did not define a method with the same signatureand same return type as the parent, it must be declared to be abstract. It is OKto have an abstract child of an abstract parent.

Abstract Class Summary

1. It is illegal to try to create actual objects of abstract types, and the computerwill report an error if you try to do so.

2. Every subclass of an abstract class must implement of the abstract methodsmust itself be declared.

3. A subclass of an abstract class can declare its own fields and methods. Thesemethods may or may not be abstract. If they are abstract, then the classmust be declared as abstract.

4. An Abstract Class is declared as abstract and may contain some methodsthat have no implementation ie. you only provide the method signature.

5. An abstract method acts as a placeholder:they are meant to be implementedin the subclasses.

6. They can contain both abstract and concrete methods.

105

Page 113: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

7. Abstract classes cannot be instantiated—they are meant only to be used assuper/base classes.

8. When extending an abstract class you can:

• leave some or all of the abstract methods undefined. In this case, youmust then declare this subclass as abstract as well.

• define all methods (ie. provide implementations). In this case thesubclass is no longer abstract.

4.2 Interfaces

Java allows single inheritance only. This means that a child class inherits fromonly one parent class. Usually this is all you need. But sometimes multipleinheritance would be convenient. Interfaces give Java some of the advantages ofmultiple inheritance without the disadvantages.

With object oriented programming, you define software objects that mimic“real world” objects. This makes programs easier to think about and more reliable.In the real world, you often think about an object in several different ways. Youcan think of your car as a vehicle or as taxable property. It would be convenientif software objects, also, could be thought of in several ways. But a Java objectbelongs to just one class. An interface describes aspects of a class other than thosethat it inherits from its parent.

An interface is a set of requirements that the class must implement. Aninterface is a list of constants and method declarations. The method decla-rations DO NOT include an implementation (there is no method body).A class that implements an interface must implement each of the methodslisted in the interface.

A class can extend one parent class to inherit the methods and instance vari-ables of that parent. A class can also implement an interface to gain additionalmethods and constants. However, the additional methods must be explicitlywritten as part of the class definition. The interface is a list of requirements thatthe class definition must explicitly meet (through code, not through inheritance).For example, a class Car might extend the Vehicle class. This gives it all the meth-ods and instance variables of Vehicle, by inheritance. If Car also implements theTaxable interface, then its definition must contain code for all the methods listedin Taxable.

An interface lists only constants and methods.

An interface definition looks like this:

106

Page 114: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Interfaces

interface InterfaceName {

constant definitions

method declarations (without implementations).

}

A method declaration is simply an access modifier, a return type, and a methodsignature followed by a semicolon. All methods in an interface definition areimplicitly abstract i.e. they are assumed abstract so the abstract modifier is notrequired..

This looks somewhat like a class definition. But no objects can be constructedfrom it. However, objects can be constructed from a class that implements aninterface. A class implements an interface by doing this:

class SomeClass extends SomeParent implements interfaceName {

}

A class always extends just one parent but may implement several interfaces.

(Review: ) If a class definition omits extends SomeParent, then it extendsthe Object class.

Here is an example interface definition. The constants don’t have to be sepa-rated from the methods, but doing so makes the interface easier to read.

interface MyInterface{

public static final int aConstant = 32; // a constant

public static final double pi = 3.14159; // a constant

public void methodA( int x ); // a method

declaration

public double methodB(); // a method

declaration

}

A method in an interface cannot be made private. A method in an interfaceis public by default. The constants in an interface are public static final bydefault. Therefore, it is not necessary to use these modifiers. Making use of thedefaults, the above interface is equivalent to the following:

interface MyInterface{

int aConstant = 32; // a constant

double pi = 3.14159; // a constant

void methodA( int x ); // a method declaration

double methodB(); // a method declaration

}

107

Page 115: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

The second interface (above) is the preferred way to define an interface. Thedefaults are assumed and not explicitly coded.

• A class that implements an interface must implement each method in theinterface.

• Methods from the interface must be declared public in the class.

• Constants from the interface can be used as if they had been defined in theclass.

• Constants should not be redefined in the class.

The following definition is incorrectinterface SomeInterface{

public final int x = 32;

public double y;

public double addup( );

}

because variables (such as y) cannot be put in an interface. Only constants andmethod declarations.

Implementing an Interface

A class definition must always extend one parent, but it can implement zero ormore interfaces:

class SomeClass extends Parent implements SomeInterface {

class definition body

}

The body of the class definition is the same as always. However, since itimplements an interface the body must have a definition of each of the methodsdeclared in the interface. The class definition can use access modifiers as usual.Here is a class definition that implements three interfaces:

public class BigClass extends Parent implements InterfaceA,

InterfaceB, InterfaceC {

class definition body

}

108

Page 116: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Interfaces

Now BigClass must provide a method definition for every method declared inthe three interfaces. Here is another class definition:

public class SmallClass implements InterfaceA {

class definition body

}

Any number of classes can implement the same interfaces.The above class definition is correct. You might wonder that it does not

extend a base class, but it does. If no other class is extended, Object is the baseclass.

SmallClass extends Object and implements InterfaceA

Example Problem

Let us create a database program for a store. The store sells:

1. Goods, each of which has the attributes:

• description• price

The types of goods are:

• Food—with an attribute “calories”. Food objects are not taxable.• Toy—with an attribute “minimumAge”. Toy objects are taxable.• Book—with an attribute “author”. Book objects are taxable.

There are many things that are taxable that are not goods, such as services orentertainment. Also, not all goods are taxable. So we want to have the concept“taxable” as a separate concept, not part of the concept of Goods. Here is whatthe concept Taxable looks like:

• A Taxable item,

– has a taxRate of 6 percent,– has a calculateTax() method.

When implemented in Java, these concepts will appear as classes and an interface.The concepts and the implementation of each concept is given in the table

below:

109

Page 117: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

Figure .: Interfaces

Concept Parent Class, Child Class, or Interface?Goods Parent ClassFood Child ClassToy Child ClassBook Child ClassTaxable Interface

The picture (see Fig. ??) that shows the classes and the interface.The children classes extend their parent. This is shown with a solid arrow

pointing at the parent. The solid arrows show inheritance. The three childreninherit the display() method from their parent, Goods. Two of the classes im-plement the interface. This is shown with a dotted arrow pointing at the interface.The dotted arrows show what a class must implement. The Toy and Book classesmust implement the calculateTax() method.

According to the picture, Food will have a display() method, Toy will havea display() method and calculateTax() method.

Here is a class definition for Goods:public class Goods{

private String description;

private double price;

public Goods( String des, double pr ) {

description = des;

price = pr;

}

public void display() {

110

Page 118: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Interfaces

System.out.println( "item: " + description + " price: " +

price );

}

}

The child class Food extends the parent class. It uses super to use the parent’sconstructor and the parent’s display method.

public class Food extends Goods{

private double calories;

public Food( String des, double pr, double cal) {

super( des, pr );

calories = cal ;

}

public void display() {

super.display( );

System.out.println( "calories: " + calories );

}

}

It would be incorrect to reverse the order of the statments in the constructorfor Food. The super must come first.

Here is Taxable: A Taxable item,

• has a taxRate of 6 percent, which should be a double constant.

• and a calculateTax() method. which should return a double value.

The Taxable interface looks like this:interface Taxable{

final double taxRate = 0.06 ;

double calculateTax() ;

}

final means that what follows is a constant, not a variable. Variables are notallowed in interfaces. In fact, the final can be omitted since the compiler will au-tomatically make the identifier a constant. The = value part cannot be omitted.The method declaration (in the second line) is public by default.

Adding another Class Since taxRate is a constant, it must be set to a value, here,6 percent. Here is a definition of Toy. Recall that it:

• Has a parent, Goods.

• Adds a variable, minimumAge.

111

Page 119: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

• Is taxable.

public class Toy extends Goods implements Taxable{

private int minimumAge;

public Toy( String des, double pr, int min) {

super( des, pr );

minimumAge = min ;

}

public void display() {

super.display() ;

System.out.println( "minimum age: " + minimumAge );

}

public double calculateTax() {

return price * taxRate ;

}

}

The constant taxRate is used in the calculateTax() method just as if it had beendefined in the Toy class. Also, it can be used in methods of the class other thanthose listed in the interface.

The calculateTax() method must be made public.Any number of classes can implement the same interface.There is one remaining class in our example, Book, which looks like this:

• Has a parent Goods.

• Adds a variable author.

• Is taxable.

You might wish to review the diagram that shows the relationships betweenthe classes.

public class Book extends Goods implements Taxable{

private String author;

public Book( String des, double pr, String auth) {

super( des, pr );

author = auth ;

}

public void display() {

super.display() ;

System.out.println( "author: " + author );

112

Page 120: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Interfaces

}

public double calculateTax() {

return price * taxRate ;

}

}

Note that the taxRate for Book is the same as for Toy. By using an interface, aconstant can be used by several classes. This helps keep the classes consistent.

Here is a tiny program that tests the classes.public class Store{

public static void main ( String[] args ) {

Goods gd = new Goods( "bubble bath", 1.40 );

Food fd = new Food ( "ox tails", 4.45, 1500 );

Book bk = new Book ( "Emma", 24.95, "Austin" );

Toy ty = new Toy ( "Legos", 54.45, 8 );

gd.display();

fd.display();

ty.display();

System.out.println("Tax is: " + ty.calculateTax() + "\n" );

bk.display();

System.out.println("Tax is: " + bk.calculateTax() + "\n" );

}

}

The calculateTax() method is only used with objects whose class imple-ments the interface. Here is a picture that shows the classes and their objects:

In the picture, clouds represent classes. Arrows with pointed head connectchild classes to parent classes. The dotted rectangle represents the interface; adotted arrow shows which classes implement it. Rectangles represent objects.Arrows with square head connect an object to its class.

These 4 objects could be kept in an array. The array would be of type Goods.

Array of Goods Objects Here is a modified testing program that uses an array:public class Store

{

public static void main ( String[] args )

113

Page 121: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

Figure .: Objects and Interfaces

Figure .: The Array

{

Goods[] inventory = new Goods[10];

inventory[0] = new Goods( "bubble bath", 1.40 );

inventory[1] = new Food ( "ox tails", 4.45, 1500 );

inventory[2] = new Book ( "Emma", 24.95, "Austin" );

inventory[3] = new Toy ( "Leggos", 54.45, 8 );

inventory[0].display();

inventory[1].display();

inventory[2].display();

inventory[3].display();

}

}

Since each child class is-a Goods, an array of type Goods[] can be used with anyof them. The array inventory has 10 slots, but the program uses only 4 of them,like this: As always, each slot of the array is a reference variable which can refer

114

Page 122: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Interfaces

to an object that has been created with a new. Each of the classes Toy and Bookare taxable.

Interface as a Type An interface can be used as a data type for a referencevariable. Since Toy and Book implement Taxable, they can both be used with areference variable of type Taxable:

public static void main ( String[] args ) {

Taxable item1 = new Book ( "Emma", 24.95, "Austin" );

Taxable item2 = new Toy ( "Leggos", 54.45, 8 );

System.out.println( "Tax on item 1 "+ item1.calculateTax() );

System.out.println( "Tax on item 2 "+ item2.calculateTax() );

}

The compiler has been told in the interface that all Taxable objects will havea calculateTax() method, so that method can be used with the variables.

The following will not work? item1.display(); All the compiler has beentold is that item1 implements the methods in the interface Taxable. The display()method is not in the interface.

Type Casts

When you use a variable of type Taxable you are asking to use the “taxable” aspectof the object. Many different kinds of objects might be referred to by the vari-able. (In a larger program there may be Taxable classes that are not Goods.) Thecompiler can only use the methods it knows that the object must have—-thosein the interface.

However, you can use a type cast to tell the compiler that in a particularstatement in the program the variable will refer to an object of a specific class:

public static void main ( String[] args ) {

Taxable item1 = new Book ( "Emma", 24.95, "Austin" );

System.out.println( "Tax on item 1 "+ item1.calculateTax() );

((Book)item1).display();

}

This program is not very sensibly written, since if the variable item1 wereof type Book everything would work without the need for a type cast. But inprograms with more complicated logic such casts are sometimes needed. Theouter parentheses is needed in: ((Book)item1).display(); because you wantto:

• First, cast item1 to type Book.

115

Page 123: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

• Then, invoke the display() method that objects of type Book have.

When is a Type Cast Needed? Now examine the following:public static void main ( String[] args ) {

Book book ;

Taxable tax = new Book ( "Emma", 24.95, "Austin" );

book = (Book)tax;

book.display();

System.out.println( "Tax on item 1 "+ book.calculateTax() );

}

The type cast is necessary to tell the compiler that the variable tax in this casecontains a Book object.

Now consider the following code:public static void main ( String[] args ) {

Goods toy ;

Taxable tax = new Toy ( "Building Blocks", 1.49, 6 );

toy = tax;

toy.display();

System.out.println( "Tax: "+ toy.calculateTax() );

}

Type casts are necessary in the above code. Here is one way.public static void main ( String[] args ) {

Goods toy ;

Taxable tax = new Toy ( "Building Blocks", 1.49, 6 );

toy = (Toy)tax;

toy.display();

System.out.println( "Tax: "+ ((Taxable)toy).calculateTax() );

}

The first type cast must cast tax to a type Goods (or an ancestor). The secondmust cast toy to Taxable or to a class that implements Taxable.

Here is another possible way:public static void main ( String[] args ) {

Goods toy ;

Taxable tax = new Toy ( "Building Blocks", 1.49, 6 );

toy = (Goods)tax;

toy.display();

System.out.println( "Tax: "+ ((Toy)toy).calculateTax() );

}

116

Page 124: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Interfaces

public static void main ( String[] args ) Goods toy ; Taxable tax = new Toy (”Building Blocks”, 1.49, 6 );

toy = (Goods)tax; toy.display(); System.out.println( ”Tax: ”+ ((Toy)toy).calculateTax());

default methods in interfaces

Java version 8, makes a few useful additions to interfaces including the idea of de-fault methods. Unlike the usual abstract methods in interfaces, a default methodhas an implementation. When a class implements the interface, it does not haveto provide an implementation for the default method—although it can do soif it wants to provide a different implementation. Essentially, default methodsare inherited from interfaces in much the same way that ordinary methods areinherited from classes. This moves Java partway towards supporting multiple in-heritance. It’s not true multiple inheritance, however, since interfaces still cannotdefine instance variables.

A default method in an interface must be marked with the modifier default.It can optionally be marked public but, as for everything else in interfaces, de-fault methods are automatically public and the public modifier can be omitted.Here is an example.:

public interface Readable { // represents a source of input

public char readChar(); // read the next character from the

input

default public String readLine() { // read up to the next

line feed

StringBuilder line = new StringBuilder();

char ch = readChar();

while (ch != ’\’n) {

line.append(ch);

ch = readChar();

}

return line.toString();

}

}

A concrete class that implements this interface must provide an implementationfor readChar(). It will inherit a definition for readLine() from the interface, butcan provide a new definition if required. Note that the default readLine() callsthe abstract method readChar(), whose definition will only be provided in theimplementing class. The reference to readChar() in the definition is polymor-phic. The default implementation of readLine() is one that would make sensein almost any class that implements Readable. Here’s a rather silly example of aclass that implements Readable, including a main() routine that tests the class.Can you figure out what it does?

117

Page 125: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

public class Stars implements Readable {

public char readChar() {

if (Math.random() > 0.02)

return ’’*;

else

return ’\’n;

}

public static void main(String[] args) {

Stars stars = new Stars();

for (int i = 0 ; i < 10; i++ ) {

String line = stars.readLine();

System.out.println( line );

}

}

}

Default methods provide Java with a capability similar to something called a“mixin” in other programming languages, namely the ability to mix functionalityfrom another source into a class. Since a class can implement several interfaces,it is possible to mix in functionality from several different sources.

Additional Facts about Interfaces

There are features of interfaces that the example did not show: A class can imple-ment several interfaces:

class SomeClass extends

SomeParent implements InterfaceA, InterfaceB, InterfaceC {

}

Now SomeClass must implement all the methods listed in all the interfaces.The interfaces cannot contain several definitions of the same constant. To

ensure consistancy, a constant should be defined only once, in one interface.However, it is OK if two interfaces ask for the same method. A class that im-

plements both interfaces only needs to provide one complete method definitionto satisfy both interfaces.

An interface can be made public. In fact, this is usually what is done. Whena class or interface is public it must be the only public class or interface in the filethat contains it.

A public interface can be implemented by any class in any file. Many graph-ical user interface components implement public interfaces. You must use themto work with the GUI features of Java.

An interface cannot be made private: private would mean that nobody coulduse it, which is not sensible.

118

Page 126: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Nested Classes

An interface can be an extension of another interface (but not an extensionof a class):

public interface ExciseTaxable extends Taxable{

double extraTax = 0.02;

double calculateExtra();

}

A complex hierarchy of interfaces can be constructed using this feature. Thisis an advanced feature which you will probably not need to use.

A class cannot extend an interface. Only classes are extended, and a class mayextend only one class.

The differences between abstract classes and interfaces is summarized in thetable below:

Variables Constructors MethodsAbstractclass

No restrictions. Constructors are in-voked by subclassesthrough constructorchaining. An abstractclass cannot be instan-tiated using the newoperator.

No restrictions

Interface All variables must bepublic static final

No constructors. An in-terface cannot be instan-tiated using the new op-erator.

All methods must bepublic abstract instancemethods

4.3 Nested Classes

Aclass is a high-level building block of a program, representing a poten-tially complex idea and its associated data and behaviors. Sometimes we cre-

ate simpler, small classes specifically to support the work of a larger class. Suchtrivial classes are often useful and even essential. For these cases, Java, allows thenesting of one class inside another class. There are other good reasons for nestingthe definition of one class inside another class. The nested class has privilegedaccess to the members of the class in which it is nested.

In Java, a nested class is any class whose definition is inside the definition ofanother class. It is also possible to nest a class inside a method. Nested classescan be either named or anonymous. A named nested class can be either static ornon-static.

119

Page 127: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

Static Nested Classes

The definition of a static nested class looks just like the definition of any otherclass, except that it is nested inside another class and it has the modifier staticas part of its declaration. A static nested class is part of the static structure of thecontaining class. It can be used inside that class to create objects in the usual way.If it is used outside the containing class, its name must indicate its membershipin the containing class. That is, the full name of the static nested class consists ofthe name of the class in which it is nested, followed by a period, followed by thename of the nested class for e.g. MyOuterClass.MyInnerClass..... This is similarto other static components of a class: A static nested class is part of the class itselfin the same way that static member variables are parts of the class itself.

For example, consider a class named WireFrameModel that represents a setof lines in three-dimensional space. (Such models are used to represent three-dimensional objects in graphics programs.) Suppose that the WireFrameModelclass contains a static nested class, Line, that represents a single line. Then, outsideof the classWireFrameModel, the Line class would be referred to as WireFrameModel.Line.This follows the normal naming convention for static members of a class. Thedefinition of the WireFrameModel class with its nested Line class would look, inoutline, like this:

public class WireFrameModel {

. . . // other members of the WireFrameModel class

static public class Line {

// Represents a line from the point (x1,y1,z1)

// to the point (x2,y2,z2) in 3-dimensional space.

double x1, y1, z1;

double x2, y2, z2;

} // end class Line

. . . // other members of the WireFrameModel class

} // end WireFrameModel

The full name of the nested class is WireFrameModel.Line. That name can be used,for example, to declare variables. Inside the WireFrameModel class, a Line objectwould be created with the constructor “new Line()”. Outside the class, “newWireFrameModel.Line()” would be used.

A static nested class has full access to the static members of the containingclass, even to the private members. Similarly, the containing class has full accessto the members of the nested class, even if they are marked private. This canbe another motivation for declaring a nested class, since it lets you give one classaccess to the private members of another class without making those members

120

Page 128: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Nested Classes

generally available to other classes. Note also that a nested class can itself beprivate, meaning that it can only be used inside the class in which it is nested.

When you compile the above class definition, two class files will be created.Even though the definition of Line is nested insideWireFrameModel, the compiledLine class is stored in a separate file. The name of the class file for Line will beWireFrameModel$Line.class.

Inner Classes

Non-static nested classes are referred to as inner classes. Inner classes are actuallyassociated with an object rather than with the class in which its definition isnested.

Any non-static member of a class is not part of the class itself (although itssource code is contained in the class definition). The non-static members of aclass specify what will be contained in objects that are created from that class.This is true for inner classes as well. Each object that belongs to the containingclass has its own copy of the nested class. This copy has access to all the instancemethods and instance variables of the object, even to those that are declaredprivate. The two copies of the inner class in two different objects differ becausethe instance variables and methods they refer to are in different objects. In fact,the rule for deciding whether a nested class should be static or non-static is simple:If the nested class needs to use any instance variable or instance method from thecontaining class, make the nested class non-static. Otherwise, it might as well bestatic.

In most cases, an inner class is used only within the class where it is defined.When that is true, using the inner class is really not much different from usingany other class. You can create variables and declare objects using the simplename of the inner class in the usual way.

From outside the containing class, however, an inner class has to be referred tousing a name of the form ⟨variableName⟩.⟨NestedClassName⟩, where ⟨variableName⟩is a variable that refers to the object that contains the inner class. In order to cre-ate an object that belongs to an inner class, you must first have an object thatbelongs to the containing class.

Consider a class that represents poker games. This class might include anested class to represent the players of the game. The structure of the PokerGameclass could be:

public class PokerGame { // Represents a game of poker.

class Player { // Represents one of the players in this

game.

.

.

.

121

Page 129: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

} // end class Player

private Deck deck; // A deck of cards for playing the game.

private int pot; // The amount of money that has been bet.

. . .

} // end class PokerGame

If game is a variable of type PokerGame, then, game contains its own copy ofthe Player class. In an instance method of a PokerGame object, a new Player objectwould be created by saying “new~Player()”, just as for any other class. You couldalso create a Player object outside the PokerGame class with an expression such as“game.new~Player()”.

The Player object will have access to the deck and pot instance variables in thePokerGame object. Each PokerGame object has its own deck and pot and Players.Players of that poker game use the deck and pot for that game; players of anotherpoker game use the other game’s deck and pot. That’s the effect of making thePlayer class non-static. This is the most natural way for players to behave. APlayer object represents a player of one particular poker game. If Player were anindependent class or a static nested class, on the other hand, it would representthe general idea of a poker player, independent of a particular poker game.

Anonymous Inner Classes

In some cases, you might find yourself writing an inner class and then using thatclass in just a single line of your program. In cases like this you have the option ofusing an anonymous inner class. An anonymous class is created with a variationof the new operator that has the form

new superclass-or-interface(parameter-list){

methods-and-variables

}

This constructor defines a new class, without giving it a name, and it simultane-ously creates an object that belongs to that class. This form of the new operatorcan be used in any statement where a regular “new” could be used. The intentionof this expression is to create: “a new object belonging to a class that is the sameas ⟨superclass-or-interface⟩ but with these ⟨methods-and-variables⟩ added.” The ef-fect is to create a uniquely customized object, just at the point in the programwhere you need it. Note that it is possible to base an anonymous class on aninterface, rather than a class. In this case, the anonymous class must implementthe interface by defining all the methods that are declared in the interface. If aninterface is used as a base, the ⟨parameter-list⟩ must be empty. Otherwise, it cancontain parameters for a constructor in the ⟨superclass⟩.

Anonymous classes are often used for handling events in graphical user inter-faces, and we will encounter them several times in the chapters on GUI program-

122

Page 130: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Nested Classes

ming. Consider the Drawable interface, which is defined earlier in this section.Suppose that we want a Drawable object that draws a filled, red, 100-pixel square.Rather than defining a new, separate class and then using that class to create theobject, we can use an anonymous class to create the object in one statement:

Drawable redSquare = new Drawable() { void draw(Graphics g) {

g.setColor(Color.RED);

g.fillRect(10,10,100,100);

}

};

Then redSquare refers to an object that implements Drawable and that draws ared square when its draw() method is called. The semicolon at the end of thestatement is not part of the class definition; it’s the semicolon that is required atthe end of every declaration statement.

Anonymous classes are often used for actual parameters. For example, con-sider the following simple method, which draws a Drawable in two differentgraphics contexts:

void drawTwice( Graphics g1, Graphics g2, Drawable figure ) {

figure.draw(g1);

figure.draw(g2);

}

When calling this method, the third parameter can be created using an anony-mous inner class. For example:

drawTwice( firstG, secondG, new Drawable() { void

draw(Graphics g) {

g.drawOval(10,10,100,100);

}

} );

When a Java class is compiled, each anonymous nested class will produce aseparate class file. If the name of the main class is MainClass, for example, thenthe names of the class files for the anonymous nested classes will be MainClass\$1.class,MainClass\$2.class, MainClass\$3.class, and so on.

Java 8 Lambda Expressions

The syntax for anonymous classes is cumbersome. In many cases, an anonymousclass implements an interface that defines just one method. Java 8 introduces anew syntax that can be used in place of the anonymous class in that circumstance:the lambda expression. Here is what the previous method call looks like using alambda expression:

drawTwice( firstG, secondG, g -> g.drawOval(10,10,100,100) )

123

Page 131: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Abstract Classes, Interfaces and Inner Classes

The lambda expression is g -\> g.drawOval(10,10,100,100). Its meaning is,“the method that has a parameter g and executes the code g.drawOval(10,10,100,100).”The computer knows that g is of type Graphics because it is expecting a Draw-able as the actual parameter, and the only method in the Drawable interface hasa parameter of type Graphics. Lambda expressions can only be used in placeswhere this kind of type inference can be made. The general syntax of a lambdaexpression is

formal-parameter-list -> method-body

where the ⟨method body⟩ can be a single expression, a single method call, or ablock of statements enclosed between { and }. When the body is a single ex-pression or function call, the value of the expression is automatically used asthe return value of the method that is being defined. The parameter list in thelambda expression does not have to specify the types of the parameters, althoughit can. Parentheses around the parameter list are optional if there is exactly oneparameter and no type is specified for the parameter; this is the form seen in theexample above. For a method with no parameters, the parameter list is just anempty set of parentheses. Here are a few more examples of lambda expressions:

() -> System.out.println("Hello World")

g -> { g.setColor(Color.RED); g.drawRect(10,10,100,100); }

(a, b) -> a + b

(int n) -> {

while (n > 0) {

System.out.println(n);

n = n/2; }

} // lambda expressions ends here

As you can see, the syntax can still get pretty complicated. There is quite a lotmore to say about lambda expressions, but my intention here is only to brieflyintroduce one of the most interesting new features in Java 8.

124

Page 132: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Chapter

5Graphical UserInterface Programming

Computer users today expect to interact with their computers using a graph-ical user interface (GUI). GUI programs differ from traditional “straight-

line” command line programs that you have in that GUI programs are event-driven. That is, user actions such as clicking on a button or pressing a key on thekeyboard generate events, and the program must respond to these events as theyoccur.

5.1 The Basic GUI Application

The command-line programs that you have learned how to program wouldseem very alien to most computer users. Today, most people interact with

their computers exclusively through a GUI. A GUI program offers a much richertype of user interface, where the user uses a mouse and keyboard to interactwith GUI components such as windows, menus, buttons, check boxes, text inputboxes, scroll bars, and so on.

A GUI program still has a main() method, but in general, that main routinejust creates one or more GUI components and displays them on the computerscreen. Once the GUI components have been created, they follow their ownprogramming—programming that tells them how to draw themselves on thescreen and how to respond to events such as being clicked on by the user.

Here is a very simple GUI “Hello World” program that says “Hello” to theuser by opening a window where the greeting is displayed:

import javax.swing.JOptionPane;

125

Page 133: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

public class HelloWorldGUI1 {

public static void main(String[] args) {

JOptionPane.showMessageDialog( null, "Hello World!" );

}

}

When this program is run, a window appears on the screen that contains themessage “Hello World!”. The window also contains an “OK” button–when theuser clicks this button, the window closes and the program ends. This program isalready doing some pretty fancy stuff. It creates a window, it draws the contentsof that window, and it handles the event that is generated when the user clicks thebutton. The reason the program was so easy to write is that all the work is doneby showMessageDialog(), a static method in the built-in class JOptionPane.

If you want to do anything serious in a GUI program, there is a lot more tolearn. To give you an idea of the types of things that are involved, we’ll look at ashort GUI program that does the same things as the previous program—open awindow containing a message and an OK button, and respond to a click on thebutton by ending the program—but does it all by hand instead of by using thebuilt-in JOptionPane class.

Here is the source code for the program. In this section, you will just get abrief overview of GUI programming.

JFrame and JPanel

In a Java GUI program, each GUI component in the interface is representedby an object in the program. A fundamental component is the window. TheJFrame class (which is included in the package javax.swing) is a built-in classto represent an independent window that can act as the main window of anapplication. A JFrame object comes with many of the behaviors of windowsalready programmed in. In particular, it comes with the basic properties sharedby all windows, such as a titlebar and the ability to be opened and closed. Sincea JFrame comes with these behaviors, you don’t have to program them yourself!What a JFrame doesn’t come with is content , the stuff that is contained in thewindow. If you don’t add any other content to a JFrame, it will just display ablank area.

The main program above declares a variable, window, of type JFrame and setsit to refer to a new window object with the statement:

JFrame window = new JFrame("GUI Test");

The parameter (the string “GUI test”) in the constructor specifies the title thatwill be displayed in the titlebar of the window. This line creates the window

126

Page 134: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. The Basic GUI Application

Listing .: Simple Graphical User Interface.

import java.awt.∗;

import java.awt.event.∗;

import javax.swing.∗;

public class HelloWorldGUI2 {

private static class HelloWorldDisplay extends JPanel {

public void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawString( "Hello World!", 20, 30 );

}

}

private static class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

}

public static void main(String[] args) {

HelloWorldDisplay displayPanel = new HelloWorldDisplay();

JButton okButton = new JButton("OK");

ButtonHandler listener = new ButtonHandler();

okButton.addActionListener(listener);

JPanel content = new JPanel();

content.setLayout(new BorderLayout());

content.add(displayPanel, BorderLayout.CENTER);

content.add(okButton, BorderLayout.SOUTH);

JFrame window = new JFrame("GUI Test");

window.add(content);

window.setSize(250,100);

window.setLocation(100,100);

window.setVisible(true);

}

}

127

Page 135: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

object, but the window itself is not yet visible on the screen. Before making thewindow visible, some of its properties are set with these statements:

window.add(content);

window.setSize(250,100);

window.setLocation(100,100);

The first line here sets the content of the window. (The content itself was createdearlier in the main program.) The second line says that the window will be 250pixels wide and 100 pixels high. The third line says that the upper left cornerof the window will be 100 pixels from the left edge of the screen and 100 pixelsdown from the top. Once all this has been set up, the window is actually madevisible on the screen with the command: window.setVisible(true);Although the main() method ends her, the window is still on the screen and theprogram as a whole does not end until the user clicks the OK button. Oncethe window was opened, a new thread was created to manage the graphical userinterface, and that thread continues to run even after main() has finished.

The content that is displayed in a JFrame is called its content pane. A basicJFrame already has a blank content pane; you can either add things to that paneor you can replace the basic content pane entirely. In the sample program, theline window.add(content) adds the JPanel object, content to the JFrame’s con-tent pane. We could replace this line with window.setContentPane(content) toreplace the original blank content pane with a different component. (Rememberthat a “component” is just a visual element of a graphical user interface.)

JPanel is a fundamental class in Swing. The basic JPanel is just a blank rect-angle. There are two ways to make a useful JPanel: The first is to add othercomponents to the panel (i.e. to use it as container) and the second is to drawsomething on the panel (i.e. use it as a drawing surface or canvas). Both ofthese techniques are illustrated in the sample program. You will find two JPan-els in the program: content, which is used to contain other components, anddisplayPanel, which is used as a drawing surface.

Let’s look more closely at displayPanel. We use it as a drawing surface sowe need to customize the JPanel. We do this by creating a subclass of the JPanelclass. We could have made a standalone subclass but in this example we chose tomake a static nested class inside the HelloWorldGUI2 class. The HelloWorldDisplayclass defines just one instance method, paintComponent(), which overrides themethod of the same name in the JPanel class:

private static class HelloWorldDisplay extends JPanel {

public void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawString( "Hello World!", 20, 30 );

}

}

128

Page 136: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. The Basic GUI Application

The paintComponent() method is called by the system when a component needsto be painted on the screen. A component needs to be painted when it is first dis-played, when it is moved by the user and when it is made visible by e.g. clickingon it. In the JPanel class, the paintComponent method simply fills the panel withthe panel’s background color. The paintComponent() method in HelloWorld-Display begins by calling super.paintComponent(g). This first calls the versionof paintComponent() that is defined in the superclass, JPanel. The superclassmethod does not do much–it fills the panel with the background color. Whenthe paintComponent method is called, it is passed a Graphics object. Every GUIcomponent has a Graphics object associated with it. This object is used to docustom painting on the component. Here, we use g.drawString() to paint thestring “Hello World!” onto the panel. The net result is that whenever a Hel-loWorldDisplay is shown on the screen, it displays the string “Hello World!”.

We will often use JPanels in this way, as drawing surfaces. Usually, whenwe do this, we will define a class that is a subclass of JPanel and we will write apaintComponent method in that class to draw the desired content in the panel.The subclass of JPanel can be defined either as a separate class in its own file oras a nested class.

Components and Layout

Another way of using a JPanel is as a container to hold other components. Javahas many classes that define GUI components. Except for top-level componentslike windows, components must be added to a container before they can appearon the screen. In the sample program, the variable named content refers toa JPanel that is used as a container. Two other components are added to thatcontainer:

content.add(displayPanel, BorderLayout.CENTER);

content.add(okButton, BorderLayout.SOUTH);

The first component that is added to content (recall that content is a JPanel) isdisplayPanel which, as discussed above, displays the message, “Hello World!”.The second is okButton which represents the button that the user clicks to closethe window. The variable okButton is of type JButton, the Java class that repre-sents push buttons.

The “BorderLayout” stuff in these statements has to do with how the twocomponents are arranged in the container. When components are added to acontainer, there has to be some way of deciding how those components are ar-ranged inside the container. This is called “laying out” the components in thecontainer, and the most common technique for laying out components is to usea layout manager. A layout manager is an object that implements some pol-icy for how to arrange the components in a container; different types of layout

129

Page 137: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

manager implement different policies. One type of layout manager is defined bythe BorderLayout class. In the program, the statement content.setLayout(newBorderLayout()); creates a new BorderLayout object and tells the content panelto use the new object as its layout manager. By adding okButton in the BorderLayout.SOUTHposition puts the button at the bottom of the panel, and putting displayPanel

in the BorderLayout.CENTER position makes it fill any space that is not taken upby the button.

This example shows a general technique for setting up a GUI: Create a con-tainer and assign a layout manager to it, create components and add them tothe container, and either add the container to the content pane of the windowor use the container as the content pane of a window. A container is itself acomponent, so it is possible that some of the components that are added to thetop-level container are themselves containers, with their own layout managersand components. This makes it possible to build up complex user interfaces in ahierarchical fashion, with containers inside containers inside containers….

Events and Listeners

Thus far we have seen the structure of containers and components that sets upthe physical appearance of a GUI. We consider now the behavior of the GUI i.e.what can the user do to the GUI and how will it respond? GUIs are largely event-driven; that is, the program waits for and responds to event that are generated bythe user’s actions. It is impossible to predict in advance the sequence of eventsthat could occur at the interface. Thus, an event-driven model is used to programGUIs. In this model, you write event-handling methods to respond to the eventsthat you are interested in. When an event occurs, the system finds and executesthe appropriate event-handling method .

There are at least three objects involved in event handling: the componenton which the event occurs (e.g. a button), the event itself, and the event-handler.The most common technique for handling events in Java is to use event listeners.A listener is an object that includes one or more event-handling methods. Whenan event is detected or generated by the component, such as a button or menu,the listener object (i.e. the event handler) is notified and responds by runningthe appropriate event-handling method. The event itself (the third object) carriesinformation about the type of event, when it occurred, and so on. This divisionof responsibilities makes it easier to organize large programs.

130

Page 138: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. The Basic GUI Application

OK

Visible button,

represented by

a JButton object

User clicks button,

button generates event

instanceof ButtonHandler

actionPerformed()

Event-handling object

of type ActionListener

responds to the event

ActionEvent object is

sent to listener

As an example, consider the OK button in the sample program. When theuser clicks the button, an event is generated. This event is represented by anobject belonging to the class ActionEvent. The event that is generated is associatedwith the button; we say that the button is the source of the event. The listenerobject in this case is an object belonging to the class ButtonHandler, which isdefined as a nested class inside HelloWorldGUI2:

private static class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

}

This class implements the ActionListener interface—a requirement for listenerobjects that handle events from buttons. The event-handling method is namedactionPerformed, as specified by the ActionListener interface. This method con-tains the code that is executed when the user clicks the button; in this case, thecode is simply a call to System.exit(), which will terminate the program.

There is one more ingredient that is necessary to get the event from the buttonto the listener object: The listener object must register itself with the button asan event listener. This is done with the statement:

okButton.addActionListener(listener);

This statement tells okButton that when the user clicks the button, the Action-Event that is generated should be sent to listener. Without this statement, thebutton has no way of knowing that there is something that would like to listenfor events from the button.

This example shows a general technique for programming the behavior ofa GUI: Write classes that include event-handling methods. Create objects thatbelong to these classes and register them as listeners with the objects that willactually detect or generate the events. When an event occurs, the listener isnotified, and the code that you wrote in one of its event-handling methods isexecuted. At first, this might seem like a very roundabout and complicated wayto get things done, but as you gain experience with it, you will find that it is veryflexible and that it goes together very well with object oriented programming.

131

Page 139: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

This section has introduced some of the fundamentals of GUI programming.We will spend the rest of the chapter exploring them in more detail.

5.2 Graphics and Painting

Everything you see on a computer screen has to be drawn there, even thetext. The Java API includes a range of classes and methods that are devoted

to drawing.The physical structure of a GUI is built of components. The term compo-

nent refers to a visual element in a GUI, including buttons, menus, text-inputboxes, scroll bars, check boxes, and so on. In Java, GUI components are repre-sented by objects belonging to subclasses of the class java.awt.Component. Mostcomponents in the Swing GUI toolkit—although not top-level components likeJFrame—belong to subclasses of the class javax.swing.JComponent, which is it-self a subclass of java.awt.Component. Every component is responsible for draw-ing itself. If you want to use a standard component, you only have to add it toyour program. You don’t have to worry about painting it on the screen. Thatwill happen automatically, since it already knows how to draw itself.

Sometimes, however, you do want to draw on a component. You will haveto do this whenever you want to display something that is not included amongthe standard, pre-defined component classes. When you want to do this, youhave to define your own component class and provide a method in that classfor drawing the component. We will use a subclass of JPanel when we need adrawing surface of this kind, as we did for the HelloWorldDisplay class in the ex-ample HelloWorldGUI2.java in the previous section. A JPanel, like any JCompo-nent, draws its content in the method public void paintComponent(Graphics

g). To create a drawing surface, define a subclass of JPanel and override thepaintComponent() method with custom drawing code. Then, create an objectof this class and use it in your program. When the component has to be drawnon the screen, the system will call its paintComponent() to do the drawing. Thatis, the code that you put into the paintComponent() method will be executedwhenever the panel needs to be drawn on the screen; by writing this method,you determine the picture that will be displayed in the panel. You do not usu-ally call a paintComponent() method–the system calls the method. You write themethod to say what will happen when the system calls it.

The paintComponent() method has a parameter of type Graphics. The Graph-ics object will be provided by the system when it calls your method. You need thisobject to do the actual drawing. Every component you create will have its ownGraphics object associated with it. This object is called the graphics context ofthat component. The Graphics object keeps track of foreground and backgroundcolors used to paint the component, fonts used to draw the strings and thickness

132

Page 140: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Graphics and Painting

of lines used to draw shapes, amongst others. Instance methods are provided inthis class for drawing shapes, text, and images. Any given Graphics object candraw to only one location. In this chapter, that location will always be a GUIcomponent belonging to some subclass of JPanel.

The Graphics class is an abstract class, which means that it is impossible tocreate a graphics context directly, with a constructor. There are actually twoways to get a graphics context for drawing on a component: First, when thepaintComponent() method of a component is called by the system, the parame-ter to that method is a graphics context for drawing on the component. Second,every component has an instance method called getGraphics() that returns agraphics context that can be used for drawing on the component outside itspaintComponent() method. It is a good idea to dispose of the graphics whenyou are done. This is done by calling the dispose(), for e.g. if g is a graphicscontext created with getGraphics(),then you call g.dispose() when finishedusing it. This releases any operating system resources that might be held by g.

The paintComponent() method in the JPanel class simply fills the panel withthe panel’s background color. When defining a subclass of JPanel for use as adrawing surface, you will usually want to fill the panel with the background colorbefore drawing other content onto the panel. This is traditionally done with acall to super.paintComponent(g), so most paintComponent() methods that youwrite will have the form:

public void paintComponent(g) {

super.paintComponent(g);

. . . // Draw the content of the component.

}

A component should do all drawing operations in its paintComponent()method.The run-time system calls paintComponent() to paint the component on thescreen. It is called when the component first appears on the screen or when thesize of the component changes, which can happen when the user resizes the win-dow that contains the component or minimizes or maximizes the window orwhen the window is uncovered etc.

Programmers should not call paintComponent() directly because the pro-grammer does not know the state of the window at each time–it may be mini-mized for example. The programmer can request that a component be repaintedby calling the component’s repaint() method. The method public void

repaint(); is defined in the Component class, and so can be used with any com-ponent. You should call repaint() to inform the system that the componentneeds to be redrawn. It is important to understand that the repaint() methodreturns immediately, without doing any painting itself. The system will call thecomponent’s paintComponent() method later, as soon as it gets a chance to doso, after processing other pending events if there are any. It is even possible that

133

Page 141: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

many calls to repaint() will all be handled by one call to paintComponent(), ifthe calls to repaint() occur in a very short timespan.

The paintComponent() should be capable of redrawing the content of thecomponent on demand. This means that, to work properly, the paintComponent()method must be smart enough to correctly redraw the component at any time.To make this possible, a program should store data in its instance variables aboutthe state of the component. These variables should contain all the informationnecessary to redraw the component completely. The paintComponent() methodshould use the data in these variables to decide what to draw. When the programwants to change the content of the component, it should not simply draw the newcontent. It should change the values of the relevant variables and call repaint().When the system calls paintComponent(), that method will use the new valuesof the variables and will draw the component with the desired modifications.

Coordinates

The screen of a computer is a grid of little squares called pixels. The color of eachpixel can be set individually, and drawing on the screen just means setting thecolors of individual pixels.

y = 0

x = width

y = height

x = 0

A graphics context draws in a rectangle made up of pixels. A position inthe rectangle is specified by a pair of integer coordinates, (x,y). The upper leftcorner has coordinates (0,0). The x coordinate increases from left to right, andthe y coordinate increases from top to bottom. The illustration shows a 20-pixelby 12-pixel component (with very large pixels). A small line, rectangle, and ovalare shown as they would be drawn by coloring individual pixels.

For any component, you can find out the size of the rectangle that it occupiesby calling the instance methods getWidth() and getHeight(), which return thenumber of pixels in the horizontal and vertical directions, respectively. In general,it’s not a good idea to assume that you know the size of a component, since thesize is often set by a layout manager and can change if the component is resized

134

Page 142: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Graphics and Painting

by the user. This means that it’s a good idea to check the size of a componentbefore doing any drawing. For example:

public void paintComponent(Graphics g) {

super.paintComponent(g);

int width = getWidth(); // Find out the width of this

component.

int height = getHeight(); // Find out its height.

. . . // Draw the content of the component.

}

Your drawing commands will have to take the size into account. That is, theywill have to use (x,y) coordinates that are calculated based on the actual heightand width of the component. (However, if you are sure that you know the size,using constants for the width and height can make the drawing easier.)

Colors

There are many ways to represent colors on a computer. We will use the simpleRGB color system. An RGB color is specified by three numbers that give the levelof red, green, and blue, respectively, in the color. A color in Java is an object ofthe class, java.awt.Color. You can construct a new color by specifying its red,blue, and green components. For example,

Color myColor = new Color(r,g,b);

where r, g, and b are integers in the range 0 to 255. The Color class defines sev-eral named constants representing common colors: Color.WHITE, Color.BLACK,Color.RED, Color.GREEN, Color.BLUE, Color.CYAN, Color.MAGENTA, Color.YELLOW,Color.PINK, Color.ORANGE, Color.LIGHT_GRAY, Color.GRAY, and Color.DARK_GRAY.

An alternative to RGB is the HSB color system. In the HSB system, a coloris specified by three numbers called the hue, the saturation, and the brightness.

The RGB system and the HSB system are just different ways of describing thesame set of colors. It is possible to translate between one system and the other.

One of the properties (fields) of a Graphics object is the current drawingcolor, which is used for all drawing of shapes and text. If g is a graphics con-text, you can change its current drawing color with g.setColor(c), where c

is a Color. For example, if you want to draw in green, you would just sayg.setColor(Color.GREEN) before doing the drawing. The graphics context con-tinues to use the color until you explicitly change it with another setColor()

command. If you want to know what the current drawing color is, you cancall the function g.getColor(), which returns an object of type Color. This canbe useful if you want to change to another drawing color temporarily and thenrestore the previous drawing color.

Every component has an associated foreground color and background color.Generally, the component is filled with the background color before anything

135

Page 143: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

else is drawn. When a new graphics context is created for a component, thecurrent drawing color is set to the foreground color. Note that the foregroundcolor and background color are properties of the component, not of a graphicscontext.

The foreground and background colors of a component can be set by callinginstance methods:component.setForeground(color) andcomponent.setBackground(color),which are defined in the Component class and therefore are available for use withany component. This can be useful even for standard components, if you wantthem to use colors that are different from the defaults.

Fonts

A font represents a particular size and style of text. The same character will appeardifferent in different fonts. In Java, a font is characterized by a font name, a style,and a size. The available font names are system dependent, but you can alwaysuse the following four strings as font names: “Serif ”, “SansSerif ”, “Monospaced”,and “Dialog”. (A “serif ” is a little decoration on a character, such as a shorthorizontal line at the bottom of the letter i. “SansSerif ” means “without serifs.”“Monospaced” means that all the characters in the font have the same width. The“Dialog” font is the one that is typically used in dialog boxes.)

The style of a font is specified using named constants that are defined in theFont class. You can specify the style as one of the four values:

• Font.PLAIN,• Font.ITALIC,• Font.BOLD, or• Font.BOLD + Font.ITALIC.

The size of a font is an integer. Size typically ranges from about 9 to 36,although larger sizes can also be used. The default size is 12.

Java uses the class named java.awt.Font for representing fonts. You canconstruct a new font by specifying its font name, style, and size in a constructor:

Font plainFont = new Font("Serif", Font.PLAIN, 12);

Font bigBoldFont = new Font("SansSerif", Font.BOLD, 24);

Every graphics context has a current font, which is used for drawing text. Youcan change the current font with the setFont() method. For example, if g is agraphics context and bigBoldFont is a font, then the command g.setFont(bigBoldFont)

will set the current font of g to bigBoldFont. The new font will be used for anytext that is drawn after the setFont() command is given. You can find out the

136

Page 144: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Graphics and Painting

current font of g by calling the method g.getFont(), which returns an object oftype Font.

Every component also has an associated font. It can be set with the instancemethod component.setFont(font), which is defined in the Component class.When a graphics context is created for drawing on a component, the graphiccontext’s current font is set equal to the font of the component.

Shapes

The Graphics class includes a large number of instance methods for drawing var-ious shapes, such as lines, rectangles, and ovals. The shapes are specified usingthe (x,y) coordinate system described above. They are drawn in the currentdrawing color of the graphics context. The current drawing color is set to theforeground color of the component when the graphics context is created, but itcan be changed at any time using the setColor() method.

Some of the drawing methods are listed here. More details may be found inthe documentation With all these commands, any drawing that is done outsidethe boundaries of the component is ignored. Note that all these methods are inthe Graphics class, so they all must be called through an object of type Graphics.It is shown here as g, but of course the name of the graphics context is up to theprogrammer.

• g.drawString(String str, int x, int y)— Draws the text given by thestring str. The string is drawn using the current color and font of thegraphics context. x specifies the x-coordinate of the left end of the string. yis the y-coordinate of the baseline of the string. The baseline is a horizontalline on which the characters rest. Some parts of the characters, such as thetail on a y or g, extend below the baseline.

• g.drawLine(int x1, int y1, int x2, int y2) — Draws a line from thepoint (x1,y1) to the point (x2,y2). The line is drawn as if with a penthat extends one pixel to the right and one pixel down from the (x,y)

point where the pen is located. For example, if g refers to an object of typeGraphics, then the command g.drawLine(x,y,x,y), which corresponds toputting the pen down at a point, colors the single pixel with upper leftcorner at the point (x,y). Remember that coordinates really refer to thelines between the pixels.

• g.drawRect(int x, int y, int width, int height) — Draws the out-line of a rectangle. The upper left corner is at (x,y), and the width andheight of the rectangle are as specified. If width equals height, then therectangle is a square. If the width or the height is negative, then noth-ing is drawn. The rectangle is drawn with the same pen that is used fordrawLine(). This means that the actual width of the rectangle as drawn

137

Page 145: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

is width+1, and similarly for the height. There is an extra pixel along theright edge and the bottom edge. For example, if you want to draw a rect-angle around the edges of the component, you can say “g.drawRect(0, 0,

getWidth()-1, getHeight()-1);”. If you use “g.drawRect(0, 0, getWidth(),

getHeight());”, then the right and bottom edges of the rectangle will bedrawn outside the component and will not appear on the screen.

• g.drawOval(int x, int y, int width, int height) — Draws the out-line of an oval. The oval is one that just fits inside the rectangle specified byx, y, width, and height. If width equals height, the oval is a circle.

• g.drawRoundRect(int x, int y, int width, int height, int xdiam,

int ydiam) — Draws the outline of a rectangle with rounded corners. Thebasic rectangle is specified by x, y, width, and height, but the corners arerounded. The degree of rounding is given by xdiam and ydiam. The cornersare arcs of an ellipse with horizontal diameter xdiam and vertical diameterydiam. A typical value for xdiam and ydiam is 16, but the value used shouldreally depend on how big the rectangle is.

• g.draw3DRect(int x, int y, int width, int height, boolean raised)

— Draws the outline of a rectangle that is supposed to have a three-dimensionaleffect, as if it is raised from the screen or pushed into the screen. The ba-sic rectangle is specified by x, y, width, and height. The raised parametertells whether the rectangle seems to be raised from the screen or pushed intoit. The 3D effect is achieved by using brighter and darker versions of thedrawing color for different edges of the rectangle. The documentation rec-ommends setting the drawing color equal to the background color beforeusing this method. The effect won’t work well for some colors.

• g.drawArc(int x, int y, int width, int height, int startAngle, int

arcAngle) — Draws part of the oval that just fits inside the rectangle spec-ified by x, y, width, and height. The part drawn is an arc that extendsarcAngle degrees from a starting angle at startAngle degrees. Angles aremeasured with 0 degrees at the 3 o’clock position (the positive direction ofthe horizontal axis). Positive angles are measured counterclockwise fromzero, and negative angles are measured clockwise. To get an arc of a circle,make sure that width is equal to height.

• g.fillRect(int x, int y, int width, int height) — Draws a filled-in rectangle. This fills in the interior of the rectangle that would be drawnby drawRect(x,y,width,height). The extra pixel along the bottom andright edges is not included. The width and height parameters give the ex-act width and height of the rectangle. For example, if you wanted to fillin the entire component, you could say “g.fillRect(0, 0, getWidth(),

getHeight());”

138

Page 146: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Graphics and Painting

• g.fillOval(int x, int y, int width, int height) — Draws a filled-in oval.

• g.fillRoundRect(int x, int y, int width, int height, int xdiam,

int ydiam) — Draws a filled-in rounded rectangle.• g.fill3DRect(int x, int y, int width, int height, boolean raised)

— Draws a filled-in three-dimensional rectangle.• g.fillArc(int x, int y, int width, int height, int startAngle, int

arcAngle) — Draw a filled-in arc. This looks like a wedge of pie, whosecrust is the arc that would be drawn by the drawArc method.

Graphics2D

All drawing in Java is done through an object of type Graphics. The Graphicsclass provides basic commands for such things as drawing shapes and text andfor selecting a drawing color. These commands are adequate in many cases, butthey fall far short of what’s needed in a serious computer graphics program. Javahas another class, Graphics2D, that provides a larger set of drawing operations.Graphics2D is a sub-class of Graphics, so all the methods from the Graphics classare also available in a Graphics2D.

The paintComponent() method of a JComponent gives you a graphics contextof type Graphics that you can use for drawing on the component. In fact, thegraphics context actually belongs to the sub-class Graphics2D, and can be type-cast to gain access to the advanced Graphics2D drawing methods:

public void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2;

g2 = (Graphics2D)g;

.

. // Draw on the component using g2.

.

}

One use of the Graphics2D class is turn on antialiasing. If g2 is a variable oftype Graphics2D, as in the paintComponent() method above, then the command

g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON

);

turns on antialiasing in the graphics context. Aliasing is a jagged appearance thatcan be seen when shapes are drawn using pixels. Antialiasing tries to reduce thejaggedness. It can make diagonal lines and the outlines of ovals look much nicer.It can also improve the appearance of text.

139

Page 147: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

Another useful command is g2.setStroke( new BasicStroke(lineWidth));

where lineWidth is an integer or a float. This command can be use to drawthicker lines. Lines drawn after the command will be lineWidth pixels wide.This also affects the thickness of the outlined shapes drawn by methods such asg.drawRect and g.drawOval().

An Example

Let’s use some of the material covered in this section to write a subclass of JPanelfor use as a drawing surface. All the drawing will be done in the paintComponent()method of the panel class. The panel will draw multiple copies of a message on ablack background. Each copy of the message is in a random color. Five differentfonts are used, with different sizes and styles. The message can be specified in theconstructor; if the default constructor is used, the message is the string “Java!”.

There is one problem with the way this class works. When the panel’spaintComponent() method is called, it chooses random colors, fonts, and loca-tions for the messages. The information about which colors, fonts, and locationsare used is not stored anywhere. The next time paintComponent() is called, it willmake different random choices and will draw a different picture. If you resize awindow containing the panel, the picture will be continually redrawn as the sizeof the window is changed! To avoid that, you would store enough informationabout the picture in instance variables to enable the paintComponent() methodto draw the same picture each time it is called.

The source code for the panel class is shown below. I use an instance variablecalled message to hold the message that the panel will display. There are five in-stance variables of type Font that represent different sizes and styles of text. Thesevariables are initialized in the constructor and are used in the paintComponent()

method.The paintComponent() method for the panel simply draws 25 copies of the

message. For each copy, it chooses one of the five fonts at random, and it usesg.setFont() to select that font for drawing. It creates a random HSB color anduses g.setColor() to select that color for drawing. It then chooses random (x,y)

coordinates for the location of the message. The x coordinate gives the horizontalposition of the left end of the string. The formula used for the x coordinate is“-50~+ (int)(Math.random()* (width+40))”. This gives a random integer inthe range from -50 to width-10. This makes it possible for the string to extendbeyond the left edge or the right edge of the panel. Similarly, the formula for yallows the string to extend beyond the top and bottom.

140

Page 148: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Graphics and Painting

Here is the complete source code for the RandomStringsPanel:

Listing .: Random Strings.

import java.awt.∗;

import javax.swing.JPanel;

/∗ ∗

∗ This panel di sp l ay s 25 copies of a message . The color ,

∗ po si t io n and font of each message is se le c te d at random .

∗/

public class RandomStringsPanel extends JPanel {

private String message;// Message to be d i s p l a y e d . Can be set in

// the constructor , if not then the string

// " Java !" is used .

private Font font1, font2, font3, font4, font5; // The five fonts .

/∗ ∗

∗ Default c o n s t r u c t o r creates a panel that di sp l ay s the message " Java !".

∗/

public RandomStringsPanel() {

this(null); // Call the other constructor , with p a r a m e t e r null .

}

/∗ ∗

∗ C o n s t r u c t o r : creates a panel to display 25 copies of message .

∗ @param m e s s a g e S t r i n g The message to be d i s p l a y e d . If this is null ,

∗ then the default message " Java !" is d i s p l a y e d .

∗/

public RandomStringsPanel(String messageString) {

message = messageString;

if (message == null)

message = "Java!";

font1 = new Font("Serif", Font.BOLD, 14);

font2 = new Font("SansSerif", Font.BOLD + Font.ITALIC, 24);

font3 = new Font("Monospaced", Font.PLAIN, 30);

font4 = new Font("Dialog", Font.PLAIN, 36);

font5 = new Font("Serif", Font.ITALIC, 48);

setBackground(Color.BLACK);

}

141

Page 149: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

Listing .: Random Strings contd.

/∗ ∗

∗ The p a i n t C o m p o n e n t method is r e s p o n s i b l e for drawing the content

∗ of the panel . It draws 25 copies of the message string , using a

∗ random color , font , and p o s i t io n for each string .

∗/

public void paintComponent(Graphics g) {

super.paintComponent(g);// Call the p a i n t C o m p o n e n t method from the

// superclass , JPanel . This simply fills

// entire panel with the b a c k g r o u n d color , black .

Graphics2D g2 = (Graphics2D)g; // ( To make the text sm oo t he r .)

g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON );

int width = getWidth();

int height = getHeight();

for (int i = 0; i \< 25; i++) {

// Draw one string . First , set the font to be one of the five

// a v a i l a b l e fonts , at random .

int fontNum = (int)(5∗Math.random()) + 1;

switch (fontNum) {

case 1:

g.setFont(font1);

break;

case 2:

g.setFont(font2);

break;

case 3:

g.setFont(font3);

break;

case 4:

g.setFont(font4);

break;

case 5:

g.setFont(font5);

break;

} // end switch

// Set the color to a bright , s a t u r a t e d color , with random hue .

142

Page 150: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mouse Events

float hue = (float)Math.random();

g.setColor( Color.getHSBColor(hue, 1.0F, 1.0F) );

// Select the po si t i o n of the string , at random .

int x,y;

x = −50 + (int)(Math.random()∗(width+40));

y = (int)(Math.random()∗(height+20));

// Draw the message .

g.drawString(message,x,y);

} // end for

} // end p a i n t C o m p o n e n t ()

} // end class R a n d o m S t r i n g s P a n e l

To complete the program, here is a class with a main() method that createsan instance of RandomStringsPanel and adds it to a frame (see Listing 5.4).

Listing .: Random Strings Frame.

import javax.swing.JFrame;

public class RandomStrings {

public static void main(String[] args) {

JFrame window = new JFrame("Java!");

RandomStringsPanel content = new RandomStringsPanel();

window.setContentPane(content);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setLocation(120,70);

window.setSize(350,250);

window.setVisible(true);

}

}

5.3 Mouse Events

Events are central to programming for a graphical user interface. A GUIprogram doesn’t have a main() routine that outlines what will happen when

the program is run, in a step-by-step process from beginning to end. Instead, theprogram must be prepared to respond to various kinds of events that can happenat unpredictable times and in an order that the program doesn’t control. The

143

Page 151: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

most basic kinds of events are generated by the mouse and keyboard. The usercan press any key on the keyboard, move the mouse, or press a button on themouse. The user can do any of these things at any time, and the computer hasto respond appropriately.

In Java, events are represented by objects. When an event occurs, the sys-tem collects all the information relevant to the event and constructs an objectto contain that information. Different types of events are represented by objectsbelonging to different classes. For example, when the user presses one of the but-tons on a mouse, an object belonging to a class called MouseEvent is constructed.The object contains information such as the source of the event (that is, the com-ponent on which the user clicked), the (x,y) coordinates of the point on thecomponent where the click occurred, the exact time of the click, and which but-ton on the mouse was pressed. When the user presses a key on the keyboard,a KeyEvent is created. After the event object is constructed, it can be passed asa parameter to a designated method. By writing that method, the programmersays what should happen when the event occurs.

There is a lot of processing that goes on between the time that the user pressesa key or moves the mouse and the time that a method in your program is calledto respond to the event. Fortunately, you don’t need to know much about thatprocessing. Every GUI program executes a loop, called an event loop of the form

while the program is still running:

Wait for the next event to occur

Call a method to handle the event

In Java, we do not explicitly program the loop–it’s part of “the system.” In thissection, we’ll look at handling mouse events in Java, and we’ll cover the frame-work for handling events in general. The next section will cover keyboard-relatedevents and timer events. Java also has other types of events, which are producedby GUI components. These will be introduced in Section 5.4.

Event Handling

For an event to have any effect, a program must detect the event and react to it.In order to detect an event, the program must “listen” for it. Listening for eventsis something that is done by an object called an event listener. An event listenerobject must contain instance methods for handling the events for which it listens.For example, if an object is to serve as a listener for events of type MouseEvent,then it must contain the following method (among several others):

public void mousePressed(MouseEvent evt){ . . . }

The body of the method defines how the object responds when it is notified that amouse button has been pressed. The parameter, evt, contains information aboutthe event. This information can be used by the listener object to determine itsresponse.

144

Page 152: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mouse Events

The methods that are required in a mouse event listener are specified in aninterface namedMouseListener. To be used as a listener for mouse events, a classmust implement this MouseListener interface. (To review briefly: An interface

in Java is just a list of instance methods. A class can “implement” an inter-face by doing two things: First, the class must be declared to implement theinterface, as in “class MouseHandler implements MouseListener” or “classMyPanel extends JPanel implements MouseListener”; and second, the classmust include a definition for each instance method specified in the interface.An interface can be used as the type for a variable or formal parameter. Notethat it is not enough for the class to include the specified methods–it must bespecifically declared to implement the interface.)

Many events in Java are associated with GUI components. For example,when the user presses a button on the mouse, the associated component is the onethat the user clicked on. Before a listener object can “hear” events associated witha given component, the listener object must be registered with the component.If a MouseListener object, mListener, needs to hear mouse events associated witha Component object, comp, the listener must be registered with the componentby calling

comp.addMouseListener(mListener);

The addMouseListener() method is an instance method in class Component, andso can be used with any GUI component object. In our first few examples, wewill listen for events on a JPanel that is being used as a drawing surface.

The event classes, such as MouseEvent, and the listener interfaces, such asMouseListener, are defined in the package java.awt.event. This means thatif you want to work with events, you should either include the line “importjava.awt.event.*;” at the beginning of your source code file or import the in-dividual classes and interfaces.

To summarize, you must

1. Import the necessary classes and interfaces with “import java.awt.event.*;”(or individual imports) at the beginning of your source code;

2. Declare that some class implements the appropriate listener interface, suchas MouseListener;

3. Provide definitions in that class for the methods specified by the interface;4. Register an object of the listener class with the component that will gen-

erate the events by calling a method such as addMouseListener() in thecomponent.

Any object can act as an event listener, provided that it implements the ap-propriate interface. A component can listen for the events that it itself generates.A panel can listen for events from components that are contained in the panel.A special class can be created just for the purpose of defining a listening object.

145

Page 153: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

It is a good idea to use anonymous inner classes to define listening objects, andnamed nested classes can also be appropriate. You will see all of these patterns inexamples in this textbook.

MouseEvent and MouseListener

The MouseListener interface specifies these five instance methods:public void mousePressed(MouseEvent evt);

public void mouseReleased(MouseEvent evt);

public void mouseClicked(MouseEvent evt);

public void mouseEntered(MouseEvent evt);

public void mouseExited(MouseEvent evt);

The mousePressed method is called as soon as the user presses down on one ofthe mouse buttons, and mouseReleased is called when the user releases a button.These are the two methods that are most commonly used, but any mouse listenerobject must define all five methods; you can leave the body of a method emptyif you don’t want to define a response. The mouseClicked method is called ifthe user presses a mouse button and then releases it, without moving the mouse.(When the user does this, all three routines—mousePressed, mouseReleased, andmouseClicked—will be called in that order.) In most cases, you should definemousePressed instead of mouseClicked. The mouseEntered and mouseExited

methods are called when the mouse cursor enters or leaves the component. Forexample, if you want the component to change appearance whenever the usermoves the mouse over the component, you could define these two methods.

As a first example, we will look at a small addition to the RandomStringsPanelexample from the previous section. In the new version, the panel will repaintitself when the user clicks on it. In order for this to happen, a mouse listenershould listen for mouse events on the panel, and when the listener detects amousePressed event, it should respond by calling the repaint() method of thepanel.

For the new version of the program, we need an object that implements theMouseListener interface. One way is to define a separate class, as in Listing 5.5.This class does three of the four things that we need to do in order to han-dle mouse events: First, it imports java.awt.event.* for easy access to event-related classes. Second, it is declared that the class “implements MouseListener”.And third, it provides definitions for the five methods that are specified in theMouseListener interface. (Note that four of the methods have empty bodies, sincewe don’t want to do anything in response to those events.)

We must do one more thing to set up the event handling for this example:We must register an event-handling object as a listener with the component thatwill generate the events. In this case, the mouse events that we are interested inwill be generated by an object of type RandomStringsPanel. If panel is a variable

146

Page 154: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mouse Events

Listing .: RepaintOnClick.java

import java.awt.Component;

import java.awt.event.∗;

/∗ ∗

∗ An object of type R e p a i n t O n C l i c k is a M o u s e L i s t e n e r that

∗ will respond to a m o u s e P r e s s e d event by calling the repaint ()

∗ method of the source of the event . That is , a R e p a i n t O n C l i c k

∗ object can be added as a mouse l i st e ne r to any C o m p o n e n t ;

∗ when the user clicks that component , the c o m p o n e n t will be

∗ r e p a i n t e d .

∗/

public class RepaintOnClick implements MouseListener {

public void mousePressed(MouseEvent evt) {

Component source = (Component)evt.getSource();

source.repaint(); // Call repaint () on the C o m p o n e n t that was

clicked .

}

public void mouseClicked(MouseEvent evt) { }

public void mouseReleased(MouseEvent evt) { }

public void mouseEntered(MouseEvent evt) { }

public void mouseExited(MouseEvent evt) { }

}

that refers to the panel object, we can create a mouse listener object and registerit with the panel with the statements:

RepaintOnClick listener = new RepaintOnClick(); // Create

MouseListener object.

panel.addMouseListener(listener); // Register MouseListener

with the panel.

This could be done, for example, in the main() routine where the panel is created.Once the listener has been registered in this way, it will be notified of mouseevents on the panel. When a mousePressed event occurs, the mousePressed()

method in the listenerwill be called. The code in this method calls the repaint()method in the component that is the source of the event, that is, in the panel.The result is that the RandomStringsPanel is repainted with its strings in newrandom colors, fonts, and positions.

147

Page 155: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

Although we have written the RepaintOnClick class for use with our Random-StringsPanel example, the event-handling class contains no reference at all tothe RandomStringsPanel class. How can this be? The mousePressed() methodin class RepaintOnClick looks at the source of the event, and calls its repaint()method. If we have registered the RepaintOnClick object as a listener on a Ran-domStringsPanel, then it is that panel that is repainted. But the listener objectcould be used with any type of component, and it would work in the same way.

Similarly, the RandomStringsPanel class contains no reference to the RepaintOnClickclass—in fact, RandomStringsPanel was written before we even knew anythingabout mouse events! The panel will send mouse events to any object that hasregistered with it as a mouse listener. It does not need to know anything aboutthat object except that it is capable of receiving mouse events.

The relationship between an object that generates an event and an object thatresponds to that event is rather loose. The relationship is set up by registering oneobject to listen for events from the other object. This is something that can po-tentially be done from outside both objects. Each object can be developed inde-pendently, with no knowledge of the internal operation of the other object. Thisis the essence of modular design: Build a complex system out of modules thatinteract only in straightforward, easy to understand ways. Then each module is aseparate design problem that can be tackled independently. Java’s event-handlingframework is designed to offer strong support for modular design.

To make this clearer, let’s look at a new version of RandomStrings.java, theprogram from Subsection ?? that uses RandomStringsPanel. The new version isClickableRandomStrings.java. For convenience, I have added RepaintOnClick as astatic nested class, although it would work just as well as a separate class:

import java.awt.Component;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JFrame;

/**

* Displays a window that shows 25 copies of the string "Java!"

in

* random colors, fonts, and positions. The content of the

window

* is an object of type RandomStringsPanel. When the user clicks

* the window, the content of the window is repainted, with the

* strings in newly selected random colors, fonts, and positions.

*/

public class ClickableRandomStrings {

public static void main(String[] args) {

JFrame window = new JFrame("Click Me to Redraw!");

148

Page 156: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mouse Events

RandomStringsPanel content = new RandomStringsPanel();

content.addMouseListener( new RepaintOnClick() );

window.setContentPane(content);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setLocation(120,70);

window.setSize(350,250);

window.setVisible(true);

}

private static class RepaintOnClick implements MouseListener

{

public void mousePressed(MouseEvent evt) {

Component source = (Component)evt.getSource();

source.repaint();

}

public void mouseClicked(MouseEvent evt) { }

public void mouseReleased(MouseEvent evt) { }

public void mouseEntered(MouseEvent evt) { }

public void mouseExited(MouseEvent evt) { }

}

} end class ClickableRandomStrings

MouseEvent Data

Often, when a mouse event occurs, you want to know the location of the mousecursor. This information is available from the MouseEvent parameter to theevent-handling method, which contains instance methods that return informa-tion about the event. If evt is the parameter, then you can find out the coordi-nates of the mouse cursor by calling evt.getX() and evt.getY(). These methodsreturn integers which give the x and y coordinates where the mouse cursor waspositioned at the time when the event occurred. The coordinates are expressedin the coordinate system of the component that generated the event, where thetop left corner of the component is (0,0).

The user can hold down certain modifier keys while using the mouse. Thepossible modifier keys include: the Shift key, the Control key, the Alt key (calledthe Option key on the Mac), and the Meta key (called the Command or Applekey on the Mac). You might want to respond to a mouse event differently whenthe user is holding down a modifier key. The boolean-valued instance methodsevt.isShiftDown(), evt.isControlDown(), evt.isAltDown(), and evt.isMetaDown()

can be called to test whether the modifier keys are pressed.

149

Page 157: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

You might also want to have different responses depending on whether theuser presses the left mouse button, the middle mouse button, or the right mousebutton. For events triggered by a mouse button, you can determine which buttonwas pressed or released by calling evt.getButton(), which returns one of the inte-ger constants MouseEvent.BUTTON1, MouseEvent.BUTTON2, or MouseEvent.BUTTON3for the left, middle, and right buttons. For events such as mouseEntered andmouseExited that are not triggered by buttons, evt.getButton() returns MouseEvent.NOBUTTON.

As an example, consider a JPanel that does the following: Clicking on thepanel with the left mouse button will place a red rectangle on the panel at thepoint where the mouse was clicked. Clicking with the right mouse button willplace a blue oval on the panel. Holding down the Shift key while clicking willclear the panel by removing all the shapes that have been placed. Here is whatthe panel looks like after some shapes have been added:

There are several ways to write this example. There could be a separate class tohandle mouse events, as in the previous example. However, in this case, I decidedto let the panel itself respond to mouse events. Any object can be a mouse listener,as long as it implements the MouseListener interface. In this case, the panel classimplements the MouseListener interface, so the object that represents the mainpanel of the program can be the mouse listener for the program. The constructorfor the panel class contains the statement

addMouseListener(this);

which is equivalent to saying this.addMouseListener(this). Now, the ordi-nary way to register a mouse listener is to say X.addMouseListener(Y) where Y

is the listener and X is the component that will generate the mouse events. Inthe statement addMouseListener(this), both roles are played by this; that is,“this object” (the panel) is generating mouse events and is also listening for thoseevents. Although this might seem a little strange, you should get used to seeingthings like this. In a large program, however, it’s usually a better idea to write a

150

Page 158: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mouse Events

separate class (a separate stand-alone class or an inner class) to do the listening inorder to have a more organized division of responsibilities.

The source code for the panel class is shown below. I have included a main()

routine to allow the class to be run as a program, as discussed in Subsection ??.You should check how the instance methods in the MouseEvent object are used.You can also check for the Four Steps of Event Handling (“import java.awt.event.*”,“implements MouseListener”, definitions for the event-handling methods, and“addMouseListener”):

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

* A simple demonstration of MouseEvents. Shapes are drawn

* on a black background when the user clicks the panel. If

* the user Shift-clicks, the panel is cleared. If the user

* right-clicks the panel, a blue oval is drawn. Otherwise,

* when the user clicks, a red rectangle is drawn. The contents

of

* the panel are not persistent. For example, they might

disappear

* if the panel is resized.

* This class has a main() routine to allow it to be run as an

application.

*/

public class SimpleStamper extends JPanel implements

MouseListener {

public static void main(String[] args) {

JFrame window = new JFrame("Simple Stamper");

SimpleStamper content = new SimpleStamper();

window.setContentPane(content);

window.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);

window.setLocation(120,70);

window.setSize(450,350);

window.setVisible(true);

}

//

----------------------------------------------------------------------

/**

* This constructor simply sets the background color of the

panel to be black

* and sets the panel to listen for mouse events on itself.

*/

151

Page 159: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

public SimpleStamper() {

setBackground(Color.BLACK);

addMouseListener(this);

}

/**

* Since this panel has been set to listen for mouse events

on itself,

* this method will be called when the user clicks the mouse

on the panel.

* This method is part of the MouseListener interface.

*/

public void mousePressed(MouseEvent evt) {

if ( evt.isShiftDown() ) {

// The user was holding down the Shift key. Just

repaint the panel.

// Since this class does not define a

paintComponent() method, the

// method from the superclass, JPanel, is called.

That method simply

// fills the panel with its background color, which

is black. The

// effect is to clear the panel.

repaint();

return;

}

int x = evt.getX(); // x-coordinate where user clicked.

int y = evt.getY(); // y-coordinate where user clicked.

Graphics g = getGraphics(); // Graphics context for

drawing directly.

// \newcode{NOTE: This is

considered to be bad

style!}

if ( evt.isMetaDown() ) {

// User right-clicked at the point (x,y). Draw a

blue oval centered

// at the point (x,y). (A black outline around the

oval will make it

// more distinct when shapes overlap.)

g.setColor(Color.BLUE); // Blue interior.

g.fillOval( x - 30, y - 15, 60, 30 );

g.setColor(Color.BLACK); // Black outline.

152

Page 160: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mouse Events

g.drawOval( x - 30, y - 15, 60, 30 );

}

else {

// User left-clicked (or middle-clicked) at (x,y).

// Draw a red rectangle centered at (x,y).

g.setColor(Color.RED); // Red interior.

g.fillRect( x - 30, y - 15, 60, 30 );

g.setColor(Color.BLACK); // Black outline.

g.drawRect( x - 30, y - 15, 60, 30 );

}

g.dispose(); // We are finished with the graphics

context, so dispose of it.

} // end mousePressed();

// The next four empty routines are required by the

MouseListener interface.

// They don't do anything in this class, so their definitions

are empty.

public void mouseEntered(MouseEvent evt) { }

public void mouseExited(MouseEvent evt) { }

public void mouseClicked(MouseEvent evt) { }

public void mouseReleased(MouseEvent evt) { }

} // end class SimpleStamper

Note that this class does not draw in a paintComponent() method. The rectan-gles and ovals are drawn directly in the mousePressed() routine. To make thispossible, we obtain a graphics context by saying “g = getGraphics()”. After us-ing g for drawing, g.dispose() is used to inform the operating system that wewill no longer be using g for drawing.

Anonymous Event Handlers and Adapter Classes

It is a fairly common practice to use anonymous inner classes to define listenerobjects. As discussed in Subsection 4.3, a special form of the new operator is usedto create an object that belongs to an anonymous class. For example, a mouselistener object can be created with an expression of the form:

new MouseListener() {

public void mousePressed(MouseEvent evt) { . . . }

public void mouseReleased(MouseEvent evt) { . . . }

public void mouseClicked(MouseEvent evt) { . . . }

public void mouseEntered(MouseEvent evt) { . . . }

153

Page 161: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

public void mouseExited(MouseEvent evt) { . . . }

}

This is one long expression that both defines an unnamed class and creates anobject that belongs to that class. To use the object as a mouse listener, it can bepassed as the parameter to some component’s addMouseListener() method in acommand of the form:

component.addMouseListener( new MouseListener() {

public void mousePressed(MouseEvent evt) { . . . }

public void mouseReleased(MouseEvent evt) { . . . }

public void mouseClicked(MouseEvent evt) { . . . }

public void mouseEntered(MouseEvent evt) { . . . }

public void mouseExited(MouseEvent evt) { . . . }

} );

In a typical application, most of the method definitions in this class will be empty.A class that implements an interface must provide definitions for all the meth-ods in that interface, even if not all methods are required. To avoid the tedium ofwriting empty method definitions in cases like this, Java provides adapter classes.An adapter class implements a listener interface by providing empty definitionsfor all the methods in the interface. An adapter class is useful only as a basis formaking subclasses. In the subclass, you can define just those methods that you ac-tually want to use by overriding the empty methods in the adapter class. For theremaining methods, the empty definitions that are provided by the adapter classwill be used. The adapter class MouseAdapter implements both the MouseLis-tener interface and the MouseMotionListener interface, so it can be used as a basisfor creating a listener for any mouse event. As an example, if you want a mouselistener that only responds to mouse-pressed events, you can use a command ofthe form:

component.addMouseListener( new MouseAdapter() {

public void mousePressed(MouseEvent evt) { . . . }

} );

To see how this works in a real example, let’s write another version of the Click-ableRandomStrings program from Subsection 5.3. This version uses an anony-mous class based on MouseAdapter to handle mouse events:

import java.awt.Component;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class ClickableRandomStrings2 {

public static void main(String[] args) {

154

Page 162: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mouse Events

JFrame window = new JFrame("Random Strings");

RandomStringsPanel content = new RandomStringsPanel();

content.addMouseListener( new MouseAdapter() {

// Register a mouse listener that is defined by an

anonymous subclass

// of MouseAdapter. This replaces the

RepaintOnClick class that was

// used in the original version.

public void mousePressed(MouseEvent evt) {

Component source = (Component)evt.getSource();

source.repaint();

}

} );

window.setContentPane(content);

window.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);

window.setLocation(100,75);

window.setSize(300,240);

window.setVisible(true);

}

}

Anonymous inner classes can be used for other purposes besides event han-dling. For example, suppose that you want to define a subclass of JPanel to rep-resent a drawing surface. The subclass will only be used once. It will redefine thepaintComponent() method, but will make no other changes to JPanel. It mightmake sense to define the subclass as an anonymous inner class. You will see thispattern used in some future examples.

Timers

Not every event is generated by an action by the user. Events can also be gen-erated by objects as part of their regular programming, and these events can

be monitored by other objects so that they can take appropriate actions when theevents occur. One example of this is the class javax.swing.Timer. A Timer gen-erates events at regular intervals. These events can be used to drive an animationor to perform some other task at regular intervals. We will begin this sectionwith a look at timer events and animation. We will then look at another type ofbasic user-generated event: the KeyEvents that are generated when the user typeson the keyboard. The example at the end of the section uses both a timer andkeyboard events to implement a simple game and introduces the important ideaof state machines.

155

Page 163: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

Timers and Animation

An object belonging to the class javax.swing.Timer exists only to generate events.A Timer, by default, generates a sequence of events with a fixed delay betweeneach event and the next. Each event belongs to the class ActionEvent. An objectthat is to listen for the events must implement the interface ActionListener, whichdefines just one method:

public void actionPerformed(ActionEvent evt)

To use a Timer, you must create an object that implements the ActionListenerinterface. That is, the object must belong to a class that is declared to “implementActionListener”, and that class must define the actionPerformedmethod. Then,if the object is set to listen for events from the timer, the code in the listener’sactionPerformed method will be executed every time the timer generates anevent.

Since there is no point to having a timer without having a listener to respondto its events, the action listener for a timer is specified as a parameter in thetimer’s constructor. The time delay between timer events is also specified in theconstructor. If timer is a variable of type Timer, then the statement

timer = new Timer( millisDelay, listener );

creates a timer with a delay of millisDelay milliseconds between events (where1000 milliseconds equal one second). Events from the timer are sent to thelistener. (millisDelay must be of type int, and listener must be of typeActionListener.) The listener’s actionPerfomed() will be executed every time thetimer emits an event. Note that a timer is not guaranteed to deliver events atprecisely regular intervals. If the computer is busy with some other task, anevent might be delayed or even dropped altogether.

A timer does not automatically start generating events when the timer objectis created. The start() method in the timer must be called to tell the timerto start running. The timer’s stop() method can be used to turn the stream ofevents off. It can be restarted later by calling start() again.

One application of timers is computer animation. A computer animation isjust a sequence of still images, presented to the user one after the other. If thetime between images is short, and if the change from one image to another isnot too great, then the user perceives continuous motion. The easiest way to doanimation in Java is to use a Timer to drive the animation. Each time the timergenerates an event, the next frame of the animation is computed and drawn onthe screen—the code that implements this goes in the actionPerformed methodof an object that listens for events from the timer.

Our first example of using a timer is not exactly an animation, but it does dis-play a new image for each timer event. The program shows randomly generatedimages that vaguely resemble works of abstract art. In fact, the program draws anew random image every time its paintComponent() method is called, and the

156

Page 164: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mouse Events

response to a timer event is simply to call repaint(), which in turn triggers acall to paintComponent. The work of the program is done in a subclass of JPanel,which starts like this:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class RandomArtPanel extends JPanel {

/**

* A RepaintAction object calls the repaint method of this

panel each

* time its actionPerformed() method is called. An object of

this

* type is used as an action listener for a Timer that

generates an

* ActionEvent every four seconds. The result is that the

panel is

* redrawn every four seconds.

*/

private class RepaintAction implements ActionListener {

public void actionPerformed(ActionEvent evt) {

repaint(); // Call the repaint() method in the panel

class.

}

}

/**

* The constructor creates a timer with a delay time of four

seconds

* (4000 milliseconds), and with a RepaintAction object as its

* ActionListener. It also starts the timer running.

*/

public RandomArtPanel() {

RepaintAction action = new RepaintAction();

Timer timer = new Timer(4000, action);

timer.start();

}

/**

* The paintComponent() method fills the panel with a random

shade of

* gray and then draws one of three types of random "art".

The type

* of art to be drawn is chosen at random.

*/

157

Page 165: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

public void paintComponent(Graphics g) {

.

. // The rest of the class is omitted

.

You can find the full source code for this class in the file RandomArt.java. Iwill only note that the very short RepaintAction class is a natural candidate tobe replaced by an anonymous inner class. That can be done where the timer iscreated:

Timer timer = new timer(4000, new ActionListener() {

public void actionPerformed(ActionEvent evt) {

repaint();

}

});

Later in this section, we will use a timer to drive the animation in a simple com-puter game.

Keyboard Events

In Java, user actions become events in a program. These events are associatedwith GUI components. When the user presses a button on the mouse, the eventthat is generated is associated with the component that contains the mouse cursor.What about keyboard events? When the user presses a key, what component isassociated with the key event that is generated?

A GUI uses the idea of input focus to determine the component associatedwith keyboard events. At any given time, exactly one interface element on thescreen has the input focus, and that is where all keyboard events are directed.If the interface element happens to be a Java component, then the informationabout the keyboard event becomes a Java object of type KeyEvent, and it is de-livered to any listener objects that are listening for KeyEvents associated withthat component. The necessity of managing input focus adds an extra twist toworking with keyboard events.

It’s a good idea to give the user some visual feedback about which componenthas the input focus. For example, if the component is the typing area of a word-processor, the feedback is usually in the form of a blinking text cursor. Anotherpossible visual clue is to draw a brightly colored border around the edge of acomponent when it has the input focus, as I do in the examples given later inthis section.

If comp is any component, and you would like it to have the input focus, youcan call requestFocusInWindow(), which should work as long as the windowthat contains the component is active and there is only one component that isrequesting focus. In some cases, when there is only one component involved,it is enough to call this method once, just after opening the window, and the

158

Page 166: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mouse Events

component will retain the focus for the rest of the program. (Note that thereis also a requestFocus() method that might work even when the window isnot active, but the newer method requestFocusInWindow() is preferred in mostcases.)

In a typical user interface, the user can choose to give the focus to a com-ponent by clicking on that component with the mouse. And pressing the tabkey will often move the focus from one component to another. This is handledautomatically by the components involved, without any programming on yourpart.

As our first example of processing key events, we look at a simple programin which the user moves a square up, down, left, and right by pressing arrowkeys. When the user hits the ’R’, ’G’, ’B’, or ’K’ key, the color of the squareis set to red, green, blue, or black, respectively. Of course, none of these keyevents are delivered to the panel unless it has the input focus. The panel in theprogram changes its appearance when it has the input focus: When it does, acyan-colored border is drawn around the panel; when it does not, a gray-coloredborder is drawn. The complete source code for this example can be found in thefile KeyboardAndFocusDemo.java. I will discuss some aspects of it below. Afterreading this section, you should be able to understand the source code in itsentirety.

In Java, keyboard event objects belong to a class called KeyEvent. An objectthat needs to listen for KeyEvents must implement the interface named KeyLis-tener. Furthermore, the object must be registered with a component by callingthe component’s addKeyListener() method. The registration is done with thecommand “component.addKeyListener(listener);” where listener is the ob-ject that is to listen for key events, and component is the object that will generatethe key events (when it has the input focus). It is possible for component andlistener to be the same object. All this is, of course, directly analogous to whatyou learned about mouse events in the previous section. The KeyListener inter-face defines the following methods, which must be included in any class thatimplements KeyListener:

public void keyPressed(KeyEvent evt);

public void keyReleased(KeyEvent evt);

public void keyTyped(KeyEvent evt);

Java makes a distinction between the keys that you press and the charactersthat you type. There are lots of keys on a keyboard: letter keys, number keys,modifier keys such as Control and Shift, arrow keys, page up and page downkeys, keypad keys, function keys, and so on. In some cases, such as the shift key,pressing a key does not type a character. On the other hand, typing a charactersometimes involves pressing several keys. For example, to type an uppercase ’A’,you have to press the Shift key and then press the A key before releasing the

159

Page 167: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

Shift key. On my Mac OS computer, I can type an accented e, by holding downthe Option key, pressing the E key, releasing the Option key, and pressing Eagain. Only one character was typed, but I had to perform three key-pressesand I had to release a key at the right time. In Java, there are three types ofKeyEvent. The types correspond to pressing a key, releasing a key, and typinga character. The keyPressed method is called when the user presses a key, thekeyReleased method is called when the user releases a key, and the keyTyped

method is called when the user types a character (whether that’s done with onekey press or several). Note that one user action, such as pressing the E key, canbe responsible for two events, a keyPressed event and a keyTyped event. Typingan upper case ’A’ can generate two keyPressed events, two keyReleased events,and one keyTyped event.

Usually, it is better to think in terms of two separate streams of events, oneconsisting of keyPressed and keyReleased events and the other consisting ofkeyTyped events. For some applications, you want to monitor the first stream; forother applications, you want to monitor the second one. Of course, the informa-tion in the keyTyped stream could be extracted from the keyPressed/keyReleasedstream, but it would be difficult (and also system-dependent to some extent).Some user actions, such as pressing the Shift key, can only be detected as keyPressedevents. I used to have a computer solitaire game that highlighted every card thatcould be moved, when I held down the Shift key. You can do something likethat in Java by hiliting the cards when the Shift key is pressed and removing thehighlight when the Shift key is released.

There is one more complication. Usually, when you hold down a key on thekeyboard, that key will auto-repeat . This means that it will generate multiplekeyPressed events with just one keyReleased at the end of the sequence. It canalso generate multiple keyTyped events. For the most part, this will not affectyour programming, but you should not expect every keyPressed event to have acorresponding keyReleased event.

Every key on the keyboard has an integer code number. When the keyPressedor keyReleased method is called, the parameter, evt, contains the code of thekey that was pressed or released. The code can be obtained by calling the functionevt.getKeyCode(). Rather than asking you to memorize a table of code numbers,Java provides a named constant for each key. These constants are defined in theKeyEvent class. For example the constant for the shift key is KeyEvent.VK_SHIFT.If you want to test whether the key that the user pressed is the Shift key, you couldsay “if (evt.getKeyCode()== KeyEvent.VK_SHIFT)”. The key codes for thefour arrow keys are KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_UP,and KeyEvent.VK_DOWN. Other keys have similar codes. (The “VK” stands for“Virtual Keyboard”. In reality, different keyboards use different key codes, butJava translates the actual codes from the keyboard into its own “virtual” codes.Your program only sees these virtual key codes, so it will work with various key-

160

Page 168: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Mouse Events

boards on various platforms without modification.)In the case of a keyTyped event, you want to know which character was typed.

This information can be obtained from the parameter, evt, in the keyTyped

method by calling the function evt.getKeyChar(). This function returns a valueof type char representing the character that was typed.

In the KeyboardAndFocusDemo program, I use the keyPressed routine to re-spond when the user presses one of the arrow keys. The program includes in-stance variables, squareLeft and squareTop, that give the position of the up-per left corner of the movable square. When the user presses one of the arrowkeys, the keyPressed routine modifies the appropriate instance variable and callsrepaint() to redraw the panel with the square in its new position. Note that thevalues of squareLeft and squareTop are restricted so that the square never movesoutside the white area of the panel:

/**

* This is called each time the user presses a key while the

panel has

* the input focus. If the key pressed was one of the arrow

keys,

* the square is moved (except that it is not allowed to move

off the

* edge of the panel, allowing for a 3-pixel border).

*/

public void keyPressed(KeyEvent evt) {

int key = evt.getKeyCode(); // keyboard code for the pressed

key

if (key == KeyEvent.VK_LEFT) { // left-arrow key; move the

square left

squareLeft -= 8;

if (squareLeft < 3)

squareLeft = 3;

repaint();

}

else if (key == KeyEvent.VK_RIGHT) { // right-arrow key;

move the square right

squareLeft += 8;

if (squareLeft > getWidth() - 3 - SQUARE_SIZE)

squareLeft = getWidth() - 3 - SQUARE_SIZE;

repaint();

}

else if (key == KeyEvent.VK_UP) { // up-arrow key; move the

square up

squareTop -= 8;

if (squareTop < 3)

161

Page 169: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

squareTop = 3;

repaint();

}

else if (key == KeyEvent.VK_DOWN) { // down-arrow key; move

the square down

squareTop += 8;

if (squareTop > getHeight() - 3 - SQUARE_SIZE)

squareTop = getHeight() - 3 - SQUARE_SIZE;

repaint();

}

} // end keyPressed()

Color changes—which happen when the user types the characters ’R’, ’G’,’B’, and ’K’, or the lower case equivalents—are handled in the keyTyped method.I won’t include it here, since it is so similar to the keyPressed method. Finally,to complete the KeyListener interface, the keyReleased method must be defined.In the sample program, the body of this method is empty since the program doesnothing in response to keyReleased events.

5.4 Basic Components

In preceding sections, you’ve seen how to use a graphics context to draw onthe screen and how to handle mouse events and keyboard events. In one

sense, that’s all there is to GUI programming. If you’re willing to program allthe drawing and handle all the mouse and keyboard events, you have nothingmore to learn. However, you would either be doing a lot more work than youneed to do, or you would be limiting yourself to very simple user interfaces. Atypical user interface uses standard GUI components such as buttons, scroll bars,text-input boxes, and menus. These components have already been written foryou, so you don’t have to duplicate the work involved in developing them. Theyknow how to draw themselves, and they can handle the details of processing themouse and keyboard events that concern them.

Consider one of the simplest user interface components, a push button. Thebutton has a border, and it displays some text. This text can be changed. Some-times the button is disabled, so that clicking on it doesn’t have any effect. Whenit is disabled, its appearance changes. When the user clicks on the push button,the button changes appearance while the mouse button is pressed and changesback when the mouse button is released. In fact, it’s more complicated thanthat. If the user moves the mouse outside the push button before releasing themouse button, the button changes to its regular appearance. To implement this,it is necessary to respond to mouse exit or mouse drag events. Furthermore, onmany platforms, a button can receive the input focus. The button changes ap-

162

Page 170: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Basic Components

pearance when it has the focus. If the button has the focus and the user pressesthe space bar, the button is triggered. This means that the button must respondto keyboard and focus events as well.

Fortunately, you don’t have to program any of this, provided you use an objectbelonging to the standard class javax.swing.JButton. A JButton object drawsitself and processes mouse, keyboard, and focus events on its own. You only hearfrom the JButton when the user triggers it by clicking on it or pressing the spacebar while the button has the input focus. When this happens, the JButton objectcreates an event object belonging to the class java.awt.event.ActionEvent. Theevent object is sent to any registered listeners to tell them that the button has beenpushed. Your program gets only the information it needs—the fact that a buttonwas pushed.

∗ ∗ ∗The standard components that are defined as part of the Swing graphical user

interface API are defined by subclasses of the class JComponent, which is itself asubclass of Component. Many useful methods are defined in the Component andJComponent classes and so can be used with any Swing component. We begin bylooking at a few of these methods. Suppose that comp is a variable that refers tosome JComponent. Then the following methods can be used:

• comp.getWidth() and comp.getHeight() are functions that give the currentsize of the component, in pixels. One warning: When a component isfirst created, its size is zero. The size will be set later, probably by a layoutmanager. A common mistake is to check the size of a component beforethat size has been set, such as in a constructor.

• comp.setEnabled(true) and comp.setEnabled(false) can be used to en-able and disable the component. When a component is disabled, its ap-pearance might change, and the user cannot do anything with it. There isa boolean-valued function, comp.isEnabled() that you can call to discoverwhether the component is enabled.

• comp.setVisible(true) and comp.setVisible(false) can be called to hideor show the component.

• comp.setFont(font) sets the font that is used for text displayed on thecomponent. See Subsection 5.2 for a discussion of fonts.

• comp.setBackground(color) and comp.setForeground(color) set the back-ground and foreground colors for the component. See Subsection 5.2.

• comp.setOpaque(true) tells the component that the area occupied by thecomponent should be filled with the component’s background color be-fore the content of the component is painted. By default, only JLabels arenon-opaque. A non-opaque, or “transparent”, component ignores its back-

163

Page 171: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

ground color and simply paints its content over the content of its container.This usually means that it inherits the background color from its container.

• comp.setToolTipText(string) sets the specified string as a “tool tip” forthe component. The tool tip is displayed if the mouse cursor is in the com-ponent and the mouse is not moved for a few seconds. The tool tip shouldgive some information about the meaning of the component or how to useit.

• comp.setPreferredSize(size) sets the size at which the component shouldbe displayed, if possible. The parameter is of type java.awt.Dimension,where an object of type Dimension has two public integer-valued instancevariables, width and height. A call to this method usually looks somethinglike “setPreferredSize( new Dimension(100,50)~)”. The preferred sizeis used as a hint by layout managers, but will not be respected in all cases.Standard components generally compute a correct preferred size automati-cally, but it can be useful to set it in some cases. For example, if you use aJPanel as a drawing surface, it is usually a good idea to set a preferred sizefor it, since its default preferred size is zero.

Note that using any component is a multi-step process. The component ob-ject must be created with a constructor. It must be added to a container. Inmany cases, a listener must be registered to respond to events from the compo-nent. And in some cases, a reference to the component must be saved in aninstance variable so that the component can be manipulated by the program af-ter it has been created. In this section, we will look at a few of the basic standardcomponents that are available in Swing. In the next section we will consider theproblem of laying out components in containers.

JButton

An object of class JButton is a push button that the user can click to trigger someaction. You’ve already seen buttons used in Section 5.1, but we consider themin much more detail here. To use any component effectively, there are severalaspects of the corresponding class that you should be familiar with. For JButton,as an example, I list these aspects explicitly:

•Constructors: The JButton class has a constructor that takes a string as aparameter. This string becomes the text displayed on the button. For ex-ample: stopGoButton = new~JButton("Go"). This creates a button objectthat will display the text, “Go” (but remember that the button must still beadded to a container before it can appear on the screen).

•Events: When the user clicks on a button, the button generates an event oftype ActionEvent. This event is sent to any listener that has been registeredwith the button as an ActionListener.

164

Page 172: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Basic Components

•Listeners: An object that wants to handle events generated by buttonsmust implement the ActionListener interface. This interface defines justone method, “public void actionPerformed(ActionEvent evt)”, whichis called to notify the object of an action event.

•Registration of Listeners: In order to actually receive notification of anevent from a button, an ActionListener must be registered with the button.This is done with the button’s addActionListener() method. For example:stopGoButton.addActionListener( buttonHandler~);

•Event methods: When actionPerformed(evt) is called by the button, theparameter, evt, contains information about the event. This informationcan be retrieved by calling methods in the ActionEvent class. In particular,evt.getActionCommand() returns a String giving the command associatedwith the button. By default, this command is the text that is displayed onthe button, but it is possible to set it to some other string. The methodevt.getSource() returns a reference to the object that produced the event,that is, to the JButton that was pressed. The return value is of type Object,not JButton, because other types of components can also produce Action-Events.

•Component methods: Several useful methods are defined in the JButtonclass, in addition to the standard Component methods. For example,stopGoButton.setText("Stop") changes the text displayed on the buttonto “Stop”. And stopGoButton.setActionCommand("sgb") changes the ac-tion command associated with this button for action events. The setEnabled()and setText() methods are particularly useful for giving the user informa-tion about what is going on in the program. A disabled button is betterthan a button that gives an obnoxious error message such as “Sorry, youcan’t click on me now!”

JLabel

JLabel is certainly the simplest type of component. An object of type JLabel existsjust to display a line of text. The text cannot be edited by the user, although itcan be changed by your program. The constructor for a JLabel specifies the textto be displayed:

JLabel message = new JLabel("Hello World!");

There is another constructor that specifies where in the label the text is located, ifthere is extra space. The possible alignments are given by the constants JLabel.LEFT,JLabel.CENTER, and JLabel.RIGHT. For example,

JLabel message = new JLabel("Hello World!", JLabel.CENTER);

creates a label whose text is centered in the available space. You can change thetext displayed in a label by calling the label’s setText() method:

165

Page 173: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

message.setText("Goodbye World!");

Since the JLabel class is a subclass of JComponent, you can use methods suchas setForeground() and setFont() with labels. If you want the backgroundcolor to have any effect, you should call setOpaque(true) on the label, sinceotherwise the JLabel might not fill in its background. For example:

JLabel message = new JLabel("Hello World!", JLabel.CENTER);

message.setForeground(Color.RED); // Display red text...

message.setBackground(Color.BLACK); // on a black

background...

message.setFont(new Font("Serif",Font.BOLD,18)); // in a big

bold font.

message.setOpaque(true); // Make sure background is filled in.

JCheckBox

A JCheckBox is a component that has two states: selected or unselected. The usercan change the state of a check box by clicking on it. The state of a checkbox isrepresented by a boolean value that is true if the box is selected and is false ifthe box is unselected. A checkbox has a label, which is specified when the box isconstructed:

JCheckBox showTime = new JCheckBox("Show Current Time");

Usually, it’s the user who sets the state of a JCheckBox, but you can alsoset the state programmatically. The current state of a checkbox is set using itssetSelected(boolean)method. For example, if you want the checkbox showTimeto be checked, you would say “showTime.setSelected(true)". To uncheck thebox, say “showTime.setSelected(false)". You can determine the current stateof a checkbox by calling its isSelected() method, which returns a boolean value.

In many cases, you don’t need to worry about events from checkboxes. Yourprogram can just check the state whenever it needs to know it by calling theisSelected() method. However, a checkbox does generate an event when itsstate is changed by the user, and you can detect this event and respond to it ifyou want something to happen at the moment the state changes. When the stateof a checkbox is changed by the user, it generates an event of type ActionEvent.If you want something to happen when the user changes the state, you mustregister an ActionListener with the checkbox by calling its addActionListener()method. (Note that if you change the state by calling the setSelected() method,no ActionEvent is generated. However, there is another method in the JCheckBoxclass, doClick(), which simulates a user click on the checkbox and does generatean ActionEvent.)

When handling an ActionEvent, you can call evt.getSource() in the actionPerformed()method to find out which object generated the event. (Of course, if you are onlylistening for events from one component, you don’t have to do this.) The re-

166

Page 174: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Basic Components

turned value is of type Object, but you can type-cast it to another type if youwant. Once you know the object that generated the event, you can ask the objectto tell you its current state. For example, if you know that the event had to comefrom one of two checkboxes, cb1 or cb2, then your actionPerformed() methodmight look like this:

public void actionPerformed(ActionEvent evt) {

Object source = evt.getSource();

if (source == cb1) {

boolean newState = cb1.isSelected();

... // respond to the change of state

}

else if (source == cb2) {

boolean newState = cb2.isSelected();

... // respond to the change of state

}

}

Alternatively, you can use evt.getActionCommand() to retrieve the actioncommand associated with the source. For a JCheckBox, the action commandis, by default, the label of the checkbox.

JTextField and JTextArea

The JTextField and JTextArea classes represent components that contain text thatcan be edited by the user. A JTextField holds a single line of text, while a JTextAreacan hold multiple lines. It is also possible to set a JTextField or JTextArea tobe read-only so that the user can read the text that it contains but cannot editthe text. Both classes are subclasses of an abstract class, JTextComponent, whichdefines their common properties.

JTextField and JTextArea have many methods in common. The instancemethod setText(), which takes a parameter of type String, can be used to changethe text that is displayed in an input component. The contents of the componentcan be retrieved by calling its getText() instance method, which returns a valueof type String. If you want to stop the user from modifying the text, you can callsetEditable(false). Call the same method with a parameter of true to makethe input component user-editable again.

The user can only type into a text component when it has the input focus. Theuser can give the input focus to a text component by clicking it with the mouse,but sometimes it is useful to give the input focus to a text field programmatically.You can do this by calling its requestFocusInWindow() method. For example,when I discover an error in the user’s input, I usually call requestFocusInWindow()on the text field that contains the error. This helps the user see where the erroroccurred and lets the user start typing the correction immediately.

167

Page 175: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

By default, there is no space between the text in a text component and theedge of the component, which usually doesn’t look very good. You can use thesetMargin() method of the component to add some blank space between theedge of the component and the text. This method takes a parameter of typejava.awt.Insets which contains four integer instance variables that specify themargins on the top, left, bottom, and right edge of the component. For example,

textComponent.setMargin( new Insets(5,5,5,5));

adds a five-pixel margin between the text in textComponent and each edge of thecomponent.

∗ ∗ ∗The JTextField class has a constructorpublic JTextField(int columns)

where columns is an integer that specifies the number of characters that shouldbe visible in the text field. This is used to determine the preferred width of thetext field. (Because characters can be of different sizes and because the preferredwidth is not always respected, the actual number of characters visible in the textfield might not be equal to columns.) You don’t have to specify the number ofcolumns; for example, you might use the text field in a context where it willexpand to fill whatever space is available. In that case, you can use the defaultconstructor JTextField(), with no parameters. You can also use the followingconstructors, which specify the initial contents of the text field:

public JTextField(String contents);

public JTextField(String contents, int columns);

The constructors for a JTextArea arepublic JTextArea()

public JTextArea(int rows, int columns)

public JTextArea(String contents)

public JTextArea(String contents, int rows, int columns)

The parameter rows specifies how many lines of text should be visible in the textarea. This determines the preferred height of the text area, just as columns de-termines the preferred width. However, the text area can actually contain anynumber of lines; the text area can be scrolled to reveal lines that are not currentlyvisible. It is common to use a JTextArea as the CENTER component of a Border-Layout. In that case, it is less useful to specify the number of lines and columns,since the TextArea will expand to fill all the space available in the center area ofthe container.

The JTextArea class adds a few useful methods to those inherited from JTextCom-ponent. For example, the instance method append(moreText), where moreText

is of type String, adds the specified text at the end of the current content of thetext area. (When using append() or setText() to add text to a JTextArea, line

168

Page 176: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Basic Layout

breaks can be inserted in the text by using the newline character, '\1n'.) AndsetLineWrap(wrap), where wrap is of type boolean, tells what should happenwhen a line of text is too long to be displayed in the text area. If wrap is true,then any line that is too long will be “wrapped” onto the next line; if wrap is false,the line will simply extend outside the text area, and the user will have to scrollthe text area horizontally to see the entire line. The default value of wrap is false.

Since it might be necessary to scroll a text area to see all the text that it con-tains, you might expect a text area to come with scroll bars. Unfortunately, thisdoes not happen automatically. To get scroll bars for a text area, you have to putthe JTextArea inside another component, called a JScrollPane. This can be doneas follows:

JTextArea inputArea = new JTextArea();

JScrollPane scroller = new JScrollPane( inputArea );

The scroll pane provides scroll bars that can be used to scroll the text in the textarea. The scroll bars will appear only when needed, that is when the size of thetext exceeds the size of the text area. Note that when you want to put the textarea into a container, you should add the scroll pane, not the text area itself, tothe container. See the program TextAreaDemo.java for a very short example ofusing a text area in a scroll pane.

∗ ∗ ∗When the user is typing in a JTextField and presses return, an ActionEvent

is generated. If you want to respond to such events, you can register an Action-Listener with the text field, using the text field’s addActionListener() method.(Since a JTextArea can contain multiple lines of text, pressing return in a text areadoes not generate an event; it simply begins a new line of text.)

JTextField has a subclass, JPasswordField, which is identical except that it doesnot reveal the text that it contains. The characters in a JPasswordField are all dis-played as asterisks (or some other fixed character). A password field is, obviously,designed to let the user enter a password without showing that password on thescreen.

Text components are actually quite complex, and I have covered only theirmost basic properties here. I will return to the topic of text components in Chap-ter ??.

5.5 Basic Layout

Components are the fundamental building blocks of a graphical user interface.But you have to do more with components besides create them. Another

aspect of GUI programming is laying out components on the screen, that is,deciding where they are drawn and how big they are. You have probably noticed

169

Page 177: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

that computing coordinates can be a difficult problem, especially if you don’tassume a fixed size for the drawing area. Java has a solution for this, as well.

Components are the visible objects that make up a GUI. Some componentsare containers, which can hold other components. Containers in Java are objectsthat belong to some subclass of java.awt.Container. The content pane of aJFrame is an example of a container. The standard class JPanel, which we havemostly used as a drawing surface up until now, is another example of a container.

Because a JPanel object is a container, it can hold other components. Becausea JPanel is itself a component, you can add a JPanel to another JPanel. Thismakes complex nesting of components possible. JPanels can be used to organizecomplicated user interfaces, as shown in this illustration:

In this picture, a large panel holds two smaller panels. Each of the two smallerpanels in turn holds three components.

The components in a container must be “laid out,” which means setting theirsizes and positions. It’s possible to program the layout yourself, but layout isordinarily done by a layout manager. A layout manager is an object associatedwith a container that implements some policy for laying out the components inthat container. Different types of layout manager implement different policies.In this section, we will cover the three most common types of layout manager,and then we will look at several programming examples that use components andlayout.

Every container has a default layout manager and has an instance method,setLayout(), that takes a parameter of type LayoutManager and that is used tospecify a different layout manager for the container. Components are added toa container by calling an instance method named add() in the container object.There are actually several versions of the add() method, with different parameterlists. Different versions of add() are appropriate for different layout managers,as we will see below.

170

Page 178: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Basic Layout

Basic Layout Managers

Java has a variety of standard layout managers that can be used as parameters inthe setLayout() method. They are defined by classes in the package java.awt.Here, we will look at just three of these layout manager classes: FlowLayout, Bor-derLayout, and GridLayout.

A FlowLayout simply lines up components in a row across the container. Thesize of each component is equal to that component’s “preferred size.” After layingout as many items as will fit in a row across the container, the layout managerwill move on to the next row. The default layout for a JPanel is a FlowLayout;that is, a JPanel uses a FlowLayout unless you specify a different layout managerby calling the panel’s setLayout() method.

The components in a given row can be either left-aligned, right-aligned, orcentered within that row, and there can be horizontal and vertical gaps betweencomponents. If the default constructor, “new FlowLayout()”, is used, then thecomponents on each row will be centered and both the horizontal and the verticalgaps will be five pixels. The constructor

public FlowLayout(int align, int hgap, int vgap)

can be used to specify alternative alignment and gaps. The possible values ofalign are FlowLayout.LEFT, FlowLayout.RIGHT, and FlowLayout.CENTER.

Suppose that container is a container object that is using a FlowLayout as itslayout manager. Then, a component, comp, can be added to the container withthe statement

container.add(comp);

The FlowLayout will line up all the components that have been added to thecontainer in this way. They will be lined up in the order in which they were added.For example, this picture shows five buttons in a panel that uses a FlowLayout:

Note that since the five buttons will not fit in a single row across the panel, theyare arranged in two rows. In each row, the buttons are grouped together and arecentered in the row. The buttons were added to the panel using the statements:

panel.add(button1);

panel.add(button2);

panel.add(button3);

panel.add(button4);

panel.add(button5);

171

Page 179: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

When a container uses a layout manager, the layout manager is ordinarilyresponsible for computing the preferred size of the container (although a differentpreferred size could be set by calling the container’s setPreferredSize method).A FlowLayout prefers to put its components in a single row, so the preferred widthis the total of the preferred widths of all the components, plus the horizontal gapsbetween the components. The preferred height is the maximum preferred heightof all the components.

∗ ∗ ∗A BorderLayout layout manager is designed to display one large, central com-

ponent, with up to four smaller components arranged around the edges of thecentral component. If a container, cntr, is using a BorderLayout, then a compo-nent, comp, should be added to the container using a statement of the form

cntr.add( comp, borderLayoutPosition );

where borderLayoutPosition specifies what position the component should oc-cupy in the layout and is given as one of the constants BorderLayout.CENTER,BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, or BorderLayout.WEST.The meaning of the five positions is shown in this diagram:

Center

North

South

East

West

Note that a border layout can contain fewer than five components, so that not allfive of the possible positions need to be filled. It would be very unusual, however,to have no center component.

A BorderLayout sets the sizes of its components as follows: The NORTH andSOUTH components (if present) are shown at their preferred heights, but theirwidth is set equal to the full width of the container. The EAST and WEST compo-nents are shown at their preferred widths, but their height is set to the height ofthe container, minus the space occupied by the NORTH and SOUTH components.Finally, the CENTER component takes up any remaining space. The preferred sizeof the CENTER component is ignored when the layout is done, but it is taken intoaccount when the preferred size of the container as a whole is computed. Youshould make sure that the components that you put into a BorderLayout are suit-able for the positions that they will occupy. A horizontal slider or text field, forexample, would work well in the NORTH or SOUTH position, but wouldn’t makemuch sense in the EAST or WEST position.

172

Page 180: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. Basic Layout

The default constructor, new BorderLayout(), leaves no space between com-ponents. If you would like to leave some space, you can specify horizontal andvertical gaps in the constructor of the BorderLayout object. For example, if yousay

panel.setLayout(new BorderLayout(5,7));

then the layout manager will insert horizontal gaps of 5 pixels between compo-nents and vertical gaps of 7 pixels between components. The background colorof the container will show through in these gaps. The default layout for the origi-nal content pane that comes with a JFrame is a BorderLayout with no horizontalor vertical gap.

∗ ∗ ∗Finally, we consider the GridLayout layout manager. A grid layout lays out

components in a grid containing rows and columns of equal sized rectangles.This illustration shows how the components would be arranged in a grid layoutwith 4 rows and 3 columns:

#1#1

#4

#7

#10

#2

#5

#8

#11

#3

#6

#9

#12

If a container uses a GridLayout, the appropriate add method for the containertakes a single parameter of type Component (for example: cntr.add(comp)). Com-ponents are added to the grid in the order shown; that is, each row is filled fromleft to right before going on the next row.

The constructor for a GridLayout takes the form “new GridLayout(R,C)”,where R is the number of rows and C is the number of columns. If you wantto leave horizontal gaps of H pixels between columns and vertical gaps of V pixelsbetween rows, use “new GridLayout(R,C,H,V)” instead.

When you use a GridLayout, it’s probably good form to add just enough com-ponents to fill the grid. However, this is not required. In fact, as long as youspecify a non-zero value for the number of rows, then the number of columns isessentially ignored. The system will use just as many columns as are necessary tohold all the components that you add to the container. If you want to depend onthis behavior, you should probably specify zero as the number of columns. Youcan also specify the number of rows as zero. In that case, you must give a non-zero number of columns. The system will use the specified number of columns,with just as many rows as necessary to hold the components that are added tothe container.

173

Page 181: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

Horizontal grids, with a single row, and vertical grids, with a single column,are very common. For example, suppose that button1, button2, and button3

are buttons and that you’d like to display them in a horizontal row in a panel. Ifyou use a horizontal grid for the panel, then the buttons will completely fill thatpanel and will all be the same size. The panel can be created as follows:

JPanel buttonBar = new JPanel();

buttonBar.setLayout( new GridLayout(1,3) );

// (Note: The "3" here is pretty much ignored, and

// you could also say "new GridLayout(1,0)".

// To leave gaps between the buttons, you could use

// "new GridLayout(1,0,5,5)".)

buttonBar.add(button1);

buttonBar.add(button2);

buttonBar.add(button3);

You might find this button bar to be more attractive than the one that uses thedefault FlowLayout layout manager.

5.6 A Simple Calculator

As our next example, we look briefly at an example that uses nested subpanels tobuild a more complex user interface. The program has two JTextFields where theuser can enter two numbers, four JButtons that the user can click to add, subtract,multiply, or divide the two numbers, and a JLabel that displays the result of theoperation. Here is a picture from the program:

This example uses a panel with a GridLayout that has four rows and one column.In this case, the layout is created with the statement:

setLayout(new GridLayout(4,1,3,3));

which allows a 3-pixel gap between the rows where the gray background color ofthe panel is visible.

The first row of the grid layout actually contains two components, a JLabeldisplaying the text “x~=” and a JTextField. A grid layout can only have one com-ponent in each position. In this case, the component in the first row is a JPanel,a subpanel that is nested inside the main panel. This subpanel in turn containsthe label and text field. This can be programmed as follows:

174

Page 182: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

.. A Simple Calculator

xInput = new JTextField("0", 10); // Create a text field sized

to hold 10 chars.

JPanel xPanel = new JPanel(); // Create the subpanel.

xPanel.add( new JLabel(" x = ")); // Add a label to the subpanel.

xPanel.add(xInput); // Add the text field to the

subpanel

add(xPanel); // Add the subpanel to the

main panel.

The subpanel uses the default FlowLayout layout manager, so the label and textfield are simply placed next to each other in the subpanel at their preferred size,and are centered in the subpanel.

Similarly, the third row of the grid layout is a subpanel that contains fourbuttons. In this case, the subpanel uses a GridLayout with one row and fourcolumns, so that the buttons are all the same size and completely fill the subpanel.

One other point of interest in this example is the actionPerformed() methodthat responds when the user clicks one of the buttons. This method must retrievethe user’s numbers from the text field, perform the appropriate arithmetic oper-ation on them (depending on which button was clicked), and set the text of theJLabel (named answer) to represent the result. However, the contents of the textfields can only be retrieved as strings, and these strings must be converted intonumbers. If the conversion fails, the label is set to display an error message:

public void actionPerformed(ActionEvent evt) {

double x, y; // The numbers from the input boxes.

try {

String xStr = xInput.getText();

x = Double.parseDouble(xStr);

}

catch (NumberFormatException e) {

// The string xStr is not a legal number.

answer.setText("Illegal data for x.");

xInput.requestFocusInWindow();

return;

}

try {

String yStr = yInput.getText();

y = Double.parseDouble(yStr);

}

catch (NumberFormatException e) {

// The string yStr is not a legal number.

answer.setText("Illegal data for y.");

yInput.requestFocusInWindow();

175

Page 183: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

return;

}

/* Perform the operation based on the action command from the

button. The action command is the text displayed on the

button.

Note that division by zero produces an error message. */

String op = evt.getActionCommand();

if (op.equals("+"))

answer.setText( "x + y = " + (x+y) );

else if (op.equals("-"))

answer.setText( "x - y = " + (x-y) );

else if (op.equals("*"))

answer.setText( "x * y = " + (x*y) );

else if (op.equals("/")) {

if (y == 0)

answer.setText("Can't divide by zero!");

else

answer.setText( "x / y = " + (x/y) );

}

} // end actionPerformed()

The complete source code for this example can be found in SimpleCalc.java.

176

Page 184: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Exercises

Exercises for Chapter 5

1. In the SimpleStamper example from Subsection 5.3, a rectangle or oval isdrawn on the panel when the user clicks the mouse. Except, when theuser shift-clicks, the panel is cleared instead. Modify this class so that themodified version will continue to draw figures as the user drags the mouse.That is, the mouse will leave a trail of figures as the user drags. However,if the user shift-clicks, the panel should simply be cleared and no figuresshould be drawn even if the user drags the mouse after shift-clicking. Hereis a picture of my solution:

The source code for the original program is SimpleStamper.java. Seethe discussion of dragging in Subsection ??. (Note that the original versionuses a black background, with a black border around each shape. Thatdidn’t work well with a lot of closely spaced shapes, so the new version usesa white background.)

If you want to make the problem a little more challenging, when draw-ing shapes during a drag operation, make sure that the shapes that are drawnare at least, say, 5 pixels apart. To implement this, you have to keep trackof the position of the last shape that was drawn.

2. Write a program that shows a small red square and a small blue square. Theuser should be able to drag either square with the mouse. (You’ll need aninstance variable to remember which square the user is dragging.) The usercan drag the square out of the panel if she wants; if she does this, there isno way to get it back.

Note that for this exercise, you should do all the drawing in the paintComponent()method (as indeed you should whenever possible).

177

Page 185: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

3. Write a program that shows a pair of dice. When the user clicks on thepanel in the program, the dice should be rolled (that is, the dice should beassigned newly computed random values). Each die should be drawn as asquare showing from 1 to 6 dots. Since you have to draw two dice, its agood idea to write a method, “void drawDie(Graphics g, int val, int

x, int y)”, to draw a die at the specified (x,y) coordinates. The secondparameter, val, specifies the value that is showing on the die. Assume thatthe size of the panel is 100 by 100 pixels. Here is a picture of the panel thatdisplays the dice:

4. In Exercise 6.3, you wrote a pair-of-dice panel where the dice are rolledwhen the user clicks on the panel. Now make a pair-of-dice program inwhich the user rolls the dice by clicking a button. The button should ap-pear under the panel that shows the dice. Also make the following change:When the dice are rolled, instead of just showing the new value, show ashort animation during which the values on the dice are changed in everyframe. The animation is supposed to make the dice look more like they areactually rolling.

5. In Exercise 3.8, you drew a checkerboard. For this exercise, write a programwhere the user can select a square by clicking on it. (Use a JPanel for thecheckerboard.) Highlight the selected square by drawing a colored borderaround it. When the program starts, no square is selected. When the userclicks on a square that is not currently selected, it becomes selected (and thepreviously selected square, if any, is unselected). If the user clicks the squarethat is selected, it becomes unselected. Assume that the size of the panel isexactly 160 by 160 pixels, so that each square on the checkerboard is 20 by20 pixels. Here is my checkerboard, with the square in row 3, column 3selected:

178

Page 186: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Exercises

6. Write a program that has a JTextArea where the user can enter some text.Then program should have a button such that when the user clicks on thebutton, the panel will count the number of lines in the user’s input, thenumber of words in the user’s input, and the number of characters in theuser’s input. This information should be displayed on three labels. Re-call that if textInput is a JTextArea, then you can get the contents of theJTextArea by calling the function textInput.getText(). This function re-turns a String containing all the text from the text area. The number ofcharacters is just the length of this String. Lines in the String are separatedby the new line character, '\1n', so the number of lines is just the numberof new line characters in the String, plus one. Words are a little harder tocount. Exercise 3.4 has some advice about finding the words in a String.Essentially, you want to count the number of characters that are first char-acters in words. Don’t forget to put your JTextArea in a JScrollPane, and addthe scroll pane to the container, not the text area. Scrollbars should appearwhen the user types more text than will fit in the available area. Here is apicture of my solution:

179

Page 187: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

7. A polygon is a geometric figure made up of a sequence of connected linesegments. The points where the line segments meet are called the verticesof the polygon. The Graphics class includes commands for drawing andfilling polygons. For these commands, the coordinates of the vertices of thepolygon are stored in arrays. If g is a variable of type Graphics then

• g.drawPolygon(xCoords, yCoords, pointCt) will draw the outlineof the polygon with vertices at the points (xCoords[0],yCoords[0]),(xCoords[1],yCoords[1]), …, (xCoords[pointCt-1],yCoords[pointCt-1]).The third parameter, pointCt, is an int that specifies the number of ver-tices of the polygon. Its value should be 3 or greater. The first two pa-rameters are arrays of type int[]. Note that the polygon automaticallyincludes a line from the last point, (xCoords[pointCt-1],yCoords[pointCt-1]),back to the starting point (xCoords[0],yCoords[0]).

• g.fillPolygon(xCoords, yCoords, pointCt) fills the interior of thepolygon with the current drawing color. The parameters have the samemeaning as in the drawPolygon() method. Note that it is OK for thesides of the polygon to cross each other, but the interior of a polygonwith self-intersections might not be exactly what you expect.

Write a program that lets the user draw polygons. As the user clicks asequence of points, count them and store their x- and y-coordinates in twoarrays. These points will be the vertices of the polygon. As the user is creat-ing the polygon, you should just connect all the points with line segments.When the user clicks near the starting point, draw the complete polygon.Draw it with a red interior and a black border. Once the user has completed

180

Page 188: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Exercises

a polygon, the next click will clear the data and start a new polygon fromscratch. All drawing should be done in the paintComponent() method.

Here is a picture of my solution after the user has drawn a fairly complexpolygon:

8. Write a GUI Blackjack program that lets the user play a game of Blackjack,with the computer as the dealer. The program should draw the user’s cardsand the dealer’s cards, just as was done for the graphical HighLow cardgame in Subsection ??. You can use the source code for that game, High-LowGUI.java, for some ideas about how to write your Blackjack game. Thestructures of the HighLow panel and the Blackjack panel are very similar.You will certainly want to use the drawCard() method from the HighLowprogram.

You can find a description of the game of Blackjack in Exercise 5.5. Addthe following rule to that description: If a player takes five cards withoutgoing over 21, that player wins immediately. This rule is used in somecasinos. For your program, it means that you only have to allow room forfive cards. You should assume that the panel is just wide enough to showfive cards, and that it is tall enough show the user’s hand and the dealer’shand.

Note that the design of a GUI Blackjack game is very different from thedesign of the text-oriented program that you wrote for Exercise 5.5. Theuser should play the game by clicking on “Hit” and “Stand” buttons. Thereshould be a “New Game” button that can be used to start another gameafter one game ends. You have to decide what happens when each of thesebuttons is pressed. You don’t have much chance of getting this right unlessyou think in terms of the states that the game can be in and how the statecan change.

181

Page 189: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

Your program will need the classes defined in Card.java, Hand.java,Deck.java, and BlackjackHand.java.

The next exercise has a picture of a blackjack game that you can use aguide, except that the version for this exercise does not allow betting.

9. In the Blackjack game from Exercise 6.10, the user can click on the “Hit”,“Stand”, and “NewGame” buttons even when it doesn’t make sense to do so.It would be better if the buttons were disabled at the appropriate times. The“New Game” button should be disabled when there is a game in progress.The “Hit” and “Stand” buttons should be disabled when there is not a gamein progress. The instance variable gameInProgress tells whether or not agame is in progress, so you just have to make sure that the buttons are prop-erly enabled and disabled whenever this variable changes value. I stronglyadvise writing a method that can be called whenever it is necessary to setthe value of the gameInProgress variable. Then the method can take re-sponsibility for enabling and disabling the buttons. Recall that if bttn isa variable of type JButton, then bttn.setEnabled(false) disables the but-ton and bttn.setEnabled(true) enables the button.

As a second (and more difficult) improvement, make it possible for theuser to place bets on the Blackjack game. When the program starts, givethe user $100. Add a JTextField to the strip of controls along the bottomof the panel. The user can enter the bet in this JTextField. When the gamebegins, check the amount of the bet. You should do this when the gamebegins, not when it ends, because several errors can occur: The contentsof the JTextField might not be a legal number, the bet that the user placesmight be more money than the user has, or the bet might be <= 0. Youshould detect these errors and show an error message instead of starting thegame. The user’s bet should be an integral number of dollars.

It would be a good idea to make the JTextField uneditable while thegame is in progress. If betInput is the JTextField, you can make it editableand uneditable by the user with the commands betInput.setEditable(true)and betInput.setEditable(false).

In the paintComponent() method, you should include commands todisplay the amount of money that the user has left.

There is one other thing to think about: Ideally, the program shouldnot start a new game when it is first created. The user should have a chanceto set a bet amount before the game starts. So, in the constructor for thedrawing surface class, you should not call doNewGame(). You might wantto display a message such as “Welcome to Blackjack” before the first gamestarts.

Here is a picture of my program:

182

Page 190: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Exercises

183

Page 191: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

. Graphical User Interface Programming

Quiz on Chapter 5

1. Programs written for a graphical user interface have to deal with “events.”Explain what is meant by the term event. Give at least two different exam-ples of events, and discuss how a program might respond to those events.

2. Explain carefully what the repaint() method does.

3. Java has a standard class called JPanel. Discuss two ways in which JPanelscan be used.

4. Draw the picture that will be produced by the following paintComponent()

method:public static void paintComponent(Graphics g) {

super.paintComponent(g);

for (int i=10; i \<= 210; i = i + 50)

for (int j = 10; j \<= 210; j = j + 50)

g.drawLine(i,10,j,60);

}

5. Suppose you would like a panel that displays a green square inside a redcircle, as illustrated. Write a paintComponent() method for the panel classthat will draw the image.

6. Java has a standard class called MouseEvent. What is the purpose of thisclass? What does an object of type MouseEvent do?

7. One of the main classes in Swing is the JComponent class. What is meantby a component? What are some examples?

8. What is the function of a LayoutManager in Java?

9. Consider the illustration of nested panels from the beginning of Section 5.5.What type of layout manager is being used for each of the three panels inthat picture?

184

Page 192: Object Oriented Programminghughm/oop/docs/comp200-2018-v0.5.pdf · Object-oriented programming(OOP) representsanattempttomakepro-Object-oriented programming is one of several programming

Quiz

10. Explain how Timers are used to do animation.

11. What is a JCheckBox and how is it used?

12. How is the preferred size of a component set, and how is it used?

185