Big Java book cover

Chapter 2 – Using Objects

Chapter Goals


Objects and Classes



Each part a home builder uses, such as a furnace or a water heater, fulfills a particular function. Similarly, you build programs from objects, each of which has a particular behavior.

Using Objects

Using Objects

soloar panel

You can think of a water heater as an object that can carry out the "get hot water" method. When you call that method to enjoy a hot shower, you don't care whether the water heater uses gas or solar power.



Self Check 2.1

In Java, objects are grouped into classes according to their behavior. Would a window object and a water heater object belong to the same class or to different classes? Why?

Self Check 2.2

Some light bulbs use a glowing filament, others use a fluorescent gas. If you consider a light bulb a Java object with an "illuminate" method, would you need to know which kind of bulb it is?


Syntax 2.1 Variable Declaration

Variable Declaration


Variable Declarations

table of variable declarations




Variable Names in Java

table 2 variable names






Syntax 2.2 Assignment

Variable Declaration

Self Check 2.3

What is wrong with the following variable declaration?
int miles per gallon = 39.4

Self Check 2.4

Declare and initialize two variables, unitPrice and quantity, to contain the unit price of a single item and the number of items purchased. Use reasonable initial values.

Self Check 2.5

Use the variables declared in Self Check 4 to display the total purchase price.

Self Check 2.6

What are the types of the values 0 and "0"?

Self Check 2.7

Which number type would you use for storing the area of a circle?

Self Check 2.8

Which of the following are legal identifiers?

Self Check 2.9

Declare a variable to hold your name. Use camel case in the variable name.

Self Check 2.10

Is 12 = 12 a valid expression in the Java language?

Self Check 2.11

How do you change the value of the greeting variable to "Hello, Nina!"?

Self Check 2.12

How would you explain assignment using the parking space analogy?

Calling Methods

The Public Interface of a Class

public interface of a car

The controls of a car form its public interface. The private implementation is under the hood.

A Representation of Two String Objects

Method Arguments

Method Arguments

sew method


At this tailor shop, the customer's measurements and the fabric are the arguments of the sew method. The return value is the finished garment.

Method Arguments

Return Values

Return Values

Return Values - replace method

Example: assume String river = "Mississippi";

Return Value - replace method - Continued

Method Arguments and Return Values

table3 method arguments and return values

Method Declaration

Method Declaration - continued

Method Declaration

Self Check 2.13

How can you compute the length of the string "Mississippi"?

Self Check 2.14

How can you print out the uppercase version of "Hello, World!"?

Self Check 2.15

Is it legal to call river.println()? Why or why not?

Self Check 2.16

What are the arguments in the method call river.replace("p", "s")?

Self Check 2.17

What is the result of the call river.replace("p", "s"), where river is "Mississippi"?

Self Check 2.18

What is the result of the call greeting.replace("World", "Dave").length(), where greeting is "Hello, World!"?

Self Check 2.19

How is the toUpperCase method declared in the String class?

Constructing Objects

Objects of the Rectangle class describe rectangular shapes.
rectangular shapes

Constructing Objects

Constructing Objects

Constructing Objects

Syntax 2.3 Object Construction

construction syntax

Self Check 2.20

How do you construct a square with center (100, 100) and side length 20?

Self Check 2.21

Initialize the variables box and box2 with two rectangles that touch each other.

Self Check 2.22

The getWidth method returns the width of a Rectangle object. What does the following statement print?
System.out.println(new Rectangle().getWidth());

Self Check 2.23

The PrintStream class has a constructor whose argument is the name of a file. How do you construct a PrintStream object with the construction argument "output.txt"?

Self Check 2.24

Write a statement to save the object that you constructed in Self Check 23 in a variable.

Accessor and Mutator Methods

Self Check 2.25

What does this sequence of statements print?
Rectangle box = new Rectangle(5, 10, 20, 30);
System.out.println("Before: " + box.getX());
box.translate(25, 40);
System.out.println("After: " + box.getX());

Self Check 2.26

What does this sequence of statements print?
Rectangle box = new Rectangle(5, 10, 20, 30);
System.out.println("Before: " + box.getWidth());
box.translate(25, 40);
System.out.println("After: " + box.getWidth());

Self Check 2.27

What does this sequence of statements print?
String greeting = "Hello";

Self Check 2.28

Is the toUpperCase method of the String class an accessor or a mutator?

