Sie sind auf Seite 1von 276

Oracle Internal & Oracle Academy Use Only

D61748GC11

Edition 1.1

June 2010

D67983

Academy Use Only D61748GC11 Edition 1.1 June 2010 D67983 Java Programming Language Java SE 6 Activity

Java Programming Language Java SE 6

Activity Guide – Vol 2 (for Windows)

SL-275-SE6 G.2

Oracle Internal & Oracle Academy 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 Internal & Oracle Academy Use Only

This page intentionally left blank.

Oracle Internal & Oracle Academy Use Only

This page intentionally left blank.

Oracle Internal & Oracle Academy Use Only

Table of Contents

About This Workbook Lab Goals Performing the Exercises Conventions Typographical Conventions Additional Conventions

Getting Started 1-1

1-1

Preface-xvii

Preface-xvii

Preface-xviii

Preface-xix

Preface-xix

Preface-xx

Objectives Exercise 1: Exploring Java™ Program Errors Preparation Task – Correcting Compilation and Runtime Errors Exercise 2: Creating a Test Program (Level 1) Preparation Task 1 – Copying the Account Class Task 2 – Creating the TestAccount Class Task 3 – Compiling the TestAccount Class Task 4 – Running the TestAccount Program Exercise 2: Creating a Test Program (Level 2) Preparation Task 1 – Copying the Account Class Task 2 – Creating the TestAccount Class Task 3 – Compiling the TestAccount Class Task 4 – Running the TestAccount Program Exercise 2: Creating a Test Program (Level 3) Preparation Task 1 – Copying the Account Class Task 2 – Creating the TestAccount Class Task 3 – Compiling the TestAccount Class Task 4 – Running the TestAccount Program Exercise Summary

1-2

1-2

1-2

1-4

1-4

1-5

1-5

1-6

1-6

1-7

1-7

1-8

1-8

1-9

1-9

1-10

1-10

1-11

1-11

1-12

1-12

1-13

Object-Oriented Programming 2-1

2-1

Objectives

v

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

Oracle Internal & Oracle Academy Use Only

Exercise 1: Using the Java API Documentation Preparation Task – Using the Java API Documentation Exercise 2: Exploring Encapsulation, Version 1 (Level 1) Preparation 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 Exercise 2: Exploring Encapsulation, Version 1 (Level 2) Preparation 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 Exercise 2: Exploring Encapsulation, Version 1 (Level 3) Preparation 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 Exercise 3: Exploring Encapsulation, Version 2 (Level 1) Preparation Task 1 – Modifying the Account Class Task 2 – Modifying the TestAccount Class Task 3 – Compiling the TestAccount Class Task 4 – Running the TestAccount Program Exercise 3: Exploring Encapsulation, Version 2 (Level 2) Preparation Task 1 – Modifying the Account Class Task 2 – Modifying the TestAccount Class Task 3 – Compiling the TestAccount Class Task 4 – Running the TestAccount Program Exercise 3: Exploring Encapsulation, Version 2 (Level 3) Preparation Task 1 – Modifying the Account Class Task 2 – Modifying the TestAccount Class Task 3 – Compiling the TestAccount Class Task 4 – Running the TestAccount Program Exercise 4: Creating Java Packages Preparation Task 1 – Creating the Java Packages Task 2 – Moving and Modifying the Account Class

vi

Java™ Programming Language

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

2-2

2-2

2-2

2-3

2-4

2-4

2-4

2-5

2-5

2-5

2-6

2-6

2-7

2-7

2-7

2-8

2-8

2-9

2-9

2-10

2-10

2-10

2-11

2-11

2-12

2-12

2-13

2-13

2-13

2-13

2-14

2-14

2-14

2-15

2-15

2-15

2-16

2-16

2-16

2-17

2-17

2-17

2-18

2-19

2-19

2-19

Oracle Internal & Oracle Academy Use Only

