![]() |
CSE 142 Summer 2001Homework #5Due: At the beginning of class, Monday, July 23, 2001 |
Your solution to the problems in this part should be written out on paper and handed in. No online turnin is available. You do not have to run your solutions on a computer, but you are encouraged to do so to check your answers.
All of the problems in this part concern collections of our venerable BankAccount
objects. You should assume that we've added extra methods to access the fields
of the bank account. Here is a summary of the public interface of BankAccount
for this assignment.
/** construct BankAccount with given name, account number, and initial balance */ public BankAccount(String name, int number, double balance) {...} /** return account name */ public String getName() {...} /** return account number */ public int getNumber() {...} /** return account balance */ public double getBalance() { ... } /** deposit given amount in account */ public void deposit(double amount) {...}
Now, assume we have a class named Accounts
, which contains a list of
BankAccount
objects.
/** A collection of BankAccounts */ public class Accounts { // instance variables ArrayList accounts; // the BankAccounts in this list /** Construct an empty list of BankAccounts */ public Accounts() { this.accounts = new ArrayList(); } /** Add the given account to this list of BankAccounts */ public void add(BankAccount account) { this.accounts.add(account); } ... }
For each of the following questions, write a new method for class Accounts
that works as specified. All of these involve iterating over the accounts
list to do something to items in that list.
/** return the total amount of money in all accounts */ public double totalBalances() { ... }
/** return the average amount of money in the accounts */ public double averageBalance() { ... }
accountList
,
accountList.numberAccountsNamed("Smith")
should return 3)./** return number of accounts with the given name */ public int numberAccountsNamed(String name) { ... }
accountList.gift(1000.00)
should deposit 1000.00 in every
account in accountList
.)/** Deposit the given amount in every account */ public void gift(double amount) { ... }
For this part of the assignment, you should write and test a set of classes to make up a simple address book. There are three classes involved (details below)
AddressRecord
defines a single entry in the address book: name
(first and last), city (string), zip code (integer), and birth year (also
integer). AddressBook
defines a list of AddressRecord
s and several methods for
creating and processing this list. AddressTest
should contain a method test()
that creates an AddressBook
and
tests the methods in it.This class should define a constructor AddressRecord(String lastName,
String firstName, String city, int zip, int birthYear)
that creates a new
AddressRecord
object, with fields initialized to the given
values. The class should also define a toString()
method that returns a
readable description of the fields in the AddressRecord.
To reduce the amount of typing for this assignment, and because AddressRecord
s
are simple data objects intended only for use as components of an AddressBook
,
we'll relax our usual information hiding rules. You can make the instance
variables in an AddressRecord
(the fields used to store the name,
address, etc.) public, and reference them directly inside AddressBook
.
If the instance variables are public, you won't need to define
additional methods to access or change them.
This is the interesting part of the problem. This class should contain
a private ArrayList
to hold AddressRecords
that are
added to the AddressBook
. The two basic methods this class
should contain are a constructor and a method to add new AddressRecord
s
to the AddressBook
.
/** Construct an empty AddressBook */ public AddressBook() { ... } /** Add the given AddressRecord to this AddressBook */ public void add(AddressRecord address) { ... }
The other methods in this class should examine the ArrayList
containing the AddressRecord
s and do the following operations:
/** Return the number of entries in this AddressBook with the given zip code */ public int numberInZip(int zipCode) { ... } /** Print on System.out all of the AddressRecords with the given last name */ public void printMatching(String lastName) { ... } /** Given the current year, return the number of entries in this AddressBook * where the person's age is at least as great as minimumAge given that * currentYear is the current year (i.e., numberOlderThan(2001,21) should * return the number of people in the AddressBook at least 21 years old if * the year is 2001. */ public int numberOlderThan(int currentYear, int minimumAge) { ... } /** Return a new ArrayList containing all of the entries in this AddressBook * with the given city */ public ArrayList entriesIn(String city) { ... }
Finally, define a class AddressTest
containing a method test()
to test your AddressBook
class. Method test()
should
create a new AddressBook
, add several entries to it, and then
verify that the other methods work properly.
Turn in your solution to the programming part by filling out this turnin form and selecting the files AddressRecord.java,
AddressBook.java, and AddressTest.java
containing your code. When
you submit your files, they will be compiled on the turnin server and you will
see a receipt indicating whether the program compiled successfully. If any
errors were detected, please fix them and turn your program in again. If
no errors were found, you are done with electronic turnin, however, we suggest that you print or save a
copy of the electronic receipt for future reference.
Hand in your written solutions to the questions in the first part of the assignment at the beginning of lecture on the due date.