copyright © 2002 prosofttraining. all rights reserved. java programming fundamentals

161
Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Upload: nigel-fox

Post on 14-Jan-2016

224 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Java Programming Fundamentals

Page 2: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 1:Java Runtime Environment

Page 3: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Identify the differences between stand-alone applications and applets

• Describe the role of the Java Virtual Machine• Create a simple Java program• Use Java comments• Compile and execute a Java program• Describe the differences between *.java and *.class files

Page 4: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

The Java Virtual Machine

• Stand-alone applications• Applets• Classes• Methods• Bytecode

Page 5: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Java Comments

• Single-line comment• Multiline comment• Javadoc comment

Page 6: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Identify the differences between stand-alone applications and applets

Describe the role of the Java Virtual Machine Create a simple Java program Use Java comments Compile and execute a Java program Describe the differences between *.java and *.class files

Page 7: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 2:Data Types,

Variables and Operators

Page 8: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Use primitive data types• Declare a variable• Distinguish between local and class variables• Distinguish between implicit and explicit

casting• Use the operators for primitive types

Page 9: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Data Types

• Primitives– Integers– Floating-point numbers– Character– Boolean

Page 10: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Declaring Variablesand Variable Scope

• Types– Local– Instance– Class

• Default variable values• Variable declaration and initialization• Casting

– Implicit– Explicit

Page 11: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Casting Rules Chart

byte short int long float double

char

Page 12: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Operators

• Arithmetic operators• Relational operators• Logical operators

– Short-circuit• Bitwise operators

Page 13: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

AutomaticCasting

• Java automatically casts– bytes or shorts ints– floats doubles

Page 14: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Use primitive data types Declare a variable Distinguish between local and class variables Distinguish between implicit and explicit

casting Use the operators for primitive types

Page 15: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 3:Control

Statements

Page 16: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Explain the block structure of Java• Use Java conditional statements• Use Java iterative statements

Page 17: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Code Blocks

• Used to group the contents of a class or a method

• Used to designate all conditional and iterative statements

• Used for exception handling

Page 18: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Conditional Statements

• if statement

• switch/case statement– break statement

Page 19: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Iterative Statements (Loops)

• while loop (entry condition loop)• do while loop (exit condition loop)• for loop• Nested loops (break and continue)

Page 20: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Explain the block structure of Java Use Java conditional statements Use Java iterative statements

Page 21: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 4:Methods

Page 22: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Create and use static methods• Return a value from a method• Explain pass by value• Describe overloading methods• Identify the method signature

Page 23: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

JavaMethods

• The executable part of a class– Member

Page 24: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Return Statement

• Data types• Void

Page 25: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Calling a Method

• Parameters• Literals

Page 26: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Pass by Value

• A copy of the value of the variable is available to the method; changes to the copy do not affect the original value

Page 27: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Overloading

• Method signature– Name– Parameter list

• Does not include return type

Page 28: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Create and use static methods Return a value from a method Explain pass by value Describe overloading methods Identify the method signature

Page 29: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 5:Arrays

Page 30: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Declare and initialize an array• Allocate space for a new array• Describe array indexing• Use the length property• Discuss why arrays are passed by reference• Discuss Java’s garbage collection mechanism• Retrieve command line parameters

Page 31: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is an Array?

• Collection of same type• Non-primitive data type

Page 32: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Initializing an Array

• Allocation• Reference• Index

Page 33: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objects

• Properties• Methods

Page 34: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Using an Array

• length property

Page 35: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

1

0

3

1

5

2

MyIntArray

tmpIntArray

Passing an Array to a Method

Page 36: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Garbage Collection

• Variable name reuse• Memory leak

Page 37: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Command Line Parameters

• Always String type data

Page 38: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Declare and initialize an array Allocate space for a new array Describe array indexing Use the length property Discuss why arrays are passed by reference Discuss Java’s garbage collection mechanism Retrieve command line parameters

Page 39: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 6:Classes

and Objects

Page 40: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Identify the parts of an object• Create and use instance members• Distinguish between instance and class

members• Define abstraction• Create object references

Page 41: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Object-Oriented Programming

