Chapter 2

Using Objects


Chapter Goals

Types and Variables

Syntax 2.1: Variable Definition

  typeName variableName = value;
or
 typeName variableName;

Example:

  String greeting = "Hello, Dave!";

Purpose:

To define a new variable of a particular type and optionally supply an initial value

Identifiers

Self Check

  1. What is the type of the values 0 and "0"?
  2. Which of the following are legal identifiers?
    Greeting1
    g
    void
    101dalmatians
    Hello, World
    <greeting>
  3. Define a variable to hold your name. Use camel case in the variable name.

Answers

  1. int and String
  2. Only the first two are legal identifiers
  3. String myName = "John Q. Public";

The Assignment Operator

Uninitialized Variables

Syntax 2.2: Assignment

  variableName = value;

Example:

  luckyNumber = 12;

Purpose:

To assign a new value to a previously defined variable.

Self Check

  1. Is 12 = 12 a valid expression in the Java language?
  2. How do you change the value of the greeting variable to "Hello, Nina!"?

Answers

  1. No, the left-hand side of the = operator must be a variable
  2. greeting = "Hello, Nina!";
    Note that
    String greeting = "Hello, Nina!";
    is not the right answer–that statement defines a new variable

Objects and Classes

Methods

A Representation of Two String Objects


String Methods

Self Check

  1. How can you compute the length of the string "Mississippi"?
  2. How can you print out the uppercase version of "Hello, World!"?
  3. Is it legal to call river.println()? Why or why not?

Answers

  1. river.length() or "Mississippi".length()
  2. System.out.println(greeting.toUpperCase());
  3. It is not legal. The variable river has type String. The println method is not a method of the String class.

Implicit and Explicit Parameters

Return Values

Passing Return Values


A More Complex Call

Method Definitions

Method Definitions

Self Check

  1. What are the implicit parameters, explicit parameters, and return values in the method call river.length()?
  2. What is the result of the call river.replace("p", "s")?
  3. What is the result of the call greeting.replace("World", "Dave").length()?
  4. How is the toUpperCase method defined in the String class?

Answers

  1. The implicit parameter is river. There is no explicit parameter. The return value is 11
  2. "Missississi"
  3. 12
  4. As public String toUpperCase(), with no explicit parameter and return type String.

Number Types

Arithmetic Operations

Self Check

  1. Which number type would you use for storing the area of a circle?
  2. Why is the expression 13.println() an error?
  3. Write an expression to compute the average of the values x and y.

Answers

  1. double
  2. An int is not an object, and you cannot call a method on it
  3. (x + y) * 0.5

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

 
new ClassName(parameters)

Example:

  new Rectangle(5, 10, 20, 30)
new Rectangle()

Purpose:

To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object

Self Check

  1. How do you construct a square with center (100, 100) and side length 20?
  2. What does the following statement print?
    System.out.println(new Rectangle().getWidth());

Answers

  1. new Rectangle(90, 90, 20, 20)
  2. 0

Accessor and Mutator Methods


Self Check

  1. Is the toUpperCase method of the String class an accessor or a mutator?
  2. Which call to translate is needed to move the box rectangle so that its top-left corner is the origin (0, 0)?

Answers

  1. An accessor–it doesn't modify the original string but returns a new string with uppercase letters
  2. box.translate(-5, -10), provided the method is called immediately after storing the new rectangle into box

Implementing a Test Program

  1. Provide a new 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

Importing Packages

Don't forget to include appropriate packages:

Syntax 2.4 : Importing a Class from a Package

  import packageName.ClassName;

Example:

  import java.awt.Rectangle;

Purpose:

To import a class from a package for use in a program.

File MoveTester.java


Output

   After moving, the top-left corner is:
20
35

Self Check

  1. 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?
  2. Why doesn't the MoveTester program print the width and height of the rectangle?

Answers

  1. Add the statement import java.util.Random; at the top of your program
  2. Because the translate method doesn't modify the shape of the rectangle

Testing Classes in an Interactive Environment


The API Documentation


The API Documentation of the Standard Java Library


The API Documentation for the Rectangle Class



Javadoc Method Summary



translate Method Documentation


Self Check

  1. 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!"?
  2. 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.)

Answers

  1. toLowerCase
  2. "Hello, Space !"–only the leading and trailing spaces are trimmed


Object References


Object Variables and Number Variables


 
 

Copying Numbers

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

Copying Object References

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

Self Check

  1. What is the effect of the assignment greeting2 = greeting?
  2. After calling greeting2.toUpperCase(), what are the contents of greeting and greeting2?

Answers

  1. Now greeting and greeting2 both refer to the same String object.
  2. Both variables still refer to the same string, and the string has not been modified. Recall that the toUpperCase method constructs a new string that contains uppercase characters, leaving the original string unchanged.

Mainframes–When Dinosaurs Ruled the Earth