Task 3 – Moving the TestAccount Class Task 4 – Compiling the TestAccount Class Task 5 – Running the TestAccount Program Exercise Summary

2-20

2-20

2-20

2-21

Identifiers, Keywords, and Types Objectives Exercise 1: Investigating Reference Assignment Preparation Task 1 – Creating the TestMyPoint Class Task 2 – Compiling the TestMyPoint Class Task 3 – Running the TestMyPoint Program Exercise 2: Creating Customer Accounts (Level 1) Preparation Task 1 – Creating the Customer Class Task 2 – Copying the TestBanking Class Task 3 – Compiling the TestBanking Class Task 4 – Running the TestBanking Program Exercise 2: Creating Customer Accounts (Level 2) Preparation Task 1 – Creating the Customer Class Task 2 – Copying the TestBanking Class Task 3 – Compiling the TestBanking Class Task 4 – Running the TestBanking Program Exercise 2: Creating Customer Accounts (Level 3) Preparation Task 1 – Creating the Customer Class Task 2 – Copying the TestBanking Class Task 3 – Compiling the TestBanking Class Task 4 – Running the TestBanking Program Exercise Summary

Expressions and Flow Control 4-1

3-1

3-1

3-2

3-2

3-3

3-4

3-4

3-5

3-6

3-6

3-6

3-7

3-7

3-8

3-8

3-9

3-9

3-9

3-10

3-11

3-11

3-12

3-13

3-13

3-13

3-14

Objectives Exercise 1: Using Loops and Branching Statements Preparation Task 1 – Creating the FooBarBaz Class Task 2 – Compiling the FooBarBaz Class Task 3 – Running the FooBarBaz Program Hints

Exercise 2: Using Conditional Statements in the Account Class (Level 1)

4-4

4-4

4-3

4-3

4-3

4-2

4-1

4-5

Preparation

4-5

Task 1 – Modifying the Account Class

4-6

Task 2 – Deleting the Current TestBanking Class

4-6

Task 3 – Copying the TestBanking Class

4-6

Task 4 – Compiling the TestBanking Class

4-6

vii

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

Oracle Internal & Oracle Academy Use Only

4-6

Exercise 2: Using Conditional Statements in the Account Class (Level 2)

Task 5 – Running the TestBanking Program

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 Task 1 – Modifying the Account Class Task 2 – Deleting the Current TestBanking Class Task 3 – Copying the TestBanking Class Task 4 – Compiling the TestBanking Class Task 5 – Running the TestBanking Program Exercise 3: Using Nested Loops (Advanced) Preparation Task 1 – Writing the isSubString Method Task 2 – Compiling the TestIsSubString Class Task 3 – Running the TestIsSubString Program Hints Exercise Summary

Arrays Objectives Exercise 1 – Using Primitive Arrays (Level 1) Preparation Task 1 – Creating the TestArrays Class Task 2 – Compiling the TestArrays Class Task 3 – Running the TestArrays Program Exercise 1 – Using Primitive Arrays (Level 2) Preparation Task 1 – Creating the TestArrays Class Task 2 – Compiling the TestArrays Class Task 3 – Running the TestArrays Program Hint Exercise 1 – Using Primitive Arrays (Level 3) Preparation Task 1 – Creating the TestArrays Class Task 2 – Compiling the TestArrays Class Task 3 – Running the TestArrays Program

4-10

4-11

4-11

4-11

4-11

4-12

4-13

4-13

4-13

4-14

4-14

4-15

4-16

5-1

5-1

5-2

5-2

5-2

5-3

5-3

5-4

5-4

5-4

5-5

5-5

5-6

5-7

5-7

5-7

5-9

5-9

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level

1)

5-10

 

Preparation

5-11

viii

Java™ Programming Language

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

Oracle Internal & Oracle Academy Use Only

Task 1 – Creating the Bank Class

5-11

Task 2 – Deleting the Current TestBanking Class

5-11

Task 3 – Copying the TestBanking Class