• Refers to the creation of a number of objects that communicate with one another

Page 42: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Isan Object?

• Instantiation• The new keyword

Page 43: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Instance andClass Members

• Class members– The exception in Java

• Instance members– The rule in Java

• Accessing instance members– Dot notation

• Accessing class members

Page 44: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Abstraction

• What does the object do versus how does it do it?

• Functionality versus implementation

Page 45: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Object References

rachael

tmpEmp

name: "Rachael"dept: "Accounting"salary: 52000

tmpEmp variable

Page 46: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Identify the parts of an object Create and use instance members Distinguish between instance and class

members Define abstraction Create object references

Page 47: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 7:Inheritance

Page 48: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Create a new class using inheritance• Create an overridden method

Page 49: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is Inheritance?

• Code reuse• Using inheritance

– extends keyword– subclass– superclass– Multiple inheritance

Page 50: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Overriding Methods

• Same signature as the superclass• super.method()

Page 51: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Create a new class using inheritance Create an overridden method

Page 52: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 8:Constructors

Page 53: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Use the default constructor• Create a constructor to initialize instance

variables• Call other constructors from the same class

and the parent class• Create a no-arguments constructor• Discuss String characteristics and define the

common methods of String

Page 54: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is a Constructor?

• new keyword• Similarities to methods

– Initialization of object– Pass parameters

• Differences from methods– Same name as class– No return type

• Callback

Page 55: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Using Constructors

• Default constructor– No arguments– Must be explicitly created if there are any

other constructors• Overloading constructors

– Unique parameter types

Page 56: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

The Keyword this

• this() as a constructor• Avoiding namespace conflicts• The super keyword

Page 57: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Constructors and Callbacks

• Constructors are a major facilitator in establishing interobject communication

Page 58: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Strings and StringBuffer

• String constructors• String characteristics• Methods of String• StringBuffer

Page 59: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Use the default constructor Create a constructor to initialize instance

variables Call other constructors from both the same

class and the parent class Create a no-arguments constructor Discuss String characteristics and define the

common methods of String

Page 60: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 9:Interfaces and Abstract Classes

Page 61: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Define and use interfaces• Define polymorphism• Use abstract classes• Create an abstract method

Page 62: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is an Interface?

• Contents of an interface– Abstract methods– Final variables

• Interface functions– Decoupling objects– Providing data type

Page 63: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Polymorphism

• Using one method name to invoke many different methods

Page 64: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is an Abstract Class?

• Cannot be instantiated• Can be used for a type

Page 65: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Define and use interfaces Define polymorphism Use abstract classes Create an abstract method

Page 66: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 10:Packages and Access Modifiers

Page 67: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Compile and run a class with packages• Identify the packages in the Java 2 API• Identify the various access levels in Java• Describe how access modifiers can be used to

set the access level of a variable, method or class

• Describe the object-oriented programming principle of encapsulation

• Identify accessor and mutator methods

Page 68: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Packages and Access Modifiers

• Packages– import statement

• Access modifiers– public– protected– package– private

Page 69: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Java 2 Application Programming Interface

• Organized into packages such as– java.lang– java.awt– java.io– java.util– java.net

Page 70: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Encapsulation

• Accessor– Method that reads a variable

• Mutator– Wrapping variables and methods together

Page 71: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

ObjectEncapsulation

Data 1Data 2Data 3Data 4

Page 72: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Compile and run a class with packages Identify the packages in the Java 2 API Identify the various access levels in Java Describe how access modifiers can be used to

set the access level of a variable, method or class

Describe the object-oriented programming principle of encapsulation

Identify accessor and mutator methods

Page 73: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 11:Swing Components

Page 74: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Distinguish between the AWT and Swing• Identify the general organization of the Swing

class structure• Define and use Swing widgets and containers

Page 75: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is the AWT?

• Heavyweight components– Peer components

• AWT 1.0– Container propagation– Single-method event-handling

• AWT 1.1– New event model

Page 76: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is Swing?

• Lightweight components– No peer components– All Java

• Customizable• Model View Controller (MVC) programming

paradigm• Included in Java 2 (JDK 1.2)

Page 77: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Basic Swing Components

