ICS 141. Programming with Objects. Lab 3.

15 Points

Due January 27, 2004 (or no later than Feb. 3)

 

Your Name:  _____________________________________________________________

Points: 15

Points earned:

 

 

You must attempt to complete the lab tonight.  If you cannot, you may work on it outside and submit it by the beginning of the next class.  But I will allow that only if you work on the problem in the assigned lab time.

 

Goals: To learn arrays of objects and the basics of inheritance.

 

Part I: Arrays of Objects.

 

Here is a class called Account.  Its attributes are just the name of the account holder and the balance.  It has both accessor and mutator methods.

 

Turn in:  Draw a class diagram for the Account class.

 

// Class:     Account

// Author:    Kuodi Jian

// Date:      Jan. 27, 2004

// Purpose:   Keep track of information for one bank account

 

public class Account {

       // Attributes

       private double balance;

       private String name;

 

       // Methods

       // Constructor

       public Account(String name, double openingBalance) {

              this.name = name;

              balance = openingBalance;

       }

 

       // Remove amount from account balance

       public double withdraw(double amount) {

              balance = balance - amount;

              return balance;

       }

 

       // Returns the account balance

       public double getBalance() {

              return balance;

       }

 

       // Initializes the account balance

       public void setBalance(double blnce) {

              balance = blnce;

       }

 

 

 

       // Returns the account name

       public String getName() {

              return name;

       }

 

       // Returns the account information in String format

       public String toString() {

              return name + "          " + balance;

       }

}

 

--------------------------------------------------------------------------

 

Here is code for a class called Bank.  Bank uses the Account class.  Another way of saying this is to say that one of the Bank’s components is a set of Account’s.  There is a composition relationship between the Bank and the Account class.

 

Some of the code is missing.  You will need to fill it in later.

 

Turn in:  Draw a class diagram for the Bank class.

 

// Class:     Bank

// Author:    Kuodi Jian

// Date:      Jan. 27, 2004

// Purpose:   One bank holds an array of accounts

 

import javax.swing.*;

 

public class Bank {

 

       // Components and attributes

       private Account[] accounts;

 

       // Methods

       // Constructor

       public Bank(int numAcct) {

       accounts = new Account[numAcct];

}

 

// Gets account data and instantiates new accounts

public void readAccountData()     {

       String name;

       double balance;

 

              for (int index = 0; index < accounts.length; index++) {

                    

// get account name and its balance

// from user inputs

 

// Fill in missing code here

 

                     accounts[index] = new Account(name, balance);

              }

       }

 

       // Retrieves the name of one bank account

public String getName(int index)  {

return accounts[index].getName();

       }

 

       // Withdraws money from one bank account

       public void withdraw(int index, double amount)  {

              /*  Fill in the missing code to reduce the

balance of the account

              referenced by the array index by

the amount.  You do this by calling

the withdraw method in the Account

class  */

       }

 

       // Creates a string containing information on all accounts in the bank

       public String toString()   {

       String acctStr = “Name        Balance       \n”;

       acctStr = acctStr + “-----------------------\n”;

 

       for(int index = 0; index < accounts.length;

              index++)      {

       acctStr = acctStr + accounts[index] + “\n”;    

}

 

return acctStr;

}

}

 

--------------------------------------------------------------------------

 

Here is code for an application class which uses the Account and Bank classes. After you have filled in the code for the Bank class, this main() method will produce the following sample run.  The following code does the following things:

 

1)                  Ask the user to input the number of accounts and then create an array of Account in the right size.

2)                  Fill the array with account information.

3)                  Withdraw 25 dollars from the third account.

4)                  Print all accounts.

 

Sample run:

 

Inputs:

Number of Account: 3

Name: Lim      Balance: 100

Name: Joe      Balance: 200

Name: Jill        Balance: 300

 

Output:

 

Withdraw $25 from Jill’s account

 

Name               Balance

-------------------------------

Lim                  100

Joe                   200

Jill                    275

 

 

 

Turn in:  Draw a class diagram for Lab3 class. 

 

// Class:     Lab3

// Author:    Kuodi Jian

// Date:      Jan. 27, 2004

// Purpose:   Demonstrates that Bank and Account classes work properly

 

import javax.swing.*;

 

public class Lab3    {

       public static void main(String[] s)      {

String numStr =

              JOptionPane.showInputDialog(“How many accounts?”);

       int numAccounts = Integer.parseInt(numStr);

 

              Bank bank = new Bank(numAccounts);

bank.readAccountData();

 

// output the message of

// Withdraw $25 from Jill’s account

 

              bank.withdraw(2, 25.0);

              System.out.println(bank.toString());

       }

}

 

 

 

Turn in: 

 

1.      A single class diagram for Account, Bank and Lab3.  The final class diagram should also show the relationships between all three classes.

2.      Print outs of all the program files.

3.      A diskette containing your source code (.java files) and your executable files (.class files).

4.      A screen capture at least one output window, paste it into a MS Word page and print it out.