Vous êtes sur la page 1sur 17

Page 1 of 17

Chapter 1. Basics of Java Programming


Supplementary Objectives

" !

1.1 Introduction
# !
$
%%&'
! ( ! ! !
" ) #

! " # (
! !
# "
!

1.2 Classes
% " " " ( *
!
%%&
!

* *
!
* ( !
! *
! " ! !
# " ! +
!

* "
! !
! ! , $ ' # "

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 2 of 17

! ! -
!

* ( " " !
# #"
#. . /
$
./ ' CharStack + 00 "
" + 00

Declaring Members: Fields and Methods

1( 00 " CharStack + 00
! !
#

* CharStack
"

stackArray " #$ '

topOfStack " #$ (
'

CharStack ! #

push() #

pop() ! #

peek() #

isEmpty() " #

isFull() " #

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 3 of 17

# " $
2' 3
* " ( "
4" ! (

// Source Filename: CharStack.java


public class CharStack { // Class name
// Class Declarations:

// (1) Fields:
private char[] stackArray; // The array implementing the stack.
private int topOfStack; // The top of the stack.

// (2) Constructor:
public CharStack(int n) { stackArray = new char[n]; topOfStack = -1; }

// (3) Methods:
public void push(char element) { stackArray[++topOfStack] = element; }
public char pop() { return stackArray[topOfStack--]; }
public char peek() { return stackArray[topOfStack]; }
public boolean isEmpty() { return topOfStack < 0; }
public boolean isFull() { return topOfStack == stackArray.length - 1; }
}

1.3 Objects
Class Instantiation
*

*
! $
' ! ! "

5 !

! !

// Declaration of two reference variables that will denote


// two distinct objects, namely two stacks of characters, respectively.
CharStack stack1, stack2;

! ! new "

// Create two distinct stacks of chars.


stack1 = new CharStack(10); // Stack length: 10 chars

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 4 of 17

stack2 = new CharStack(5); // Stack length: 5 chars

new " CharStack


!

1 ) "
" # stack1 stack2 " ! " stackArray
topOfStack

new 6
" " CharStack
new
! ! 6
topOfStack

CharStack stack1 = new CharStack(10),


stack2 = new CharStack(5);

+ 02 " ./ !
+ 02 " "
! ( " ':'
! + 02 3
! ! "
+ 02 " CharStack ( CharStack
"

Object References

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 5 of 17

* ! !
! " ! * !
! !

// Create two distinct stacks of chars.


CharStack stackA = new CharStack(12); // Stack length: 12 chars
CharStack stackB = new CharStack(6); // Stack length: 6 chars

stackB = stackA; // (1) aliases after assignment


// Stack previously referenced by stackB can now be garbage collected.

" # ! $
0'
+ 07 * $
0' ! stackA stackB "
# + 07 8 ! stackA stackB
9 #
" ! stackB :9

; ! #

! "

1.4 Instance Members


1 " ! "
! !
" ! ! ! !
!

" "

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 6 of 17

! " !
" 3
3 0<

Invoking Methods
% (
! !# !
( '.' *
! !#
!# !
# ! !

CharStack stack = new CharStack(5); // Create a stack


stack.push('J'); // (1) Character 'J' pushed
char c = stack.pop(); // (2) One character popped and returned: 'J'
stack.printStackElements(); // (3) Compile time error: No such method in CharStack

! !# ! stack
$
0' # $
2'
# push() pop() CharStack
push() ! pop()
!# printStackElements() #
CharStack

'.' "
! CharStack !
private

stack.topOfStack++; // Compile time error: topOfStack is a private field.

1.5 Static Members

* ( " " # # "


! 5 !
# ! 1
" ! " 9 :
static 3
* ! 6 "
3 !
3 ! ! # "
# " static

+ 0= " CharStack "


" CharStack !

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 7 of 17

1( 02 counter ! $
0' "
6 ! 0" 1 CharStack
$2' ( (
getInstanceCount() $
7'
! "

# $% & $

+ 0< " CharStack "


! 00 !

// Source Filename CharStack.java


public class CharStack {
// Instance variables
private char[] stackArray; // The array implementing the stack.
private int topOfStack; // The top of the stack.

// Static variable
private static int counter; // (1)

// Constructor now increments the counter for each object created.


public CharStack(int capacity) { // (2)
stackArray = new char[capacity];
topOfStack = -1;
counter++;
}

// Instance methods
public void push(char element) { stackArray[++topOfStack] = element; }
public char pop() { return stackArray[topOfStack--]; }
public char peek() { return stackArray[topOfStack]; }
public boolean isEmpty() { return topOfStack < 0; }
public boolean isFull() { return topOfStack == stackArray.length - 1; }

// Static method (3)


public static int getInstanceCount() { return counter; }

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 8 of 17

'

, " !#
getInstanceCount() CharStack

int count = CharStack.getInstanceCount(); // Class name to invoke static method

3 !

CharStack stack1 = new CharStack(10);


int count1 = stack1.getInstanceCount(); // Reference invokes static method

3 !

( ( )
!
/ !#
* " " '
> * non-static field

* %
/
3 !
/ !#
3 * "
> * static field class variable

3 * " *
/ class method

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 9 of 17

1.6 Inheritance
" " (
# ( Vehicle Car
! Vehicle ! ? #
Vehicle Motor Axle
GearBox " # !

( #
" # ! CharStack
;!
# # ! CharStack
" + 0@ PrintableCharStack
CharStack CharStack #
" PrintableCharStack #

* +% ,

! ! " ( ) extends
*
" PrintableCharStack

class PrintableCharStack extends CharStack { // (1)


// Instance method
public void printStackElements() { // (2)
// ... implementation of the method...
}

// The constructor calls the constructor of the superclass explicitly.


public PrintableCharStack(int capacity) { super(capacity); } // (3)
}

PrintableCharStack ( CharStack $
0'
printStackElements() PrintableCharStack )
stackArray CharStack 4 " !

CharStack 1( 07 ! CharStack
" printStackElements()

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 10 of 17

" $
2' PrintableCharStack $
7'
CharStack 6 #

! $

// Source Filename: CharStack.java


public class CharStack {
// Instance variables
protected char[] stackArray; // The array that implements the stack.
protected int topOfStack; // The top of the stack.

// The rest of the definition is the same as in Example 1.2.


}

// Source Filename: PrintableCharStack.java


public class PrintableCharStack extends CharStack { // (1)
// Instance method
public void printStackElements() { // (2)
for (int i = 0; i <= topOfStack; i++)
System.out.print(stackArray[i]); // print each char on terminal
System.out.println();
}
// Constructor calls the constructor of the superclass explicitly.
PrintableCharStack(int capacity) { super(capacity); } // (3)
}

% PrintableCharStack " # CharStack


" !

PrintableCharStack aPrintableCharStack = new PrintableCharStack(3);


aPrintableCharStack.push('H');
aPrintableCharStack.push('i');
aPrintableCharStack.push('!');
aPrintableCharStack.printStackElements(); // Prints "Hi!" on the terminal

1.7 Aggregation
9 " (

!
( ! !
1 CharStack
1 # ! int (!
# CharStack "
! "
! !
./ + 0A
" CharStack " ! char "

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 11 of 17

- "

1.8 Tenets of Java


, !

" # ! ! ! !

% !

% ! ? !

Review Questions

9 :

3 "

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 12 of 17

. * !

* #

9 :

3 "

* "

* 6

. *

* !

! 9 :

public class Counter { // (1)


int current, step;

public Counter(int startValue, int stepValue) { // (2)


set(startValue);
setStepValue(stepValue);
}

public int get() { return current; } // (3)

public void set(int value) { current = value; } // (4)

public void setStepValue(int stepValue) { step = stepValue; } // (5)


}

3 "

, # " $
0'

, # " $
2'

, # " $
7'

. , # " $
='

, # " $
<'

# ;! Thing " " !

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 13 of 17

" :

Thing item, stuff;


item = new Thing();
Thing entity = new Thing();

3 " "

"

. % !

" !

' 9 :

3 "

* "

* !

. * "

* "

* 4" !:

3 "

! -

. -

- ;! " " :

class A {
int value1;
}

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 14 of 17

class B extends A {
int value2;
}

3 " "

, A ( B

, B A

, A B

. , B A

% A ! value2

% B ! value1

1.9 Java Programs


* ! " -
( * ! ! 2
35B public
" .java ( 1

" .class ( *
! 235B !
( 00C3 ! *

1.10 Sample Java Application


* "
( ! !
main main() (

Essential Elements of a Java Application


1( 0= ( " CharStack !

#" "

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 15 of 17

// Source Filename: Client.java


public class Client {

public static void main(String[] args) {

// Create a stack
CharStack stack = new CharStack(40);

// Create a string to push on the stack


String str = "!no tis ot nuf era skcatS";
int length = str.length();
System.out.println("Original string: " + str);

// Push the string char by char onto the stack


for (int i = 0; i<length; i++) {
stack.push(str.charAt(i));
}

System.out.print("Reversed string: ");


// Pop and print each char from the stack
while (!stack.isEmpty()) {
System.out.print(stack.pop());
}
System.out.println();
}
}

// Source Filename: CharStack.java


public class CharStack {
// Same as in Example 1.2.
}

Original string: !no tis ot nuf era skcatS


Reversed string: Stacks are fun to sit on!

Client " main main()


!# ! ! > /
$>/' main() "

public static void main(String[] args) {


// ...
}

main() public # "


static # " void
! String[] args
main() "

Compiling and Running an Application


! ! javac " ! 2
35B

Client.java Client

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 16 of 17

! "

>javac Client.java

Client.class ! Client
Client CharStack CharStack.class (
" CharStack.java

, ( ! java " ! 2
35B 1( 0= ! "

>java Client

D ( main()
1( 0= " (
main()

Review Questions

/ 9 ! 235B "
SmallProg.java:

public class SmallProg {


public static void main(String[] args) { System.out.println("Good luck!"); }
}

3 "

java SmallProg

javac SmallProg

java SmallProg.java

. javac SmallProg.java

java SmallProg main

0 9 ! 235B ( main()
SmallProg:

3 "

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006


Page 17 of 17

java SmallProg

javac SmallProg

java SmallProg.java

. java SmallProg.class

java SmallProg.main()

Chapter Summary

" "

%%& " !

Programming Exercises

/ 1( 0= PrintableCharStack
CharStack . 6 printStackElements()
PrintableCharStack " ! "
1( 0=:

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm 6/16/2006

Vous aimerez peut-être aussi