5
Section 2 Lesson 1: Working with Pre-Written Code 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 Directions: Identify the vocabulary word for each definition below. 1. A method that can modify an object. 2. A method that can access the contents of an object but does not modify that object. 3. Object that can store multiple object types and can grow and shrink dynamically as required. 4. The process where one object acquires the properties of another. 5. 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. Install the JavaBank Case Study. 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

JP_V01_S02_L01_try

Embed Size (px)

Citation preview

Page 1: JP_V01_S02_L01_try

Section 2 Lesson 1: Working with Pre-Written Code

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

VocabularyDirections: Identify the vocabulary word for each definition below.

1. A method that can modify an object.2. A method that can access the contents of an object but

does not modify that object.3. Object that can store multiple object types and can grow

and shrink dynamically as required.4. The process where one object acquires the properties of

another.5. 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. Install the JavaBank Case Study. 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

Page 2: JP_V01_S02_L01_try

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 application2. 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];

3. 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());

Page 3: JP_V01_S02_L01_try

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 Account

◦ Delete Account

◦ 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:Action Account Name Account Number BalanceCreate Sanjay Gupta 11556 300Create He Xia 22338 500Create Ilya Mustafana 44559 1000

Page 4: JP_V01_S02_L01_try

• 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 BalanceWithdrawal Sanjay Gupta 11556 100Deposit He Xia 22338 200Withdrawal 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. Modify the TestBank application to create and print the details of accounts as follows:

Action Account Name Account Number BalanceCreate Julia Wen 11559 3000Create Raj Muthiah 22340 5000Create 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 BalanceWithdrawal Julia Wen 11559 1000Deposit Raj Muthiah 22340 200Withdrawal Kato Nishizaki 44575 700

13.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 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

Page 5: JP_V01_S02_L01_try

// 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.