• JComponent• JLabel• JButton• JTextField• JTextArea• JScrollBar

• ImageIcon• JScrollPane• JPanel• JFrame• JFileChooser• JApplet

Page 78: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Graphical Widgets

• The graphical components with which users interact– JButton– JLabel– JScrollBar

Page 79: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

SwingContainers

• Two types– Top level (includes JFrame, Window)– Lower level (includes JPanel, JScrollPane)

Page 80: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Distinguish between the AWT and Swing Identify the general organization of the Swing

class structure Define and use Swing widgets and containers

Page 81: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 12:Layout Managers

Page 82: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Define a layout manager• Set a layout manager for a Container• Effectively use FlowLayout, GridLayout, BorderLayout and BoxLayout

• Nest containers and layout managers to form more complex GUI layouts

• Separate a complex design into its component containers and layout managers

Page 83: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is a Layout Manager?

• FlowLayout• GridLayout• BorderLayout• BoxLayout• CardLayout

• GridBagLayout• GridLayout• OverlayLayout• ScrollPaneLayout• ViewportLayout

Page 84: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

FlowLayout

• Centers components on each line in a flowing manner

Page 85: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

GridLayout

• Adds components in a gridlike format

Page 86: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

BorderLayout

CENTER

EAST

WEST

SOUTH

NORTH

Page 87: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

BoxLayout

• Allows either a horizontal or a vertical layout of components

Page 88: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

CombiningLayouts

Button 1

Button 2

Button 3

Button 4

Page 89: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Define a layout manager Set a layout manager for a Container Effectively use FlowLayout, GridLayout, BorderLayout and BoxLayout

Nest containers and layout managers to form more complex GUI layouts

Separate a complex design into its component containers and layout managers

Page 90: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 13:Graphics in Java

Page 91: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Identify the AWT class structure for graphics• Gain access to a container’s graphic context

by overriding the paint(Graphics g) method

• Use methods of the Graphics class via the graphics context

• Effectively use the Color and Font classes

Page 92: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Graphics Class

• The graphics context is used to access the functionality of the Graphics class (an abstract class)

Page 93: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Graphics Class Methods

• draw3DRect()• drawArc()• drawLine()• drawOval()• drawPolygon()• drawPolyline()• drawRect()

• drawRoundRect()• fill3DRect()• fillArc()• fillOval()• fillPolygon()• fillRect()• fillRoundRect()

Page 94: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Other Classes

• Color setColor()• Font setFont()

– FontMetrics

Page 95: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Identify the AWT class structure for graphics

Gain access to a container’s graphic context by overriding the paint(Graphics g) method

Use methods of the Graphics class via the graphics context

Effectively use the Color and Font classes

Page 96: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 14:The Event

Delegation Model

Page 97: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Describe the event delegation model• Create listener classes that can respond to

events• Register listener classes with their Component sources

• Capture events and deal with them in meaningful ways

Page 98: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is an Event?

• What caused the event?• What was the exact nature of the event?• Is additional information available about

the event?

Page 99: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

JDK 1.0 Event Handling

• Tightly coupled with the AWT• Inefficient• General• Unruly• Code and event handling could not be

separated

Page 100: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

SDK 1.2 Event Handling

• Generating the event object• Sending the event object to the listener• Preparing the listener to receive the event• Example: Creating a closeable JFrame• JFrame convenience methods for event

handling• Example: Event handling and callbacks

Page 101: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Describe the event delegation model Create listener classes that can respond to

events Register listener classes with their Component sources

Capture events and deal with them in meaningful ways

Page 102: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 15:Inner Classes

Page 103: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Define an inner class• Recognize the advantages of inner classes

over package-level classes in relation to event handling

• Design and implement inner classes for event handling

Page 104: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is an Inner Class?

• A class defined within other classes• Introduced with the SDK 1.2

Page 105: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Inner Classes for Event Handling

• Member inner classes• Anonymous inner classes

Page 106: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Define an inner class Recognize the advantages of inner classes

over package-level classes in relation to event handling

Design and implement inner classes for event handling

Page 107: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 16:Java Applets

Page 108: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Compare and contrast Java applets and applications

