jedi - introduction.to.java.programming.vol.2

Download JEDI - Introduction.to.Java.programming.vol.2

Post on 17-Nov-2014

2.475 views

Category:

Documents

6 download

Embed Size (px)

TRANSCRIPT

J.E.D.I.

Introduction to Programming II

Version 2.0 May 2006

Introduction to Programming II

Page 1

J.E.D.I.

Author Rebecca Ong Team Joyce Avestro Florence Balagtas Rommel Feria Rebecca Ong John Paul Petines Sun Microsystems Sun Philippines

Requirements For the Laboratory ExercisesMinimum Hardware Configuration

Microsoft Windows operating systems:

Processor: 500 MHz Intel Pentium III workstation or equivalent Memory: 384 megabytes Disk space: 125 megabytes of free disk space Processor: 500 MHz Ultra 60, SunBlade 150, or equivalent workstation Memory: 512 megabytes Disk space: 150 megabytes of free disk space Processor: AMD Opteron 100 series Sun Ultra 20 workstation or equivalent Memory: 512 megabytes Disk space: 150 megabytes of free disk space Processor: 500 MHz Intel Pentium III workstation or equivalent Memory: 384 megabytes Disk space: 125 megabytes of free disk space Processor: PowerPC G4 Memory: 512 megabytes

SolarisTM operating system (SPARC version):

SolarisTM operating system (x86 version):

Linux operating system:

Macintosh OS X operating system:

Disk space: 125 megabytes of free disk space Recommended Hardware Configuration

Microsoft Windows operating systems: Processor: 780 MHz Intel Pentium III workstation or equivalent Memory: 512 megabytes Disk space: 125 megabytes of free disk space Processor: UltraSPARC IIIi 1.5 GHz SunBlade 1500 workstation or equivalent Memory: 1 gigabyte Disk space: 150 megabytes of free disk space Processor: AMD Opteron 100 series Sun Ultra 20 workstation or equivalent Memory: 1 gigabyte Disk space: 150 megabytes of free disk space Processor: 800 MHz Intel Pentium III workstation or equivalent Memory: 512 megabytes Disk space: 125 megabytes of free disk space Processor: PowerPC G5 Memory: 1 gigabyte

SolarisTM operating system (SPARC version):

SolarisTM operating system (x86 version):

Linux operating system:

Macintosh OS X operating system:

Disk space: 125 megabytes of free disk space Operating System NetBeans IDE runs on operating systems that support the JavaTM VM. Below is a list of platforms that NetBeans IDE has been tested on.

Mac OS X 10.4 Software NetBeans IDE runs on the J2SE JDK 5.0 (JavaTM 2 JDK, Standard Edition), which consists of the Java Runtime Environment plus developers tools for compiling, debugging, and running applications written in the JavaTM language. NetBeans IDE 5.0 has also runs on J2SE SDK version 1.4.2, but it has only been tested on JDK 5.0. For more information, please visit: http://www.netbeans.org/community/releases/50/relnotes.html

Microsoft Windows XP Professional SP2 Solaris operating system (SPARC and x86 Platform Editions) versions 8, 9, and 10 Red Hat Fedora Core 3

Table of Contents1 Review of Basic Concepts in Java........................................................................14 1.1 Objectives................................................................................................14 1.2 Object-Oriented Concepts...........................................................................14 1.2.1 Object-Oriented Design....................................................................... 14

Introduction to Programming II

Page 2

J.E.D.I.

1.2.2 Class.................................................................................................14 1.2.3 Object...............................................................................................15 1.2.4 Attribute............................................................................................15 1.2.5 Method..............................................................................................15 1.2.6 Constructor........................................................................................15 1.2.7 Package ............................................................................................15 1.2.8 Encapsulation.....................................................................................15 1.2.9 Abstraction........................................................................................15 1.2.10 Inheritance....................................................................................... 16 1.2.11 Polymorphism...................................................................................16 1.2.12 Interface..........................................................................................16 1.3 Java Program Structure..............................................................................17 1.3.1 Declaring Java Classes.........................................................................17 1.3.2 Declaring Attributes............................................................................ 17 1.3.3 Declaring Methods.............................................................................. 18 1.3.4 Declaring a Constructor.......................................................................18 1.3.5 Instantiating a Class............................................................................ 19 1.3.6 Accessing Object Members...................................................................19 1.3.7 Packages...........................................................................................19 1.3.8 The Access Modifiers........................................................................... 20 1.3.9 Encapsulation.....................................................................................20 1.3.10 Inheritance....................................................................................... 21 1.3.11 Overriding Methods...........................................................................21 1.3.12 Abstract Classes and Methods.............................................................22 1.3.13 Interface..........................................................................................23 1.3.14 The this Keyword..............................................................................24 1.3.15 The super Keyword............................................................................25 1.3.16 The static Keyword............................................................................26 1.3.17 The final Keyword.............................................................................27 1.3.18 Inner Classes....................................................................................28 1.4 Exercises..................................................................................................29 1.4.1 Multiplication Table.............................................................................29 1.4.2 Greatest Common Factor (GCF)............................................................29 1.4.3 Shapes..............................................................................................29 1.4.4 Animals.............................................................................................29 2 Exceptions and Assertions.................................................................................30 2.1 Objectives................................................................................................30 2.2 What are Exceptions?.................................................................................30 2.2.1 Introduction.......................................................................................30 2.2.2 The Error and Exception Classes........................................................... 30 2.2.3 An Example........................................................................................31 2.3 Catching Exceptions...................................................................................31 2.3.1 The try-catch Statements.....................................................................31 2.3.2 The finally Keyword.............................................................................34 2.4 Throwing Exceptions..................................................................................36 2.4.1 The throw Keyword.............................................................................36 2.4.2 The throws Keyword............................................................................36 2.5 Exception Categories..................................................................................37 2.5.1 Exception Classes and Hierarchy...........................................................37 2.5.2 Checked and Unchecked Exceptions...................................................... 38 2.5.3 User-Defined Exceptions......................................................................38 2.6 Assertions................................................................................................. 39 2.6.1 What are Assertions?...........................................................................39 2.6.2 Enabling or Disabling Assertions........................................................... 39Introduction to Programming II Page 3

J.E.D.I.

3

4

5

6

2.6.3 Assert Syntax.....................................................................................43 2.7 Exercises..................................................................................................45 2.7.1 Hexadecimal to Decimal.......................................................................45 2.7.2 Printing a Diamond.............................................................................45 Advanced Programming Techniques....................................................................46 3.1 Objectives................................................................................................46 3.2 Recursion.................................................................................................46 3.2.1 What is R