CPT121-Task 5.1.3 Managing Arrays of Objects Solved
1. Assume that the application class below makes use of the Account class discussed previously import java.util.*; public class TestAccounts2
{ public static void main(String args[])
{
String accountID, name; double balance; Scanner sc = new Scanner(System.in));
/*** Array used to store up to 10 accounts ***/
Account[] accounts = new Account[1];
/*** CODE for part a) goes here ***/ ...
/*** CODE for part c) goes here *** ...
// print details for all accounts
System.out.println("Account Details");
/*** Code for part b) goes here ***/ ...
}
}
a) Write code to populate the array with the Accounts containing the following details:
ID Name Balance S5234 David $1000 S1239 Cliff $2000 S4236 Martin $3000 b) Write code which iterates through the objects that are currently stored in the array and prints their details to the screen. Your iteration loop should cease executing once it has gone past the last object in the array.
c) What will be the output printed by the program if we replace the indicated comment for part C in the code above with each of the following blocks?
Tip: You may find drawing diagrams of the array and the objects each element is pointing to useful when considering the effect of each code segment.
i. a[1] = a[0]; a[2] = a[1]; a[0] = a[2];
ii. a[0] = a[1];
a[0].withdraw(100); a[1].withdraw(100);
iii. a[0].transfer(a[1],500); a[1].transfer(a[2],500);