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
!
!
-
! *
(
" "
!
#
#" #.
$ ./ '
"
.
/ 00 "
+ 00
CharStack +
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; private int topOfStack;
// The array implementing the stack. // The top of the stack.
// (2) Constructor: public CharStack(int n) { stackArray = new char[n]; topOfStack = -1; } // (3) public public public public public
Methods: void push(char element) char pop() char peek() boolean isEmpty() boolean isFull()
{ { { { {
stackArray[++topOfStack] = element; } return stackArray[topOfStack--]; } return stackArray[topOfStack]; } return topOfStack < 0; } 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
) " topOfStack
" #
stack1
stack2 "
!
" stackArray
6
new " CharStack
" new ! ! topOfStack
6
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. "
#
! +
07 * #
+
"
!
$ 0' 07 8
! ! 9
$ 0' stackA stackA
stackB " stackB #
:9
stackB
;
!
#
! "
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 ! $ 0' #
!#
stack ! $ 2' CharStack
# push()
pop()
pop() ! printStackElements() CharStack
push() !#
#
"
'.' !
!
CharStack
private stack.topOfStack++;
// Compile time error: topOfStack is a private field.
1.5 Static Members *
"
!
( !
# "
+
0=
"
"
# !
! 9 static 3 * !
3 3
" 5
#
"
1 :
!
!
6
"
!
# " # "
"
CharStack "
file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
static
CharStack
!
6/16/2006
Page 7 of 17
1(
02
counter 0" !
6
!
!
$ 2' ( getInstanceCount() "
#
+
0<
$% &
"
" CharStack (
$ 7'
$
"
CharStack !
00
!
$ 0' 1
$ // 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) public char pop() public char peek() public boolean isEmpty() public boolean isFull()
{ { { { {
stackArray[++topOfStack] = element; } return stackArray[topOfStack--]; } return stackArray[topOfStack]; } return topOfStack < 0; } return topOfStack == stackArray.length - 1; }
// Static method public static int getInstanceCount() { return counter; }
file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
(3)
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 >
*
3 /
* " class method
" *
static field
file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
class variable *
6/16/2006
Page 9 of 17
1.6 Inheritance "
" # !
GearBox "
Vehicle Vehicle !
#
( " # " + CharStack
#
!
?
0@
#
# #
! ;!
# #
+%
"
(
CharStack
CharStack
*
!
Car Motor Axle
! PrintableCharStack CharStack PrintableCharStack
"
!
( Vehicle
(
,
)
extends
* "
PrintableCharStack
class PrintableCharStack extends CharStack { // Instance method public void printStackElements() { // ... implementation of the method... }
// (1) // (2)
// The constructor calls the constructor of the superclass explicitly. public PrintableCharStack(int capacity) { super(capacity); } // (3) } PrintableCharStack printStackElements() stackArray
(
CharStack PrintableCharStack CharStack 4 " ! CharStack
"
1(
07
$ 0' )
CharStack ! printStackElements()
file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
6/16/2006
Page 10 of 17
"
$ 2' CharStack
!
$ 7'
PrintableCharStack 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
" !
! ./
"
(! "
CharStack
"
!
file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
+ char
0A "
6/16/2006
Page 11 of 17
-
"
1.8 Tenets of Java ,
! " #
!
!
!
!
8 %
!
%
!
?
!
5
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 { int current, step;
// (1)
public Counter(int startValue, int stepValue) { set(startValue); setStepValue(stepValue); }
// (2)
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
,
A
. ,
B
B A B A
%
A
!
value2
%
B
!
value1
1.9 Java Programs * ! ( 35B
" *
! 2
! " " .class ! 235B !
(
00C3
public .java
(
(
1
*
! *
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
Client !# $>/'
main
main()
!
! >
/
"
main()
public static void main(String[] args) { // ... } main() static !
# "
public # " String[] args main()
void
"
Compiling and Running an Application ! 35B
!
Client.java
javac "
! 2
Client
file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
6/16/2006
Page 16 of 17
!
"
>javac Client.java Client.class CharStack
Client " , 35B 1(
! CharStack.class CharStack.java
( 0=
! !
Client (
java "
! 2
"
>java Client D 1(
0=
( "
main() (
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( CharStack . 6 PrintableCharStack 1( 0=:
PrintableCharStack 0= printStackElements() " ! "
file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
6/16/2006