5-11

Task 4 – Compiling the TestBanking Class

5-12

Task 5 – Running the TestBanking Program

5-12

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level

2)

5-13

 

Task 1 – Creating the Bank Class

5-14

Task 2 – Deleting the Current TestBanking Class

5-14

Task 3 – Copying the TestBanking Class

5-14

Task 4 – Compiling the TestBanking Class

5-14

Task 5 – Running the TestBanking Program

5-15

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level

5-16

5-17

5-18

5-18

5-18

5-18

5-19

Class Design 6-1

6-1

6-2

6-3

6-3

6-4

6-4

6-4

6-5

6-5

6-5

6-7

6-7

6-7

6-8

6-9

6-10

6-10

6-10

6-10

6-12

6-12

6-12

6-13

6-14

6-15

6-15

3)

Task 1 – Creating the Bank Class Task 2 – Deleting the Current TestBanking Class Task 3 – Copying the TestBanking Class Task 4 – Compiling the TestBanking Class Task 5 – Running the TestBanking Program Exercise Summary

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

ix

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

Oracle Internal & Oracle Academy Use Only

Task 6 – Compiling the TestBanking Class

6-15

Task 7 – Running the TestBanking Program

6-16

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-21

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-24

Task 2 – Copying and Completing the CustomerReport Class

6-25

Task 3 – Copying the TestReport Class

6-26

Task 4 – Compiling the TestReport Class

6-26

Task 5 – Running the TestReport Program

6-26

Exercise 3: Creating a Batch Program (Advanced)

6-27

Preparation

6-28

Task 1 – Modifying the SavingsAccount Class

6-28

Task 2 – Creating the AccumulateSavingsBatch Class

6-28

Task 3 – Copying the TestBatch Class

6-29

Task 4 – Compiling the TestBatch Class

6-29

Task 5 – Running the TestBatch Program

6-29

Exercise Summary

6-31

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-3

Task 4 – Copying the TestReport Class

7-4

Task 5 – Compiling the TestReport Class

7-4

x

Java™ Programming Language

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

Oracle Internal & Oracle Academy Use Only

Task 6 – Running the TestReport Program Exercise 1: Applying Static Members to a Design (Level 2) Task 1 – Modifying the Bank Class Task 2 – Modifying the CustomerReport Class Task 3 – Deleting the Current TestReport Class Task 4 – Copying the TestReport Class Task 5 – Compiling the TestReport Class Task 6 – Running the TestReport Program Exercise 1: Applying Static Members to a Design (Level 3) Task 1 – Modifying the Bank Class Task 2 – Modifying the CustomerReport Class Task 3 – Deleting the Current TestReport Class Task 4 – Copying the TestReport Class Task 5 – Compiling the TestReport Class Task 6 – Running the TestReport Program Exercise 2: Working With Interfaces and Abstract Classes (Level 1) Preparation Task 1 – Creating the Pet Interface Task 2 – Creating the Animal Classes Task 3 – Creating the TestAnimals Class Task 4 – Compiling the TestAnimals Class Task 5 – Running the TestAnimals Program Exercise 2: Working With Interfaces and Abstract Classes (Level 2) Preparation Task 1 – Creating the Pet Interface Task 2 – Creating the Animal Classes Task 3 – Creating the TestAnimals Class Task 4 – Compiling the TestAnimals Class Task 5 – Running the TestAnimals Program Exercise 2: Working With Interfaces and Abstract Classes (Level 3) Preparation Task 1 – Creating the Pet Interface Task 2 – Creating the Animal Classes Task 3 – Creating the TestAnimals Class Task 4 – Compiling the TestAnimals Class Task 5 – Running the TestAnimals Program Exercise Summary

Exceptions and Assertions Objectives Exercise: Creating Your Own Exception (Level 1) Task 1 – Creating the OverdraftException Class Task 2 – Modifying the Account Class

7-4

7-5

7-6

7-6

7-6

