You are on page 1of 5

Section 2 Lesson 1: Working with Pre-Written Code

Try It: Practice Activities

Objectives
• Read and understand a pre-written java program consisting of classes and interacting
objects
• Apply the concept of inheritance in the solutions of problems
• Test classes in isolation
• Describe when it is more appropriate to use an ArrayList than an Array

Vocabulary
Identify the vocabulary word for each definition below.
A method that can modify an object.
A method that can access the contents of an
object but does not modify that object.
Object that can store multiple object types and can grow
and shrink dynamically as required.
The process where one object acquires the properties of
another.
Allows you to check the quality of the code for a class
independent of the rest of the program code.

Try It/Solve It
1. If you did not install the JavaBank Case Study during the lesson, follow these steps. To
install JavaBank:
• Unzip the JavaBank.jar.zip file and move it to a convenient location on your
computer.
• In Eclipse, select File → New → Java Project.
• Select “Create project from existing source”.
• Click Browse.
• Select the first project folder from the location where you have downloaded and

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
extracted the JavaBank.jar.zip files.
• Click OK.
• Click Finish.
• In the Package Explorer window, select the project.
• From the menu, select Project → Properties.
• Click Java Build Path.
• Select the Libraries tab.
• Click the Add External JARs... button.
• Navigate to the folder containing Banking.jar. Select the Banking.jar file.
• Click the + sign next to Default Package and run the Banking.java application
2. Examine the code in JavaBankList to see how to declare and use the ArrayList
Accounts.

// ArrayList to store Account details


ArrayList<Account> Accounts = new ArrayList<Account>();

replaces

// Array to store Account details


static Account myAccounts[] = new Account[MaxAccounts];

3. Examine the code in JavaBankList to see how the code for creating an account has
changed by using an ArrayList.

//add a new account to the list using the Account constuctor


Accounts.add(new Account(Name,Accountnum,Balance));

//Set a temp Account for display purposes


Account tempAccount = (Account)Accounts.get(noAccounts);

//Display tempAccount
displayJTextArea.setText(tempAccount.getaccountname() + " " +
tempAccount.getaccountnum() + " " + tempAccount.getbalance());

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
4. Examine the code in JavaBankArrayList to see how the code for making a deposit or
withdrawal is used.
5. Amend the JavaBankArrayList program to delete an account using the remove
operation. Hint: This is similar to the set operation for transactions.
6. Unit test your new code.
7. Explore JavaBank. Record your observations and the answers to the following
questions in your journal.
• What happens when you:
◦ Display Accounts
◦ Create Accounts
◦ Delete Accounts
◦ Make a Withdrawal Transaction
◦ Make a Deposit Transaction
• Can you display accounts before any are created?
• Can you create an account without entering anything in the fields?
• Can you make a withdrawal transaction with no amount in the Withdrawal field?
• Can you do a deposit transaction with no amount in the Deposit field?
• What other questions do you have about the JavaBank application?
• What changes would you make to the current application to make it function better?
• What additions would you make to the current application to increase its
functionality?
8. Examine the code files and record the answers to the following questions in your
Journal.
• How many code files are there?
• Where is main coded?
• What does main call?
• What is called when you click the Create button?
• What other questions do you have about the JavaBank application?
• What changes would you make to the current application to make it function better?
• What additions would you make to the current application to increase it's
functionality?
9. Run JavaBank and perform the following actions:

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
Action Account Name Account Number Balance
Create Sanjay Gupta 11556 300
Create He Xia 22338 500
Create Ilya Mustafana 44559 1000

• Display the accounts to ensure they have been created.


10. Perform the following transactions and display the accounts to ensure they have been
completed.
Action Account Name Account Number Balance
Withdrawal Sanjay Gupta 11556 100
Deposit He Xia 22338 200
Withdrawal Ilya Mustafana 44559 700

• What are the accessor methods in the account class?


• Which procedure calls these accessors?
• What are the modifier methods?
• Which procedure calls these modifiers?
11. Run TestBank.java. View the results. Modify the TestBank application to create and print the
details of accounts as follows:
Action Account Name Account Number Balance
Create Julia Wen 11559 3000
Create Raj Muthiah 22340 5000
Create Kato Nishizaki 44575 1500

• Were the instances created as you expected?


12. Modify the TestBank application to modify the instances and print the details of
accounts as follows:
Action Account Name Account Number Balance
Withdrawal Julia Wen 11559 1000
Deposit Raj Muthiah 22340 200
Withdrawal Kato Nishizaki 44575 700

13. Extend the BankAccount class by writing a SavingsAccount class. The


SavingsAccount class should behave like the BankAccount class in that it should
provide methods for displaying the balance, making deposits, and making withdrawals.
The Savings Account should extend the BankAccount class by updating the balance
during each operation by computing accumulated interest.
• Create a set of test data that tests the functionality of the SavingsAccount class.
• Modify the testBank program to fully test the newly created SavingsAccount using

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
the test data you have created.
14. Examine the code in JavaBankArrayList to see how to declare and use the ArrayList
Accounts.

// ArrayList to store Account details


ArrayList<Account> Accounts = new ArrayList<Account>();

replaces

// Array to store Account details


static Account myAccounts[] = new Account[MaxAccounts];

15. Examine the code in JavaBankArrayList to see how the code for creating an account
has changed by using an ArrayList.

//add a new account to the list using the Account constuctor


Accounts.add(new Account(Name,Accountnum,Balance));

//Set a temp Account for display purposes


Account tempAccount = (Account)Accounts.get(noAccounts);

//Display tempAccount
displayJTextArea.setText(tempAccount.getaccountname() + " " +
tempAccount.getaccountnum() + " " + tempAccount.getbalance());

16. Examine the code in JavaBankArrayList to see how the code for making a deposit or
withdrawal is used.
17. Amend the JavaBankArrayList program to delete an account using the remove
operation. Hint: This is similar to the set operation for transactions.
18. Amend the JavaBankArrayList program to delete an account using the remove
operation. Hint: This is similar to the set operation for transactions.
19. Unit test your new code.

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.

You might also like