• Implement the life cycle of an applet through its inherited methods

• Embed an applet into an HTML document• Pass parameters from an HTML document to

its contained applet• Identify applet security restrictions• Convert an applet into an application

Page 109: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

JApplet Class Hierarchy

Object

Component

Applet

Panel

Container

JApplet

Page 110: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Applets and Web Browsers

• Applets do not have a main() method• Applets have a life cycle• Applets are started from HTML pages, and

receive information from HTML pages• Because they are program code, applets

should not be trusted

Page 111: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Converting an Application into an Applet

• Use init() method to perform instantiation

• Instantiating the applet is unnecessary• Applets must be derived from JApplet• Applet size should be set within the HTML

document (the Web browser makes it visible)

• Applets must be declared public

Page 112: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Converting an Applet into an Application

• By adding an instance of the JApplet to the JFrame and calling its init() and start() methods, the conversion from JApplet to stand-alone JFrame readily takes place

Page 113: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Compare and contrast Java applets and applications

Implement the life cycle of an applet through its inherited methods

Embed an applet into an HTML document Pass parameters from an HTML document to

its contained applet Identify applet security restrictions Convert an applet into an application

Page 114: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 17:Exceptions

Page 115: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives• Differentiate between errors and

exceptions• Differentiate between runtime exceptions

and explicit exceptions• Propagate an exception using the throws

statement• Handle an exception using the try/catch

statement• Create and use a user-defined exception

Page 116: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is an Exception?

• Exceptions– Runtime exceptions (unchecked)– Other exceptions (checked)

Page 117: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Exception Class Hierarchy

Page 118: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

HandlingExceptions

• Ignore the Exception• Handle the Exception with a try/catch

statement• Throw the Exception to the calling method• Handle the Exception and rethrow it to the

calling method

Page 119: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Creating User-Defined Exceptions

• Creating the exception• Throwing the exception• Exception handling tips

Page 120: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Exception Handling Tips

• Use simple tests instead of try/catch• Choose user-defined exceptions wisely• Use one try/catch block for multiple

exceptions• Do something meaningful with caught

exceptions

Page 121: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Differentiate between errors and exceptions Differentiate between runtime exceptions and

explicit exceptions Propagate an exception using the throws

statement Handle an exception using the try/catch

statement Create and use a user-defined exception

Page 122: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 18:Creating Threads

and Thread Methods

Page 123: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Define threads• Create and instantiate threads using two

different techniques• Control single-thread flow• Define the four thread states and their

relationships to thread methods

Page 124: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Are Threads?

• Multitasking• Multiprocessing• Multithreading

Page 125: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

How Operating Systems Handle Multitasking

• Pre-emptive multitasking• Cooperative multitasking

Page 126: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Types of Threads in Java

• Daemon thread• User thread

– Main thread– Other user threads

Page 127: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

The Main Thread

main thread

user thread user thread

user thread user thread

Page 128: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Creating Threads

• Subclassing the Thread class• Implementing the Runnable interface• Which technique?

Page 129: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Define threads Create and instantiate threads using two

different techniques Control single-thread flow Define the four thread states and their

relationships to thread methods

Page 130: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 19:Thread

Synchronization

Page 131: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Define synchronization in relation to object monitors

• Control thread racing using thread synchronization

• Convert non-atomic to atomic processes to avoid thread racing

• Use sophisticated methods for controlling threads

• Stop, suspend and resume threads• Explain thread deadlock

Page 132: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is Thread Synchronization?

• Controlling threads in a predictable manner

Page 133: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Thread Racing

• Two threads trying to access the same data

Page 134: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Synchronized and the Object Monitor

• synchronized keyword• Threads waiting in a queue to obtain an

objects monitor• All synchronized methods of an object use a

single monitor

Page 135: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Thread Race Condition

• Competing for resources• Synchronizing the methods• Atomic processes

Page 136: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Sophisticated Thread Synchronization

• Consumer/producer scenario

Page 137: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Stopping, Suspending and Resuming Threads

• Stopping a thread• Suspending a thread• Resuming a thread

Page 138: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Define synchronization in relation to object monitors