7-7

7-7

7-7

7-8

7-9

7-10

7-10

7-10

7-11

7-11

7-12

7-13

7-13

7-14

7-14

7-15

7-15

7-16

7-16

7-16

7-17

7-19

7-20

7-20

7-21

7-21

7-21

7-22

7-25

7-26

7-26

7-27

8-1

8-1

8-2

8-4

8-4

xi

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

Oracle Internal & Oracle Academy Use Only

xii

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

Collections and Generics Framework Objectives Exercise 1: Using Collections to Represent Association (Level 1) Preparation Task 1 – Modifying the Bank Class Task 2 – Modifying the Customer Class Task 3 – Compiling the TestReport Class Task 4 – Running the TestReport Program Exercise 1: Using Collections to Represent Association (Level 2) Preparation Task 1 – Modifying the Bank Class Task 2 – Modifying the Customer Class Task 3 – Compiling the TestReport Class Task 4 – Running the TestReport Program Exercise 1: Using Collections to Represent Association (Level 3) Preparation Task 1 – Modifying the Bank Class Task 2 – Modifying the Customer Class Task 3 – Compiling the TestReport Class Task 4 – Running the TestReport Program

8-4

8-4

8-4

8-5

8-5

8-6

8-7

8-7

8-8

8-8

8-8

8-8

8-8

8-9

8-10

8-11

8-11

8-12

8-12

8-12

8-12

8-13

9-1

9-1

9-2

9-3

9-3

9-3

9-3

9-4

9-5

9-5

9-5

9-6

9-6

9-6

9-8

9-8

9-9

9-9

9-10

9-11

Java™ Programming Language

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

Oracle Internal & Oracle Academy Use Only

9-12

I/O Fundamentals

Console I/O and File I/O Objectives Exercise 1: Reading a Data File (Level 1) Preparation Task 1 – Creating a data Directory Task 2 – Copying the Resource File Task 3 – Creating the DataSource Class Task 4 – Deleting Unnecessary Classes Task 5 – Copying the TestReport Class Task 6 – Compiling the TestReport Class Task 7 – Running the BankPrj Project Exercise 1: Reading a Data File (Level 2) Preparation Task 1 – Creating a data Directory Task 2 – Copying the Resource File Task 3 – Creating the DataSource Class Task 4 – Deleting Unnecessary Classes Task 5 – Copying the TestReport Class Task 6 – Compiling the TestReport Class Task 7 – Running the BankPrj Project Exercise 1: Reading a Data File (Level 3) Preparation Task 1 – Creating a data Directory Task 2 – Copying the Resource File Task 3 – Creating the DataSource Class Task 4 – Deleting Unnecessary Classes Task 5 – Copying the TestReport Class Task 6 – Compiling the TestReport Class Task 7 – Running the BankPrj Project Exercise Summary

Building Java GUIs Using the Swing API 12-1

11-17

11-15

11-15

11-15

11-15

11-12

11-12

11-12

Exercise Summary

10-1

11-1

11-1

11-2

11-4

11-4

11-4

11-5

11-5

11-5

11-5

11-6

11-7

11-7

11-8

11-8

11-8

11-9

11-9

11-9

11-9

11-11

11-11

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-5

Task 2 – Compiling the ChatClient Class

12-6

xiii

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

Oracle Internal & Oracle Academy Use Only

Task 3 – Running the ChatClient Program

12-6

Task 4 – Terminating the Running ChatClient Program

12-7

Exercise 1: Creating the ChatClient GUI Part 1 (Level 3)

12-8

Preparation

12-8

Task 1 – Creating the ChatClient Class

12-8

Task 2 – Compiling the ChatClient Class

12-10

Task 3 – Running the ChatClient Program

12-10

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

xiv

Java™ Programming Language

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

Oracle Internal & Oracle Academy Use Only

