![]() |
![]() Second Summer 2002 |
Questions:
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).
BankAccount
new BankAccount()
and
BankAccount
.
BankAccount myChecking;
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.
andnew BankAccount(5000);This is an actual object that is being returned.
Overall this exercise is exercise 2 in reverse.BankAccount b;This is only an object variable that is being declared.
BankAccount
object?
There are two constructors: one takes no parameters, the other one takes an initial balance.
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.
new Rectangle(88, 88, 25, 25) // why 88?
new BankAccount(5000)
System.in
new ConsoleReader(System.in)
Write just objects, not object variables.
Rectangle mySquare = new Rectangle(88, 88, 25, 25); BankAccount myChecking = new BankAccount(5000); ConsoleReader console = new ConsoleReader(System.in);
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.
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.
b
after the following operations?
BankAccount b = new BankAccount(10); b.deposit(5000); b.withdraw(b.getBalance() / 2);![]()
b1
and b2
store objects of class BankAccount
,
consider the following instructions.
Are the balances ofb1.deposit(b2.getBalance()); b2.deposit(b1.getBalance());
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.
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.