CPSC 120 Lab 12
Two-Dimensional Arrays
Dependencies Among Objects of the Same Class
Lab Objectives
- Experience using 2 dimensional arrays.
- Gain experience (and a better understanding of )
passing an object of the same class as a parameter
and using static (class) variables.
|
As usual, create a lab12 subdirectory for today's lab, open up
Mozilla Firefox and
the Web version of this handout, and open Emacs.
Magic Squares
One interesting application of two dimensional arrays is magic
squares. A magic square is a square matrix of numbers in which the
sum of every row, every column, and both diagonals is the same.
Magic squares have been studied for many years, and there are some
particularly famous magic squares. In this exercise you will write code
to determine whether a square is magic.
The file Square.java contains the
shell for a class that represents a square matrix. It contains
a constructor that takes a file object to read
from and uses it to initialize the matrix,
plus headers for a toString method and methods to
find the sum of a given row, find the sum of a given column,
find the sum of the main (or other) diagonal, and determine whether the
square is magic.
The file SquareTest.java
contains the shell for a program that prompts the user to enter file names
for text files that contain input data for squares and tells
whether each is a magic square. Follow the comments to fill in the
remaining code, but take it a piece at a time as follows:
- First, study the code for the constructor to be sure you
understand what it is doing.
- Fill in the code for the toString method in the Square
class. Then, add code to SquareTest just to read and print each square.
(Note that the methods in the Square class have dummy return
values so it will compile.)
- Download and extract the text files in
SquareData.tgz and use them to test
your constructor and toString methods. The command to
extract from the tar file is tar xzf SquareData.tgz.
- Now, add code to various sum methods in the square class. Add
code to SquareTest to test each of these methods as you write them.
- Finally, complete the isMagic method following the comments
in the code then add code to SquareTest
to indicate whether each square is magic. You should find that the first,
second, and third squares files are magic, and that the rest (fourth
through seventh) are not.
Print Square.java to hand in.
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.
- Print Account.java and TestAccounts2.java to turn in.
What to turn in
Turn in hardcopy of Square.java,
Account.java, and TestAccounts2.java. Zip your lab12 directory
and submit it to Inquire.