Answers to Test #1, CS 46A, October 6, 2003

    1. 4
    2. 1000.0
    3. "bc"
    4. 'c'

    1. illegal -- strings are not coerced to integers
    2. this is legal -- integers may be coereced to doubles
    3. illegal -- strings are not coerced to characters
    4. this is legal -- println accepts any numeric type

  1. At the position (44, 66).

  2. The values are 150.0, 100.0, and 150.0 respectively.

  3. One way:
          Rectangle r = new Rectangle(20, 30, 100, 100);
          g2.setColor(Color.red);
          g2.fill(r);
    The argument in line 2 could also be new Color(1F,0F,0F);

  4. One way:
       public void combine(BankAccount b)                        {
         deposit(b.getBalance());
         b.withdraw(b.getBalance());                             }
    It would also be possible to replace the last instruction by b.balance = 0;

  5. One way:
       public Purse(Purse p)           {
          nickels = p.nickels;
          dimes = p.dimes;
          quarters = p.quarters;       }

  6. To prevent unauthorized modification. More precisely, so that they may be modified only by a known collection of methods -- the methods defined within the same class.