CS 46A - Lecture 7

Cover page image

Pre-class reading

Providing the Class Implementation

Implementing Constructors

Constructor Call Example

Syntax 3.3 Method Declaration

Syntax 3.3 Method Declaration

Implementing Methods

Method Call Example

Implementing Methods

Your browser does not support the <object> tag.

Lecture 7 Clicker Question 1

Suppose we modify the BankAccount class so that each bank account has an account number. We add an instance variable

Where else do we need to make changes?

  1. In the constructor
  2. In the deposit method
  3. In the withdraw method
  4. All of the above

Lecture 7 Clicker Question 2

The Rectangle class has four instance variables: x, y, width, and height. Which of the following is an appropriate body for the method

public void translate(int dx, int dy)
  1. width = width + dx;
    height = height + dy;
  2. int newx = x + dx;
    x = newx;
    int newy = y + dy;
    y = newy;
  3. x = x + width;
    y = y + height;
  4. dx = x + dx;
    dy = x + dy;

Unit Testing

Unit Testing

Testing With BlueJ

Figure 6 - The Return Value of the getBalance Method in BlueJ

Self Check 3.16

When you run the BankAccountTester program, how many objects of class BankAccount are constructed? How many objects of type BankAccountTester?

  1. No objects of BankAccountTester, no objects of BankAccount
  2. One object of BankAccountTester, no objects of BankAccount
  3. No objects of BankAccountTester, one object of BankAccount
  4. One object of BankAccountTester, one object of BankAccount

Local Variables

Local Variables

Lecture 7 Clicker Question 4

What does the following program segment print?

public void printArea()
{
   int area;
   int width = 10;
   int height = 20;
   System.out.println(area);
}
  1. Nothing—the code contains a compile-time error
  2. An error message is printed at run-time
  3. 200
  4. Expected: 200

Lecture 7 Clicker Question 5

Consider this class:

public class Label
{
   private String text;
   private int start;

   public int end() { return start + text.length();
}

What is the result of executing

System.out.println(new Label().end());
  1. Nothing—the code contains a compile-time error
  2. An error message is printed at run-time
  3. 0
  4. null0