CS 46A - Lecture 5

Cover page image

Pre-class reading

Accessor and Mutator Methods

Lecture 5 Clicker Question 1

Consider these methods:

Which of them are mutators?

  1. None of them
  2. Only Rectangle.grow
  3. Rectangle.grow and PrintStream.println
  4. All of them

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

Lecture 5 Clicker Question 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.)

  1. " Hello, Space ! "
  2. " Hello,Space! "
  3. "Hello, Space !"
  4. "Hello,Space!"

Lecture 5 Clicker Question 3

Find the Random class in the API documentation. What statement do you need to add to your program in order to use that class?

  1. import Random;
  2. import java.util;
  3. import java.util.Random;
  4. import java.*.Random;

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.

Display the Values that You Expect to Get

import java.awt.Rectangle; 

public class MoveTester
{
   public static void main(String[] args)
   {
      Rectangle box = new Rectangle(5, 10, 20, 30);

      // Move the rectangle
      box.translate(15, 25);

      // Print information about the moved rectangle 
      System.out.print("x: "); 
      System.out.println(box.getX());
      System.out.println("Expected: 20"); 

      System.out.print("y: "); 
      System.out.println(box.getY());
      System.out.println("Expected: 35");   
   }
}

Expected Values

Lecture 5 Clicker Question 4

We want to  write a tester for trim. Which of these is best?

  1. System.out.println("Hello");
    System.out.println("Expected: Hello");
  2. System.out.println(" Hello ".trim());
    System.out.println("Expected: Hello");
  3. System.out.println(" Hello ".trim());
    System.out.println("Expected:  Hello ");
  4. System.out.print("|"); 
    System.out.print(" Hello ".trim());
    System.out.println("|\nExpected: |Hello|");

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

Lecture 5 Clicker Question 5

To which strings do greeting and greeting2 refer after the following code segment:

String greeting = "Hello";
String greeting2 = greeting;
greeting2.toUpperCase();
  1. greeting and greeting2 refer to "Hello"
  2. greeting refers to "Hello", greeting2 to "HELLO"
  3. greeting and greeting2 refer to "HELLO"
  4. None of the above because there is a syntax error