Javanotes5 152 202
Javanotes5 152 202
SUBROUTINES
∗ ∗ ∗
Here are a few more examples of functions. The first one computes a letter grade corre-
sponding to a given numerical grade, on a typical grading scale:
/**
* Returns the letter grade corresponding to the numerical
* grade that is passed to this function as a parameter.
*/
static char letterGrade(int numGrade) {
if (numGrade >= 90)
return ’A’; // 90 or above gets an A
else if (numGrade >= 80)
return ’B’; // 80 to 89 gets a B
else if (numGrade >= 65)
return ’C’; // 65 to 79 gets a C
else if (numGrade >= 50)
return ’D’; // 50 to 64 gets a D
else
4.4. RETURN VALUES 137
/**
* print3NSequence prints a 3N+1 sequence to standard output, using
4.4. RETURN VALUES 139
while (N > 1) {
N = nextN(N); // compute next term
count++; // count this term
if (onLine == 5) { // If current output line is full
TextIO.putln(); // ...then output a carriage return
onLine = 0; // ...and note that there are no terms
// on the new line.
}
TextIO.putf("%8d", N); // Print this term in an 8-char column.
onLine++; // Add 1 to the number of terms on this line.
}
} // end of Print3NSequence
/**
* nextN computes and returns the next term in a 3N+1 sequence,
* given that the current term is currentN.
*/
static int nextN(int currentN) {
if (currentN % 2 == 1)
return 3 * currentN + 1;
else
return currentN / 2;
} // end of nextN()
You should read this program carefully and try to understand how it works. (Try using 27 for
the starting value!)
140 CHAPTER 4. SUBROUTINES
4.5.1 Toolboxes
Someone who wants to program for Macintosh computers—and to produce programs that look
and behave the way users expect them to—must deal with the Macintosh Toolbox, a collection of
well over a thousand different subroutines. There are routines for opening and closing windows,
for drawing geometric figures and text to windows, for adding buttons to windows, and for
responding to mouse clicks on the window. There are other routines for creating menus and
for reacting to user selections from menus. Aside from the user interface, there are routines
for opening files and reading data from them, for communicating over a network, for sending
output to a printer, for handling communication between programs, and in general for doing
all the standard things that a computer has to do. Microsoft Windows provides its own set
of subroutines for programmers to use, and they are quite a bit different from the subroutines
used on the Mac. Linux has several different GUI toolboxes for the programmer to choose from.
The analogy of a “toolbox” is a good one to keep in mind. Every programming project
involves a mixture of innovation and reuse of existing tools. A programmer is given a set of
tools to work with, starting with the set of basic tools that are built into the language: things
like variables, assignment statements, if statements, and loops. To these, the programmer can
add existing toolboxes full of routines that have already been written for performing certain
tasks. These tools, if they are well-designed, can be used as true black boxes: They can be called
to perform their assigned tasks without worrying about the particular steps they go through to
accomplish those tasks. The innovative part of programming is to take all these tools and apply
them to some particular project or problem (word-processing, keeping track of bank accounts,
processing image data from a space probe, Web browsing, computer games, . . . ). This is called
applications programming .
A software toolbox is a kind of black box, and it presents a certain interface to the program-
mer. This interface is a specification of what routines are in the toolbox, what parameters they
use, and what tasks they perform. This information constitutes the API , or Applications
Programming Interface, associated with the toolbox. The Macintosh API is a specification
of all the routines available in the Macintosh Toolbox. A company that makes some hard-
ware device—say a card for connecting a computer to a network—might publish an API for
that device consisting of a list of routines that programmers can call in order to communicate
with and control the device. Scientists who write a set of routines for doing some kind of
complex computation—such as solving “differential equations,” say—would provide an API to
allow others to use those routines without understanding the details of the computations they
perform.
∗ ∗ ∗
4.5. APIS, PACKAGES, AND JAVADOC 141
The Java programming language is supplemented by a large, standard API. You’ve seen
part of this API already, in the form of mathematical subroutines such as Math.sqrt(), the
String data type and its associated routines, and the System.out.print() routines. The
standard Java API includes routines for working with graphical user interfaces, for network
communication, for reading and writing files, and more. It’s tempting to think of these routines
as being built into the Java language, but they are technically subroutines that have been
written and made available for use in Java programs.
Java is platform-independent. That is, the same program can run on platforms as diverse as
Macintosh, Windows, Linux, and others. The same Java API must work on all these platforms.
But notice that it is the interface that is platform-independent; the implementation varies
from one platform to another. A Java system on a particular computer includes implementations
of all the standard API routines. A Java program includes only calls to those routines. When
the Java interpreter executes a program and encounters a call to one of the standard routines,
it will pull up and execute the implementation of that routine which is appropriate for the
particular platform on which it is running. This is a very powerful idea. It means that you only
need to learn one API to program for a wide variety of platforms.
java package, its sub-packages, the classes in those sub-packages, and the subroutines in those
classes. This is not a complete picture, since it shows only a very few of the many items in each
element:
j a v a
l a n g a w t u t i l
M a t h G r a p h i c s
s q r t ( ) d r a w R e c t ( )
s e t C o l o r ( )
r a n d o m ( )
t r i n g C o l o r
F o n t
I n t e g e r
S u b r o u t i n e s n e s t e d i n c l a s s e s n e s t e d i n t w o l a y e r s o f p a c k a g e s .
e f u l l n a m e o f s q r t ( ) i s j a v a . l a n g . M a t . s q r t ( )
T h h
The official documentation for the standard Java 5.0 API lists 165 different packages, in-
cluding sub-packages, and it lists 3278 classes in these packages. Many of these are rather
obscure or very specialized, but you might want to browse through the documentation to see
what is available. As I write this, the documentation for the complete API can be found at
http://java.sun.com/j2se/1.5.0/docs/api/index.html
Even an expert programmer won’t be familiar with the entire API, or even a majority of it. In
this book, you’ll only encounter several dozen classes, and those will be sufficient for writing a
wide variety of programs.
at the beginning of a Java source code file, then, in the rest of the file, you can abbreviate the
full name java.awt.Color to just the simple name of the class, Color. Note that the import
4.5. APIS, PACKAGES, AND JAVADOC 143
line comes at the start of a file and is not inside any class. Although it is sometimes referred
to as a statement, it is more properly called an import directive since it is not a statement
in the usual sense. Using this import directive would allow you to say
Color rectColor;
to declare the variable. Note that the only effect of the import directive is to allow you to use
simple class names instead of full “package.class” names; you aren’t really importing anything
substantial. If you leave out the import directive, you can still access the class—you just have
to use its full name. There is a shortcut for importing all the classes from a given package. You
can import all the classes from java.awt by saying
import java.awt.*;
The “*” is a wildcard that matches every class in the package. (However, it does not match
sub-packages; you cannot import the entire contents of all the sub-packages of the java package
by saying import java.*.)
Some programmers think that using a wildcard in an import statement is bad style, since
it can make a large number of class names available that you are not going to use and might
not even know about. They think it is better to explicitly import each individual class that
you want to use. In my own programming, 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 given package.
In fact, any Java program that uses a graphical user interface is likely to use many
classes from the java.awt and java.swing packages as well as from another package 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 the line “import java.net.*;”, while
one that reads or writes files might use “import java.io.*;”. (But when you start importing
lots of packages in this way, you have to be careful about one thing: It’s possible for two classes
that are in different packages to have the same name. For example, both the java.awt package
and the java.util package contain 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 of type
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 or java.util.List. Another
solution, of course, is to use import to import the individual classes you need, instead of
importing entire packages.)
Because the package java.lang is so fundamental, all the classes in java.lang are auto-
matically imported into every program. It’s as if every program began with the statement
“import java.lang.*;”. This is why we have been able to use the class name String instead
of java.lang.String, and Math.sqrt() instead of java.lang.Math.sqrt(). It would still,
however, be perfectly legal to use the longer forms of the names.
Programmers can create new packages. Suppose that you want some classes that you are
writing to be in a package named utilities. Then the source code file that defines those
classes must begin with the line
package utilities;
144 CHAPTER 4. SUBROUTINES
This would come even before any import directive in that file. Furthermore, as mentioned in
Subsection 2.6.4, 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 same
package; 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 organize those classes
into packages. It also makes sense for programmers to create new packages as toolboxes that
provide functionality and API’s for dealing with areas not covered in the standard Java API.
(And in fact such “toolmaking” programmers often have more prestige than the applications
programmers who use their tools.)
However, I will not be creating any packages in this textbook. For the purposes of this
book, you need to know about packages mainly so that you will be able to import the standard
packages. These packages are always available to the programs that you write. You might
wonder where the standard classes are actually located. Again, that can depend to some extent
on the version of Java that you are using, but in the standard Java 5.0, they are stored in jar
files in a 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 classes can be found in a
jar file named classes.jar. In fact, Java programs are generally distributed in the form of jar
files, instead of as individual class files.
Although we won’t be creating packages explicitly, every class is actually part of a package.
If a class is not specifically placed in a package, then it is put in something called the default
package, which has no name. All the examples that you see in this book are in the default
package.
4.5.4 Javadoc
To use an API effectively, you need good documentation for it. The documentation for most
Java APIs is prepared using a system called Javadoc. For example, this system is used to
prepare the documentation for Java’s standard packages. And almost everyone who creates a
toolbox in Java publishes Javadoc documentation for it.
Javadoc documentation is prepared from special comments that are placed in the 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 /*. You
have already seen comments of this form in some of the examples in this book, such as this
subroutine from Section 4.3:
/**
* This subroutine 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) { ...
Note that the Javadoc comment is placed just before the subroutine that it is commenting
on. This rule is always followed. You can have Javadoc comments for subroutines, for member
variables, and for classes. The Javadoc comment always immediately precedes the thing it is
commenting on.
Like any comment, a Javadoc comment is ignored by the computer when the file is compiled.
But there is a tool called javadoc that reads Java source code files, extracts any Javadoc
4.5. APIS, PACKAGES, AND JAVADOC 145
comments that it finds, and creates a set of Web pages containing the comments in a nicely
formatted, interlinked form. By default, javadoc will only collect information about public
classes, subroutines, and member variables, but it allows the option of creating documentation
for non-public things as well. If javadoc doesn’t find any Javadoc comment for something, it
will construct one, but the comment will contain only basic information such as the name and
type of a member variable or the name, return type, and parameter list of a subroutine. This
is syntactic information. To add information about semantics and pragmatics, you have to
write a Javadoc comment.
As an example, you can look at the documentation Web page for TextIO. The documentation
page was created by applying the javadoc tool to the source code file, TextIO.java. If you
have downloaded the on-line version of this book, the documentation can be found in the
TextIO Javadoc directory, or you can find a link to it in the on-line version of this section.
In a Javadoc comment, the *’s at the start of each line are optional. The javadoc tool
will remove them. In addition to normal text, the comment can contain certain special codes.
For one thing, the comment can contain HTML mark-up commands. HTML is the language
that is used to create web pages, and Javadoc comments are meant to be shown on web pages.
The javadoc tool will copy any HTML commands in the comments to the web pages that it
creates. You’ll learn some basic HTML in Section 6.2, but as an example, you can add <p> to
indicate the start of a new paragraph. (Generally, in the absence of HTML commands, 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 name that begins with the
character @. I will only discuss three tags: @param, @return, and @throws. These tags are used
in Javadoc comments for subroutines to provide information about its parameters, its return
value, and the exceptions that it might throw. These tags are always placed at the end of the
comment, after any description of the subroutine itself. The syntax for using them is:
@param hparameter-name i hdescription-of-parameter i
@return hdescription-of-return-value i
@throws hexception-class-name i hdescription-of-exception i
The hdescriptionsi can extend over several lines. The description ends at the next tag or at the
end of the comment. You can include a @param tag for every parameter of the subroutine and a
@throws for as many types of exception as you want to document. You should have a @return
tag only for a non-void subroutine. These tags do not have to be given in any particular order.
Here is an example that doesn’t do anything exciting but that does use all three types of
doc tag:
/**
* This subroutine 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.");
146 CHAPTER 4. SUBROUTINES
double area;
area = width * height;
return area;
}
I will use Javadoc comments for some of my examples. I encourage you to use them in your
own code, even if you don’t plan to generate Web page documentation of your work, since it’s
a standard format that other Java programmers will be familiar with.
If you do want to create Web-page documentation, you need to run the javadoc tool. This
tool is available as a command in the Java Development Kit that was discussed in Section 2.6.
You can use javadoc in a command line interface similarly to the way that the javac and
java commands are used. Javadoc can also be applied in the Eclipse integrated development
environment that was also discussed in Section 2.6: Just right-click the class or package that
you want to document in the Package Explorer, select “Export,” and select “Javadoc” in the
window that pops up. I won’t go into any of the details here; see the documentation.
of the subroutine, as discussed in Section 4.1. A convenient way to express the contract of a
subroutine is in terms of preconditions and postconditions.
The precondition of a subroutine is something that must be true when the subroutine
is called, if the subroutine is to work correctly. For example, for the built-in function
Math.sqrt(x), a precondition is that the parameter, x, is greater than or equal to zero, since it
is not possible to take the square root of a negative number. In terms of a contract, a precon-
dition represents an obligation of the caller of the subroutine. If you call a subroutine without
meeting 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 yourself, not the subroutine.
A postcondition of a subroutine represents the other side of the contract. It is something
that will be true after the subroutine has run (assuming that its preconditions were met—and
that there are no bugs in the subroutine). The postcondition of the function Math.sqrt() is
that the square of the value that is returned by this function is equal to the parameter that is
provided when the subroutine is called. Of course, this will only be true if the preconditiion—
that the parameter is greater than or equal to zero—is met. A postcondition of the built-in
subroutine System.out.print() is that the value of the parameter has been displayed on the
screen.
Preconditions most often give restrictions on the acceptable values of parameters, as in the
example of Math.sqrt(x). However, they can also refer to global variables that are used in
the subroutine. The postcondition of a subroutine specifies the task that it performs. For a
function, the postcondition should specify the value that the function returns.
Subroutines are often described by comments that explicitly specify their preconditions and
postconditions. When you are given a pre-written subroutine, a statement of its preconditions
and postconditions tells you how to use it and what it does. When you are assigned to write
a subroutine, the preconditions and postconditions give you an exact specification of what the
subroutine is expected to do. I will use this approach in the example that constitutes the rest
of this section. The comments are given in the form of Javadoc comments, but I will explicitly
label the preconditions and postconditions. (Many computer scientists think that new doc
tags @precondition and @postcondition should be added to the Javadoc system for explicit
labeling of preconditions and postconditions, but that has not yet been done.)
/**
* Sets the color of one of the rectangles in the window.
*
* Precondition: row and col are in the valid range of row and column numbers,
* and r, g, and b are in the range 0 to 255, inclusive.
* Postcondition: The color of the rectangle in row number row and column
* number col has been set to the color specified by r, g,
* and b. r gives the amount of red in the color with 0
* representing no red and 255 representing the maximum
* possible amount of red. The larger the value of r, the
* more red in the color. g and b work similarly for the
* green and blue color components.
*/
public static void setColor(int row, int col, int r, int g, int b)
/**
* Gets the red component of the color of one of the rectangles.
*
* Precondition: row and col are in the valid range of row and column numbers.
* Postcondition: The red component of the color of the specified rectangle is
* returned as an integer in the range 0 to 255 inclusive.
*/
public static int getRed(int row, int col)
/**
* Like getRed, but returns the green component of the color.
*/
public static int getGreen(int row, int col)
/**
* Like getRed, but returns the blue component of the color.
*/
public static int getBlue(int row, int col)
/**
* Tests whether the mosaic window is currently open.
*
* Precondition: None.
* Postcondition: The return value is true if the window is open when this
* function is called, and it is false if the window is
* closed.
*/
public static boolean isOpen()
/**
4.6. MORE ON PROGRAM DESIGN 149
* Inserts a delay in the program (to regulate the speed at which the colors
* are changed, for example).
*
* Precondition: milliseconds is a positive integer.
* Postcondition: The program has paused for at least the specified number
* of milliseconds, where one second is equal to 1000
* milliseconds.
*/
public static void delay(int milliseconds)
Remember that these subroutines are members of the Mosaic class, so when they are called
from outside Mosaic, the name of the class must be included as part of the name of the routine.
For example, we’ll have to use the name Mosaic.isOpen() rather than simply isOpen().
∗ ∗ ∗
My idea is to use the Mosaic class as the basis for a neat animation. I want to fill the
window with randomly colored squares, and then randomly change the colors in a loop that
continues as long as the window is open. “Randomly change the colors” could mean a lot
of different things, but after thinking for a while, I decide it would be interesting to have a
“disturbance” that wanders randomly around the window, changing the color of each square
that it encounters. Here’s a picture showing what the contents of the window might look like
at one point in time:
With basic routines for manipulating the window as a foundation, I can turn to the specific
problem at hand. A basic outline for my program is
Open a Mosaic window
Fill window with random colors;
Move around, changing squares at random.
Filling the window with random colors seems like a nice coherent task that I can work on
separately, so let’s decide to write a separate subroutine to do it. The third step can be
expanded a bit more, into the steps: Start in the middle of the window, then keep moving to a
new square and changing the color of that square. This should continue as long as the mosaic
window is still open. Thus we can refine the algorithm to:
Open a Mosaic window
Fill window with random colors;
Set the current position to the middle square in the window;
As long as the mosaic window is open:
Randomly change color of the square at the current position;
Move current position up, down, left, or right, at random;
150 CHAPTER 4. SUBROUTINES
I need to represent the current position in some way. That can be done with two int variables
named currentRow and currentColumn that hold the row number and the column number of
the square where the disturbance is currently located. I’ll use 10 rows and 20 columns of squares
in my mosaic, so setting the current position to be in the center means setting currentRow to 5
and currentColumn to 10. I already have a subroutine, Mosaic.open(), to open the window,
and I have a function, Mosaic.isOpen(), to test whether the window is open. To keep the
main routine simple, I decide that I will write two more subroutines of my own to carry out
the two tasks in the while loop. The algorithm can then be written in Java as:
Mosaic.open(10,20,10,10)
fillWithRandomColors();
currentRow = 5; // Middle row, halfway down the window.
currentColumn = 10; // Middle column.
while ( Mosaic.isOpen() ) {
changeToRandomColor(currentRow, currentColumn);
randomMove();
}
With the proper wrapper, this is essentially the main() routine of my program. It turns out I
have to make one small modification: To prevent the animation from running too fast, the line
“Mosaic.delay(20);” is added to the while loop.
The main() routine is taken care of, but to complete the program, I still have to write the
subroutines fillWithRandomColors(), changeToRandomColor(int,int), and randomMove().
Writing each of these subroutines is a separate, small task. The fillWithRandomColors()
routine is defined by the postcondition that “each of the rectangles in the mosaic has been
changed to a random color.” Pseudocode for an algorithm to accomplish this task can be given
as:
For each row:
For each column:
set the square in that row and column to a random color
“For each row” and “for each column” can be implemented as for loops. We’ve already planned
to write a subroutine changeToRandomColor that can be used to set the color. (The possi-
bility of reusing subroutines in several places is one of the big payoffs of using them!) So,
fillWithRandomColors() can be written in proper Java as:
static void fillWithRandomColors() {
for (int row = 0; row < 10; row++)
for (int column = 0; column < 20; column++)
changeToRandomColor(row,column);
}
Turning to the changeToRandomColor subroutine, we already have a method in the Mosaic
class, Mosaic.setColor(), that can be used to change the color of a square. If we want a ran-
dom color, we just have to choose random values for r, g, and b. According to the precondition
of the Mosaic.setColor() subroutine, these random values must be integers in the range from
0 to 255. A formula for randomly selecting such an integer is “(int)(256*Math.random())”.
So the random color subroutine becomes:
static void changeToRandomColor(int rowNum, int colNum) {
int red = (int)(256*Math.random());
int green = (int)(256*Math.random());
int blue = (int)(256*Math.random());
4.6. MORE ON PROGRAM DESIGN 151
mosaic.setColor(rowNum,colNum,red,green,blue);
}
Finally, consider the randomMove subroutine, which is supposed to randomly move the
disturbance up, down, left, or right. To make a random choice among four directions, we
can choose a random integer in the range 0 to 3. If the integer is 0, move in one direction;
if it is 1, move in another direction; and so on. The position of the disturbance is given
by the variables currentRow and currentColumn. To “move up” means to subtract 1 from
currentRow. This leaves open the question of what to do if currentRow becomes -1, which
would put the disturbance above the window. Rather than let this happen, I decide to move
the disturbance to the opposite edge of the applet by setting currentRow to 9. (Remember
that the 10 rows are numbered from 0 to 9.) Moving the disturbance down, left, or right is
handled similarly. If we use a switch statement to decide which direction to move, the code
for randomMove becomes:
int directionNum;
directionNum = (int)(4*Math.random());
switch (directionNum) {
case 0: // move up
currentRow--;
if (currentRow < 0) // CurrentRow is outside the mosaic;
currentRow = 9; // move it to the opposite edge.
break;
case 1: // move right
currentColumn++;
if (currentColumn >= 20)
currentColumn = 0;
break;
case 2: // move down
currentRow++;
if (currentRow >= 10)
currentRow = 0;
break;
case 3: // move left
currentColumn--;
if (currentColumn < 0)
currentColumn = 19;
break;
}
/**
* This program opens a window full of randomly colored squares. A "disturbance"
* moves randomly around in the window, randomly changing the color of each
* square that it visits. The program runs until the user closes the window.
*/
public class RandomMosaicWalk {
static int currentRow; // Row currently containing the disturbance.
static int currentColumn; // Column currently containing disturbance.
/**
* The main program creates the window, fills it with random colors,
* and then moves the disturbances in a random walk around the window
* as long as the window is open.
*/
public static void main(String[] args) {
Mosaic.open(10,20,10,10);
fillWithRandomColors();
currentRow = 5; // start at center of window
currentColumn = 10;
while (Mosaic.isOpen()) {
changeToRandomColor(currentRow, currentColumn);
randomMove();
Mosaic.delay(20);
}
} // end main
/**
* Fills the window with randomly colored squares.
* Precondition: The mosaic window is open.
* Postcondition: Each square has been set to a random color.
*/
static void fillWithRandomColors() {
for (int row=0; row < 10; row++) {
for (int column=0; column < 20; column++) {
changeToRandomColor(row, column);
}
}
} // end fillWithRandomColors
/**
* Changes one square to a new randomly selected color.
* Precondition: The specified rowNum and colNum are in the valid range
* of row and column numbers.
* Postcondition: The square in the specified row and column has
* been set to a random color.
* @param rowNum the row number of the square, counting rows down
* from 0 at the top
* @param colNum the column number of the square, counting columns over
* from 0 at the left
*/
static void changeToRandomColor(int rowNum, int colNum) {
int red = (int)(256*Math.random()); // Choose random levels in range
int green = (int)(256*Math.random()); // 0 to 255 for red, green,
int blue = (int)(256*Math.random()); // and blue color components.
4.7. THE TRUTH ABOUT DECLARATIONS 153
Mosaic.setColor(rowNum,colNum,red,green,blue);
} // end of changeToRandomColor()
/**
* Move the disturbance.
* Precondition: The global variables currentRow and currentColumn
* are within the legal range of row and column numbers.
* Postcondition: currentRow or currentColumn is changed to one of the
* neighboring positions in the grid -- up, down, left, or
* right from the current position. If this moves the
* position outside of the grid, then it is moved to the
* opposite edge of the grid.
*/
static void randomMove() {
int directionNum; // Randomly set to 0, 1, 2, or 3 to choose direction.
directionNum = (int)(4*Math.random());
switch (directionNum) {
case 0: // move up
currentRow--;
if (currentRow < 0)
currentRow = 9;
break;
case 1: // move right
currentColumn++;
if (currentColumn >= 20)
currentColumn = 0;
break;
case 2: // move down
currentRow++;
if (currentRow >= 10)
currentRow = 0;
break;
case 3: // move left
currentColumn--;
if (currentColumn < 0)
currentColumn = 19;
break;
}
} // end randomMove
This feature is especially common in for loops, since it makes it possible to declare a loop control
variable at the same point in the loop where it is initialized. Since the loop control variable
generally has nothing to do with the rest of the program outside the loop, it’s reasonable to
have its declaration in the part of the program where it’s actually used. For example:
for ( int i = 0; i < 10; i++ ) {
System.out.println(i);
}
Again, you should remember that this is simply an abbreviation for the following, where I’ve
added an extra pair of braces to show that i is considered to be local to the for statement and
no longer exists after the for loop ends:
{
int i;
for ( i = 0; i < 10; i++ ) {
System.out.println(i);
}
}
(You might recall, by the way, that for “for-each” loops, the special type of for statement
that is used with enumerated types, declaring the variable in the for is required. See Subsec-
tion 3.4.4.)
A member variable can also be initialized at the point where it is declared, just as for a
local variable. For example:
public class Bank {
static double interestRate = 0.05;
static int maxWithdrawal = 200;
4.7. THE TRUTH ABOUT DECLARATIONS 155
.
. // More variables and subroutines.
.
}
A static member variable is created as soon as the class is loaded by the Java interpreter, and
the initialization is also done at that time. In the case of member variables, this is not simply
an abbreviation for a declaration followed by an assignment statement. Declaration statements
are the only type of statement that can occur outside of a subroutine. Assignment statements
cannot, so the following is illegal:
public class Bank {
static double interestRate;
interestRate = 0.05; // ILLEGAL:
. // Can’t be outside a subroutine!:
.
.
Because of this, declarations of member variables often include initial values. In fact, as
mentioned in Subsection 4.2.4, if no initial value is provided for a member variable, then a
default initial value is used. For example, when declaring an integer member variable, count,
“static int count;” is equivalent to “static int count = 0;”.
Similarly, the Color class contains named constants such as Color.RED and Color.YELLOW
which are public final static variables of type Color. Many named constants are created just to
give meaningful names to be used as parameters in subroutine calls. For example, the standard
class named Font contains named constants Font.PLAIN, Font.BOLD, and Font.ITALIC. These
constants are used for specifying different styles of text when calling various subroutines in the
Font class.
Enumerated type constants (See Subsection 2.3.3.) are also examples of named constants.
The enumerated type definition
enum Alignment { LEFT, RIGHT, CENTER }
defines the constants Alignment.LEFT, Alignment.RIGHT, and Alignment.CENTER. Technically,
Alignment is a class, and the three constants are public final static members of that class.
Defining the enumerated type is similar to defining three constants of type, say, int:
public static final int ALIGNMENT LEFT = 0;
public static final int ALIGNMNENT RIGHT = 1;
public static final int ALIGNMENT CENTER = 2;
In fact, this is how things were generally done before the introduction of enumerated types in
Java 5.0, and it is what is done with the constants Font.PLAIN, Font.BOLD, and Font.ITALIC
mentioned above. Using the integer constants, you could define a variable of type int and assign
it the values ALIGNMENT LEFT, ALIGNMENT RIGHT, or ALIGNMENT CENTER to represent different
types of alignment. The only problem with this is that the computer has no way of knowing
that you intend the value of the variable to represent an alignment, and it will not raise any
objection if the value that is assigned to the variable is not one of the three valid alignment
values.
With the enumerated type, on the other hand, the only values that can be assigned to
a variable of type Alignment are the constant values that are listed in the definition of the
enumerated type. Any attempt to assign an invalid value to the variable is a syntax error
which the computer will detect when the program is compiled. This extra safety is one of the
major advantages of enumerated types.
∗ ∗ ∗
Curiously enough, one of the major reasons to use named constants is that it’s easy to
change the value of a named constant. Of course, the value can’t change while the program
is running. But between runs of the program, it’s easy to change the value in the source code
and recompile the program. Consider the interest rate example. It’s quite possible that the
value of the interest rate is used many times throughout the program. Suppose that the bank
changes the interest rate and the program has to be modified. If the literal number 0.05 were
used throughout the program, the programmer would have to track down each place where
the interest rate is used in the program and change the rate to the new value. (This is made
even harder by the fact that the number 0.05 might occur in the program with other meanings
besides the interest rate, as well as by the fact that someone might have used 0.025 to represent
half the interest rate.) On the other hand, if the named constant INTEREST RATE is declared
and used consistently throughout the program, then only the single line where the constant is
initialized needs to be changed.
As an extended example, I will give a new version of the RandomMosaicWalk program from
the previous section. This version uses named constants to represent the number of rows in
the mosaic, the number of columns, and the size of each little square. The three constants are
declared as final static member variables with the lines:
4.7. THE TRUTH ABOUT DECLARATIONS 157
currentRow--;
if (currentRow < 0)
currentRow = ROWS - 1;
break;
case 1: // move right
currentColumn++;
if (currentColumn >= COLUMNS)
currentColumn = 0;
break;
case 2: // move down
currentRow ++;
if (currentRow >= ROWS)
currentRow = 0;
break;
case 3: // move left
currentColumn--;
if (currentColumn < 0)
currentColumn = COLUMNS - 1;
break;
}
} // end randomMove
} // end class RandomMosaicWalk2
.
. // More variables and subroutines.
.
} // end Game
In the statements that make up the body of the playGame() subroutine, the name “count”
refers to the local variable. In the rest of the Game class, “count” refers to the member vari-
able, unless hidden by other local variables or parameters named count. However, there is
one further complication. The member variable named count can also be referred to by the
full name Game.count. Usually, the full name is only used outside the class where count is
defined. However, there is no rule against using it inside the class. The full name, Game.count,
can be used inside the playGame() subroutine to refer to the member variable. So, the full
scope rule is that the scope of a static member variable includes the entire class in which it
is defined, but where the simple name of the member variable is hidden by a local variable
or formal parameter name, the member variable must be referred to by its full name of the
form hclassNamei.hvariableNamei. (Scope rules for non-static members are similar to those
for static members, except that, as we shall see, non-static members cannot be used in static
subroutines.)
The scope of a formal parameter of a subroutine is the block that makes up the body of the
subroutine. The scope of a local variable extends from the declaration statement that defines
the variable to the end of the block in which the declaration occurs. As noted above, it is
possible to declare a loop control variable of a for loop in the for statement, as in “for (int
i=0; i < 10; i++)”. The scope of such a declaration is considered as a special case: It is
valid only within the for statement and does not extend to the remainder of the block that
contains the for statement.
It is not legal to redefine the name of a formal parameter or local variable within its scope,
even in a nested block. For example, this is not allowed:
void badSub(int y) {
int x;
while (y > 0) {
int x; // ERROR: x is already defined.
.
.
.
}
}
In many languages, this would be legal; the declaration of x in the while loop would hide
the original declaration. It is not legal in Java; however, once the block in which a variable is
declared ends, its name does become available for reuse in Java. For example:
void goodSub(int y) {
while (y > 10) {
int x;
.
.
.
// The scope of x ends here.
}
while (y > 0) {
160 CHAPTER 4. SUBROUTINES
1. To “capitalize” a string means to change the first letter of each word in the string to upper
case (if it is not already upper case). For example, a capitalized version of “Now is the time
to act!” is “Now Is The Time To Act!”. Write a subroutine named printCapitalized
that will print a capitalized version of a string to standard output. The string to be printed
should be a parameter to the subroutine. Test your subroutine with a main() routine that
gets a line of input from the user and applies the subroutine to it.
Note that a letter is the first letter of a word if it is not immediately preceded in
the string by another letter. Recall that there is a standard boolean-valued function
Character.isLetter(char) that can be used to test whether its parameter is a letter.
There is another standard char-valued function, Character.toUpperCase(char), that
returns a capitalized version of the single character passed to it as a parameter. That is,
if the parameter is a letter, it returns the upper-case version. If the parameter is not a
letter, it just returns a copy of the parameter.
2. The hexadecimal digits are the ordinary, base-10 digits ’0’ through ’9’ plus the letters ’A’
through ’F’. In the hexadecimal system, these digits represent the values 0 through 15,
respectively. Write a function named hexValue that uses a switch statement to find the
hexadecimal value of a given character. The character is a parameter to the function, and
its hexadecimal value is the return value of the function. You should count lower case
letters ’a’ through ’f’ as having the same value as the corresponding upper case letters.
If the parameter is not one of the legal hexadecimal digits, return -1 as the value of the
function.
A hexadecimal integer is a sequence of hexadecimal digits, such as 34A7, FF8, 174204,
or FADE. If str is a string containing a hexadecimal integer, then the corresponding
base-10 integer can be computed as follows:
value = 0;
for ( i = 0; i < str.length(); i++ )
value = value*16 + hexValue( str.charAt(i) );
Of course, this is not valid if str contains any characters that are not hexadecimal digits.
Write a program that reads a string from the user. If all the characters in the string are
hexadecimal digits, print out the corresponding base-10 value. If not, print out an error
message.
3. Write a function that simulates rolling a pair of dice until the total on the dice comes up
to be a given number. The number that you are rolling for is a parameter to the function.
The number of times you have to roll the dice is the return value of the function. The
parameter should be one of the possible totals: 2, 3, . . . , 12. The function should throw
an IllegalArgumentException if this is not the case. Use your function in a program that
computes and prints the number of rolls it takes to get snake eyes. (Snake eyes means
that the total showing on the dice is 2.)
4. This exercise builds on Exercise 4.3. Every time you roll the dice repeatedly, trying to
get a given total, the number of rolls it takes can be different. The question naturally
arises, what’s the average number of rolls to get a given total? Write a function that
performs the experiment of rolling to get a given total 10000 times. The desired total is
162 CHAPTER 4. SUBROUTINES
a parameter to the subroutine. The average number of rolls is the return value. Each
individual experiment should be done by calling the function you wrote for Exercise 4.3.
Now, write a main program that will call your function once for each of the possible totals
(2, 3, ..., 12). It should make a table of the results, something like:
Total On Dice Average Number of Rolls
------------- -----------------------
2 35.8382
3 18.0607
. .
. .
6. For this exercise, you will write another program based on the non-standard Mosaic class
that was presented in Section 4.6. While the program does not do anything particularly
interesting, it’s interesting as a programming problem. An applet that does the same
thing as the program can be seen in the on-line version of this book. Here is a picture
showing what it looks like at several different times:
The program will show a rectangle that grows from the center of the applet to the edges,
getting brighter as it grows. The rectangle is made up of the little squares of the mosaic.
You should first write a subroutine that draws a rectangle on a Mosaic window. More
specifically, write a subroutine named rectangle such that the subroutine call statement
rectangle(top,left,height,width,r,g,b);
Exercises 163
will call Mosaic.setColor(row,col,r,g,b) for each little square that lies on the outline
of a rectangle. The topmost row of the rectangle is specified by top. The number of
rows in the rectangle is specified by height (so the bottommost row is top+height-1).
The leftmost column of the rectangle is specified by left. The number of columns in the
rectangle is specified by width (so the rightmost column is left+width-1.)
The animation loops through the same sequence of steps over and over. In each step,
a rectangle is drawn in gray (that is, with all three color components having the same
value). There is a pause of 200 milliseconds so the user can see the rectangle. Then the
very same rectangle is drawn in black, effectively erasing the gray rectangle. Finally, the
variables giving the top row, left column, size, and color level of the rectangle are adjusted
to get ready for the next step. In the applet, the color level starts at 50 and increases by
10 after each step. When the rectangle gets to the outer edge of the applet, the loop ends.
The animation then starts again at the beginning of the loop. You might want to make a
subroutine that does one loop through all the steps of the animation.
The main() routine simply opens a Mosaic window and then does the animation loop
over and over until the user closes the window. There is a 1000 millisecond delay between
one animation loop and the next. Use a Mosaic window that has 41 rows and 41 columns.
(I advise you not to used named constants for the numbers of rows and columns, since
the problem is complicated enough already.)
164 CHAPTER 4. SUBROUTINES
Quiz on Chapter 4
1. A “black box” has an interface and an implementation. Explain what is meant by the
terms interface and implementation.
3. Briefly explain how subroutines can be a useful tool in the top-down design of programs.
4. Discuss the concept of parameters. What are parameters for? What is the difference
between formal parameters and actual parameters?
5. Give two different reasons for using named constants (declared with the final modifier).
7. Write a subroutine named “stars” that will output a line of stars to standard output. (A
star is the character “*”.) The number of stars should be given as a parameter to the
subroutine. Use a for loop. For example, the command “stars(20)” would output
********************
8. Write a main() routine that uses the subroutine that you wrote for Question 7 to output
10 lines of stars with 1 star in the first line, 2 stars in the second line, and so on, as shown
below.
*
**
***
****
*****
******
*******
********
*********
**********
9. Write a function named countChars that has a String and a char as parameters. The
function should count the number of times the character occurs in the string, and it should
return the result as the value of the function.
10. Write a subroutine with three parameters of type int. The subroutine should determine
which of its parameters is smallest. The value of the smallest parameter should be returned
as the value of the subroutine.
Chapter 5
Whereas a subroutine represents a single task, an object can encapsulate both data (in
the form of instance variables) and a number of different tasks or “behaviors” related to that
data (in the form of instance methods). Therefore objects provide another, more sophisticated
type of structure that can be used to help manage the complexity of large programs.
This chapter covers the creation and use of objects in Java. Section 5.5 covers the central
ideas of object-oriented programming: inheritance and polymorphism. However, in this text-
book, we will generally use these ideas in a limited form, by creating independent classes and
building on existing classes rather than by designing entire hierarchies of classes from scratch.
Section 5.6 and Section 5.7 cover some of the many details of object oriented programming in
Java. Although these details are used occasionally later in the book, you might want to skim
through them now and return to them later when they are actually needed.
165
166 CHAPTER 5. OBJECTS AND CLASSES
In a program that uses this class, there is only one copy of each of the variables UserData.name
and UserData.age. There can only be one “user,” since we only have memory space to store
data about one user. The class, UserData, and the variables it contains exist as long as the
program runs. Now, consider a similar class that includes non-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. So, there is nothing much in the class at all—
except the potential to create objects. But, it’s a lot of potential, since it can be used to create
any number of objects! Each object will have its own variables called name and age. There
can be many “players” because we can make new objects to represent new players on demand.
A program might use this class to store information about multiple players in a game. Each
player has a name and an age. When a player joins the game, a new PlayerData object can
be created to represent that player. If a player leaves the game, the PlayerData object that
represents that player can be destroyed. A system of objects in the program is being used to
dynamically model what is happening in the game. You can’t do this with “static” variables!
In Section 3.8, we worked with applets, which are objects. The reason they didn’t seem to
be any different from classes is because we were only working with one applet in each class that
we looked at. But one class can be used to make many applets. Think of an applet that scrolls
5.1. OBJECTS AND INSTANCE METHODS 167
a message across a Web page. There could be several such applets on the same page, all created
from the same class. If the scrolling message in the applet is stored in a non-static variable,
then each applet will have its own variable, and each applet can show a different message. The
situation is even clearer if you think about windows, which, like applets, are objects. As a
program runs, many windows might be opened and closed, but all those windows can belong
to the same class. Here again, we have a dynamic situation where multiple objects are created
and destroyed as a program runs.
∗ ∗ ∗
An object that belongs to a class is said to be an instance of that class. The variables that
the object contains are called instance variables. The subroutines that the object contains
are called instance methods. (Recall that in the context of object-oriented programming,
method is a synonym for “subroutine”. From now on, since we are doing object-oriented
programming, I will prefer the term “method.”) For example, if the PlayerData class, as
defined above, is used to create an object, then that object is an instance of the PlayerData
class, and name and age are instance variables in the object. It is important to remember that
the class of an object determines the types of the instance variables; however, the actual data
is contained inside the individual objects, not the class. Thus, each object has its own set of
data.
An applet that scrolls a message across a Web page might include a subroutine named
scroll(). Since the applet is an object, this subroutine is an instance method of the applet.
The source code for the method is in the class that is used to create the applet. Still, it’s better
to think of the instance method as belonging to the object, not to the class. The non-static
subroutines in the class merely specify the instance methods that every object created from the
class will contain. The scroll() methods in two different applets do the same thing in the
sense that they both scroll messages across the screen. But there is a real difference between
the two scroll() methods. The messages that they scroll can be different. You might say that
the method definition in the class specifies what type of behavior the objects will have, but
the specific behavior can vary from object to object, depending on the values of their instance
variables.
As you can see, the static and the non-static portions of a class are very different things and
serve very different purposes. Many classes contain only static members, or only non-static.
However, it is possible to mix static and non-static members in a single class, and we’ll see
a few examples later in this chapter where it is reasonable to do so. You should distiguish
between the source code for the class, and the class itself. The source code determines both
the class and the objects that are created from that class. The “static” definitions in the source
code specify the things that are part of the class itself, whereas the non-static definitions in the
source code specify things that will become part of every instance object that is created from
the class. By the way, static member variables and static member subroutines in a class are
sometimes called class variables and class methods, since they belong to the class itself,
rather than to instances of that class.
You should think of objects as floating around independently in the computer’s memory. In
fact, there is a special portion of memory called the heap where objects live. Instead of holding
an object itself, a variable holds the information necessary to find the object in memory. This
information is called a reference or pointer to the object. In effect, a reference to an object
is the address of the memory location where the object is stored. When you use a variable of
class type, the computer uses the reference in the variable to find the actual object.
In a program, objects are created using an operator called new, which creates an object
and returns a reference to that object. For example, assuming that std is 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 would store a
reference to that object in the variable std. The value of the variable is a reference to the
object, not the object itself. It is not quite true, then, to say that the object is the “value of
the variable std” (though sometimes it is hard to avoid using this terminology). It is certainly
not at all true to say that the object is “stored in the variable std.” The proper terminology
is that “the variable std refers to the object,” and I will try to stick to that terminology as
much as possible.
So, suppose that the variable std refers to an object belonging to the class Student. That
object has instance variables name, test1, test2, and test3. These instance variables can
5.1. OBJECTS AND INSTANCE METHODS 169
be referred to as std.name, std.test1, std.test2, and std.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. Simi-
larly, std can be used to call the getAverage() instance method in the object by saying
std.getAverage(). To print out the student’s average, you could say:
System.out.println( "Your average is " + std.getAverage() );
More generally, you could use std.name any place where a variable of type String is legal.
You can use it in expressions. You can assign a value to it. You can even use it to call subroutines
from the String class. For example, std.name.length() is the number of characters in the
student’s name.
It is possible for a variable like std, whose type is given by a class, to refer to no object at
all. We say in this case that std holds a null reference. The null reference is written in Java
as “null”. You can store a null reference in the variable std by saying
std = null;
and you could test whether the value of std is null by testing
if (std == null) . . .
If the value of a variable is null, then it is, of course, illegal to refer to instance variables
or instance methods through that variable—since there is no object, and hence no instance
variables to refer to. For example, if the value of the variable std is null, then it would be
illegal to refer to std.test1. If your program attempts to use a null reference illegally like this,
the result is an error called a null pointer exception.
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.)
170 CHAPTER 5. OBJECTS AND CLASSES
After the computer executes these statements, the situation in the computer’s memory looks
like this:
This picture shows variables as little boxes, labeled with the names of the variables. Objects
are shown as boxes with round corners. When a variable contains a reference to an object, the
value of that variable is shown as an arrow pointing to the object. The variable std3, with a
value of null, doesn’t point anywhere. The arrows from std1 and std2 both point to the same
object. This illustrates a Very Important Point:
When the assignment “std2 = std1;” was executed, no new object was created. 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.name and std2.name are two different names for the
same variable, namely the instance variable in the object that both std1 and std2 refer to.
After the string "Mary Jones" is assigned to the variable std1.name, it is also true that the
value of std2.name is "Mary Jones". There is a potential for a lot of confusion here, but you
can help protect yourself from it if you keep telling yourself, “The object is not 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. When you make a test
“if (std1 == std2)”, you are testing whether the values stored in std1 and std2 are the
same. But the values are references to objects, not objects. So, you are testing whether
std1 and std2 refer to the same object, that is, whether they point to the same location
5.1. OBJECTS AND INSTANCE METHODS 171
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 the same 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 variable of type String can only hold
a reference to a string, not the string itself. It could also hold the value null, meaning that
it does not refer to any string at all. This explains why using the == operator to test strings
for equality is not a good idea. Suppose that greeting is a variable of type String, and that
the string it refers to is "Hello". Then would the test greeting == "Hello" be true? Well,
maybe, maybe not. The variable greeting and the String literal "Hello" each refer to a
string that contains the characters H-e-l-l-o. But the strings could still be different objects,
that just happen to contain the same characters. The function greeting.equals("Hello")
tests whether greeting and "Hello" contain the same characters, which is almost certainly
the question you want to ask. The expression greeting == "Hello" tests whether greeting
and "Hello" contain the same characters stored in the same memory location.
∗ ∗ ∗
The fact that variables hold references to objects, not objects themselves, has a couple 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. This means that
the value stored in the variable can never be changed, once the variable has 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 variable exists. However, this does not prevent the data
in the object from changing. The variable 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 consider what happens
when obj is passed as an actual parameter to a subroutine. The value of obj is assigned to
a formal parameter in the subroutine, and the subroutine is executed. The subroutine has no
power to change the value stored in the variable, obj. It only has a copy of that value. However,
that value is a reference to an object. Since the subroutine has a reference to the object, it can
change the data stored in the object. After the subroutine ends, obj still points to the same
object, but the data stored in the object might have changed. Suppose x is a variable of type
int and stu is a variable of type Student. Compare:
void dontChange(int z) { void change(Student s) {
z = 42; s.name = "Fred";
} }
The lines: The lines:
x = 17; stu.name = "Jane";
dontChange(x); change(stu);
System.out.println(x); System.out.println(stu.name);
172 CHAPTER 5. OBJECTS AND CLASSES
die2 = (int)(Math.random()*6) + 1;
}
} // end class PairOfDice
The instance variables die1 and die2 are initialized to the values 3 and 4 respectively. These
initializations are executed whenever a PairOfDice object is constructed. It’s important to
understand when and how this happens. There can be many PairOfDice objects. Each time one
is created, it gets its own instance variables, and the assignments “die1 = 3” and “die2 = 4”
are executed to fill in the values of those variables. To make this clearer, consider a variation
of the PairOfDice 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 being thrown onto
the gaming table. Since the initialization is executed for each new 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 static member variables, of course, the situation is quite
different. There is only one copy 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 initial value is pro-
vided 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 Unicode character with code number zero. An instance variable can also
be a variable of object 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.)
5.2.2 Constructors
Objects are created with the operator, new. For example, a program that wants to 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 memory for the object,
initializes the object’s instance variables, and then returns a reference to the object. This
reference is the value of the expression, and that value is stored by the assignment statement in
the variable, dice, so that after the assignment statement is executed, dice refers to the newly
created object. Part of this expression, “PairOfDice()”, looks like a subroutine call, and that
is no accident. It is, in fact, a call to a special type of subroutine called a constructor . This
might puzzle you, since there is no such subroutine in the class definition. However, every class
has at least one constructor. If the programmer doesn’t write a constructor definition in a class,
5.2. CONSTRUCTORS AND OBJECT INITIALIZATION 175
then the system will provide a default constructor for that class. This default constructor
does nothing beyond the basics: allocate memory and initialize instance variables. If you want
more than that to happen when an object is created, you can include one or more constructors
in the class definition.
The definition of a constructor looks much like the definition of any other subroutine, with
three exceptions. A constructor does not have any return type (not even void). The name of
the constructor must be the same as the name of the class in which it is defined. The only
modifiers that can be used on a constructor definition are the access modifiers public, private,
and protected. (In particular, a constructor can’t be declared static.)
However, a constructor does have a subroutine body of the usual form, a block of statements.
There are no restrictions on what statements can be used. And it can have a list of formal
parameters. In fact, the ability to include parameters is one of the main reasons for using
constructors. The parameters can provide data to be used in the construction of the object.
For example, a constructor for the PairOfDice class could provide the values that are initially
showing on the dice. Here is what the class would look like in that case:
public class PairOfDice {
public int die1; // Number showing on the first die.
public int die2; // Number showing on the second die.
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
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 how the Java com-
piler 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 3 and 4. Of course, in a program, the value returned by
the constructor should be used 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 longer create
an object by saying “new PairOfDice()”! The system provides a default constructor for a
class only if the class definition does not already include a constructor, so there is only one
constructor in the class, and it requires two actual parameters. However, this is not a big
176 CHAPTER 5. OBJECTS AND CLASSES
problem, since we can add a second constructor to the class, one that has no parameters. In
fact, you can have as many different constructors as you want, as long as their signatures are
different, that is, as long as they have different numbers or types of formal parameters. In the
PairOfDice class, we might have a constructor with no parameters which produces 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
// 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 either with “new PairOfDice()”
or with “new PairOfDice(x,y)”, where x and y are int-valued expressions.
This class, once it is written, can be used in any program that needs to work with one
or more pairs of dice. None of those programs will ever have to use the obscure incantation
“(int)(Math.random()*6)+1”, because it’s done inside the PairOfDice class. And the pro-
grammer, having once gotten the dice-rolling thing straight will never have to worry about it
again. Here, for example, is a main program that uses the PairOfDice class to count how many
times two pairs of dice are rolled before the two pairs come up showing the same value. This
illustrates 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.
int total1; // Total showing on first pair of dice.
int total2; // Total showing on second pair of dice.
5.2. CONSTRUCTORS AND OBJECT INITIALIZATION 177
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 subroutines, but they are subroutines of a special type. They are certainly
not instance methods, since they don’t belong to objects. Since they are responsible for creating
objects, they exist before any objects have been created. They are more like static member
subroutines, but they are not and cannot be declared to be static. In fact, according to the
Java language specification, they are technically not members of the class at all! In particular,
constructors are not referred to as “methods.”
Unlike other subroutines, a constructor can only be called using the new operator, in an
expression that has the form
new hclass-name i ( hparameter-list i )
where the hparameter-listi is possibly empty. I call this an expression because it computes and
returns a value, namely a reference to the object that is constructed. Most often, you will store
the returned reference in a variable, but it is also legal to use a constructor call in other ways,
for example as a parameter in a subroutine call or as part of a more complex expression. Of
course, if you don’t save the reference in a variable, you won’t have any way of referring to the
object that was just created.
A constructor call is more complicated than an ordinary subroutine or function call. It is
helpful to understand the exact steps that the computer goes through to execute a constructor
call:
1. First, the computer gets a block of unused memory in the heap, large enough to hold an
object of the specified type.
2. It initializes the instance variables of the object. If the declaration of an instance variable
specifies an initial value, then that value is computed and stored in the instance variable.
Otherwise, the default initial value is used.
3. The actual parameters in the constructor, if any, are evaluated, and the values are assigned
to the formal parameters of the constructor.
178 CHAPTER 5. OBJECTS AND CLASSES
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 call its instance methods.
∗ ∗ ∗
For another example, let’s rewrite the Student class that was used in Section 1. I’ll add a
constructor, and I’ll also take the opportunity to make the instance variable, name, private.
public class Student {
Student(String theName) {
// Constructor for Student objects;
// provides a name for the Student.
name = theName;
}
An object of type Student contains information about some particular student. The con-
structor in this class has a parameter of type String, which specifies the name of that student.
Objects of type Student can be created with statements such 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 a program after
it created the object of type Student. There was no guarantee that the programmer would
always remember to set the name properly. In the new version of the class, there is no way to
create a Student object except by calling the constructor, and that constructor automatically
sets the name. The programmer’s life is made easier, and whole hordes of frustrating bugs are
squashed before they even have a chance to be born.
Another type of guarantee is provided by the private modifier. Since the instance variable,
name, is private, there is no way for any part of the program outside the Student class to
get at the name directly. The program sets the value of name, indirectly, when it calls the
constructor. I’ve provided a function, getName(), that can be used from outside the class to
find out the name of the student. But I haven’t provided any setter method or other way to
change the name. Once a student object is created, it keeps the same name as long as it exists.
5.3. PROGRAMMING WITH OBJECTS 179
In the first line, a reference to a newly created Student object is stored in the variable std.
But in the next line, the value of std is changed, and the reference to the Student object is
gone. In fact, there are now no references whatsoever to that object stored in any variable. So
there is no way for the program ever to use the object again. It might as well not exist. In fact,
the memory occupied by the object should be reclaimed to be used for another purpose.
Java uses a procedure called garbage collection to reclaim memory occupied by objects
that are no longer accessible to a program. It is the responsibility of the system, not the
programmer, to keep track of which objects are “garbage.” In the above example, it was very
easy to see that the Student object had become garbage. Usually, it’s much harder. If an
object has been used for a while, there might be several references to the object stored in several
variables. The object doesn’t become garbage until all those references have been dropped.
In many other programming languages, it’s the programmer’s responsibility to delete the
garbage. Unfortunately, keeping track of memory usage is very error-prone, and many serious
program bugs are caused by such errors. A programmer might accidently delete an object even
though there are still references to that object. This is called a dangling pointer error , and
it leads to problems when the program tries to access an object that is no longer there. Another
type of error is a memory leak , where a programmer neglects to delete objects that are no
longer in use. This can lead to filling memory with objects that are completely inaccessible,
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. Garbage collection
is an old idea and has been used in some programming languages since the 1960s. You might
wonder why all languages don’t use garbage collection. In the past, it was considered too slow
and wasteful. However, research into garbage collection techniques combined with the incredible
speed of modern computers have combined to make garbage collection feasible. Programmers
should rejoice.
Of course, for the most part, you will experience “generalized software components” by
using the standard classes that come along with Java. We begin this section by looking at some
built-in classes that are used for creating objects. At the end of the section, we will get back
to generalities.
Math.random() uses one of these objects behind the scenes to generate its random numbers.
An object of type Random can generate random integers, as well as random real numbers. If
randGen is created with the command:
Random randGen = new Random();
and if N is a positive integer, then randGen.nextInt(N) generates a random integer in the range
from 0 to N-1. For example, this makes it a little easier to roll a pair of dice. Instead of say-
ing “die1 = (int)(6*Math.random())+1;”, one can say “die1 = randGen.nextInt(6)+1;”.
(Since you also have to import the class java.util.Random and create the Random object, you
might not agree that it is actually easier.) An object of type Random can also be used to generate
so-called Gaussian distributed random real numbers.
The main point here, again, is that many problems have already been solved, and the
solutions are available in Java’s standard classes. If you are faced with a task that looks like
it should be fairly common, it might be worth looking through a Java reference to see whether
someone has already written a class that you can use.
The value of d contains the same information as the value of type double, but it is an object. If
you want to retrieve the double value that is wrapped in the object, you can call the function
d.doubleValue(). Similarly, you can wrap an int 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 classes that will be studied in Chapter 10 can only hold objects. If you want to add
a primitive 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 introduced automatic
conversion between a primitive type and the corresponding wrapper class. For example, if
you use a value of type int in a context that requires an object of type Integer, the int will
automatically be wrapped in an Integer object. For example, you can say
Integer answer = 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 expression such as 2*d. The double value
inside d is automatically unboxed and multiplied by 2. Autoboxing and unboxing also apply
to subroutine calls. For example, you can pass an actual parameter of type int to a subroutine
that has a formal parameter of type Integer. In fact, autoboxing and unboxing make it possible
in many circumstances to ignore the difference between primitive types and objects.
∗ ∗ ∗
The wrapper classes contain a few other things that deserve to be mentioned. Integer, for
example, contains constants Integer.MIN VALUE and Integer.MAX VALUE, which are equal to
the largest and smallest possible values of type int, that is, to -2147483648 and 2147483647
respectively. It’s certainly easier to remember the names than the numerical values. There are
similar named constants in Long, Short, and Byte. 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 times 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 actually being
equal to zero.
The class Double deserves special mention, since doubles are so much more complicated than
integers. The encoding of real numbers into values of type double has room for a few special val-
ues that are not real numbers at all in the mathematical sense. These values are given by named
constants in class Double: Double.POSITIVE INFINITY, Double.NEGATIVE INFINITY, and
Double.NaN. The infinite values can occur as the values of certain mathematical expressions. For
example, dividing a positive number by zero will give the result Double.POSITIVE INFINITY.
(It’s even more complicated than this, actually, because the double type includes a value
called “negative zero”, written -0.0. Dividing a positive number by negative zero gives
Double.NEGATIVE INFINITY.) You also get Double.POSITIVE INFINITY whenever the mathe-
matical value of an expression is greater than Double.MAX VALUE. For example, 1e200*1e200
is considered to be infinite. The value Double.NaN is even more interesting. “NaN” stands for
Not a Number , and it represents an undefined value such as the square root of a negative
number or the result of dividing zero by zero. Because of the existence of Double.NaN, no math-
ematical operation on real numbers will ever throw an exception; it simply gives Double.NaN
as the result.
You can test whether a value, x, of type double is infinite or undefined by calling the
boolean-valued static functions Double.isInfinite(x) and Double.isNaN(x). (It’s especially
important to use Double.isNaN() to test for undefined values, because Double.NaN has re-
ally weird behavior when used with relational operators such as ==. In fact, the values of
x == Double.NaN and x != Double.NaN are both false, no matter what the value of x, so
you really can’t use these expressions to test whether x is Double.NaN.)
some other class, then it automatically becomes a subclass of the special class named Object.
(Object is the one class that is not a subclass of any other class.)
Class Object defines several instance methods that are inherited by every other class. These
methods can be used with any object whatsoever. I will mention just one of them here. You
will encounter more of them later in the book.
The instance method toString() in class Object returns a value of type String that is
supposed to be a string representation of the object. You’ve already used this method implicitly,
any time you’ve printed out an object or concatenated an object onto a string. When you use
an object in a context that requires a string, the object is automatically converted to type
String by calling its toString() method.
The version of toString that is defined in Object just returns the name of the class that
the object belongs to, concatenated with a code number called the hash code of the object;
this is not very useful. When you create a class, you can write a new toString() method for
it, which will replace the inherited version. For example, we might add the following method
to any of the PairOfDice classes from the previous section:
public String toString() {
// Return a String representation of a pair of dice, where die1
// and die2 are instance variables containing the numbers that are
// showing on the two dice.
if (die1 == die2)
return "double " + die1;
else
return die1 + " and " + die2;
}
If dice refers to a PairOfDice object, then dice.toString() will return strings such as
“3 and 6”, “5 and 1”, and “double 2”, depending on the numbers showing on the dice. This
method would be used automatically to convert dice to type String in a statement such as
System.out.println( "The dice came up " + dice );
so this statement might output, “The dice came up 5 and 1” or “The dice came up double 2”.
You’ll see another example of a toString() method in the next section.
are candidates for methods. This is your starting point. Further analysis might uncover the
need for more classes and methods, and it might reveal that subclassing can be used to take
advantage of similarities among classes.
This is perhaps a bit simple-minded, but the idea is clear and the general approach can be
effective: Analyze the problem to discover the concepts that are involved, and create classes to
represent those concepts. The design should arise from the problem itself, and you should end
up with a program whose structure reflects the structure of the problem in a natural way.