Preparation Task 1 – Modifying the ChatClient Class Task 2 – Compiling the ChatClient Class Task 3 – Running the ChatClient Program Exercise: Creating the ChatClient GUI, Part 3 (Level 2) Preparation Task 1 – Modifying the ChatClient Class Task 2 – Compiling the ChatClient Class Task 3 – Running the ChatClient Program Exercise: Creating the ChatClient GUI, Part 3 (Level 3) Preparation Task 1 – Modifying the ChatClient Class Task 2 – Compiling the ChatClient Class Task 3 – Running the ChatClient Program Exercise Summary

14-3

14-3

14-3

14-3

14-4

14-4

14-4

14-5

14-5

14-6

14-6

14-6

14-9

14-9

14-10

Threads 15-1

15-1

Objectives Exercise: Using Multithreaded Programming (Level 1) Preparation Task 1 – Creating the PrintMe Class Task 2 – Creating the TestThreeThreads Class Task 3 – Compiling the TestThreeThreads Class Task 4 – Running the TestThreeThreads Program Exercise: Using Multithreaded Programming (Level 2) Preparation Task 1 – Creating the PrintMe Class Task 2 – Creating the TestThreeThreads Class Task 3 – Compiling the TestThreeThreads Class Task 4 – Running the TestThreeThreads Program Exercise: Using Multithreaded Programming (Level 3) Preparation Task 1 – Creating the PrintMe Class Task 2 – Creating the TestThreeThreads Class Task 3 – Compiling the TestThreeThreads Class Task 4 – Running the TestThreeThreads Program Exercise Summary

15-2

15-2

15-3

15-3

15-3

15-4

15-5

15-5

15-6

15-6

15-6

15-7

15-8

15-8

15-8

15-9

15-10

15-10

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

xv

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

Oracle Internal & Oracle Academy Use Only

Task 1 – Modifying the ChatClient Class

16-6

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

Task 3 – Running the ChatRoomPrj Project

16-13

Exercise Summary

16-14

xvi

Java™ Programming Language

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

Oracle Internal & Oracle Academy 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.

Lab Preface-xvii

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

Performing the Exercises

Oracle Internal & Oracle Academy Use Only

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.

Lab Preface-xviii

Java™ Programming Language

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

Conventions

Oracle Internal & Oracle Academy Use Only

Conventions

The following conventions are used in this course to represent various training 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 dir to list all files. system% You have mail.

Courier is 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 bold is used for characters and numbers that you type; for example:

To list the files in this directory, type:

# dir

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

About This Workbook

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

Lab Preface-xix

Conventions

Oracle Internal & Oracle Academy Use Only

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

“The

arguments.

doIt() method

refers to any method called doIt.

refers to a method called doIt that takes no

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:

Lab Preface-xx

If working in the Solaris OS

$ cd $SERVER_ROOT/bin

If working in Microsoft Windows

C:\> cd %SERVER_ROOT%\bin

Java™ Programming Language

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

Lab 1

Oracle Internal & Oracle Academy Use Only

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

Lab 1-1

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

Exercise 1: Exploring Java™ Program Errors

Oracle Internal & Oracle Academy Use Only

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”

“Task – Correcting Compilation and Runtime Errors” Preparation No preparation is needed for this exercise. Tool

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 d:\labs\student\exercises\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.

Lab 1-2

Java™ Programming Language

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

Exercise 1: Exploring Java™ Program Errors

Oracle Internal & Oracle Academy Use Only

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 Tool Reference –

1.

Open the Java file.

Tool Reference – Java Development: Java Classes: Modifying Java Classes:

Compile the Java file.

If a compilation error occurs, identify the source of the error and fix it.

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

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

2.

3.

4.

Tool Reference – Java Development: Java Classes: Modifying Java Classes:

Execute the Java program.

If a runtime error occurs, identify the source of the error and fix it.

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

Executing Java Programsit. Repeat step 2 and step 3 until there are no more errors. 5. 6. 7.

5.

6.

7.

Getting Started

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

Lab 1-3

