Java Programming Language, Java SE 6
Activity Guide - Volume 1 SL-275-SE6 G.2
D61748GC11 Edition 1.1 June 2010 D67982
Oracle University and GUIDANCE VIEW SDN BHD use only
Copyright © 2009, 2010, Oracle and/or its affiliates. All rights reserved.
Disclaimer
This document contains proprietary information, is provided under a license agreement containing restrictions on use and disclosure, and is protected by copyright and other intellectual property laws. You may copy and print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way. Except as expressly permitted in your license agreement or allowed by law, you may not use, share, download, upload, copy, print, display, perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization of Oracle.
The information contained in this document is subject to change without notice. If you find any problems in the document, please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not warranted to be error-free.
Sun Microsystems, Inc. Disclaimer
This training manual may include references to materials, offerings, or products that were previously offered by Sun Microsystems, Inc. Certain materials, offerings, services, or products may no longer be offered or provided.Oracle and its affiliates cannot be held responsible for any such references should they appear in the text provided.
Restricted Rights Notice
If this documentation is delivered to the U.S. Government or anyone using the documentation on behalf of the U.S.
Government, the following notice is applicable:
U.S. GOVERNMENT RIGHTS
The U.S. Government’s rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract.
Trademark Notice
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. UNIX is a registered trademark licensed through X/Open Company, Ltd.
Oracle University and GUIDANCE VIEW SDN BHD use only
This page intentionally left blank.
Oracle University and GUIDANCE VIEW SDN BHD use only
This page intentionally left blank.
Oracle University and GUIDANCE VIEW SDN BHD use only
Table of Contents
About This Workbook ...Preface-xvii Lab Goals... Preface-xvii Performing the Exercises ... Preface-xviii Conventions ... Preface-xix Typographical Conventions ... Preface-xix Additional Conventions... Preface-xx Getting Started...1-1 Objectives ... 1-1 Exercise 1: Exploring Java™ Program Errors ... 1-2 Preparation... 1-2 Task – Correcting Compilation and Runtime Errors... 1-2 Exercise 2: Creating a Test Program (Level 1) ... 1-4 Preparation... 1-4 Task 1 – Copying the Account Class ... 1-5 Task 2 – Creating the TestAccount Class... 1-5 Task 3 – Compiling the TestAccount Class ... 1-6 Task 4 – Running the TestAccount Program ... 1-6 Exercise 2: Creating a Test Program (Level 2) ... 1-7 Preparation... 1-7 Task 1 – Copying the Account Class ... 1-8 Task 2 – Creating the TestAccount Class... 1-8 Task 3 – Compiling the TestAccount Class ... 1-9 Task 4 – Running the TestAccount Program ... 1-9 Exercise 2: Creating a Test Program (Level 3) ... 1-10 Preparation... 1-10 Task 1 – Copying the Account Class ... 1-11 Task 2 – Creating the TestAccount Class... 1-11 Task 3 – Compiling the TestAccount Class ... 1-12 Task 4 – Running the TestAccount Program ... 1-12 Exercise Summary... 1-13
Oracle University and GUIDANCE VIEW SDN BHD use only
Object-Oriented Programming ... 2-1 Objectives ... 2-1 Exercise 1: Using the Java API Documentation... 2-2 Preparation... 2-2 Task – Using the Java API Documentation ... 2-2 Exercise 2: Exploring Encapsulation, Version 1 (Level 1) ... 2-3 Preparation... 2-4 Task 1 – Deleting the Account Class... 2-4 Task 2 – Creating the Account Class ... 2-4 Task 3 – Creating the TestAccount2 Class ... 2-5 Task 4 – Compiling the TestAccount2 Class... 2-5 Task 5 – Running the TestAccount2 Program ... 2-5 Exercise 2: Exploring Encapsulation, Version 1 (Level 2) ... 2-6 Preparation... 2-6 Task 1 – Deleting the Account Class... 2-7 Task 2 – Creating the Account Class ... 2-7 Task 3 – Creating the TestAccount2 Class ... 2-7 Task 4 – Compiling the TestAccount2 Class... 2-8 Task 5 – Running the TestAccount2 Program ... 2-8 Exercise 2: Exploring Encapsulation, Version 1 (Level 3) ... 2-9 Preparation... 2-9 Task 1 – Deleting the Account Class... 2-10 Task 2 – Creating the Account Class ... 2-10 Task 3 – Creating the TestAccount2 Class ... 2-10 Task 4 – Compiling the TestAccount2 Class... 2-11 Task 5 – Running the TestAccount2 Program ... 2-11 Exercise 3: Exploring Encapsulation, Version 2 (Level 1) ... 2-12 Preparation... 2-12 Task 1 – Modifying the Account Class ... 2-13 Task 2 – Modifying the TestAccount Class ... 2-13 Task 3 – Compiling the TestAccount Class ... 2-13 Task 4 – Running the TestAccount Program ... 2-13 Exercise 3: Exploring Encapsulation, Version 2 (Level 2) ... 2-14 Preparation... 2-14 Task 1 – Modifying the Account Class ... 2-14 Task 2 – Modifying the TestAccount Class ... 2-15 Task 3 – Compiling the TestAccount Class ... 2-15 Task 4 – Running the TestAccount Program ... 2-15 Exercise 3: Exploring Encapsulation, Version 2 (Level 3) ... 2-16 Preparation... 2-16 Task 1 – Modifying the Account Class ... 2-16 Task 2 – Modifying the TestAccount Class ... 2-17 Task 3 – Compiling the TestAccount Class ... 2-17 Task 4 – Running the TestAccount Program ... 2-17 Exercise 4: Creating Java Packages... 2-18 Preparation... 2-19
Oracle University and GUIDANCE VIEW SDN BHD use only
Task 1 – Creating the Java Packages ... 2-19 Task 2 – Moving and Modifying the Account Class ... 2-19 Task 3 – Moving the TestAccount Class ... 2-20 Task 4 – Compiling the TestAccount Class ... 2-20 Task 5 – Running the TestAccount Program ... 2-20 Exercise Summary... 2-21 Identifiers, Keywords, and Types ...3-1 Objectives ... 3-1 Exercise 1: Investigating Reference Assignment ... 3-2 Preparation... 3-2 Task 1 – Creating the TestMyPoint Class... 3-3 Task 2 – Compiling the TestMyPoint Class ... 3-4 Task 3 – Running the TestMyPoint Program ... 3-4 Exercise 2: Creating Customer Accounts (Level 1) ... 3-5 Preparation... 3-6 Task 1 – Creating the Customer Class ... 3-6 Task 2 – Copying the TestBanking Class... 3-6 Task 3 – Compiling the TestBanking Class ... 3-7 Task 4 – Running the TestBanking Program ... 3-7 Exercise 2: Creating Customer Accounts (Level 2) ... 3-8 Preparation... 3-8 Task 1 – Creating the Customer Class ... 3-9 Task 2 – Copying the TestBanking Class... 3-9 Task 3 – Compiling the TestBanking Class ... 3-9 Task 4 – Running the TestBanking Program ... 3-10 Exercise 2: Creating Customer Accounts (Level 3) ... 3-11 Preparation... 3-11 Task 1 – Creating the Customer Class ... 3-12 Task 2 – Copying the TestBanking Class... 3-13 Task 3 – Compiling the TestBanking Class ... 3-13 Task 4 – Running the TestBanking Program ... 3-13 Exercise Summary... 3-14 Expressions and Flow Control...4-1 Objectives ... 4-1 Exercise 1: Using Loops and Branching Statements ... 4-2 Preparation... 4-3 Task 1 – Creating the FooBarBaz Class ... 4-3 Task 2 – Compiling the FooBarBaz Class ... 4-4 Task 3 – Running the FooBarBaz Program... 4-4 Hints... 4-4 Exercise 2: Using Conditional Statements in the Account Class
(Level 1) ... 4-5 Preparation... 4-5 Task 1 – Modifying the Account Class ... 4-6 Task 2 – Deleting the Current TestBanking Class ... 4-6
Oracle University and GUIDANCE VIEW SDN BHD use only
Task 3 – Copying the TestBanking Class... 4-6 Task 4 – Compiling the TestBanking Class ... 4-6 Task 5 – Running the TestBanking Program ... 4-7 Exercise 2: Using Conditional Statements in the Account Class
(Level 2) ... 4-8 Preparation... 4-8 Task 1 – Modifying the Account Class ... 4-9 Task 2 – Deleting the Current TestBanking Class ... 4-9 Task 3 – Copying the TestBanking Class... 4-9 Task 4 – Compiling the TestBanking Class ... 4-9 Task 5 – Running the TestBanking Program ... 4-9 Exercise 2: Using Conditional Statements in the Account Class
(Level 3) ... 4-10 Preparation... 4-10 Task 1 – Modifying the Account Class ... 4-11 Task 2 – Deleting the Current TestBanking Class ... 4-11 Task 3 – Copying the TestBanking Class... 4-11 Task 4 – Compiling the TestBanking Class ... 4-11 Task 5 – Running the TestBanking Program ... 4-12 Exercise 3: Using Nested Loops (Advanced) ... 4-13 Preparation... 4-13 Task 1 – Writing the isSubString Method... 4-14 Task 2 – Compiling the TestIsSubString Class ... 4-14 Task 3 – Running the TestIsSubString Program... 4-14 Hints... 4-15 Exercise Summary... 4-16 Arrays... 5-1 Objectives ... 5-1 Exercise 1 – Using Primitive Arrays (Level 2) ... 5-2 Preparation... 5-2 Task 1 – Creating the TestArrays Class... 5-2 Task 2 – Compiling the TestArrays Class ... 5-3 Task 3 – Running the TestArrays Program... 5-3 Hint ... 5-4 Exercise 1 – Using Primitive Arrays (Level 3) ... 5-5 Preparation... 5-5 Task 1 – Creating the TestArrays Class... 5-5 Task 2 – Compiling the TestArrays Class ... 5-7 Task 3 – Running the TestArrays Program... 5-7 Exercise 2 – Using Arrays to Represent One-to-Many Associations
(Level 1) ... 5-8 Preparation... 5-9 Task 1 – Creating the Bank Class ... 5-9 Task 2 – Deleting the Current TestBanking Class... 5-9 Task 3 – Copying the TestBanking Class... 5-10
Oracle University and GUIDANCE VIEW SDN BHD use only
Task 4 – Compiling the TestBanking Class ... 5-10 Task 5 – Running the TestBanking Program ... 5-10 Exercise 2 – Using Arrays to Represent One-to-Many Associations
(Level 2) ... 5-11 Task 1 – Creating the Bank Class ... 5-12 Task 2 – Deleting the Current TestBanking Class... 5-12 Task 3 – Copying the TestBanking Class... 5-12 Task 4 – Compiling the TestBanking Class ... 5-12 Task 5 – Running the TestBanking Program ... 5-13 Exercise 2 – Using Arrays to Represent One-to-Many Associations
(Level 3) ... 5-14 Task 1 – Creating the Bank Class ... 5-15 Task 2 – Deleting the Current TestBanking Class... 5-16 Task 3 – Copying the TestBanking Class... 5-16 Task 4 – Compiling the TestBanking Class ... 5-16 Task 5 – Running the TestBanking Program ... 5-16 Exercise Summary... 5-17 Class Design...6-1 Objectives ... 6-1 Exercise 1: Creating Bank Account Subclasses (Level 1)... 6-2 Preparation... 6-3 Task 1 – Modifying the Account Class ... 6-4 Task 2 – Creating the SavingsAccount Class ... 6-4 Task 3 – Creating the CheckingAccount Class... 6-4 Task 4 – Deleting the Current TestBanking Class ... 6-5 Task 5 – Copying the TestBanking Class... 6-5 Task 6 – Compiling the TestBanking Class ... 6-5 Task 7 – Running the TestBanking Program ... 6-5 Exercise 1: Creating Bank Account Subclasses (Level 2)... 6-7 Preparation... 6-7 Task 1 – Modifying the Account Class ... 6-8 Task 2 – Creating the SavingsAccount Class ... 6-8 Task 3 – Creating the CheckingAccount Class... 6-9 Task 4 – Deleting the Current TestBanking Class ... 6-10 Task 5 – Copying the TestBanking Class... 6-10 Task 6 – Compiling the TestBanking Class ... 6-10 Task 7 – Running the TestBanking Program ... 6-10 Exercise 1: Creating Bank Account Subclasses (Level 3)... 6-12 Preparation... 6-12 Task 1 – Modifying the Account Class ... 6-13 Task 2 – Creating the SavingsAccount Class ... 6-13 Task 3 – Creating the CheckingAccount Class... 6-14 Task 4 – Deleting the Current TestBanking Class ... 6-15 Task 5 – Copying the TestBanking Class... 6-15 Task 6 – Compiling the TestBanking Class ... 6-16 Task 7 – Running the TestBanking Program ... 6-16
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Creating a Heterogeneous Collection of Customer Accounts (Level 1)... 6-18
Preparation... 6-19 Task 1 – Modifying the Customer Class... 6-19 Task 2 – Copying and Completing the CustomerReport
Class ... 6-19 Task 3 – Copying the TestReport Class... 6-20 Task 4 – Compiling the TestReport Class ... 6-20 Task 5 – Running the TestReport Program... 6-20 Exercise 2: Creating a Heterogeneous Collection of Customer
Accounts (Level 2)... 6-21 Task 1 – Modifying the Customer Class... 6-22 Task 2 – Copying and Completing the CustomerReport
Class ... 6-22 Task 3 – Copying the TestReport Class... 6-23 Task 4 – Compiling the TestReport Class ... 6-23 Task 5 – Running the TestReport Program... 6-23 Exercise 2: Creating a Heterogeneous Collection of Customer
Accounts (Level 3)... 6-24 Task 1 – Modifying the Customer Class... 6-25 Task 2 – Copying and Completing the CustomerReport
Class ... 6-26 Task 3 – Copying the TestReport Class... 6-26 Task 4 – Compiling the TestReport Class ... 6-26 Task 5 – Running the TestReport Program... 6-27 Exercise 3: Creating a Batch Program (Advanced) ... 6-28 Preparation... 6-29 Task 1 – Modifying the SavingsAccount Class... 6-29 Task 2 – Creating the AccumulateSavingsBatch
Class ... 6-29 Task 3 – Copying the TestBatch Class ... 6-30 Task 4 – Compiling the TestBatch Class ... 6-30 Task 5 – Running the TestBatch Program... 6-30 Exercise Summary... 6-32 Advanced Class Features ... 7-1 Objectives ... 7-1 Exercise 1: Applying Static Members to a Design (Level 1) ... 7-2 Preparation... 7-3 Task 1 – Modifying the Bank Class... 7-3 Task 2 – Modifying the CustomerReport Class... 7-3 Task 3 – Deleting the Current TestReport Class ... 7-4 Task 4 – Copying the TestReport Class... 7-4 Task 5 – Compiling the TestReport Class ... 7-4 Task 6 – Running the TestReport Program... 7-4 Exercise 1: Applying Static Members to a Design (Level 2) ... 7-5
Oracle University and GUIDANCE VIEW SDN BHD use only
Task 1 – Modifying the Bank Class... 7-6 Task 2 – Modifying the CustomerReport Class... 7-6 Task 3 – Deleting the Current TestReport Class ... 7-7 Task 4 – Copying the TestReport Class... 7-7 Task 5 – Compiling the TestReport Class ... 7-7 Task 6 – Running the TestReport Program... 7-7 Exercise 1: Applying Static Members to a Design (Level 3) ... 7-8 Task 1 – Modifying the Bank Class... 7-9 Task 2 – Modifying the CustomerReport Class... 7-10 Task 3 – Deleting the Current TestReport Class ... 7-10 Task 4 – Copying the TestReport Class... 7-10 Task 5 – Compiling the TestReport Class ... 7-11 Task 6 – Running the TestReport Program... 7-11 Exercise 2: Working With Interfaces and Abstract Classes
(Level 1) ... 7-12 Preparation... 7-13 Task 1 – Creating the Pet Interface ... 7-13 Task 2 – Creating the Animal Classes ... 7-14 Task 3 – Creating the TestAnimals Class... 7-14 Task 4 – Compiling the TestAnimals Class ... 7-15 Task 5 – Running the TestAnimals Program ... 7-15 Exercise 2: Working With Interfaces and Abstract Classes
(Level 2) ... 7-16 Preparation... 7-16 Task 1 – Creating the Pet Interface ... 7-17 Task 2 – Creating the Animal Classes ... 7-17 Task 3 – Creating the TestAnimals Class... 7-19 Task 4 – Compiling the TestAnimals Class ... 7-20 Task 5 – Running the TestAnimals Program ... 7-20 Exercise 2: Working With Interfaces and Abstract Classes
(Level 3) ... 7-21 Preparation... 7-21 Task 1 – Creating the Pet Interface ... 7-22 Task 2 – Creating the Animal Classes ... 7-22 Task 3 – Creating the TestAnimals Class... 7-25 Task 4 – Compiling the TestAnimals Class ... 7-26 Task 5 – Running the TestAnimals Program ... 7-26 Exercise Summary... 7-27 Exceptions and Assertions ...8-1 Objectives ... 8-1 Exercise: Creating Your Own Exception (Level 1) ... 8-2 Task 1 – Creating the OverdraftException Class ... 8-4 Task 2 – Modifying the Account Class ... 8-4 Task 3 – Modifying the CheckingAccount Class ... 8-4 Task 4 – Deleting the Current TestBanking Class ... 8-4
Oracle University and GUIDANCE VIEW SDN BHD use only
Task 5 – Copying the TestBanking Class... 8-4 Task 6 – Compiling the TestBanking Class ... 8-5 Task 7 – Running the TestBanking Program ... 8-5 Exercise: Creating Your Own Exception (Level 2) ... 8-6 Task 1 – Creating the OverdraftException Class ... 8-7 Task 2 – Modifying the Account Class ... 8-7 Task 3 – Modifying the CheckingAccount Class ... 8-8 Task 4 – Deleting the Current TestBanking Class ... 8-8 Task 5 – Copying the TestBanking Class... 8-8 Task 6 – Compiling the TestBanking Class ... 8-8 Task 7 – Running the TestBanking Program ... 8-8 Exercise: Creating Your Own Exception (Level 3) ... 8-9 Task 1 – Creating the OverdraftException Class ... 8-10 Task 2 – Modifying the Account Class ... 8-11 Task 3 – Modifying the CheckingAccount Class ... 8-11 Task 4 – Deleting the Current TestBanking Class ... 8-12 Task 5 – Copying the TestBanking Class... 8-12 Task 6 – Compiling the TestBanking Class ... 8-12 Task 7 – Running the TestBanking Program ... 8-12 Exercise Summary... 8-13 Collections and Generics Framework... 9-1 Objectives ... 9-1 Exercise 1: Using Collections to Represent Association
(Level 1) ... 9-2 Preparation... 9-3 Task 1 – Modifying the Bank Class... 9-3 Task 2 – Modifying the Customer Class... 9-3 Task 3 – Compiling the TestReport Class ... 9-4 Task 4 – Running the TestReport Program ... 9-4 Exercise 1: Using Collections to Represent Association
(Level 2) ... 9-5 Preparation... 9-5 Task 1 – Modifying the Bank Class... 9-5 Task 2 – Modifying the Customer Class... 9-6 Task 3 – Compiling the TestReport Class ... 9-6 Task 4 – Running the TestReport Program... 9-6 Exercise 1: Using Collections to Represent Association
(Level 3) ... 9-8 Preparation... 9-8 Task 1 – Modifying the Bank Class... 9-9 Task 2 – Modifying the Customer Class... 9-9 Task 3 – Compiling the TestReport Class ... 9-10 Task 4 – Running the TestReport Program... 9-11 Exercise Summary... 9-12
Oracle University and GUIDANCE VIEW SDN BHD use only
I/O Fundamentals ...10-1 Console I/O and File I/O ...11-1 Objectives ... 11-1 Exercise 1: Reading a Data File (Level 1) ... 11-2 Preparation... 11-4 Task 1 – Creating a data Directory ... 11-4 Task 2 – Copying the Resource File... 11-4 Task 3 – Creating the DataSource Class... 11-5 Task 4 – Deleting Unnecessary Classes ... 11-5 Task 5 – Copying the TestReport Class... 11-5 Task 6 – Compiling the TestReport Class ... 11-5 Task 7 – Running the BankPrj Project... 11-6 Exercise 1: Reading a Data File (Level 2) ... 11-7 Preparation... 11-7 Task 1 – Creating a data Directory ... 11-8 Task 2 – Copying the Resource File... 11-8 Task 3 – Creating the DataSource Class... 11-8 Task 4 – Deleting Unnecessary Classes ... 11-9 Task 5 – Copying the TestReport Class... 11-9 Task 6 – Compiling the TestReport Class ... 11-9 Task 7 – Running the BankPrj Project... 11-9 Exercise 1: Reading a Data File (Level 3) ... 11-11 Preparation... 11-11 Task 1 – Creating a data Directory ... 11-12 Task 2 – Copying the Resource File... 11-12 Task 3 – Creating the DataSource Class... 11-12 Task 4 – Deleting Unnecessary Classes ... 11-15 Task 5 – Copying the TestReport Class... 11-15 Task 6 – Compiling the TestReport Class ... 11-15 Task 7 – Running the BankPrj Project... 11-15 Exercise Summary... 11-17 Building Java GUIs Using the Swing API...12-1 Objectives ... 12-1 Exercise 1: Creating the ChatClient GUI Part 1 (Level 1)... 12-2 Preparation... 12-3 Task 1 – Creating the ChatClient Class... 12-3 Task 2 – Compiling the ChatClient Class ... 12-4 Task 3 – Running the ChatClient Program... 12-4 Task 4 – Terminating the Running ChatClient Program 12-4 Exercise 1: Creating the ChatClient GUI Part 1 (Level 2)... 12-5 Preparation... 12-5 Task 1 – Creating the ChatClient Class... 12-6 Task 2 – Compiling the ChatClient Class ... 12-6 Task 3 – Running the ChatClient Program... 12-7 Task 4 – Terminating the Running ChatClient Program 12-7
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 1: Creating the ChatClient GUI Part 1 (Level 3)... 12-8 Preparation... 12-8 Task 1 – Creating the ChatClient Class... 12-9 Task 2 – Compiling the ChatClient Class ... 12-10 Task 3 – Running the ChatClient Program... 12-11 Task 4 – Terminating the Running ChatClient
Program ... 12-11 Exercise 2: Creating the Bank ATM GUI Part 1
(Advanced)... 12-12 Preparation... 12-13 Task 1 – Copying the ATMClient Class ... 12-13 Task 2 – Modifying the ATMClient Class... 12-14 Task 3 – Compiling the ATMClient Class ... 12-14 Task 4 – Running the ATMClient Program... 12-14 Hints... 12-15 Exercise Summary... 12-16 Handling GUI-Generated Events ... 13-1 Objectives ... 13-1 Exercise 1: Creating the ChatClient GUI Part 2 (Level 1)... 13-2 Preparation... 13-2 Task 1 – Modifying the ChatClient Class ... 13-3 Task 2 – Compiling the ChatClient Class ... 13-3 Task 3 – Running the ChatClient Program... 13-3 Hints... 13-3 Exercise 1: Creating the ChatClient GUI Part 2 (Level 2)... 13-4 Preparation... 13-4 Task 1 – Modifying the ChatClient Class ... 13-4 Task 2 – Compiling the ChatClient Class ... 13-5 Task 3 – Running the ChatClient Program... 13-5 Exercise 1: Creating the ChatClient GUI Part 2 (Level 3)... 13-6 Preparation... 13-6 Task 1 – Modifying the ChatClient Class ... 13-6 Task 2 – Compiling the ChatClient Class ... 13-8 Task 3 – Running the ChatClient Program... 13-8 Exercise 2: Creating the Bank ATM GUI Part 2
(Advanced)... 13-9 Preparation... 13-10 Task 1 – Modifying the ATMClient Class... 13-10 Task 2 – Compiling the ATMClient Class ... 13-10 Task 3 – Running the BankPrj Project... 13-10 Exercise Summary... 13-12 GUI-Based Applications... 14-1 Objectives ... 14-1 Exercise: Creating the ChatClient GUI, Part 3 (Level 1)... 14-2
Oracle University and GUIDANCE VIEW SDN BHD use only
Task 1 – Modifying the ChatClient Class ... 14-3 Task 2 – Compiling the ChatClient Class ... 14-3 Task 3 – Running the ChatClient Program... 14-3 Exercise: Creating the ChatClient GUI, Part 3 (Level 2)... 14-4 Preparation... 14-4 Task 1 – Modifying the ChatClient Class ... 14-4 Task 2 – Compiling the ChatClient Class ... 14-5 Task 3 – Running the ChatClient Program... 14-5 Exercise: Creating the ChatClient GUI, Part 3 (Level 3)... 14-6 Preparation... 14-6 Task 1 – Modifying the ChatClient Class ... 14-6 Task 2 – Compiling the ChatClient Class ... 14-9 Task 3 – Running the ChatClient Program... 14-10 Exercise Summary... 14-11 Threads...15-1 Objectives ... 15-1 Exercise: Using Multithreaded Programming (Level 1)... 15-2 Preparation... 15-2 Task 1 – Creating the PrintMe Class ... 15-3 Task 2 – Creating the TestThreeThreads Class... 15-3 Task 3 – Compiling the TestThreeThreads Class ... 15-3 Task 4 – Running the TestThreeThreads Program ... 15-4 Exercise: Using Multithreaded Programming (Level 2)... 15-5 Preparation... 15-5 Task 1 – Creating the PrintMe Class ... 15-6 Task 2 – Creating the TestThreeThreads Class... 15-6 Task 3 – Compiling the TestThreeThreads Class ... 15-7 Task 4 – Running the TestThreeThreads Program ... 15-7 Exercise: Using Multithreaded Programming (Level 3)... 15-8 Preparation... 15-8 Task 1 – Creating the PrintMe Class ... 15-8 Task 2 – Creating the TestThreeThreads Class... 15-9 Task 3 – Compiling the TestThreeThreads Class ... 15-10 Task 4 – Running the TestThreeThreads Program ... 15-10 Exercise Summary... 15-11 Networking...16-1 Objectives ... 16-1 Exercise: Creating a Socket Client (Level 1) ... 16-2 Preparation... 16-4 Task 1 – Modifying the ChatClient Class ... 16-4 Task 2 – Compiling the ChatClient Class ... 16-4 Task 3 – Running the ChatRoomPrj Project... 16-4 Exercise: Creating a Socket Client (Level 2) ... 16-6 Preparation... 16-6 Task 1 – Modifying the ChatClient Class ... 16-6
Oracle University and GUIDANCE VIEW SDN BHD use only
Task 2 – Compiling the ChatClient Class ... 16-7 Task 3 – Running the ChatRoomPrj Project... 16-7 Exercise: Creating a Socket Client (Level 3) ... 16-9 Preparation... 16-9 Task 1 – Modifying the ChatClient Class ... 16-9 Task 2 – Compiling the ChatClient Class ... 16-12 Task 3 – Running the ChatRoomPrj Project... 16-12 Exercise Summary... 16-13
Oracle University and GUIDANCE VIEW SDN BHD use only
LabPreface
About This Workbook
Lab Goals
Upon completion of this workbook, you should be able to:
● Write a Java™ technology program using the fundamental language elements: primitive types, reference types, arithmetic operators, relational operators, conditional statements, and iterative statements
● Write a Java technology program using good object-oriented programming concepts and principles: encapsulation, inheritance, interfaces, polymorphism, object association, and multiplicity in associations
● Write a robust Java technology program using exception handling, generic collections, and concurrency control
● Write a rich Java technology program using the graphical user interface (GUI) application programming interfaces (APIs), input/output (I/O) APIs, and networking APIs
This workbook presents the lab exercises for each module of the Student Guide.
Oracle University and GUIDANCE VIEW SDN BHD use only
Performing the Exercises
Performing the Exercises
You have the option to complete any one of three versions of a lab. To decide which to choose, consult the following descriptions of the levels:
● Level 1 – This version of the lab provides the least amount of guidance. Each bulleted paragraph provides a task description, but you must determine your own way of accomplishing each task.
● Level 2 – This version of the lab provides more guidance. Although each step describes what you should do, you must determine which commands (and options) to input.
● Level 3 – This version of the lab is the easiest to accomplish because each step provides exactly what you should input to the system. This level also includes the task solutions for all three levels.
Also, several modules have advanced labs. These labs are optional. These labs are intended for more advanced students who complete the primary labs easily.
Oracle University and GUIDANCE VIEW SDN BHD use only
Conventions
Conventions
The following conventions are used in this course to represent various training elements and alternative learning resources.
Typographical Conventions
Courieris used for the names of commands, files, directories, programming code, and on-screen computer output; for example:
Use ls -al to list all files.
system% You have mail.
Courieris also used to indicate programming constructs, such as class names, methods, and keywords; for example:
The getServletInfo method is used to get author information.
The java.awt.Dialog class contains Dialog constructor.
Courier boldis used for characters and numbers that you type; for example:
To list the files in this directory, type:
# ls
Courier boldis also used for each line of programming code that is referenced in a textual description; for example:
1 import java.io.*;
2 import javax.servlet.*;
3 import javax.servlet.http.*;
Notice the javax.servlet interface is imported to allow access to its life cycle methods (Line 2).
Courier italicsis used for variables and command-line placeholders that are replaced with a real name or value; for example:
To delete a file, use the rm filename command.
Courier italic boldis used to represent variables whose values are to be entered by the student as part of an activity; for example:
Type chmod a+rwx filename to grant read, write, and execute rights for filename to world, group, and users.
Oracle University and GUIDANCE VIEW SDN BHD use only
Conventions
Palatino italics is used for book titles, new words or terms, or words that you want to emphasize; for example:
Read Chapter 6 in the User’s Guide.
These are called class options.
Additional Conventions
Java programming language examples use the following additional conventions:
● Method names are not followed with parentheses unless a formal or actual parameter list is shown; for example:
“The doIt method...” refers to any method called doIt.
“The doIt() method...” refers to a method called doIt that takes no arguments.
● Line breaks occur only where there are separations (commas), conjunctions (operators), or white space in the code. Broken code is indented four spaces under the starting code.
● If a command used in the Solaris™ Operating System (Solaris OS) is different from a command used in the Microsoft Windows platform, both commands are shown; for example:
If working in the Solaris OS
$ cd $SERVER_ROOT/bin
If working in Microsoft Windows C:\> cd %SERVER_ROOT%\bin
Oracle University and GUIDANCE VIEW SDN BHD use only
Lab 1
Getting Started
Objectives
Upon completion of this lab, you should be able to:
● Diagnose simple compilation and runtime errors
● Create a test program for an existing class
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 1: Exploring Java™ Program Errors
Exercise 1: Exploring Java™ Program Errors
In this exercise, you view the source of several simple Java programs, and correct their compilation and runtime errors.
This exercise contains the following sections:
● “Task – Correcting Compilation and Runtime Errors”
Preparation
No preparation is needed for this exercise.
Tool Reference –Tool references used in this exercise:
● Java Development: Java Classes: Opening Java Classes
● Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
● Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
For this exercise, you work in the TestProject project in the exercises/01_intro/exercise1 directory.
1 2 3
Demonstration – The demonstration for this exercise can be found in the demos/01_intro/exercise1 directory.
Task – Correcting Compilation and Runtime Errors
In this task, you are presented with four Java programs that contain errors. The errors can be either compilation or runtime errors. Your job is to diagnose and fix those errors so the programs will execute.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 1: Exploring Java™ Program Errors Complete the following steps for the files Test1.java, Test2.java, Test3.java and Test4.java:
Tool Reference – Java Development: Java Classes: Opening Java Classes 1. Open the Java file.
Tool Reference – Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
2. Compile the Java file.
3. If a compilation error occurs, identify the source of the error and fix it.
4. Repeat step 2 and step 3 until there are no more errors.
Tool Reference – Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
5. Execute the Java program.
6. If a runtime error occurs, identify the source of the error and fix it.
7. Repeat step 5 and step 6 until there are no more errors.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Creating a Test Program (Level 1)
Exercise 2: Creating a Test Program (Level 1)
In this exercise, you create a test program (also known as a test harness) to exercise a pre-provided class. These are the Level 1 instructions that provide additional hints.
Figure 1-1 shows the definition of the Account class using a Unified Modeling Language (UML) Class diagram.
Figure 1-1 Banking Account Class
This exercise contains the following sections:
● “Task 1 – Copying the Account Class”
● “Task 2 – Creating the TestAccount Class”
● “Task 3 – Compiling the TestAccount Class”
● “Task 4 – Running the TestAccount Program”
Preparation
No preparation is needed for this exercise.
Tool Reference – Tool references used in this exercise:
● Java Development: Java Application Projects: Creating Projects
● Java Development: Java Classes: Copying Existing Resources
● Java Development: Java Classes: Creating Java Classes
● Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
● Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
Account
+getBalance():double
+withdraw(amt:double):void +deposit(amt:double):void
«constructors»
«methods»
+Account(initBalance:double)
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Creating a Test Program (Level 1)
1 2 3
Demonstration – The demonstration for this exercise can be found in the demos/01_intro/exercise2 directory.
Task 1 – Copying the Account Class
In this task, you create the BankPrj project, and copy the pre-provided Accountclass to the project.
Tool Reference – Java Development: Java Application Projects: Creating Projects
1. Create the BankPrj Java Application Project with the following characteristics:
Project Name: BankPrj Project Location: projects
Project Folder: projects/BankPrj Set as Main Project: No
Create Main Class: No
Tool Reference – Java Development: Java Classes: Modifying Java Classes: Copying Java Classes
2. Copy the pre-provided Account.java source file from the resources/01_intro/exercise2directory to the source package of the BankPrj project.
Task 2 – Creating the TestAccount Class
In this task, you complete the following steps to create a new TestAccount Java class.
Tool Reference – Java Development: Java Classes: Creating Java Classes 1. Create a new Java class in the BankPrj project with the following
characteristics:
Class Name: TestAccount Project: BankPrj
Location: Source Packages
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Creating a Test Program (Level 1)
2. Edit the source file for the TestAccount class to add a main method. The main method of the TestAccount class creates an Accountobject with an initial balance of 100. It deposits 50 to and then withdraws 147 from the Account object. Finally, it must print out the balance of the Account object to the standard output stream.
Task 3 – Compiling the TestAccount Class
Compile the TestAccount class, and make necessary changes to correct compilation errors.
Task 4 – Running the TestAccount Program
Run the TestAccount program. If there are runtime errors, make necessary changes to the TestAccount class, recompile it, and run the program again.
The output of the TestAccount program should be similar to the following:
Final account balance is: 3.0
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Creating a Test Program (Level 2)
Exercise 2: Creating a Test Program (Level 2)
In this exercise, you create a test harness (a test class) to exercise a pre- provided class. These are the Level 2 instructions that provide additional hints.
This exercise contains the following sections:
● “Task 1 – Copying the Account Class”
● “Task 2 – Creating the TestAccount Class”
● “Task 3 – Compiling the TestAccount Class”
● “Task 4 – Running the TestAccount Program”
Preparation
No preparation is needed for this exercise.
Tool Reference – Tool references used in this exercise:
● Java Development: Java Application Projects: Creating Projects
● Java Development: Java Classes: Copying Existing Resources
● Java Development: Java Classes: Creating Java Classes
● Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
● Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
1 2 3
Demonstration – The demonstration for this exercise can be found in the demos/01_intro/exercise2 directory.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Creating a Test Program (Level 2)
Task 1 – Copying the Account Class
In this task, you create the BankPrj project, and copy the pre-provided Accountclass to the project.
Tool Reference –Java Development: Java Application Projects: Creating Projects
1. Create the BankPrj Java Application Project with the following characteristics:
Project Name: BankPrj Project Location: projects
Project Folder: projects/BankPrj Set as Main Project: No
Create Main Class: No
Tool Reference –Java Development: Java Classes: Modifying Java Classes: Copying Java Classes
2. Copy the pre-provided Account.java source file from the resources/01_intro/exercise2 directory to the source package of the BankPrj project.
Task 2 – Creating the TestAccount Class
In this task, you complete the following steps to create a new TestAccountJava class.
Tool Reference –Java Development: Java Classes: Creating Java Classes 1. Create a Java class with the following characteristics:
Class Name: TestAccount Project: BankPrj
Location: Source Packages Package: default package 2. Add the main method.
3. Declare a variable in the main method. The variable has a type of Accountand a name of acct. Initialize the variable by creating an instance of the Account class with an initial balance of 100.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Creating a Test Program (Level 2) 4. Use the deposit method to add 50 to the account.
5. Use the withdraw method to subtract 147 from the account.
6. Use the getBalance method to retrieve the new account balance and use the System.out.println method to display the balance to the standard output stream.
Task 3 – Compiling the TestAccount Class
Compile the TestAccount class, and make necessary changes to correct compilation errors.
Task 4 – Running the TestAccount Program
Run the TestAccount program. If there are runtime errors, make necessary changes to the TestAccount class, recompile it and run the program again.
The output of the TestAccount program should be similar to the following:
Final account balance is: 3.0
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Creating a Test Program (Level 3)
Exercise 2: Creating a Test Program (Level 3)
In this exercise, you create a test harness (a test class) to exercise a pre- provided class. These are the Level 3 instructions that provide additional hints with code snippets.
This exercise contains the following sections:
● “Task 1 – Copying the Account Class”
● “Task 2 – Creating the TestAccount Class”
● “Task 3 – Compiling the TestAccount Class”
● “Task 4 – Running the TestAccount Program”
Preparation
No preparation is needed for this exercise.
Tool Reference –Tool references used in this exercise:
● Java Development: Java Application Projects: Creating Projects
● Java Development: Java Classes: Copying Existing Resources
● Java Development: Java Classes: Creating Java Classes
● Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
● Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
1 2 3
Demonstration – The demonstration for this exercise can be found in the demos/01_intro/exercise2 directory.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Creating a Test Program (Level 3)
Task 1 – Copying the Account Class
In this task, you create the BankPrj project, and copy the pre-provided Accountclass to the project.
Tool Reference – Java Development: Java Application Projects: Creating Projects
1. Create the BankPrj Java Application Project with the following characteristics:
Project Name: BankPrj Project Location: projects
Project Folder: projects/BankPrj Set as Main Project: No
Create Main Class: No
Tool Reference – Java Development: Java Classes: Modifying Java Classes: Copying Java Classes
2. Copy the pre-provided Account.java source file from the resources/01_intro/exercise2directory to the source package of the BankPrj project.
Task 2 – Creating the TestAccount Class
In this task, you complete the following steps to create a new TestAccount Java class.
Tool Reference – Java Development: Java Classes: Creating Java Classes 1. Create a Java class with the following characteristics:
Class Name: TestAccount Project: BankPrj
Location: Source Packages Package: default package
2. Add the main method to the TestAccount class:
public class TestAccount {
public static void main(String[] args) { // code here
} }
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Creating a Test Program (Level 3)
3. Declare a variable of type Account and initialize that variable by creating an instance of the Account class with an initial balance of 100.
Account acct = new Account(100.0);
4. Use the deposit method to add 50 to the account.
acct.deposit(50.0);
5. Use the withdraw method to subtract 147 from the account.
acct.withdraw(147.0);
6. Use the getBalance method to retrieve the new account balance and use the System.out.println method to display the balance to the standard output stream.
System.out.println(“Final account balance is ” + acct.getBalance());
Task 3 – Compiling the TestAccount Class
Compile the TestAccount class, and make necessary changes to correct compilation errors.
Task 4 – Running the TestAccount Program
Run the TestAccount program. If there are runtime errors, make necessary changes to the TestAccount class, recompile it and run the program again.
The output of the TestAccount program should be similar to the following:
Final account balance is: 3.0
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise Summary
Exercise Summary
?
!
Discussion – Take a few minutes to discuss what experiences, issues, or discoveries you had during the lab exercise.
● Experiences
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
● Interpretations
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
● Conclusions
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
● Applications
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
Oracle University and GUIDANCE VIEW SDN BHD use only
Oracle University and GUIDANCE VIEW SDN BHD use only
Lab 2
Object-Oriented Programming
Objectives
Upon completion of this lab, you should be able to:
● Use the Java API documentation to research a class
● Explore information hiding and encapsulation
● Use packages to organize your classes
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 1: Using the Java API Documentation
Exercise 1: Using the Java API Documentation
In this exercise, you explore the Java™ Platform, Standard Edition 6 (Java SE 6) API documentation to explore the methods of a class.
This exercise contains the following sections:
● “Task – Using the Java API Documentation”
Preparation
You must have a web browser window open.
Task – Using the Java API Documentation
Complete the following steps to use the Java API documentation:
1. Open the Java™ Platform, Standard Edition 6 API Specification Web page in the browser.
Note – The API documentation for Java Platform SE 6 can be found at http://java.sun.com/javase/6/docs/api.
2. Select the java.text package in the package list in the upper-left corner panel of the API frameset.
3. Select the NumberFormat class in the class list in the lower-left panel.
4. Read about the class in the top part of the documentation window on the right panel of the frameset. Review the format and parse methods.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Exploring Encapsulation, Version 1 (Level 1)
Exercise 2: Exploring Encapsulation, Version 1 (Level 1)
In this exercise, you explore the purpose of proper object encapsulation. You create a class in two steps to demonstrate the use of information hiding. In this version, you create an Account class with public data members. You will then create a test program that demonstrates the danger of using the public data directly.
Figure 2-1 shows the UML class diagram of the Account class that you will create in this exercise. This class will have one public data member (or instance variable), called balance, that maintains the monetary value of the customer’s bank account.
Figure 2-1 UML Class Diagram of Account With No Hiding There is only one business rule that applies to the Account class: The balance of the bank account must never go below zero. In this exercise, you will discover that the Account class cannot ensure this business rule.
This exercise contains the following sections:
● “Task 1 – Deleting the Account Class”
● “Task 2 – Creating the Account Class”
● “Task 3 – Creating the TestAccount2 Class”
● “Task 4 – Compiling the TestAccount2 Class”
● “Task 5 – Running the TestAccount2 Program”
Account
«constructors»
+Account(initBalance:double) +balance : double
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Exploring Encapsulation, Version 1 (Level 1)
Preparation
No preparation is needed for this exercise.
Tool Reference – Tool references used in this exercise:
● Java Development: Other Files: Deleting Files
● Java Development: Java Classes: Creating Java Classes
● Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
● Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
For this exercise, you work in the BankPrj project in the projects directory.
1 2 3
Demonstration – The demonstration for this exercise can be found in the demos/02_OOP/exercise2directory.
Task 1 – Deleting the Account Class
Tool Reference – Java Development: Other Files: Deleting Files In this task, you delete the Account class copied in Lab 1.
Task 2 – Creating the Account Class
In this task, you create the Account class according to the UML diagram in Figure 2-1. The class should have the following characteristics:
Class Name: Account Project: BankPrj
Location: Source Packages Package: default package
After creating the Account class, add an instance variable balance and
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Exploring Encapsulation, Version 1 (Level 1)
Task 3 – Creating the TestAccount2 Class
In this task, you create the TestAccount2 class with the following characteristics:
Class Name: TestAccount2 Project: BankPrj
Location: Source Packages Package: default package
This class acts as a program to create an Account object with an initial balance of 100. The test program will then add 47 and then subtract 150.
Finally, the test program must print out the balance of the object to the standard output stream.
Task 4 – Compiling the TestAccount2 Class
In this task, you compile the TestAccount2 class, and make necessary changes to correct compilation errors.
Task 5 – Running the TestAccount2 Program
In this task, you run the TestAccount2 program.
The output should be similar to the following:
Final account balance is -3.0
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Exploring Encapsulation, Version 1 (Level 2)
Exercise 2: Exploring Encapsulation, Version 1 (Level 2)
In this exercise, you explore the purpose of proper object encapsulation.
These are the Level 2 instructions, which provide additional hints.
This exercise contains the following sections:
● “Task 1 – Deleting the Account Class”
● “Task 2 – Creating the Account Class”
● “Task 3 – Creating the TestAccount2 Class”
● “Task 4 – Compiling the TestAccount2 Class”
● “Task 5 – Running the TestAccount2 Program”
Preparation
No preparation is needed for this exercise.
Tool Reference –Tool references used in this exercise:
● Java Development: Other Files: Deleting Files
● Java Development: Java Classes: Creating Java Classes
● Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
● Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
For this exercise, you work in the BankPrj project in the projects directory.
1 2 3
Demonstration – The demonstration for this exercise can be found in the demos/02_OOP/exercise2directory.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Exploring Encapsulation, Version 1 (Level 2)
Task 1 – Deleting the Account Class
Tool Reference – Java Development: Other Files: Deleting Files In this task, you delete the Account class copied in Lab 1.
Task 2 – Creating the Account Class
In this task, you complete the following steps to create a Java class named Account:
1. Create the Account class with the following characteristics:
Class Name: Account Project: BankPrj
Location: Source Packages Package: default package 2. Add the balance instance variable.
3. Add a constructor that sets the balance instance variable to the initial balance argument passed to the constructor.
Task 3 – Creating the TestAccount2 Class
In this task, you complete the following steps to create a Java class named TestAccount2:
1. Create the TestAccount2 class with the following characteristics:
Class Name: TestAccount2 Project: BankPrj
Location: Source Packages Package: default package 2. Add the main method:
a. Declare a variable within the main method of type Account named acct. Also, in the same statement, initialize the variable acctto a new instance of Account by passing 100.00 to the constructor as the initial balance.
b. Use the addition operator to add 47 to the account object’s balance.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Exploring Encapsulation, Version 1 (Level 2)
c. Use the subtraction operator to subtract 150 from the account object’s balance.
d. Use the System.out.println method to display the balance to the standard output stream.
Task 4 – Compiling the TestAccount2 Class
In this task, you compile the TestAccount2 class and the Account class, and make necessary changes to correct compilation errors.
Task 5 – Running the TestAccount2 Program
In this task, you run the TestAccount2 program.
The output should be similar to the following:
Final account balance is -3.0
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Exploring Encapsulation, Version 1 (Level 3)
Exercise 2: Exploring Encapsulation, Version 1 (Level 3)
In this exercise, you explore the purpose of proper object encapsulation.
These are the Level 3 instructions, which provide additional hints with code snippets.
This exercise contains the following sections:
● “Task 1 – Deleting the Account Class”
● “Task 2 – Creating the Account Class”
● “Task 3 – Creating the TestAccount2 Class”
● “Task 4 – Compiling the TestAccount2 Class”
● “Task 5 – Running the TestAccount2 Program”
Preparation
No preparation is needed for this exercise.
Tool Reference – Tool references used in this exercise:
● Java Development: Other Files: Deleting Files
● Java Development: Java Classes: Creating Java Classes
● Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
● Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
For this exercise, you work in the BankPrj project in the projects directory.
1 2 3
Demonstration – The demonstration for this exercise can be found in the demos/02_OOP/exercise2directory.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Exploring Encapsulation, Version 1 (Level 3)
Task 1 – Deleting the Account Class
Tool Reference –Java Development: Other Files: Deleting Files In this task, you delete the Account class copied in Lab 1.
Task 2 – Creating the Account Class
In this task, you complete the following steps to create a Java class named Account:
1. Create the Account class with the following characteristics:
Class Name: Account Project: BankPrj
Location: Source Packages Package: default package 2. Add the balance instance variable.
public double balance;
3. Add a constructor that sets the balance to the initial balance argument passed to the constructor.
public Account(double initBalance) { balance = initBalance;
}
Task 3 – Creating the TestAccount2 Class
In this task, you complete the following steps to create a Java class named TestAccount2:
1. Create the TestAccount2 class with the following characteristics:
Class Name: TestAccount2 Project: BankPrj
Location: Source Packages Package: default package
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 2: Exploring Encapsulation, Version 1 (Level 3) 2. Add the main method:
public static void main(String[] args) { // code here
}
3. Declare a variable within the main method of type Account named acct. Also, in the same statement, initialize the variable acct to a new instance of Account by passing 100.00 to the constructor as the initial balance.
Account acct = new Account(100.0);
4. Use the addition operator to add 47 to the account object’s balance.
acct.balance = acct.balance + 47.0;
5. Use the subtraction operator to subtract 150 from the account object’s balance.
acct.balance = acct.balance - 150.0;
6. Use the System.out.println method to display the balance to the standard output stream.
System.out.println(“Final account balance is ” + acct.balance);
Task 4 – Compiling the TestAccount2 Class
In this task, you compile the TestAccount2 class and the Account class, and make necessary changes to correct compilation errors.
Task 5 – Running the TestAccount2 Program
In this task, you run the TestAccount2 program.
The output should be similar to the following:
Final account balance is -3.0
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 3: Exploring Encapsulation, Version 2 (Level 1)
Exercise 3: Exploring Encapsulation, Version 2 (Level 1)
In this exercise, you explore the purpose of proper object encapsulation. You modify the Account class to hide its data member and provide public methods to manipulate the balance. You then use the test program that you created in Lab 1 to test that the business rule (balance must not fall below zero) is satisfied.
Figure 2-2 shows the UML class diagram of the Account class that you create. This design for the Account class hides the instance variable, balance, and supplies public methods to manipulate the account balance.
The deposit method adds money to the account. The withdraw method removes money from the account. The getBalance method returns the current value of the balance instance variable.
Figure 2-2 UML Class Diagram of Account With Information Hiding Remember, there is still one business rule that must be maintained: The balance of the bank account must never go below zero. This business rule should be enforced in the withdraw method.
This exercise contains the following sections:
● “Task 1 – Modifying the Account Class”
● “Task 2 – Modifying the TestAccount Class”
● “Task 3 – Compiling the TestAccount Class”
● “Task 4 – Running the TestAccount Program”
Preparation
There is no preparation for this exercise.
Account
+getBalance():double
+withdraw(amt:double):void +deposit(amt:double):void
«constructors»
«methods»
+Account(initBalance:double) -balance : double
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 3: Exploring Encapsulation, Version 2 (Level 1)
Tool Reference – Tool references used in this exercise:
● Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
● Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
For this exercise, you work in the BankPrj project in the projects directory.
1 2 3
Demonstration – The demonstration for this exercise can be found in the demos/02_OOP/exercise3directory.
Task 1 – Modifying the Account Class
In this task, you modify the Account class source file. This class must satisfy the UML diagram in Figure 2-2.
Task 2 – Modifying the TestAccount Class
In this task, you modify the TestAccount class to deposit 47 to and withdraw 150 from the Account object.
Task 3 – Compiling the TestAccount Class
In this task, you compile the TestAccount class and the Account class.
Task 4 – Running the TestAccount Program
In this task, you run the TestAccount program. The output should be similar to the following:
Final account balance is 147.0
The 150 withdraw command did not take effect, because it would have made the balance drop below zero. However, the Account object did not tell program that the withdraw command failed, it ignored the command.
You will fix this problem in future exercises.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 3: Exploring Encapsulation, Version 2 (Level 2)
Exercise 3: Exploring Encapsulation, Version 2 (Level 2)
In this exercise, you explore the purpose of proper object encapsulation.
These are the Level 2 instructions, which provide additional hints.
This exercise contains the following sections:
● “Task 1 – Modifying the Account Class”
● “Task 2 – Modifying the TestAccount Class”
● “Task 3 – Compiling the TestAccount Class”
● “Task 4 – Running the TestAccount Program”
Preparation
There is no preparation for this exercise.
Tool Reference –Tool references used in this exercise:
● Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
● Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
For this exercise, you work in the BankPrj project in the projects directory.
1 2 3
Demonstration – The demonstration for this exercise can be found in the demos/02_OOP/exercise3directory.
Task 1 – Modifying the Account Class
In this task, you complete the following steps to modify the Account class:
1. Change the balance instance variable from public to private.
2. Add the deposit method that takes an amount (of type double) and adds that amount to the balance. Save the new balance in the
instance variable.
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 3: Exploring Encapsulation, Version 2 (Level 2) 3. Add the withdraw method that takes an amount (of type double)
and subtracts that amount from the balance. Save the new balance in the instance variable.
This operation might set the balance to a value below zero if the amount to withdraw is larger than the current balance. You must use a conditional statement to verify that the amount is not greater than the balance before doing the subtraction. The conditional statement looks like the following:
if ( <boolean_test> ) {
<statement_when_true>*
} else {
<statement_when_false>*
}
4. Add the getBalance method to return the balance instance variable.
Task 2 – Modifying the TestAccount Class
In this task, you complete the following steps to modify the TestAccount class:
1. Change the amount in the call to the deposit method to 47.0.
2. Change the amount in the call to the withdraw method to 150.0.
Task 3 – Compiling the TestAccount Class
In this task, you compile the TestAccount class and the Account class.
Task 4 – Running the TestAccount Program
In this task, you run the TestAccount program. The output should be similar to the following:
Final account balance is 147.0
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 3: Exploring Encapsulation, Version 2 (Level 3)
Exercise 3: Exploring Encapsulation, Version 2 (Level 3)
In this exercise, you explore the purpose of proper object encapsulation.
These are the Level 3 instructions, which provide additional hints with code snippets.
This exercise contains the following sections:
● “Task 1 – Modifying the Account Class”
● “Task 2 – Modifying the TestAccount Class”
● “Task 3 – Compiling the TestAccount Class”
● “Task 4 – Running the TestAccount Program”
Preparation
There is no preparation for this exercise.
Tool Reference – Tool references used in this exercise:
● Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes
● Java Development: Java Classes: Modifying Java Classes: Executing Java Programs
For this exercise, you work in the BankPrj project in the projects directory.
1 2 3
Demonstration – The demonstration for this exercise can be found in the demos/02_OOP/exercise3directory.
Task 1 – Modifying the Account Class
In this task, you complete the following steps to modify the Account class:
1. Change the balance instance variable from public to private.
private double balance;
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 3: Exploring Encapsulation, Version 2 (Level 3) 2. Add the deposit method that takes an amount (of type double) and
adds that amount to the balance. Save the new balance in the balanceinstance variable.
public void deposit(double amt) { balance = balance + amt;
}
3. Add the withdraw method that takes an amount (of type double) and subtracts that amount from the balance. Save the new balance in the balance instance variable.
public void withdraw(double amt) { if ( amt <= balance ) {
balance = balance - amt;
} }
4. Add the getBalance method to return the balance instance variable.
public double getBalance() { return balance;
}
Task 2 – Modifying the TestAccount Class
In this task, you complete the following steps to modify the TestAccount class:
1. Change the amount in the call to the deposit method to 47.0.
acct.deposit(47.0);
2. Change the amount in the call to the withdraw method to 150.0.
acct.withdraw(150.0);
Task 3 – Compiling the TestAccount Class
In this task, you compile the TestAccount class and the Account class.
Task 4 – Running the TestAccount Program
In this task, you run the TestAccount program. The output should be similar to the following:
Final account balance is 147.0
Oracle University and GUIDANCE VIEW SDN BHD use only
Exercise 4: Creating Java Packages
Exercise 4: Creating Java Packages
In this exercise, you will place the Account class and TestAccount class into different packages.
Figure 2-3 shows a UML diagram of the packages that you will create in this exercise. The Account class is placed in the com.mybank.domain package and the TestAccount class is placed in the com.mybank.test package.
Figure 2-3 The Package Structure of the Banking Project This exercise contains the following sections:
● “Task 1 – Creating the Java Packages”
● “Task 2 – Moving and Modifying the Account Class”
● “Task 3 – Moving the TestAccount Class”
● “Task 4 – Compiling the TestAccount Class”
● “Task 5 – Running the TestAccount Program”
com.mybank
domain
Account
+getBalance():double
+withdraw(amt:double):void +deposit(amt:double):void
«constructors»
«methods»
+Account(initBalance:double) -balance : double
test
TestAccount
+main(args:String[])