Big Java book cover

Chapter 2 – Using Objects

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.

Classes

Classes

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?

Variables

Syntax 2.1 Variable Declaration

Variable Declaration

Variables

Variable Declarations

table of variable declarations

 

Types

Names

Variable Names in Java

table 2 variable names

 

Comments

Assignment

Assignment

Assignment

Syntax 2.2 Assignment

Variable Declaration

Question 1 Feb 4

What is wrong with the following variable declaration?
int miles per gallon = 39.4
  1. You cannot have spaces in variable names.
  2. The variable type should be double because it holds a fractional value.
  3. There is a semicolon missing at the end of the statement.
  4. There is no error

Question 2 Feb 4

What are the types of the values 0 and "0"?
  1. int and int
  2. String and String
  3. int and String
  4. String and int

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?

Greeting1
g
void
101dalmatians
Hello, World
<greeting>

Self Check 2.10

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

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 value

Return Value

Return Value - 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

Question 3 Feb 4

How can you print out the uppercase version of the string defined by String greetings ="Hello, World!"?
  1. greetings.toUpperCase();
  2. System.out.println( greetings.toUpperCase);
  3. System.out.println( greetings.toUpperCase());
  4. System.out.println( greetings.ToUpperCase());

Self Check 2.15

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

Self Check 2.18

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

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

Question 4 Feb 4

How do you construct a square with center (100, 100) and side length 20?
  1. new Rectangle(100, 100, 20, 20);
  2. new Rectangle(90, 90, 20, 20)
  3. new Rectangle(100, 100, 10, 10);
  4. new Rectangle(90, 90, 10, 10);

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());

Question 1 Feb 6

What does this sequence of statements print?
String greeting = "Hello";
System.out.println(greeting.toUpperCase());
System.out.println(greeting);
  1. HELLO
    HELLO
  2. Hello
    Hello
  3. HELLO
    Hello
  4. Hello
    HELLO

Self Check 2.28

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

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:

Packages

Syntax 2.4 Importing a Class from a Package

package syntax

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?

Question 2 Feb 6

In which package is the BigInteger class located? Look it up in the API documentation.
  1. java.util package
  2. java.lang package
  3. java.math package
  4. java.numbers package

Implementing a Test Program

Implementing a Test Program

section_7/MoveTester.java

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

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;

Numbers

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?

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

Question 1 Feb 18

Look is the Java API ( ). What do you need to import in order to use the JFrame class?
  1. java.awt.JFrame
  2. java.swing.JFrame
  3. java.util.JFrame
  4. javax.swing.JFrame
  5. something else

section_9_1/EmptyFrameViewer.java

Your browser does not support this feature

 

a frame

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 21 Drawing Rectangles

section_9_2/RectangleComponent.java

Your browser does not support this feature

Displaying a Component in a Frame

section_9_3/RectangleViewer.java

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)?

Ellipses

Ellipses

Circles

Lines

Drawing Text

Colors

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

section_10/FaceComponent.java

Your browser does not support this feature

 

section_10/FaceViewer.java

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

What are the RGB color values of Color.BLUE?

Class Activity

Open Bluej, make a new project, and do the following