Exercise 2: Creating a Test Program (Level 1)

Oracle Internal & Oracle Academy Use Only

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.

class using a Unified Modeling Language (UML) Class diagram. Lab 1-4 Account «constructors»

Lab 1-4

Account

«constructors»

+Account(initBalance:double)

«methods»

+getBalance():double

+deposit(amt:double):void

+withdraw(amt:double):void

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: Modifying Java Classes: Copying Java Classes

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

Java™ Programming Language

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

Exercise 2: Creating a Test Program (Level 1)

Oracle Internal & Oracle Academy Use Only

Task 1 – Copying the Account Class

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

Tool Reference – Java Development: Java Application Projects: Creating ProjectsTool Reference –

1.

Create the BankPrj Java Application Project with the following characteristics:

Project Name: BankPrj

Project Location: d:\labs\student\projects

Project Folder: d:\labs\student\projects\BankPrj

Set as Main Project: No

Create Main Class: No

Tool Reference – Java Development: Java Classes: Modifying Java Classes:

Copying Java Classes– Java Development: Java Classes: Modifying Java Classes: 2. Copy the pre-provided Account.java source file from

2.

Copy the pre-provided Account.java source file from the d:\labs\student\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 TestAccount Java class.

Tool Reference – Java Development: Java Classes: Creating Java Classes 1.Tool Reference –

Create a new Java class in the BankPrj project with the following characteristics:

Class Name: TestAccount

Project: BankPrj

Location: Source Packages

Package: default package

2. Edit the source file for the TestAccount class to add a main method. The main method of the TestAccount class creates an Account object 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.

Getting Started

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

Lab 1-5

Exercise 2: Creating a Test Program (Level 1)

Oracle Internal & Oracle Academy Use Only

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:

Lab 1-6

Final account balance is: 3.0

Java™ Programming Language

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

Exercise 2: Creating a Test Program (Level 2)

Oracle Internal & Oracle Academy Use Only

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.

are the Level 2 instructions that provide additional hints. This exercise contains the following sections: ●

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: Modifying Java Classes: Copying Java Classes

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

Getting Started

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

Lab 1-7

Exercise 2: Creating a Test Program (Level 2)

Oracle Internal & Oracle Academy Use Only

Task 1 – Copying the Account Class

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

Tool Reference – Java Development: Java Application Projects: Creating ProjectsTool Reference –

1.

Create the BankPrj Java Application Project with the following characteristics:

Project Name: BankPrj

Project Location: d:\labs\student\projects

Project Folder: d:\labs\student\projects\BankPrj

Set as Main Project: No

Create Main Class: No

Tool Reference – Java Development: Java Classes: Modifying Java Classes:

Copying Java Classes– Java Development: Java Classes: Modifying Java Classes: 2. Copy the pre-provided Account.java source file from

2.

Copy the pre-provided Account.java source file from the d:\labs\student\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 TestAccount Java class.

Tool Reference – Java Development: Java Classes: Creating Java Classes 1.Tool Reference –

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 Account and a name of acct. Initialize the variable by creating an instance of the Account class with an initial balance of 100.

Lab 1-8

Java™ Programming Language

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

Exercise 2: Creating a Test Program (Level 2)

Oracle Internal & Oracle Academy Use Only

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

Getting Started

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

Lab 1-9

Exercise 2: Creating a Test Program (Level 3)

Oracle Internal & Oracle Academy Use Only

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.

that provide additional hints with code snippets. Lab 1-10 This exercise contains the following sections: ●

Lab 1-10

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: Modifying Java Classes: Copying Java Classes

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

Java™ Programming Language

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

Exercise 2: Creating a Test Program (Level 3)

Oracle Internal & Oracle Academy Use Only

Task 1 – Copying the Account Class

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

Tool Reference – Java Development: Java Application Projects: Creating ProjectsTool Reference –

1.

Create the BankPrj Java Application Project with the following characteristics:

