Second Summer 2002


Lab Notes Four: The warm-ups. Solutions

Questions:

  1. Explain the difference between an object and a class.

    A class is like a factory, while an object is like the gadgets that the factory is producing. The difference between a class and an object is that between a factory that produces a certain kind of watches and a watch (of the one kind that the factory can produce) that is produced by that factory. Also, for a given class there could be 0 (zero), 1 (one), or more than one object(s) produced according to the blueprint that the class contains (or defines).

  2. Give the Java code for an

  3. Explain the differences between an instance variable and a local (or method) variable.

    An instance variable is global to a method over time: an instance variable survives the successive invocations of a method, and exists for as long as the object to which it belongs exists. A local (method) variable on the other hand, is in effect only during the life time of a method invocation: while the method that contains it is in action the variable is used for whatever the method needs it for. When the method is finished the variable is also gone.

    Another difference is that local variables need to be initialized by the programmer, instance variables are initialized by default by Java.

  4. Explain the difference between
    new BankAccount(5000); 
    This is an actual object that is being returned.
    and
    BankAccount b;
    This is only an object variable that is being declared.
    Overall this exercise is exercise 2 in reverse.

  5. What are the construction parameters for a BankAccount object?
    There are two constructors: one takes no parameters, the other one takes an initial balance.

  6. What is default construction?
    Java supplies a default constructor. It has no arguments and does nothing. Its action results in "default construction": all instance variables remain set to the default values for their types.

  7. Give Java code to construct the following objects:

    Write just objects, not object variables.

  8. Repeat the preceding exercise, but now define object variables that are initialized with the required objects.
    Rectangle mySquare = new Rectangle(88, 88, 25, 25); 
    BankAccount myChecking = new BankAccount(5000); 
    ConsoleReader console = new ConsoleReader(System.in);  

  9. Find the errors in the following statements:

    Rectangle r = new Rectangle(5, 10, 15, 20); 
    // this creates a new Rectangle  
    
    double x = (new BankAccount(10000)).getBalance(); 
    // see how this works? first a new BankAccount object 
    // is created, and as soon as it is available we call 
    // getBalance on it. The net result is that x will be 
    // set to 10000. Given that in this process the newly
    // created BankAccount object is lost, all we can say 
    // is that this is a contrived exercise with a certain
    // educational purpose, as there are definitely better 
    // ways to set a double variable x to 10000. 
    
    BankAccount b;
    b = new BankAccount();
    b.deposit(10000); 
    //if you don't initialize b you can't use it to deposit 
    b = new BankAccount(10000);
    // contrived but legal: b discards previous account 
    // and points now to a brand new account of 10000. 
    b.addCoins(new Coin(0.25, "quarters")); 
    // as b is not pointing to a Purse this won't work!  
    
    Purse p = null; // fine as long as you don't need p just yet.
    p = new Purse();// we create a new Purse and call it p
    p.addCoins(6, new Coin(0.25, "quarters")); 
    // Purse objects know how to add Coins, and we took care
    // in creating a Purse so what is wrong? addCoins needs to 
    // be called with the right number of arguments and in the 
    // right order. More precisely: an int and a Coin. The int
    // parameter was missing here, so we added one. 
    
    Purse p = new Purse(); 
    p.addCoins(4, new Coin(0.10, "dimes")); 
    // Note that Coin does not have a no-args constructor any 
    // longer!! Also addCoins needs an int parameter in addition, 
    // and preceding the Coin parameter, by its definition.
    

  10. Describe all constructors of the BankAccount class. List all methods that can be used to change a BankAccount object. List all methods that don't change the BankAccount object.

    public class BankAccount {
      public BankAccount() { // no-arg constructor 
        balance = 0; 
      } 
      public BankAccount(double initialBalance) { // one-arg constructor 
        balance = initialBalance; 
      } 
      public void deposit(double amount) { // mutator method  
        balance += amount; 
      }
      public void withdraw(double amount) { // mutator method  
        balance -= amount; 
      }
      public double getBalance() { // accessor method  
        return balance; 
      }
      private double balance; // private instance variable  
    }
    You should, of course, remember that a mutator method is one that changes the values of instance variables, mutating the state of the object; first two methods do that. The third one is an accessor method, since it only reports the value of the instance variable, without touching (or changing) it.

  11. What is the value of b after the following operations?
    BankAccount b = new BankAccount(10); 
    b.deposit(5000);
    b.withdraw(b.getBalance() / 2); 

  12. If b1 and b2 store objects of class BankAccount, consider the following instructions.
    b1.deposit(b2.getBalance());
    b2.deposit(b1.getBalance());
    Are the balances of b1 and b2 now identical?

    Only if the account pointed to by b2 was empty initially.

    Explain.

    Let's say the first account has a balance of x and the second one a balance of y. The first statement adds y to x to make the first account's balance ( x + y); the second statement adds this value, the current balance in the first account, to the balance stored by the second account, so the second account becomes (y + ( x + y). These two formulas can be equal only when y is 0 (zero) that is, if the second account starts up by being empty.

  13. What is the this reference?
    A reference that points to the object that contains the instance method that uses the reference. Or, as in lab notes 4:

Hope you found this useful.


Last updated: Jun 16, 2002 by Adrian German for A201