Self Check 2.29

Which call to translate is needed to move the box rectangle so that its top-left corner is the origin (0, 0)?

The API Documentation

Browsing the API Documentation

API screenshot

Figure 13 The API Documentation of the Standard Java Library

Browsing the API Documentation - Method Summary

method summary in API

Figure 14 The Method Summary for the Rectangle Class

Browsing the API Documentation

The detailed description of a method shows:


Syntax 2.4 Importing a Class from a Package

package syntax

Self Check 2.30

Look at the API documentation of the String class. Which method would you use to obtain the string "hello, world!" from the string "Hello, World!"?

Self Check 2.31

In the API documentation of the String class, look at the description of the trim method. What is the result of applying trim to the string " Hello, Space ! "? (Note the spaces in the string.)

Self Check 2.32

Look into the API documentation of the Rectangle class. What is the difference between the methods void translate(int x, int y) and void setLocation(int x, int y)?

Self Check 2.33

The Random class is declared in the java.util package. What do you need to do in order to use that class in your program?

Self Check 2.34

In which package is the BigInteger class located? Look it up in the API documentation.

Implementing a Test Program

Implementing a Test Program


Your browser does not support this feature Program Run:
x: 20
Expected: 20
y: 35
Expected: 35

Self Check 2.35

Suppose we had called box.translate(25, 15) instead of box.translate(15, 25). What are the expected outputs?

Self Check 2.36

Why doesn't the MoveTester program print the width and height of the rectangle?

Object References

Object References

Object References

Multiple object variables can refer to the same object:

Rectangle box = new Rectangle(5, 10, 20, 30);
Rectangle box2 = box;

Figure 16 Two Object Variables Referring to the Same Object


Copying Numbers

Copying Object References

Self Check 2.37

What is the effect of the assignment greeting2 = greeting?

Self Check 2.38

After calling greeting2.toUpperCase(), what are the contents of greeting and greeting2?

Mainframe Computer

Mainframe Computer

Graphical Applications: Frame Windows

butterflies in a frame

A graphical application shows information inside a frame.

Frame Windows

To show a frame:

  1. Construct an object of the JFrame class:
    JFrame frame = new JFrame();
  2. Set the size of the frame:
    frame.setSize(300, 400);
  3. If you'd like, set the title of the frame:
    frame.setTitle("An empty Frame");
  4. Set the "default close operation":
  5. Make the frame visible:


Your browser does not support this feature


a frame

Figure 20 A Frame Window

Drawing on a Component

Classes Graphics and Graphics2D

Classes Graphics and Graphics2D

Coordinate System of a Component

Drawing Rectangles

We want to create an application to display two rectangles.

two rectangles

Figure 2 Drawing Rectangles


Your browser does not support this feature

Displaying a Component in a Frame


Your browser does not support this feature

Self Check 2.39

How do you display a square frame with a title bar that reads "Hello, World!"?

Self Check 2.40

How can a program display two frames at once?

Self Check 2.41

How do you modify the program to draw two squares?

Self Check 2.42

How do you modify the program to draw one rectangle and one square?

Self Check 2.43

What happens if you call g.draw(box) instead of g2.draw(box)?





Drawing Text


Colors - continued

Predefined Colors

Color RGB Value
Color.BLACK 0, 0, 0     
Color.BLUE 0, 0, 255     
Color.CYAN 0, 255, 255     
Color.GRAY 128, 128, 128     
Color.DARK_GRAY 64, 64, 64     
Color.LIGHT_GRAY 192, 192, 192     
Color.GREEN 0, 255, 0     
Color.MAGENTA 255, 0, 255     
Color.ORANGE 255, 200, 0     
Color.PINK 255, 175, 175     
Color.RED 255, 0, 0     
Color.WHITE 255, 255, 255     
Color.YELLOW 255, 255, 0     


Alien Face

drawing of alien face

Figure 24 An Alien Face

The code is on following slides:


Your browser does not support this feature



Your browser does not support this feature

Self Check 2.44

Give instructions to draw a circle with center (100, 100) and radius 25.

Self Check 2.45

Give instructions to draw a letter "V" by drawing two line segments.

Self Check 2.46

Give instructions to draw a string consisting of the letter "V".

Self Check 2.47

What are the RGB color values of Color.BLUE?

Self Check 2.48

How do you draw a yellow square on a red background?