Computer Science A, Spring 2007
Roskilde University

Lecture 2:  Using Objects


1: Chapter Goals


2: Types and Variables


3: 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

4: Identifiers


5: 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>

6: Answers

  1. int and String
  2. Only the first two are legal identifiers

7: The Assignment Operator


8: Uninitialized Variables


9: Syntax 2.2: Assignment

  variableName = value;

Example:

  luckyNumber = 12;

Purpose:

To assign a new value to a previously defined variable.

10: 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!"?

11: 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

12: Objects and Classes


13: Methods


14: A Representation of Two String Objects



15: String Methods


16: 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?

17: 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.

18: Implicit and Explicit Parameters


19: Return Values


20: Passing Return Values



21: A More Complex Call


22: Method Definitions


23: Method Definitions


24: 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?

25: 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.


26: Number Types


27: Arithmetic Operations


28: 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.

29: Answers

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

30: Rectangular Shapes and Rectangle Objects


31: Rectangular Shapes and Rectangle Objects


32: Constructing Objects

   new Rectangle(5, 10, 20, 30)

33: Constructing Objects


34: 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

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

36: Answers

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

37: Accessor and Mutator Methods


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

39: 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

40: 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

41: Importing Packages

Don't forget to include appropriate packages:

42: 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.

43: File MoveTester.java

01: import java.awt.Rectangle; 
02: 
03: public class MoveTester
04: {
05:    public static void main(String[] args)
06:    {
07:       Rectangle box = new Rectangle(5, 10, 20, 30);
08: 
09:       // Move the rectangle
10:       box.translate(15, 25);
11: 
12:       // Print information about the moved rectangle
13:       System.out.println("After moving, the top-left corner is:"); 
14:       System.out.println(box.getX()); 
15:       System.out.println(box.getY()); 
16:    }
17: }

Output

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


44: 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?

45: 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

46: The API Documentation of the Standard Java Library

This page is found at http://java.sun.com/j2se/1.5.0/docs/api/.


47: The API Documentation for the Rectangle Class


48: Javadoc Method Summary


49: translate Method Documentation


50: 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 following string? (Note the spaces in the string.)
    " Hello, Space ! "

51: Answers

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

52: Object References


53: Object Variables and Number Variables


 
 

54: Copying Numbers

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


55: Copying Object References

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


56: 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?

57: 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.