Control thread racing using thread synchronization

Convert non-atomic to atomic processes to avoid thread racing

Use sophisticated methods for controlling threads

Stop, suspend and resume threads Explain thread deadlock

Page 139: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 20:Streams and Serialization

Page 140: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Define a stream• Differentiate between byte streams and

character streams• Recognize the abstraction of byte streams

through the InputStream and OutputStream classes

• Recognize the abstraction of character streams through the Reader and Writer classes

• Create and use file objects

Page 141: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives (cont’d)

• Use System.in and System.out to perform stream operations

• Nest streams using wrapper classes to enhance basic stream behavior

• Perform file I/O• Define object serialization• Use serialization to save an object to a file• Explain the transient keyword

Page 142: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is a Stream?

• A path of information from a source to a destination

Page 143: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

InputStream, OutputStream, Reader and Writer

• JDK 1.0 (bytes)– InputStream– OutputStream

• JDK 1.1 (characters)– Reader– Writer

Page 144: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Files

• Instantiating a file object• Working with a file object

– Methods of File– Directories– FileDialog/FileChooser

Page 145: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Stream Classes of java.io.*

• System.in and System.out• Reading bytes from System.in• Converting a byte stream into a character

stream• Wrapper streams• File I/O

Page 146: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Serialization

• The process of object serialization– Marking an object for serialization– Writing the object to file– Reading the serialized object from a file

• Transient variables and security

Page 147: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Define a stream Differentiate between byte streams and

character streams Recognize the abstraction of byte streams

through the InputStream and OutputStream classes

Recognize the abstraction of character streams through the Reader and Writer classes

Create and use file objects

Page 148: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary (cont’d)

Use System.in and System.out to perform stream operations

Nest streams using wrapper classes to enhance basic stream behavior

Perform file I/O Define object serialization Use serialization to save an object to a file Explain the transient keyword

Page 149: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Copyright © 2002 ProsoftTraining. All rights reserved.

Lesson 21:Networking in Java

Page 150: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Objectives

• Define networking, IP addresses, the Domain Name System, and ports

• Discuss the socket model and socket-to-socket communication

• Explain the client/server model• Write a single-threaded client/server echo

system• Write a multithreaded client/server echo

system

Page 151: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

What Is Networking?

• Computers communicating across distances• Java networking

– TCP/IP– UDP

Page 152: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Connecting Computers Across the Internet

• IP addresses– Dotted quad– 32 bits– IPv6

• DNS• Sockets• Well-known ports

Page 153: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Well-Known Ports and Their Protocols

Port Protocol

21 FTP

23 Telnet

25 E-mail

79 Finger

80 HTTP

119 NNTP/Usenet

Page 154: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Networking Classes of java.net.*

• Common networking classes– InetAddress– Socket– ServerSocket

Page 155: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

The Java Client/Server Model

• Server– Delivers information– ServerSocket object– accept method returns a socket

• Client– Requests information from the server– Connects to server with socket

Page 156: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Building the EchoServer

• The client– Step 1: getConnection– Step 2: sendMessage– Step 3: receiveMessage

• The server– Step 1: getConnection– Step 2: receiveMessage– Step 3: sendMessage

Page 157: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

MultithreadingYour Client/Server Model

• Modifications to the server• The Connection class• The Client class• Running the threaded client/server example

Page 158: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Summary

Define networking, IP addresses, the Domain Name System, and ports

Discuss the socket model and socket-to-socket communication

Explain the client/server model Write a single-threaded client/server echo

system Write a multithreaded client/server echo

system

Page 159: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Java Programming Fundamentals

Java Runtime Environment Data Types, Variables and Operators Control Statements Methods Arrays Classes and Objects Inheritance Constructors

Page 160: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Java Programming Fundamentals

Interfaces and Abstract Classes Packages and Access Modifiers Swing Components Layout Managers Graphics in Java The Event Delegation Model Inner Classes

Page 161: Copyright © 2002 ProsoftTraining. All rights reserved. Java Programming Fundamentals

Java Applets Exceptions Creating Threads and Thread Methods Thread Synchronization Streams and Serialization Networking in Java

Java Programming Fundamentals