CS 46A - Lecture 4

Cover page image

Pre-class reading

Objects and Classes

Methods

A Representation of Two String Objects

String Methods

Lecture 4 Clicker Question 1

What does the following program segment print?

String name = "Fred";
System.out.print(name);
System.out.println("name".toUpperCase());
  1. Nothing—the code contains a compile-time error
  2. FredNAME
  3. FredFRED
  4. Fred
    FRED

Lecture 4 Clicker Question 2

What does the following program segment print?

String name = "Fred";
name.println();
int length = name.length();
length.println();
  1. Nothing—the code contains a compile-time error
  2. Fred4
  3. Fred
    4
  4. FRED
    4

Parameters

Return Values

Passing Return Values

A More Complex Call

Lecture 4 Clicker Question 3

What is true about the method call river.length()?

  1. It has one implicit parameter, no explicit parameter, and no return value
  2. It has one implicit parameter, no explicit parameter, and one return value
  3. It has no implicit parameter, one explicit parameter, and no return value
  4. It has no implicit parameter, one explicit parameter, and oe return value

Lecture 4 Clicker Question 4

What does the following program segment print?

String name = "Fred";
PrintStream out = System.out;
out.println(name.replace("r", "e").replace("e", "o").toLowerCase());
  1. Nothing—the code contains a compile-time error
  2. frrd
  3. food
  4. 4

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

Lecture 4 Clicker Question 5

What does the following program segment print?

Rectangle rect = new Rectangle(10, 10, 10, 10);
PrintStream out = System.out;
out.println(rect.length());
  1. Nothing—the code contains a compile-time error
  2. 10
  3. 20
  4. 40