Project Name: BankPrj

Project Location: d:\labs\student\projects

Project Folder: d:\labs\student\projects\BankPrj

Set as Main Project: No

Create Main Class: No

Tool Reference – Java Development: Java Classes: Modifying Java Classes:

Copying Java Classes– Java Development: Java Classes: Modifying Java Classes: 2. Copy the pre-provided Account.java source file from

2.

Copy the pre-provided Account.java source file from the d:\labs\student\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 TestAccount Java class.

Tool Reference – Java Development: Java Classes: Creating Java Classes 1.Tool Reference –

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

}

}

Getting Started

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

Lab 1-11

Exercise 2: Creating a Test Program (Level 3)

Oracle Internal & Oracle Academy Use Only

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:

Lab 1-12

Final account balance is: 3.0

Java™ Programming Language

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

Exercise Summary

Oracle Internal & Oracle Academy Use Only

Exercise Summary

! ?
!
?

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

Experiences

Interpretations

Conclusions

Applications

Getting Started

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

Lab 1-13

Oracle Internal & Oracle Academy Use Only

Lab 2

Oracle Internal & Oracle Academy Use Only

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

Lab 2-1

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

Exercise 1: Using the Java API Documentation

Oracle Internal & Oracle Academy Use Only

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.

Edition 6 API Specification Web page in the browser. Lab 2-2 Note – The API documentation

Lab 2-2

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.

Java™ Programming Language

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

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

Oracle Internal & Oracle Academy Use Only

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.

Account

+balance : double

«constructors»

+Account(initBalance:double)

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”

Object-Oriented Programming

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

Lab 2-3

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

Oracle Internal & Oracle Academy Use Only

Preparation

No preparation is needed for this exercise.

Tool Reference – Tool references used in this exercise:

● Java Development: Other Files: Deleting Files

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 d:\labs\student\projects directory.

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.

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 a constructor according to the UML diagram in Figure 2-1. Initialize the balance instance variable with the parameter of the constructor.

Lab 2-4

Java™ Programming Language

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

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

Oracle Internal & Oracle Academy Use Only

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

Object-Oriented Programming

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

Lab 2-5

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

Oracle Internal & Oracle Academy Use Only

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”

● “Task 5 – Running the TestAccount2 Program” Preparation No preparation is needed for this exercise.

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 d:\labs\student\projects directory.

Lab 2-6

Java™ Programming Language

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

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

Oracle Internal & Oracle Academy Use Only

Task 1 – Deleting the Account Class

Academy Use Only Task 1 – Deleting the Account Class Tool Reference – Java Development: Other

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 acct to 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.

c. Use the subtraction operator to subtract 150 from the account object’s balance.

Object-Oriented Programming

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

Lab 2-7

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

Oracle Internal & Oracle Academy Use Only

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

Lab 2-8

Java™ Programming Language

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

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

Oracle Internal & Oracle Academy Use Only

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”

● “Task 5 – Running the TestAccount2 Program” Preparation No preparation is needed for this exercise.

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 d:\labs\student\projects directory.

Object-Oriented Programming

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

Lab 2-9

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

Oracle Internal & Oracle Academy Use Only

Task 1 – Deleting the Account Class

Academy Use Only Task 1 – Deleting the Account Class Tool Reference – Java Development: Other

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:

Lab 2-10

Class Name: TestAccount2

Project: BankPrj

Location: Source Packages

Package: default package

Java™ Programming Language

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

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

Oracle Internal & Oracle Academy Use Only

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

Object-Oriented Programming

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

Lab 2-11

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

Oracle Internal & Oracle Academy Use Only

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.

Account

-balance : double

«constructors»

+Account(initBalance:double)

«methods»

+getBalance():double

+deposit(amt:double):void

+withdraw(amt:double):void

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”

Lab 2-12

Preparation

There is no preparation for this exercise.

Java™ Programming Language

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

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

Oracle Internal & Oracle Academy Use Only

