CPSC 120 Lab 12
Dependencies Among Objects of the Same Class
Lab Objectives
- Gain a better understanding of the equals method and references.
- Experience using static variables in a class.
- Gain experience (and a better understanding of )
passing an object of the same class as a parameter.
|
As usual, create a lab12 subdirectory for today's lab, open up
Mozilla Firefox and
the Web version of this handout, and open Emacs.
Comparing Objects of the Same Class
All Java classes are derived from the Object class in the Java Class
Library. That class provides default versions of several methods. Two of
those methods are toString and equals. We have used
toString often. We know that if we want to print an object we
can use the object in a print statement and, if we have written a
toString method in our class, that method will be invoked automatically
and the string it returns will be printed.
If we do not have a toString method,
the reference (a hexadecimal number) to the object will be printed. Similarly,
we can use equals to compare objects from a class we have written
but if we haven't written our own equals method, the method will compare
references.
In this exercise you will define equals for two different classes, Die
and PairOfDice. Save the files
Die.java, PairOfDice.java,
and TestEquals.java to your directory and
do the following.
- The Die class is from the text book (we've used it before). The
constructor initializes the face on a die to 1. The PairOfDice class,
written in Post Lab 10,
models a pair of dice (its instance variables are two Die objects).
Study the TestEquals program to see what it does, then compile and
run it. Study the output to see what is happening.
You should notice the following:
- Even though all three pairs of dice are
initially snake eyes (two 1s) they aren't all "equal" because the
references are not the same. Be sure you understand why pair1 and
pair2 are not equal, pair2 and pair3 are not equal,
but pair1 and pair3 are equal!
- When pair1 and pair2 are rolled 10 times note the values of pair3.
Be sure you understand why pair3 has the values it has!
- Open up Die.java and add an equals method. Two Die objects will
be equal if they have the same face value. The equals method
signature is as follows. You add the body.
public boolean equals (Die otherDie)
- Open up PairOfDice.java and add an equals method. When writing an
equals method for a class you need to decide what you mean for two
objects to be equal. For a pair of dice there are two possibilites -
you could mean the sum on the dice is the same (that's often what
is needed in games) or you could mean that the dice have the same
faces (so, for example if the faces are 3 and 4 on one pair, then another
pair would be equal with a 3 and 4 (OR 4 and 3) but not 2 and 5). Write
your method using the latter definition; that is, two pair are
equal if the dice have the same face values. Note the following:
- Be sure to
think about what the signature for
equals should be in the PairOfDice class - it will be comparing
this PairOfDice object to one passed in as a parameter.
- You should use the equals method
in the Die class in your method.
- There is a getDie method in the
PairOfDice class that takes an integer parameter (1 to get die1 and
2 to get die2) and returns the Die object. You will need to
use this to access die1 and die2 of the other pair.
- Compile and run TestEquals again. Now all pairs should be equal.
- Add a for loop to the program that rolls pair1 and pair2
20 times and counts the times they are equal. Print out each roll in the
loop along with a message if the two pair are equal.
After the loop print the number of times the rolls were the same. So
your output should look something like this:
Roll 1: [1 : 6] and [3 : 2]
Roll 2: [3 : 4] and [4 : 3] - equal
Roll 3: [6 : 2] and [4 : 4]
...
Roll 20: [1 : 1] and [5 : 2]
The pair were equal 3 times.
Run the program enough to be sure that your equals is correct. Note
above that [6 : 2] and [4 : 4] are not equal even though the sum is
the same but [3 : 4] and [4 : 3] are equal.
Static Methods and Variables; Objects of the Same Class as Parameters
File Account.java contains a class representing
a bank account similar to the one we used in earlier labs.
(Note that a second constructor
has been added that takes only two parameters and
generates the account number randomly.)
Save this file to your directory and use it in the exercises below.
- Suppose the bank wants to keep track of how many accounts exist. We'll
add a static variable to do this as follows:
- Declare a private static integer variable numAccounts
to hold this value. Like all instance and static variables, it
will be initialized (to 0, since it's an int) automatically.
- Add code to both constructors to increment this variable every time
an account is created.
- Add a static method getNumAccounts
that returns the total number of accounts.
(Think about why this method should be static -- its information is not related
to any particular account.)
- Now open TestAccounts1.java, study it
to see what it does, and save it to your
directory. Run it to test your
Account class.
- Next we will add the capability of transferring funds from one
account to another. We'll do it two different ways as follows:
- Add a method to the Account class that allows money to be transferred
between accounts. This could be done by using two separate methods
transferIn(Account otherAcct, double amt) and
transferOut(Account otherAcct, double amt), or by using just one method
transfer(Account otherAcct, double amt) and defining
(and clearly documenting - points off if you don't!)
it so that it transfers money FROM the current
account TO the account that is passed as a parameter. Use this second
strategy (just one method). Inside the transfer method you can manipulate
the balance directly or use
the deposit and withdraw methods you have
already written. Either way, first you'll still have to check that the
balance in the from account is sufficient; if not, print a message and
don't carry out the transfer.
- Now save TestAccounts2.java to your
directory. Add code as indicated by the comments to transfer
$50 from Madison's account to Spud's account and then to transfer $25 from
Spud's account to Madison's account. Print both accounts after each transfer.
Don't fill in the rest of the missing
code yet. Run it to be sure it works.
- Add a static method to the Account class that lets the user
transfer money between two accounts without going through either account.
You can call the method transfer just like the other one --
you are overloading it.
Your new method should
take two Account objects and an amount and transfer the amount from the
first account to the second account. The signature will look like this:
public static void transfer(Account fromAcct, Account toAcct, double amt)
- Following the next comment in TestAccounts2.java, fill in code
to use the static transfer method to transfer
another $10 from Spud's account to Madison's account. Test your
program.
- Following the next comment in TestAccounts2.java, fill in code
to transfer $250 from Madison's account to Spud's account. (Use the
transfer method of your choice.) Run the
program to see what happens. If your code is correct this will not
be allowed (insufficient funds).
- Finally we will add the capability of closing accounts and consolidating
accounts to the class. Do the following:
- Add a method void close() to your Account class that closes
an account. It should set the name to null (note: this is the
null reference which is not the same as an empty string),
the balance to 0, and the
account number to 0. It should also decrement the total number of
accounts.
- Also add a static method Account consolidate(Account acct1, Account acct2) to your Account class that creates a new account whose balance is
the sum of the balances in acct1 and acct2 and closes acct1 and acct2.
The new account should be
returned. Two important rules of consolidation:
- Only accounts with the same name can be consolidated. The new
account gets the name on the old accounts.
- Two accounts with the same number cannot be consolidated. (This would
be an easy way to double your money!)
Check these conditions before creating the new account. If either condition
fails, do not create the new account or close
the old accounts; print a useful message and return null.
- Following the comments in TestAccounts2.java, add code to create
a second account for Madison with a balance of 500. Print this account
and the total number of accounts.
Then consolidate Madison's two accounts into account madConsolidated;
print all three accounts and the total number of accounts.
- Add code to consolidate Spud's account with itself, putting
the new account into spudConsolidated.
Of course, this consolidation shouldn't
work, but it's useful to see what happens when you try it.
Print both spudAcct and spudConsolidated after the call.
- Finally, add code to consolidate Madison's consolidated account
with Spud's account, putting the new account into jointAcct. Once
again, the consolidation shouldn't work. Print all three accounts
after the call to consolidate. Run the program and make sure it does
what it should.
To submit your code: Tar the files in your lab12 directory
(be sure to name the file with your names, not lab12.tgz)
and cp the tgz file to the directory:
/home/staff/bouchard/CPSC120B/lab12