fundamentals of java - java certification oracle training manuals

276
Java Programming Language Java SE 6 Activity Guide – Vol 1 (for Solaris) SL-275-SE6 G.2 D61748GC11 Edition 1.1 June 2010 D67982

Upload: williamsock

Post on 09-Oct-2015

121 views

Category:

Documents


2 download

DESCRIPTION

JAVA Certification ORacle Training Manuals

TRANSCRIPT

  • Java Programming Language Java SE 6

    Activity Guide Vol 1 (for Solaris)

    SL-275-SE6 G.2

    D61748GC11

    Edition 1.1

    June 2010

    D67982

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • 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 anddisclosure, and is protected by copyright and other intellectual property laws. You may copy and print this document solely foryour own use in an Oracle training course. The document may not be modified or altered in any way. Except as expresslypermitted 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 expressauthorization 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. Thisdocument 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 SunMicrosystems, Inc. Certain materials, offerings, services, or products may no longer be offered or provided.Oracle and itsaffiliates 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 RIGHTSThe U.S. Governments rights to use, modify, reproduce, release, perform, display, or disclose these training materials arerestricted 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 respectiveowners.

    AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced MicroDevices. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are usedunder license and are trademarks or registered trademarks of SPARC International, Inc. UNIX is a registered trademarklicensed through X/Open Company, Ltd.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • This page intentionally left blank.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • This page intentionally left blank.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • vCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Table of ContentsAbout This Workbook .......................................................Preface-xvii

    Lab Goals............................................................................. Preface-xviiPerforming the Exercises ................................................. Preface-xviiiConventions ..........................................................................Preface-xix

    Typographical Conventions ......................................Preface-xixAdditional Conventions.............................................. Preface-xx

    Getting Started..................................................................................1-1Objectives ........................................................................................... 1-1Exercise 1: Exploring Java Program Errors ................................ 1-2

    Preparation................................................................................. 1-2Task Correcting Compilation and Runtime Errors........... 1-2

    Exercise 2: Creating a Test Program (Level 1) ............................... 1-4Preparation................................................................................. 1-4Task 1 Copying the Account Class ..................................... 1-5Task 2 Creating the TestAccount Class............................. 1-5Task 3 Compiling the TestAccount Class ......................... 1-6Task 4 Running the TestAccount Program ...................... 1-6

    Exercise 2: Creating a Test Program (Level 2) ............................... 1-7Preparation................................................................................. 1-7Task 1 Copying the Account Class ..................................... 1-8Task 2 Creating the TestAccount Class............................. 1-8Task 3 Compiling the TestAccount Class ......................... 1-9Task 4 Running the TestAccount Program ...................... 1-9

    Exercise 2: Creating a Test Program (Level 3) ............................. 1-10Preparation............................................................................... 1-10Task 1 Copying the Account Class ................................... 1-11Task 2 Creating the TestAccount Class........................... 1-11Task 3 Compiling the TestAccount Class ....................... 1-12Task 4 Running the TestAccount Program .................... 1-12

    Exercise Summary............................................................................ 1-13

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • vi Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Object-Oriented Programming ....................................................... 2-1Objectives ........................................................................................... 2-1Exercise 1: Using the Java API Documentation............................. 2-2

    Preparation................................................................................. 2-2Task Using the Java API Documentation ........................... 2-2

    Exercise 2: Exploring Encapsulation, Version 1 (Level 1) ............ 2-3Preparation................................................................................. 2-4Task 1 Deleting the Account Class...................................... 2-4Task 2 Creating the Account Class ..................................... 2-4Task 3 Creating the TestAccount2 Class .......................... 2-5Task 4 Compiling the TestAccount2 Class....................... 2-5Task 5 Running the TestAccount2 Program .................... 2-5

    Exercise 2: Exploring Encapsulation, Version 1 (Level 2) ............ 2-6Preparation................................................................................. 2-6Task 1 Deleting the Account Class...................................... 2-7Task 2 Creating the Account Class ..................................... 2-7Task 3 Creating the TestAccount2 Class .......................... 2-7Task 4 Compiling the TestAccount2 Class....................... 2-8Task 5 Running the TestAccount2 Program .................... 2-8

    Exercise 2: Exploring Encapsulation, Version 1 (Level 3) ............ 2-9Preparation................................................................................. 2-9Task 1 Deleting the Account Class.................................... 2-10Task 2 Creating the Account Class ................................... 2-10Task 3 Creating the TestAccount2 Class ........................ 2-10Task 4 Compiling the TestAccount2 Class..................... 2-11Task 5 Running the TestAccount2 Program .................. 2-11

    Exercise 3: Exploring Encapsulation, Version 2 (Level 1) .......... 2-12Preparation............................................................................... 2-12Task 1 Modifying the Account Class ................................ 2-13Task 2 Modifying the TestAccount Class ....................... 2-13Task 3 Compiling the TestAccount Class ....................... 2-13Task 4 Running the TestAccount Program .................... 2-13

    Exercise 3: Exploring Encapsulation, Version 2 (Level 2) .......... 2-14Preparation............................................................................... 2-14Task 1 Modifying the Account Class ................................ 2-14Task 2 Modifying the TestAccount Class ....................... 2-15Task 3 Compiling the TestAccount Class ....................... 2-15Task 4 Running the TestAccount Program .................... 2-15

    Exercise 3: Exploring Encapsulation, Version 2 (Level 3) .......... 2-16Preparation............................................................................... 2-16Task 1 Modifying the Account Class ................................ 2-16Task 2 Modifying the TestAccount Class ....................... 2-17Task 3 Compiling the TestAccount Class ....................... 2-17Task 4 Running the TestAccount Program .................... 2-17

    Exercise 4: Creating Java Packages................................................ 2-18Preparation............................................................................... 2-19

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • viiCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 1 Creating the Java Packages .................................... 2-19Task 2 Moving and Modifying the Account Class ......... 2-19Task 3 Moving the TestAccount Class ............................ 2-20Task 4 Compiling the TestAccount Class ....................... 2-20Task 5 Running the TestAccount Program .................... 2-20

    Exercise Summary............................................................................ 2-21

    Identifiers, Keywords, and Types ...................................................3-1Objectives ........................................................................................... 3-1Exercise 1: Investigating Reference Assignment ........................... 3-2

    Preparation................................................................................. 3-2Task 1 Creating the TestMyPoint Class............................. 3-3Task 2 Compiling the TestMyPoint Class ......................... 3-4Task 3 Running the TestMyPoint Program ...................... 3-4

    Exercise 2: Creating Customer Accounts (Level 1) ....................... 3-5Preparation................................................................................. 3-6Task 1 Creating the Customer Class ................................... 3-6Task 2 Copying the TestBanking Class............................. 3-6Task 3 Compiling the TestBanking Class ......................... 3-7Task 4 Running the TestBanking Program ...................... 3-7

    Exercise 2: Creating Customer Accounts (Level 2) ....................... 3-8Preparation................................................................................. 3-8Task 1 Creating the Customer Class ................................... 3-9Task 2 Copying the TestBanking Class............................. 3-9Task 3 Compiling the TestBanking Class ......................... 3-9Task 4 Running the TestBanking Program .................... 3-10

    Exercise 2: Creating Customer Accounts (Level 3) ..................... 3-11Preparation............................................................................... 3-11Task 1 Creating the Customer Class ................................. 3-12Task 2 Copying the TestBanking Class........................... 3-13Task 3 Compiling the TestBanking Class ....................... 3-13Task 4 Running the TestBanking Program .................... 3-13

    Exercise Summary............................................................................ 3-14

    Expressions and Flow Control........................................................4-1Objectives ........................................................................................... 4-1Exercise 1: Using Loops and Branching Statements ..................... 4-2

    Preparation................................................................................. 4-3Task 1 Creating the FooBarBaz Class ................................. 4-3Task 2 Compiling the FooBarBaz Class ............................. 4-4Task 3 Running the FooBarBaz Program........................... 4-4Hints............................................................................................ 4-4

    Exercise 2: Using Conditional Statements in the Account Class(Level 1) ............................................................................................ 4-5

    Preparation................................................................................. 4-5Task 1 Modifying the Account Class .................................. 4-6Task 2 Deleting the Current TestBanking Class ................ 4-6

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • viii Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 3 Copying the TestBanking Class............................. 4-6Task 4 Compiling the TestBanking Class ......................... 4-6Task 5 Running the TestBanking Program ...................... 4-7

    Exercise 2: Using Conditional Statements in the Account Class(Level 2) ............................................................................................ 4-8

    Preparation................................................................................. 4-8Task 1 Modifying the Account Class .................................. 4-9Task 2 Deleting the Current TestBanking Class ................ 4-9Task 3 Copying the TestBanking Class............................. 4-9Task 4 Compiling the TestBanking Class ......................... 4-9Task 5 Running the TestBanking Program ...................... 4-9

    Exercise 2: Using Conditional Statements in the Account Class(Level 3) .......................................................................................... 4-10

    Preparation............................................................................... 4-10Task 1 Modifying the Account Class ................................ 4-11Task 2 Deleting the Current TestBanking Class .............. 4-11Task 3 Copying the TestBanking Class........................... 4-11Task 4 Compiling the TestBanking Class ....................... 4-11Task 5 Running the TestBanking Program .................... 4-12

    Exercise 3: Using Nested Loops (Advanced) ............................... 4-13Preparation............................................................................... 4-13Task 1 Writing the isSubString Method........................ 4-14Task 2 Compiling the TestIsSubString Class .............. 4-14Task 3 Running the TestIsSubString Program............ 4-14Hints......................................................................................... 4-15

    Exercise Summary............................................................................ 4-16

    Arrays................................................................................................ 5-1Objectives ........................................................................................... 5-1Exercise 1 Using Primitive Arrays (Level 2) ............................... 5-2

    Preparation................................................................................. 5-2Task 1 Creating the TestArrays Class............................... 5-2Task 2 Compiling the TestArrays Class ........................... 5-3Task 3 Running the TestArrays Program......................... 5-3Hint ............................................................................................ 5-4

    Exercise 1 Using Primitive Arrays (Level 3) ............................... 5-5Preparation................................................................................. 5-5Task 1 Creating the TestArrays Class............................... 5-5Task 2 Compiling the TestArrays Class ........................... 5-7Task 3 Running the TestArrays Program......................... 5-7

    Exercise 2 Using Arrays to Represent One-to-Many Associations(Level 1) ............................................................................................ 5-8

    Preparation................................................................................. 5-9Task 1 Creating the Bank Class ............................................ 5-9Task 2 Deleting the Current TestBanking Class............. 5-9Task 3 Copying the TestBanking Class........................... 5-10

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • ixCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 4 Compiling the TestBanking Class ....................... 5-10Task 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-12Task 2 Deleting the Current TestBanking Class........... 5-12Task 3 Copying the TestBanking Class........................... 5-12Task 4 Compiling the TestBanking Class ....................... 5-12Task 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-15Task 2 Deleting the Current TestBanking Class........... 5-16Task 3 Copying the TestBanking Class........................... 5-16Task 4 Compiling the TestBanking Class ....................... 5-16Task 5 Running the TestBanking Program .................... 5-16

    Exercise Summary............................................................................ 5-17

    Class Design.....................................................................................6-1Objectives ........................................................................................... 6-1Exercise 1: Creating Bank Account Subclasses (Level 1).............. 6-2

    Preparation................................................................................. 6-3Task 1 Modifying the Account Class .................................. 6-4Task 2 Creating the SavingsAccount Class ...................... 6-4Task 3 Creating the CheckingAccount Class.................... 6-4Task 4 Deleting the Current TestBanking Class .............. 6-5Task 5 Copying the TestBanking Class............................. 6-5Task 6 Compiling the TestBanking Class ......................... 6-5Task 7 Running the TestBanking Program ...................... 6-5

    Exercise 1: Creating Bank Account Subclasses (Level 2).............. 6-7Preparation................................................................................. 6-7Task 1 Modifying the Account Class .................................. 6-8Task 2 Creating the SavingsAccount Class ...................... 6-8Task 3 Creating the CheckingAccount Class.................... 6-9Task 4 Deleting the Current TestBanking Class ............ 6-10Task 5 Copying the TestBanking Class........................... 6-10Task 6 Compiling the TestBanking Class ....................... 6-10Task 7 Running the TestBanking Program .................... 6-10

    Exercise 1: Creating Bank Account Subclasses (Level 3)............ 6-12Preparation............................................................................... 6-12Task 1 Modifying the Account Class ................................ 6-13Task 2 Creating the SavingsAccount Class .................... 6-13Task 3 Creating the CheckingAccount Class.................. 6-14Task 4 Deleting the Current TestBanking Class ............ 6-15Task 5 Copying the TestBanking Class........................... 6-15Task 6 Compiling the TestBanking Class ....................... 6-16Task 7 Running the TestBanking Program .................... 6-16

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • x Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Exercise 2: Creating a Heterogeneous Collection of CustomerAccounts (Level 1)......................................................................... 6-18

    Preparation............................................................................... 6-19Task 1 Modifying the Customer Class.............................. 6-19Task 2 Copying and Completing the CustomerReport

    Class ....................................................................................... 6-19Task 3 Copying the TestReport Class............................. 6-20Task 4 Compiling the TestReport Class ......................... 6-20Task 5 Running the TestReport Program....................... 6-20

    Exercise 2: Creating a Heterogeneous Collection of CustomerAccounts (Level 2)......................................................................... 6-21

    Task 1 Modifying the Customer Class.............................. 6-22Task 2 Copying and Completing the CustomerReport

    Class ....................................................................................... 6-22Task 3 Copying the TestReport Class............................. 6-23Task 4 Compiling the TestReport Class ......................... 6-23Task 5 Running the TestReport Program....................... 6-23

    Exercise 2: Creating a Heterogeneous Collection of CustomerAccounts (Level 3)......................................................................... 6-24

    Task 1 Modifying the Customer Class.............................. 6-25Task 2 Copying and Completing the CustomerReport

    Class ....................................................................................... 6-26Task 3 Copying the TestReport Class............................. 6-26Task 4 Compiling the TestReport Class ......................... 6-26Task 5 Running the TestReport Program....................... 6-27

    Exercise 3: Creating a Batch Program (Advanced) ..................... 6-28Preparation............................................................................... 6-29Task 1 Modifying the SavingsAccount Class................. 6-29Task 2 Creating the AccumulateSavingsBatch

    Class ....................................................................................... 6-29Task 3 Copying the TestBatch Class ............................... 6-30Task 4 Compiling the TestBatch Class ........................... 6-30Task 5 Running the TestBatch Program......................... 6-30

    Exercise Summary............................................................................ 6-32

    Advanced Class Features ............................................................... 7-1Objectives ........................................................................................... 7-1Exercise 1: Applying Static Members to a Design (Level 1) ........ 7-2

    Preparation................................................................................. 7-3Task 1 Modifying the Bank Class......................................... 7-3Task 2 Modifying the CustomerReport Class................... 7-3Task 3 Deleting the Current TestReport Class ................ 7-4Task 4 Copying the TestReport Class............................... 7-4Task 5 Compiling the TestReport Class ........................... 7-4Task 6 Running the TestReport Program......................... 7-4

    Exercise 1: Applying Static Members to a Design (Level 2) ........ 7-5

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • xiCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 1 Modifying the Bank Class......................................... 7-6Task 2 Modifying the CustomerReport Class................... 7-6Task 3 Deleting the Current TestReport Class ................ 7-7Task 4 Copying the TestReport Class............................... 7-7Task 5 Compiling the TestReport Class ........................... 7-7Task 6 Running the TestReport Program......................... 7-7

    Exercise 1: Applying Static Members to a Design (Level 3) ........ 7-8Task 1 Modifying the Bank Class......................................... 7-9Task 2 Modifying the CustomerReport Class................. 7-10Task 3 Deleting the Current TestReport Class .............. 7-10Task 4 Copying the TestReport Class............................. 7-10Task 5 Compiling the TestReport Class ......................... 7-11Task 6 Running the TestReport Program....................... 7-11

    Exercise 2: Working With Interfaces and Abstract Classes(Level 1) .......................................................................................... 7-12

    Preparation............................................................................... 7-13Task 1 Creating the Pet Interface ....................................... 7-13Task 2 Creating the Animal Classes .................................. 7-14Task 3 Creating the TestAnimals Class........................... 7-14Task 4 Compiling the TestAnimals Class ....................... 7-15Task 5 Running the TestAnimals Program .................... 7-15

    Exercise 2: Working With Interfaces and Abstract Classes(Level 2) .......................................................................................... 7-16

    Preparation............................................................................... 7-16Task 1 Creating the Pet Interface ....................................... 7-17Task 2 Creating the Animal Classes .................................. 7-17Task 3 Creating the TestAnimals Class........................... 7-19Task 4 Compiling the TestAnimals Class ....................... 7-20Task 5 Running the TestAnimals Program .................... 7-20

    Exercise 2: Working With Interfaces and Abstract Classes(Level 3) .......................................................................................... 7-21

    Preparation............................................................................... 7-21Task 1 Creating the Pet Interface ....................................... 7-22Task 2 Creating the Animal Classes .................................. 7-22Task 3 Creating the TestAnimals Class........................... 7-25Task 4 Compiling the TestAnimals Class ....................... 7-26Task 5 Running the TestAnimals Program .................... 7-26

    Exercise Summary............................................................................ 7-27

    Exceptions and Assertions .............................................................8-1Objectives ........................................................................................... 8-1Exercise: Creating Your Own Exception (Level 1) ........................ 8-2

    Task 1 Creating the OverdraftException Class ............. 8-4Task 2 Modifying the Account Class .................................. 8-4Task 3 Modifying the CheckingAccount Class ................ 8-4Task 4 Deleting the Current TestBanking Class .............. 8-4

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • xii Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 5 Copying the TestBanking Class............................. 8-4Task 6 Compiling the TestBanking Class ......................... 8-5Task 7 Running the TestBanking Program ...................... 8-5

    Exercise: Creating Your Own Exception (Level 2) ........................ 8-6Task 1 Creating the OverdraftException Class ............. 8-7Task 2 Modifying the Account Class .................................. 8-7Task 3 Modifying the CheckingAccount Class ................ 8-8Task 4 Deleting the Current TestBanking Class .............. 8-8Task 5 Copying the TestBanking Class............................. 8-8Task 6 Compiling the TestBanking Class ......................... 8-8Task 7 Running the TestBanking Program ...................... 8-8

    Exercise: Creating Your Own Exception (Level 3) ........................ 8-9Task 1 Creating the OverdraftException Class ........... 8-10Task 2 Modifying the Account Class ................................ 8-11Task 3 Modifying the CheckingAccount Class .............. 8-11Task 4 Deleting the Current TestBanking Class ............ 8-12Task 5 Copying the TestBanking Class........................... 8-12Task 6 Compiling the TestBanking Class ....................... 8-12Task 7 Running the TestBanking Program .................... 8-12

    Exercise Summary............................................................................ 8-13

    Collections and Generics Framework............................................ 9-1Objectives ........................................................................................... 9-1Exercise 1: Using Collections to Represent Association

    (Level 1) ............................................................................................ 9-2Preparation................................................................................. 9-3Task 1 Modifying the Bank Class......................................... 9-3Task 2 Modifying the Customer Class................................ 9-3Task 3 Compiling the TestReport Class ........................... 9-4Task 4 Running the TestReport Program ....................... 9-4

    Exercise 1: Using Collections to Represent Association(Level 2) ............................................................................................ 9-5

    Preparation................................................................................. 9-5Task 1 Modifying the Bank Class......................................... 9-5Task 2 Modifying the Customer Class................................ 9-6Task 3 Compiling the TestReport Class ........................... 9-6Task 4 Running the TestReport Program......................... 9-6

    Exercise 1: Using Collections to Represent Association(Level 3) ............................................................................................ 9-8

    Preparation................................................................................. 9-8Task 1 Modifying the Bank Class......................................... 9-9Task 2 Modifying the Customer Class................................ 9-9Task 3 Compiling the TestReport Class ......................... 9-10Task 4 Running the TestReport Program....................... 9-11

    Exercise Summary............................................................................ 9-12

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • xiiiCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    I/O Fundamentals ...........................................................................10-1

    Console I/O and File I/O .................................................................11-1Objectives ......................................................................................... 11-1Exercise 1: Reading a Data File (Level 1) ...................................... 11-2

    Preparation............................................................................... 11-4Task 1 Creating a data Directory ...................................... 11-4Task 2 Copying the Resource File...................................... 11-4Task 3 Creating the DataSource Class............................. 11-5Task 4 Deleting Unnecessary Classes ............................... 11-5Task 5 Copying the TestReport Class............................. 11-5Task 6 Compiling the TestReport Class ......................... 11-5Task 7 Running the BankPrj Project................................. 11-6

    Exercise 1: Reading a Data File (Level 2) ...................................... 11-7Preparation............................................................................... 11-7Task 1 Creating a data Directory ...................................... 11-8Task 2 Copying the Resource File...................................... 11-8Task 3 Creating the DataSource Class............................. 11-8Task 4 Deleting Unnecessary Classes ............................... 11-9Task 5 Copying the TestReport Class............................. 11-9Task 6 Compiling the TestReport Class ......................... 11-9Task 7 Running the BankPrj Project................................. 11-9

    Exercise 1: Reading a Data File (Level 3) .................................... 11-11Preparation............................................................................. 11-11Task 1 Creating a data Directory .................................... 11-12Task 2 Copying the Resource File.................................... 11-12Task 3 Creating the DataSource Class........................... 11-12Task 4 Deleting Unnecessary Classes ............................. 11-15Task 5 Copying the TestReport Class........................... 11-15Task 6 Compiling the TestReport Class ....................... 11-15Task 7 Running the BankPrj Project............................... 11-15

    Exercise Summary.......................................................................... 11-17

    Building Java GUIs Using the Swing API.....................................12-1Objectives ......................................................................................... 12-1Exercise 1: Creating the ChatClient GUI Part 1 (Level 1)........ 12-2

    Preparation............................................................................... 12-3Task 1 Creating the ChatClient Class............................. 12-3Task 2 Compiling the ChatClient Class ......................... 12-4Task 3 Running the ChatClient Program....................... 12-4Task 4 Terminating the Running ChatClient Program 12-4

    Exercise 1: Creating the ChatClient GUI Part 1 (Level 2)........ 12-5Preparation............................................................................... 12-5Task 1 Creating the ChatClient Class............................. 12-6Task 2 Compiling the ChatClient Class ......................... 12-6Task 3 Running the ChatClient Program....................... 12-7Task 4 Terminating the Running ChatClient Program 12-7

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • xiv Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Exercise 1: Creating the ChatClient GUI Part 1 (Level 3)........... 12-8Preparation............................................................................... 12-8Task 1 Creating the ChatClient Class............................. 12-9Task 2 Compiling the ChatClient Class ....................... 12-10Task 3 Running the ChatClient Program..................... 12-11Task 4 Terminating the Running ChatClient

    Program............................................................................... 12-11Exercise 2: Creating the Bank ATM GUI Part 1

    (Advanced)................................................................................... 12-12Preparation............................................................................. 12-13Task 1 Copying the ATMClient Class ............................. 12-13Task 2 Modifying the ATMClient Class.......................... 12-14Task 3 Compiling the ATMClient Class ......................... 12-14Task 4 Running the ATMClient Program....................... 12-14Hints........................................................................................ 12-15

    Exercise Summary.......................................................................... 12-16

    Handling GUI-Generated Events .................................................. 13-1Objectives ......................................................................................... 13-1Exercise 1: Creating the ChatClient GUI Part 2 (Level 1)........ 13-2

    Preparation............................................................................... 13-2Task 1 Modifying the ChatClient Class ......................... 13-3Task 2 Compiling the ChatClient Class ......................... 13-3Task 3 Running the ChatClient Program....................... 13-3Hints.......................................................................................... 13-3

    Exercise 1: Creating the ChatClient GUI Part 2 (Level 2)........ 13-4Preparation............................................................................... 13-4Task 1 Modifying the ChatClient Class ......................... 13-4Task 2 Compiling the ChatClient Class ......................... 13-5Task 3 Running the ChatClient Program....................... 13-5

    Exercise 1: Creating the ChatClient GUI Part 2 (Level 3)........ 13-6Preparation............................................................................... 13-6Task 1 Modifying the ChatClient Class ......................... 13-6Task 2 Compiling the ChatClient Class ......................... 13-8Task 3 Running the ChatClient Program....................... 13-8

    Exercise 2: Creating the Bank ATM GUI Part 2(Advanced)..................................................................................... 13-9

    Preparation............................................................................. 13-10Task 1 Modifying the ATMClient Class.......................... 13-10Task 2 Compiling the ATMClient Class ......................... 13-10Task 3 Running the BankPrj Project............................... 13-10

    Exercise Summary.......................................................................... 13-12

    GUI-Based Applications ................................................................ 14-1Objectives ......................................................................................... 14-1Exercise: Creating the ChatClient GUI, Part 3 (Level 1).......... 14-2

    Preparation............................................................................... 14-3

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • xvCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 1 Modifying the ChatClient Class ......................... 14-3Task 2 Compiling the ChatClient Class ......................... 14-3Task 3 Running the ChatClient Program....................... 14-3

    Exercise: Creating the ChatClient GUI, Part 3 (Level 2).......... 14-4Preparation............................................................................... 14-4Task 1 Modifying the ChatClient Class ......................... 14-4Task 2 Compiling the ChatClient Class ......................... 14-5Task 3 Running the ChatClient Program....................... 14-5

    Exercise: Creating the ChatClient GUI, Part 3 (Level 3).......... 14-6Preparation............................................................................... 14-6Task 1 Modifying the ChatClient Class ......................... 14-6Task 2 Compiling the ChatClient Class ......................... 14-9Task 3 Running the ChatClient Program..................... 14-10

    Exercise Summary.......................................................................... 14-11

    Threads............................................................................................15-1Objectives ......................................................................................... 15-1Exercise: Using Multithreaded Programming (Level 1)............. 15-2

    Preparation............................................................................... 15-2Task 1 Creating the PrintMe Class ................................... 15-3Task 2 Creating the TestThreeThreads Class................ 15-3Task 3 Compiling the TestThreeThreads Class ............ 15-3Task 4 Running the TestThreeThreads Program ......... 15-4

    Exercise: Using Multithreaded Programming (Level 2)............. 15-5Preparation............................................................................... 15-5Task 1 Creating the PrintMe Class ................................... 15-6Task 2 Creating the TestThreeThreads Class................ 15-6Task 3 Compiling the TestThreeThreads Class ............ 15-7Task 4 Running the TestThreeThreads Program ......... 15-7

    Exercise: Using Multithreaded Programming (Level 3)............. 15-8Preparation............................................................................... 15-8Task 1 Creating the PrintMe Class ................................... 15-8Task 2 Creating the TestThreeThreads Class................ 15-9Task 3 Compiling the TestThreeThreads Class .......... 15-10Task 4 Running the TestThreeThreads Program ....... 15-10

    Exercise Summary.......................................................................... 15-11

    Networking ......................................................................................16-1Objectives ......................................................................................... 16-1Exercise: Creating a Socket Client (Level 1) ................................. 16-2

    Preparation............................................................................... 16-4Task 1 Modifying the ChatClient Class ......................... 16-4Task 2 Compiling the ChatClient Class ......................... 16-4Task 3 Running the ChatRoomPrj Project........................ 16-4

    Exercise: Creating a Socket Client (Level 2) ................................. 16-6Preparation............................................................................... 16-6Task 1 Modifying the ChatClient Class ......................... 16-6

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • xvi Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 2 Compiling the ChatClient Class ......................... 16-7Task 3 Running the ChatRoomPrj Project........................ 16-7

    Exercise: Creating a Socket Client (Level 3) ................................. 16-9Preparation............................................................................... 16-9Task 1 Modifying the ChatClient Class ......................... 16-9Task 2 Compiling the ChatClient Class ....................... 16-12Task 3 Running the ChatRoomPrj Project...................... 16-12

    Exercise Summary.......................................................................... 16-13

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • LabPreface-xviiCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    LabPreface

    AboutThisWorkbook

    Lab Goals

    Upon completion of this workbook, you should be able to:

    Write a Java technology program using the fundamental languageelements: primitive types, reference types, arithmetic operators,relational operators, conditional statements, and iterative statements

    Write a Java technology program using good object-orientedprogramming concepts and principles: encapsulation, inheritance,interfaces, polymorphism, object association, and multiplicity inassociations

    Write a robust Java technology program using exception handling,generic collections, and concurrency control

    Write a rich Java technology program using the graphical userinterface (GUI) application programming interfaces (APIs),input/output (I/O) APIs, and networking APIs

    This workbook presents the lab exercises for each module of the StudentGuide.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Performing the Exercises

    Lab Preface-xviii Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Performing the Exercises

    You have the option to complete any one of three versions of a lab. Todecide which to choose, consult the following descriptions of the levels:

    Level 1 This version of the lab provides the least amount ofguidance. Each bulleted paragraph provides a task description, butyou must determine your own way of accomplishing each task.

    Level 2 This version of the lab provides more guidance. Althougheach step describes what you should do, you must determine whichcommands (and options) to input.

    Level 3 This version of the lab is the easiest to accomplish becauseeach step provides exactly what you should input to the system. Thislevel also includes the task solutions for all three levels.

    Also, several modules have advanced labs. These labs are optional. Theselabs are intended for more advanced students who complete the primarylabs easily.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Conventions

    About This Workbook Lab Preface-xixCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Conventions

    The following conventions are used in this course to represent varioustraining elements and alternative learning resources.

    Typographical Conventions

    Courier is 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.

    Courier is also used to indicate programming constructs, such as classnames, methods, and keywords; for example:

    The getServletInfo method is used to get author information.The java.awt.Dialog class contains Dialog constructor.

    Courier bold is used for characters and numbers that you type; forexample:

    To list the files in this directory, type:# ls

    Courier bold is also used for each line of programming code that isreferenced in a textual description; for example:

    1 import java.io.*;2 import javax.servlet.*;3 import javax.servlet.http.*;Notice thejavax.servlet interface is imported to allow access to its life cyclemethods (Line 2).

    Courier italics is used for variables and command-line placeholdersthat are replaced with a real name or value; for example:

    To delete a file, use the rm filename command.

    Courier italic bold is used to represent variables whose values are tobe entered by the student as part of an activity; for example:

    Type chmod a+rwx filename to grant read, write, and executerights for filename to world, group, and users.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Conventions

    Lab Preface-xx Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Palatino italics is used for book titles, new words or terms, or words thatyou want to emphasize; for example:

    Read Chapter 6 in the Users Guide.These are called class options.

    Additional Conventions

    Java programming language examples use the following additionalconventions:

    Method names are not followed with parentheses unless a formal oractual 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 takesno arguments.

    Line breaks occur only where there are separations (commas),conjunctions (operators), or white space in the code. Broken code isindented four spaces under the starting code.

    If a command used in the Solaris Operating System (Solaris OS) isdifferent from a command used in the Microsoft Windows platform,both commands are shown; for example:

    If working in the Solaris OS

    $ cd $SERVER_ROOT/binIf working in Microsoft Windows

    C:\> cd %SERVER_ROOT%\binWaqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Lab 1-1Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Lab 1

    GettingStarted

    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

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 1: Exploring Java Program Errors

    Lab 1-2 Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Exercise 1: Exploring Java Program Errors

    In this exercise, you view the source of several simple Java programs, andcorrect 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: CompilingJava Classes

    Java Development: Java Classes: Modifying Java Classes: ExecutingJava Programs

    For this exercise, you work in the TestProject project in theexercises/01_intro/exercise1 directory.

    123

    Demonstration The demonstration for this exercise can be found in thedemos/01_intro/exercise1 directory.

    Task Correcting Compilation and Runtime Errors

    In this task, you are presented with four Java programs that containerrors. The errors can be either compilation or runtime errors. Your job isto diagnose and fix those errors so the programs will execute.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 1: Exploring Java Program Errors

    Getting Started Lab 1-3Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    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 JavaClasses: Compiling Java Classes

    2. Compile the Java file.

    3. If a compilation error occurs, identify the source of the error and fixit.

    4. Repeat step 2 and step 3 until there are no more errors.

    Tool Reference Java Development: Java Classes: Modifying JavaClasses: 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.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Creating a Test Program (Level 1)

    Lab 1-4 Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Exercise 2: Creating a Test Program (Level 1)

    In this exercise, you create a test program (also known as a test harness) toexercise a pre-provided class. These are the Level 1 instructions thatprovide additional hints.

    Figure 1-1 shows the definition of the Account class using a UnifiedModeling 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: CompilingJava Classes

    Java Development: Java Classes: Modifying Java Classes: ExecutingJava Programs

    Account

    +getBalance():double

    +withdraw(amt:double):void+deposit(amt:double):void

    constructors

    methods

    +Account(initBalance:double)

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Creating a Test Program (Level 1)

    Getting Started Lab 1-5Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    123

    Demonstration The demonstration for this exercise can be found in thedemos/01_intro/exercise2 directory.

    Task 1 Copying the AccountClass

    In this task, you create the BankPrj project, and copy the pre-providedAccount class to the project.

    Tool Reference Java Development: Java Application Projects: CreatingProjects

    1. Create the BankPrj Java Application Project with the followingcharacteristics:

    Project Name: BankPrjProject Location: projectsProject Folder: projects/BankPrjSet as Main Project: No

    Create Main Class: No

    Tool Reference Java Development: Java Classes: Modifying JavaClasses: Copying Java Classes

    2. Copy the pre-provided Account.java source file from theresources/01_intro/exercise2 directory to the sourcepackage of the BankPrj project.

    Task 2 Creating the TestAccountClass

    In this task, you complete the following steps to create a newTestAccount Java class.

    Tool Reference Java Development: Java Classes: Creating Java Classes

    1. Create a new Java class in the BankPrj project with the followingcharacteristics:

    Class Name: TestAccount

    Project: BankPrjLocation: Source Packages

    Package: default package

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Creating a Test Program (Level 1)

    Lab 1-6 Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    2. Edit the source file for the TestAccount class to add a mainmethod. The main method of the TestAccount class creates anAccount object with an initial balance of 100. It deposits 50 to andthen withdraws 147 from the Account object. Finally, it must printout the balance of the Account object to the standard output stream.

    Task 3 Compiling the TestAccountClass

    Compile the TestAccount class, and make necessary changes to correctcompilation errors.

    Task 4 Running the TestAccountProgram

    Run the TestAccount program. If there are runtime errors, makenecessary changes to the TestAccount class, recompile it, and run theprogram again.

    The output of the TestAccount program should be similar to thefollowing:

    Final account balance is: 3.0

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Creating a Test Program (Level 2)

    Getting Started Lab 1-7Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.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 additionalhints.

    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: CompilingJava Classes

    Java Development: Java Classes: Modifying Java Classes: ExecutingJava Programs

    123

    Demonstration The demonstration for this exercise can be found in thedemos/01_intro/exercise2 directory.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Creating a Test Program (Level 2)

    Lab 1-8 Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 1 Copying the AccountClass

    In this task, you create the BankPrj project, and copy the pre-providedAccount class to the project.

    Tool Reference Java Development: Java Application Projects: CreatingProjects

    1. Create the BankPrj Java Application Project with the followingcharacteristics:

    Project Name: BankPrjProject Location: projectsProject Folder: projects/BankPrjSet as Main Project: No

    Create Main Class: No

    Tool Reference Java Development: Java Classes: Modifying JavaClasses: Copying Java Classes

    2. Copy the pre-provided Account.java source file from theresources/01_intro/exercise2 directory to the sourcepackage of the BankPrj project.

    Task 2 Creating the TestAccountClass

    In this task, you complete the following steps to create a newTestAccount Java class.

    Tool Reference Java Development: Java Classes: Creating Java Classes

    1. Create a Java class with the following characteristics:

    Class Name: TestAccount

    Project: BankPrjLocation: Source Packages

    Package: default package

    2. Add the main method.

    3. Declare a variable in the main method. The variable has a type ofAccount and a name of acct. Initialize the variable by creating aninstance of the Account class with an initial balance of 100.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Creating a Test Program (Level 2)

    Getting Started Lab 1-9Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.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 getBalancemethod to retrieve the new account balance anduse the System.out.println method to display the balance to thestandard output stream.

    Task 3 Compiling the TestAccountClass

    Compile the TestAccount class, and make necessary changes to correctcompilation errors.

    Task 4 Running the TestAccountProgram

    Run the TestAccount program. If there are runtime errors, makenecessary changes to the TestAccount class, recompile it and run theprogram again.

    The output of the TestAccount program should be similar to thefollowing:

    Final account balance is: 3.0

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Creating a Test Program (Level 3)

    Lab 1-10 Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    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 additionalhints 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: CompilingJava Classes

    Java Development: Java Classes: Modifying Java Classes: ExecutingJava Programs

    123

    Demonstration The demonstration for this exercise can be found in thedemos/01_intro/exercise2 directory.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Creating a Test Program (Level 3)

    Getting Started Lab 1-11Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 1 Copying the AccountClass

    In this task, you create the BankPrj project, and copy the pre-providedAccount class to the project.

    Tool Reference Java Development: Java Application Projects: CreatingProjects

    1. Create the BankPrj Java Application Project with the followingcharacteristics:

    Project Name: BankPrjProject Location: projectsProject Folder: projects/BankPrjSet as Main Project: No

    Create Main Class: No

    Tool Reference Java Development: Java Classes: Modifying JavaClasses: Copying Java Classes

    2. Copy the pre-provided Account.java source file from theresources/01_intro/exercise2 directory to the sourcepackage of the BankPrj project.

    Task 2 Creating the TestAccountClass

    In this task, you complete the following steps to create a newTestAccount Java class.

    Tool Reference Java Development: Java Classes: Creating Java Classes

    1. Create a Java class with the following characteristics:

    Class Name: TestAccount

    Project: BankPrjLocation: 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

    }}

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Creating a Test Program (Level 3)

    Lab 1-12 Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    3. Declare a variable of type Account and initialize that variable bycreating an instance of the Account class with an initial balance of100.

    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 getBalancemethod to retrieve the new account balance and

    use the System.out.println method to display the balance to thestandard output stream.

    System.out.println(Final account balance is + acct.getBalance());

    Task 3 Compiling the TestAccountClass

    Compile the TestAccount class, and make necessary changes to correctcompilation errors.

    Task 4 Running the TestAccountProgram

    Run the TestAccount program. If there are runtime errors, makenecessary changes to the TestAccount class, recompile it and run theprogram again.

    The output of the TestAccount program should be similar to thefollowing:

    Final account balance is: 3.0

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise Summary

    Getting Started Lab 1-13Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Exercise Summary

    ?!

    Discussion Take a few minutes to discuss what experiences, issues, ordiscoveries you had during the lab exercise.

    Experiences

    _____________________________________________________________

    _____________________________________________________________

    _____________________________________________________________

    Interpretations

    _____________________________________________________________

    _____________________________________________________________

    _____________________________________________________________

    Conclusions

    _____________________________________________________________

    _____________________________________________________________

    _____________________________________________________________

    Applications

    _____________________________________________________________

    _____________________________________________________________

    _____________________________________________________________

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Lab 2-1Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Lab 2

    Object-OrientedProgramming

    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

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 1: Using the Java API Documentation

    Lab 2-2 Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Exercise 1: Using the Java API Documentation

    In this exercise, you explore the Java Platform, Standard Edition 6 (JavaSE 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 Webpage in the browser.

    Note The API documentation for Java Platform SE 6 can be found athttp://java.sun.com/javase/6/docs/api.

    2. Select the java.text package in the package list in the upper-leftcorner 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 windowon the right panel of the frameset. Review the format and parsemethods.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Exploring Encapsulation, Version 1 (Level 1)

    Object-Oriented Programming Lab 2-3Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Exercise 2: Exploring Encapsulation, Version 1 (Level 1)

    In this exercise, you explore the purpose of proper object encapsulation. Youcreate a class in two steps to demonstrate the use of information hiding. Inthis version, you create an Account class with public data members. Youwill then create a test program that demonstrates the danger of using thepublic data directly.

    Figure 2-1 shows the UML class diagram of the Account class that youwill create in this exercise. This class will have one public data member(or instance variable), called balance, that maintains the monetary valueof the customers 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: Thebalance of the bank account must never go below zero. In this exercise, you willdiscover 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

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Exploring Encapsulation, Version 1 (Level 1)

    Lab 2-4 Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    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: CompilingJava Classes

    Java Development: Java Classes: Modifying Java Classes: ExecutingJava Programs

    For this exercise, you work in the BankPrj project in the projectsdirectory.

    123

    Demonstration The demonstration for this exercise can be found in thedemos/02_OOP/exercise2 directory.

    Task 1 Deleting the AccountClass

    Tool Reference Java Development: Other Files: Deleting Files

    In this task, you delete the Account class copied in Lab 1.

    Task 2 Creating the AccountClass

    In this task, you create the Account class according to the UML diagramin Figure 2-1. The class should have the following characteristics:

    Class Name: Account

    Project: BankPrjLocation: Source Packages

    Package: default package

    After creating the Account class, add an instance variable balance anda constructor according to the UML diagram in Figure 2-1. Initialize thebalance instance variable with the parameter of the constructor.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Exploring Encapsulation, Version 1 (Level 1)

    Object-Oriented Programming Lab 2-5Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 3 Creating the TestAccount2Class

    In this task, you create the TestAccount2 class with the followingcharacteristics:

    Class Name: TestAccount2

    Project: BankPrjLocation: Source Packages

    Package: default package

    This class acts as a program to create an Account object with an initialbalance 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 thestandard output stream.

    Task 4 Compiling the TestAccount2Class

    In this task, you compile the TestAccount2 class, and make necessarychanges to correct compilation errors.

    Task 5 Running the TestAccount2Program

    In this task, you run the TestAccount2 program.

    The output should be similar to the following:

    Final account balance is -3.0Waqar

    Nisar (w

    nisar@p

    dippk) h

    as a non

    -transfer

    able lice

    nse to u

    se

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Exploring Encapsulation, Version 1 (Level 2)

    Lab 2-6 Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.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: CompilingJava Classes

    Java Development: Java Classes: Modifying Java Classes: ExecutingJava Programs

    For this exercise, you work in the BankPrj project in the projectsdirectory.

    123

    Demonstration The demonstration for this exercise can be found in thedemos/02_OOP/exercise2 directory.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Exploring Encapsulation, Version 1 (Level 2)

    Object-Oriented Programming Lab 2-7Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    Task 1 Deleting the AccountClass

    Tool Reference Java Development: Other Files: Deleting Files

    In this task, you delete the Account class copied in Lab 1.

    Task 2 Creating the AccountClass

    In this task, you complete the following steps to create a Java class namedAccount:

    1. Create the Account class with the following characteristics:

    Class Name: Account

    Project: BankPrjLocation: Source Packages

    Package: default package

    2. Add the balance instance variable.

    3. Add a constructor that sets the balance instance variable to theinitial balance argument passed to the constructor.

    Task 3 Creating the TestAccount2Class

    In this task, you complete the following steps to create a Java class namedTestAccount2:

    1. Create the TestAccount2 class with the following characteristics:

    Class Name: TestAccount2

    Project: BankPrjLocation: Source Packages

    Package: default package

    2. Add the main method:

    a. Declare a variable within the main method of type Accountnamed acct. Also, in the same statement, initialize the variableacct to a new instance of Account by passing 100.00 to theconstructor as the initial balance.

    b. Use the addition operator to add 47 to the account objectsbalance.

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Exploring Encapsulation, Version 1 (Level 2)

    Lab 2-8 Java Programming LanguageCopyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    c. Use the subtraction operator to subtract 150 from the accountobjects balance.

    d. Use the System.out.printlnmethod to display the balance tothe standard output stream.

    Task 4 Compiling the TestAccount2Class

    In this task, you compile the TestAccount2 class and the Account class,and make necessary changes to correct compilation errors.

    Task 5 Running the TestAccount2Program

    In this task, you run the TestAccount2 program.

    The output should be similar to the following:

    Final account balance is -3.0

    Waqar N

    isar (wni

    sar@pdip

    pk) has

    a non-t

    ransfera

    ble licen

    se to use

    this Stud

    ent Guid

    e

    Una

    utho

    rized

    repr

    oduc

    tion

    or d

    istri

    butio

    n pr

    ohib

    ited

    Cop

    yrig

    ht

    201

    2, O

    racl

    e an

    d/or

    its

    affil

    iate

    s

  • Exercise 2: Exploring Encapsulation, Version 1 (Level 3)

    Object-Oriented Programming Lab 2-9Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

    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, whic