2 (Level 1) Oracle Internal & Or acle Academy Use Only Tool Reference – Tool references

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 d:\labs\student\projects directory.

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.

Object-Oriented Programming

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

Lab 2-13

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

Oracle Internal & Oracle Academy Use Only

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”

● “Task 4 – Running the TestAccount Program” Preparation There is no preparation for this exercise.

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 d:\labs\student\projects directory.

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.

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.

Lab 2-14

Java™ Programming Language

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

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

Oracle Internal & Oracle Academy Use Only

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

Object-Oriented Programming

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

Lab 2-15

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

Oracle Internal & Oracle Academy Use Only

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”

● “Task 4 – Running the TestAccount Program” Preparation There is no preparation for this exercise.

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 d:\labs\student\projects directory.

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;

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 balance instance 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.

Lab 2-16

Java™ Programming Language

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

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

Oracle Internal & Oracle Academy Use Only

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

Object-Oriented Programming

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

Lab 2-17

Exercise 4: Creating Java Packages

Oracle Internal & Oracle Academy Use Only

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.

com.mybank

test

com.mybank test domain «methods» Account -balance : double «constructors» +Account(initBalance:double)

domain

«methods»

domain «methods» Account -balance : double «constructors» +Account(initBalance:double) +getBalance():double
domain «methods» Account -balance : double «constructors» +Account(initBalance:double) +getBalance():double

Account

-balance : double

«constructors»

+Account(initBalance:double)

+getBalance():double

+deposit(amt:double):void

+withdraw(amt:double):void

TestAccount +main(args:String[])
TestAccount
+main(args:String[])

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

Lab 2-18

Java™ Programming Language

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

Exercise 4: Creating Java Packages

Oracle Internal & Oracle Academy Use Only

Preparation

There is no preparation for this exercise.

Tool Reference – Tool references used in this exercise:

● Java Development: Java Packages: Creating Java Packages

Java Development: Java Packages: Creating Java Packages

Java Development: Java Classes: Moving Java Classes (without refactoring)

 

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 d:\labs\student\projects directory.

Task 1 – Creating the Java Packages

Tool Reference – Java Development: Java Packages: Creating Java Packages

In this task, you create two Java Source Packages in the BankPrj project with the

In this task, you create two Java Source Packages in the BankPrj project with the following names:

com.mybank.domain

com.mybank.test

Task 2 – Moving and Modifying the Account Class

In this task, you complete the following steps to move the Account class and add the appropriate package statement to the class:

Tool Reference – Java Development: Java Classes: Moving Java Classes (without refactoring)

Tool Reference – Java Development: Java Classes: Moving Java Classes (without refactoring)

1. Move the Account class source file to the com.mybank.domain package.

2. Add the following package statement at the top of the Account class:

package com.mybank.domain;

Object-Oriented Programming

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

Lab 2-19

Exercise 4: Creating Java Packages

Oracle Internal & Oracle Academy Use Only

Task 3 – Moving the TestAccount Class

In this task, you complete the following steps to move the TestAccount class and add the appropriate package and import statements to the class:

1. Move the TestAccount class source file to the com.mybank.test package.

2. Add the following package statement at the top of the TestAccount class:

package com.mybank.test;

3. Add the following import statement under the package statement in the TestAccount class if has not already been added by your editor:

import com.mybank.domain.Account;

Task 4 – Compiling the TestAccount Class

In this task, you compile the TestAccount class and the Account class.

Task 5 – Running the TestAccount Program

In this task, you run the TestAccount program. The code has not changed, so the output should be similar to the following:

Final account balance is 147.0

Lab 2-20

Java™ Programming Language

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

Exercise Summary

Oracle Internal & Oracle Academy Use Only

Exercise Summary

! ?
!
?

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

Experiences

Interpretations

Conclusions

Applications

Object-Oriented Programming

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

Lab 2-21

Oracle Internal & Oracle Academy Use Only