Midterm 1

One-page version suitable for printing.

Statistics:

(All numbers are out of the possible 80 points, including the 5-point
curve.)

mean 64.073 (2627.000/41) stddev 11.785 median 64.000 midrange 53.250-74.000 # avg 1 7.95 / 10 2 15.05 / 25 3 21.15 / 25 4 7.83 / 10 5 7.20 / 10 (+ 5-point curve)

Question 1-1

Select which of the following best describes the purpose of each of the circled pieces of the code. (Note that some choices may not appear at all, and others may occur multiple times.)

A. class method definition
B. instance method definition
C. constructor method definition
D. class variable declaration
E. instance variable declaration
F. local variable declaration

Solution

Question 1-2

[25 pts] Suppose somebody has already written for us two classes Bank and Account.

Bank class documentation Account class documentation
Bank()
(Constructor method) Constructs an empty bank, holding no accounts.
void add(Account acct)
Adds \verb!acct! directly into the bank.
Account get(int which)
Returns the account at index which in the bank. (Indices begin at 0.)
int getNumAccounts()
Returns the number of accounts currently in the bank.
Account()
(Constructor method) Constructs an account with a balance of $0.

void deposit(double amt)
Deposits amt into the account.

double getBalance()
Returns the amount of money currently in the account.

Complete the following method so that, given one bank b as a parameter, it creates and returns a copy of that bank, containing copies of all of the accounts held by b.
Bank duplicateBank(Bank b) {



}

Question 1-3

Define a class PassCount to track whether all the students of a class have passed a test. It should support the following methods.

PassCount()
(Constructor method) Constructs an object representing an empty class.

void addGrade(double grade)
Adds grade into the class.

boolean isAnyFailing()
Returns true only if there is somebody in the class who received a grade of less than 60.

For example, if you defined this class properly, I should be able to write the following class to test it.
public class PassCountTest {
    public static void run() {
        PassCount a = new PassCount();
        a.addGrade(45.0);
        a.addGrade(76.0);
        IO.println(a.isAnyFailing()); // should print ``true''

        PassCount b = new PassCount();
        b.addGrade(60.0);
        IO.println(b.isAnyFailing()); // should print ``false''
    }
}
Note that, to accomplish this, a PassCount object need not remember every single grade --- that is, you do not need to use arrays: It just needs to remember whether any of the ones it has seen were below 60.

Solution

Question 1-4

[10 pts] Suppose we have the following two class definitions.

class P {
  public int f(int x) {
    return x + 1;
  }
  public int g(int x) {
    return f(x + 2);
  }
}
class Q extends P {
  public int f(int x) {
    return x + 4;
  }
  public int h(int x) {
    return f(x + 8);
  }
}
And suppose we execute the following run method.
public static void run() {
  P a = new P();
  Q b = new Q();
  P c = b;
  IO.println(a.f(0) + " " + a.g(0));
  IO.println(b.f(0) + " " + b.g(0) + " " + b.h(0));
  IO.println(c.f(0) + " " + c.g(0));
}
What would the method print?

Solution

Question 1-5

[10 pts] Suppose we have the class defined as below.

class C {
    static int y = 0;
    int z;

    C() {
        z = 0;
    }
    void incrX() {
        int x = 0;
        x++;
        IO.println(x);
    }
    void incrY() {
        y++;
        IO.println(y);
    }
    void incrZ() {
        z++;
        IO.println(z);
    }
}
public class Example {
    public static void run() {
        C a = new C();
        C b = new C();
        a.incrX();
        a.incrX();
        b.incrX();
        a.incrY();
        a.incrY();
        b.incrY();
        a.incrZ();
        a.incrZ();
        b.incrZ();
    }
}
What would the computer print when it executes the Example class's run() method?

Solution