Big Java 4

Chapter 2 – Using Objects

Chapter Goals

Types

Number Literals

Number Literals

Number Types

Self Check 2.1

What is the type of the values 0 and "0"?

Self Check 2.2

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

Self Check 2.3

Why is the expression 13.println() an error?

Self Check 2.4

Write an expression to compute the average of the values x and y.

Variables

Variable Declarations

Variable Declarations

Identifiers

Syntax 2.1 Variable Declaration

Variable Declaration

Variable Names

Variable Names

Self Check 2.5

Which of the following are legal identifiers?
Greeting1
g
void
101dalmatians
Hello, World
<greeting>

Self Check 2.6

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

The Assignment Operator

The Assignment Operator

int width = 10;

The Assignment Operator

int width = 10;
width = 20;

Uninitialized Variables

Syntax 2.2 Assignment

Assignment

Assignment

Animation 2.1: Variable Initialization and Assignment

Link to Flash animation

Self Check 2.7

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

Self Check 2.8

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

Objects and Classes

Methods

A Representation of Two String Objects

String Methods

Self Check 2.9

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

Self Check 2.10

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

Self Check 2.11

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

Parameters

Return Values

Passing Return Values

A More Complex Call

Animation 2.2: Parameter Passing

Link to Flash animation

Self Check 2.12

What are the implicit parameters, explicit parameters, and return values in the method call river.length()?

Self Check 2.13

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

Self Check 2.14

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

Self Check 2.15

How is the toUpperCase method defined in the String class?

Rectangular Shapes and Rectangle Objects

Rectangular Shapes and Rectangle Objects

Constructing Objects

new Rectangle(5, 10, 20, 30)

Constructing Objects

Syntax 2.3 Object Construction

Object Construction

Self Check 2.16

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

Self Check 2.17

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

Accessor and Mutator Methods

Self Check 2.18

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

Self Check 2.19

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

The API Documentation of the Standard Java Library

The API Documentation for the Rectangle Class

Method Summary

Detailed Method Description

The detailed description of a method shows:

Packages

Syntax 2.4 Importing a Class from a Package

Importing a Class from a Package

Self Check 2.20

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.21

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.22

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

Implementing a Test Program

  1. Provide a tester class.
  2. Supply a main method.
  3. Inside the main method, construct one or more objects.
  4. Apply methods to the objects.
  5. Display the results of the method calls.
  6. Display the values that you expect to get.

ch02/rectangle/MoveTester.java

Your browser does not support the <object> tag. Program Run:

Self Check 2.23

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

Self Check 2.24

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

Testing Classes in an Interactive Environment

Object References

Object Variables and Number Variables


 
 

Copying Numbers

int luckyNumber = 13;


Copying Numbers

int luckyNumber = 13;
int luckyNumber2 = luckyNumber;

Copying Numbers

int luckyNumber = 13;
int luckyNumber2 = luckyNumber;
luckyNumber2 = 12;

Copying Object References

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

Copying Object References

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

Copying Object References

Rectangle box = new Rectangle(5, 10, 20, 30);
Rectangle box2 = box;
box2.translate(15, 25);

Self Check 2.25

What is the effect of the assignment greeting2 = greeting?

Self Check 2.26

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

Mainframes – When Dinosaurs Ruled the Earth

Graphical Applications and 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":
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5. Make the frame visible:
    frame.setVisible(true);

A Frame Window

ch02/emptyframe/EmptyFrameViewer.java

Your browser does not support the <object> tag.

Self Check 2.27

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

Self Check 2.28

How can a program display two frames at once?

Drawing on a Component

Classes Graphics and Graphics2D

Drawing Rectangles

ch02/rectangles/RectangleComponent.java

Your browser does not support the <object> tag.

Using a Component

  1. Construct a frame.
  2. Construct an object of your component class:
    RectangleComponent component = new RectangleComponent();
  3. Add the component to the frame:
    frame.add(component);
  4. Make the frame visible.

ch02/rectangles/RectangleViewer.java

Your browser does not support the <object> tag.

Self Check 2.29

How do you modify the program to draw two squares?

Self Check 2.30

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

Self Check 2.31

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

Applets

Applets

ch02/applet/RectangleApplet.java

Your browser does not support the <object> tag.

ch02/applet/RectangleApplet.html

Your browser does not support the <object> tag.

ch02/applet/RectangleAppletExplained.html

Your browser does not support the <object> tag.

Applets

Ellipses and Circles

An Ellipse

Lines

Drawing Text

g2.drawString("Message", 50, 100);

Colors

Predefined Colors and Their RGB Values

Color RGB Value
Color.BLACK 0, 0, 0     
Color.BLUE 0, 0, 255     
Color.CYAN 0, 255, 255     
Color.GRAY 128, 128, 128     
Color.DARKGRAY 64, 64, 64     
Color.LIGHTGRAY 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

ch02/face/FaceComponent.java

Your browser does not support the <object> tag.

ch02/face/FaceViewer.java

Your browser does not support the <object> tag.

Self Check 2.32

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

Self Check 2.33

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

Self Check 2.34

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

Self Check 2.35

What are the RGB color values of Color.BLUE?

Self Check 2.36

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