Memory & Reference Variables


Reference Variables & Memory

WhiteMouse.java
WhiteMouseFamily.java

Deitel 3, 8.10


Classwork


Part A

In a Netbeans project

Create a class Account representing a bank account and a class Customer, representing a bank customer.

 

Give Account two public instance variables: bal, a double representing a balance, and own, a Customer.

Write a toString() for Account that returns the balance with a dollar sign in front of it (it doesn't show Customer info).

 

Give Customer two public instance variables: name, a String, and acct, an Account.

Write a toString() for Customer that returns the name with "Account Owner: " in front of it (not showing Account info)

 

Create a class with a main, and in main create one Account and one Customer.  Set their instance variables to point to each other so the Customer is the owner of the Account and the Account is the account of the Customer. 

Set the balance using the Customer variable and the name using the Account variable., then print them.


Part B

The following code is not useful; it does several silly things; but it is the shortest code I could write with examples of most of the things people tend to get confused about.

1 package memory;
 2 public class BankTrace {
 3
 4     public static void method2(Customer cust1, Customer cust2) {
 5         cust2 = cust1.acct.own;
 6         cust1.acct = new Account();
 7         cust2.acct.bal = 3999.50;
 8         cust2 = new Customer();
 9         cust2.acct = new Account();
10         cust2.acct.bal = 77.0;
11     }
12
13     public static void method1(Customer cust, Account money, double bal) {
14         money.bal = bal;
15         bal = 99.33;
16         money.own = cust;
17         cust.acct = money;
18         Account newAct = new Account();
19         newAct.bal = 6001.33;
20         method2(cust, newAct.own); // not a typo I promise
21     }
22
23     public static void main(String[] args) {
24         Customer customerJoan = new Customer();
25         Account bankAccount = new Account();
26         bankAccount.bal = 828.00;
27         double x = 4403.20;
28
29         method1(customerJoan, bankAccount, x);
30
31     }
32 }

 

Questions (in a Word document attached to your submission, NOT hidden in your zipped directory):
Consider how the above code would run with your classes; do not actually run it.  

At the end of this program:

  1. How many customer objects total were created?
  2. How many account objects total were created?
  3. For the local variable in main bankAccount,  what value is in bankAccount.bal?
  4. For the local variable in main customerJoan,  is customerJoan.acct null?  If not, what value is in customerJoan.acct.bal? 

Explain briefly (noting line numbers), how you got your answers.