khalid mughal chapter 1
DESCRIPTION
Programmer’s Guide to Java™ Certification, A: A ComprehensivePrimer, Second Edition written by Khalid A. Mughal , Rolf W. RasmussenTRANSCRIPT
�
�
�
���������� �
Chapter 1. Basics of Java Programming
�
Supplementary Objectives
� �� ������� ��������� ��������������������� ���������� ������ ����������������
�������������� ������������������������� ��������������������� ������������� �����
� ���� ���� �������� ���������� ������� �!��������� �����
� ��������"� �������������������� �!��������� �����
���������� �
���������� �
1.1 Introduction
�����������#������� �������� �� �!���������������� ����� ������ ��������� �� � �������� ���� ���
������ ��������������������� ���������� ������ ���������������$%%&'����� �������� ���� ���
������������������!����������� ����� ������ ���� ��������(���� �!����!������������� ����!���������
��������� �������"������������������������)��� ����� ������� ������#��
�!�������� �� ���"�� �������������������� �#���������(��� �������������� ����������� �������
���!�� ������������������� ������� �!��������� ���������� ����������� �������� ������������������ �� �
���� ������#��������� ������� ������ ����"��������������������������������������������������
��������������� �������� ���� ���������������������������������������� �!��������� ��������
�� �������
���������� �
���������� �
1.2 Classes
%������ ����������� ���"�������"�����"���������������(� ������������������*����� ��� ����
���� ��� �������� ���������� ������������!���������������� � �� ��������� �� ��� ������� ��������� ���
���������������%%&����������������� ��� ����������������������������� ��������������� ���� ����
�����!��������������� ������� ���� ��� ������
*���������� ������� �������������� ��������� ��������������� ��������� �������������� ���*�������
������������� ��� ���������������� ��������� ������������!��������� �������� ���������� ���� ���
��� ��� �����*������� ��(���� �� ��������� ������������!����������������� ������������������� �������
�������� ����������������������������������������������������������� �������� �!���*� ���������������
������ ���������!��������"���������� ������!����� �� ��������� ������� ������������� �����������!�����
����������� ���������������������#��"������������������������������������������������� �!���+������
������ ���������������������� �������������� �!��������������������
*������� �� ���� ��� �������������� "���� ��������������� ��������������� �� ���������
���!���������� ������� ���������� ��� �����������������!���������� ����������� � ����������������
��������!������������!�������� ����������,���� ��$������� ��������� �'����������� ��#��"� ���
Page 1 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
��� ��� ������������ �������� �� ���������� � �������������� ���!���� ������!������ �������� -��
���!������
*������(�������"��"������������ ��������� �!������������������� �� �������� ������ ��� ���������
� ��#� �� ������������������������ ���������� ��#�"�������������������������� ���� ��� ���� ���
������ ����������������� �������� �� ��� ��������� ���� ���� ��#��.�����.�������/�����������������
$./�'��� � ��������������������Char St ack��������������������� ������+������0�0��"������������ ������ ��� ������ ��������������� �����������������"�����+������0�0���
������������� ��������������������
�
Declaring Members: Fields and Methods
1(������0�0����"�� ��������� ������� ���������Char St ack������ ������+������0�0��� ���� �� ������� ������� �� �� ��������� ���� ���������������������� ������� �!��������� ������ �!���������� � �������
� ��#���
*������������� ���������� ������������������������������� ��������� ����������� ���������Char St ack��� ����� "����������
� st ackAr r ay��"����������������� ������� ��������� ����� ���� ��#�$��� ��������������� ���'�
� t opOf St ack��"��������� ��� ��� ��������� ���� ���� ��#�$����������(���� ������ ������� ���� �������� ��������'�
����������Char St ack�������!���� ����� �� ��������� � �������� ��������� ����������� ��#��
� push( ) ���������������� ������ �� ���� ��#�
� pop( ) �����!��������� ����� ��� ��������� ���� ���� ��#�
� peek( ) ��� ����� ��� ��������� ���� ���� ��#����������� ����
� i sEmpt y( ) ��� ��������"�� ���� ���� ��#������� ��
� i sFul l ( ) ��� ��������"�� ���� ���� ��#���������
Page 2 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
�
���������������� ����������������� ������#��������� ����"� �� ���������������� ����������$2'��3����
������� �����������������������������*��"������������������� ��� �������(��� ���"������������ ����
���� �������� ����������4�"�!���� ����������� � ������ �������� ����(������������ ������ �� �����
��������� �������������
������������������������������������������������
/ / Sour ce Fi l ename: Char St ack. j ava publ i c c l ass Char St ack { / / Cl ass name / / Cl ass Decl ar at i ons: / / ( 1) Fi el ds: pr i vat e char [ ] s t ackAr r ay; / / The ar r ay i mpl ement i ng t he st ack. pr i vat e i nt t opOf St ack; / / The t op of t he st ack. / / ( 2) Const r uct or : publ i c Char St ack( i nt n) { s t ackAr r ay = new char [ n] ; t opOf St ack = - 1; } / / ( 3) Met hods: publ i c voi d push( char el ement ) { s t ackAr r ay[ ++t opOf St ack] = el ement ; } publ i c char pop( ) { r et ur n st ackAr r ay[ t opOf St ack- - ] ; } publ i c char peek( ) { r et ur n st ackAr r ay[ t opOf St ack] ; } publ i c bool ean i sEmpt y( ) { r et ur n t opOf St ack < 0; } publ i c bool ean i sFul l ( ) { r et ur n t opOf St ack == st ackAr r ay. l engt h - 1; } }
���������� �
���������� �
1.3 Objects
Class Instantiation
������������������� ��������� �������������������������������������*������������������ ����������
���������������� �������� ��� ��������� ���������������������� ���������������� ����� �������� ���
��� ��� ���� �� � ����������������� ���*������� ���� �������� ����������� ������������������
������������ �!�������� �������������� ��� ���������������� �������$�������������� ������������
������������ ������'��������������������� ��������� ������������!��!��� ��������"����� �����
� 5������ ���������!�������� ��� ���� �������� ������������
�������!��!����������������� ����������������� ������������ �������� ��� ���� ������������� ��
�������� ��
/ / Decl ar at i on of t wo r ef er ence var i abl es t hat wi l l denot e / / t wo di st i nct obj ect s, namel y t wo st acks of char act er s, r espect i vel y. Char St ack st ack1, st ack2;
� ,��� ������������ ��
�������!��!��������� ���new������ ������������� ����"� ��������� �������� ��� ���� ������ �������� �������� ����������
/ / Cr eat e t wo di st i nct st acks of char s. st ack1 = new Char St ack( 10) ; / / St ack l engt h: 10 char s
Page 3 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
st ack2 = new Char St ack( 5) ; / / St ack l engt h: 5 char s
����new������ ����� ����������������� ������"���� �������� ���Char St ack��������������������������������������� ��������������!����������� ������������ ���������
1��������� ����������)������� � ����������� ���"���������� ���������������������� ���������
������ ��������� "��� ��#������� ������st ack1�����st ack2��"������!�� ������"��st ackAr r ay�����t opOf St ack���������
��������������� ������� ��� ����������� ������� ��������� ���new������ ������ ����� ����6�� �����"������� �������� ����� ������� ������������������������"�Char St ack���� ��������� ��������� ���new������ ���� ������� ��� ������� ��������������������� ������������� ����� ���������������!������ ���!�������� ���������� � �� ������� ��� ������������ ��� ����������� ����6��� ���t opOf St ack��������
����������� �������� ������ �� �� ��������������������������
Char St ack st ack1 = new Char St ack( 10) , s t ack2 = new Char St ack( 5) ;
+������0�2����"�� ���./���� � ������������� ������������������������� � ��������������� ����!����
�������� �� �� �������������+������0�2����"�� ��������������� � �����"����� ����������� �������������
!������������ ���� �������� ���������(��� �� ��������������"� ����������' : ' ����� ����������� �������������!�������������� ����������+������0�2��� �������� ��������������������� ��3���������� �����
�!������� ���!����������� ��������� �������������������������������� ���� � ����������"�����
+������0�2���"���������� ���������� ������������������Char St ack ��������(����� ��������� ��Char St ack ������ ��������� �������� �������������� ��� � ����"�������������
������������� �������������� �����
�
Object References
Page 4 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
�
*��������������!�������������� ���������� � �� �������� �������� ������������������� �!�������� ��
�������������������� ���!���������������"������������� ��������!����������*������� �������!��
��!������������������� ����������� ������������������� ���������������� ���!��������������� ��
���������
/ / Cr eat e t wo di st i nct st acks of char s. Char St ack st ackA = new Char St ack( 12) ; / / St ack l engt h: 12 char s Char St ack st ackB = new Char St ack( 6) ; / / St ack l engt h: 6 char s st ackB = st ackA; / / ( 1) al i ases af t er assi gnment / / St ack pr evi ousl y r ef er enced by st ackB can now be gar bage col l ect ed.
�"��� ��#���������� ������ �����������!��������� ������������ �� �$0'�� ����� �� ����������
����� ������+������0�7���*� ��� ������������ �� �$0'������������!���������st ackA�����st ackB�"�������� �� ��������� ��#���������� ������+������0�7���8���������!���������st ackA�����st ackB��������������� ��� ������������ ����� ���������� �� ������������� ��9�� ��������� �� ���� ��#������ �
�� �"������� ������ �������������!��������st ackB�������� ������������ :�9��������� ������������������������ ���������������������������������������������������� �������� ��������� �����������
����������������������������������;������������� ������� �!����� �#�������������� ������ ����
��� ����
��������!�"�������
�
���������� �
���������� �
1.4 Instance Members
1��������� ����� ���"������!��� ���"������������ ���������������������� ������������������������������� �
��������������������������������!��������� ������ �����!�������������������� ����������� ���������
�"����� ��� ������ ��������!�� ��������� � ������ �������� �����!�����������!�� ��������!�����������
�� ���������������� ��������� ������!������������� ��������������������������������� ����
����� �� � ���� �� �� � ������� �������� ���� ������������ ���� ������������������������ ����
���������"� �� ����������� � ������� ����� ������"������������������������� ��������� ����������
Page 5 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
�
��� �����!���������������� ������� ������"������������ ������� ������������� �!������������������
��������� ����� �������� �������������������������"����������������� �� ����������3 � ���
�������������������������3�� ����0�<��
Invoking Methods
%���� ����������� ��������������������������������� �� ��������� ������������� ���(���� ���
��� �����������!���������!�#���� ������������ ������� ������� �������� ����� �!��� ���������������
��������� ������� �������� ������� ��������������(��� �' . ' ������ ����*�������������������� � ��������� ����������� �������� � �� ���� ��������!������ ������������ ����� ���� �������!�#��������
���������� �� �� ����� ������������������� ������!�#������ ��������!�����������������
������� �������#� �� �����������!������� ����!������������ �������������� �������� �� ������������
���� �������� ��
Char St ack st ack = new Char St ack( 5) ; / / Cr eat e a st ack st ack. push( ' J ' ) ; / / ( 1) Char act er ' J ' pushed char c = st ack. pop( ) ; / / ( 2) One char act er popped and r et ur ned: ' J ' s t ack. pr i nt St ackEl ement s( ) ; / / ( 3) Compi l e t i me er r or : No such met hod i n Char St ack
�������������������!����!�#����� �������� �������� ����� ������ �������������!��������st ack�������� ���������� �$0'������������������ ������ ���� ��#������ ����� ���������� �$2'����������
������ ������� ���� ��#��� ��push( ) �����pop( ) ��� �������������������� ���������Char St ack ������push( ) ��� ����������� ��� ��������!�������� � ���pop( ) ��� ������ ����� ��������� ������������������ ����!�#������ ����pr i nt St ackEl ement s( ) ���� ���� ��#������ ��������������� ������������������������ ������������������ ���������Char St ack ��
������ �' . ' ��� � ���������������������"� �������������� ��������������������������� ������������� ����� ��� � ���������!��������� ������������������� �������������������������������Char St ack���!��pr i vat e������������ ��������� ���� �� � ���������� ������������������� ����� ����������
st ack. t opOf St ack++; / / Compi l e t i me er r or : t opOf St ack i s a pr i vat e f i el d.
����������� �
���������� �
1.5 Static Members
������������������ ������������������������������� �� ���������������� ������� ������������� �
���� �������� ����������*���(������������������ �� �������"������������"�� �� ��#���� ���#������"�
���������� ����� �����������!����������� ����5�������������� ������������ �����!����������� ���
������������ �������� ���#���� ������������������ ������ ����������� ����!�� ������������1��������� �
���� ���"������!��� ���"������ ����������9��������� ���������� ����������� ��:��������� ������� ��
�������� ������� ������������������st at i c��3����������������������������������������� ��������� �� ���������������� � ����������� ���� ����������*�� � ���!�������������� ����6���"���� �������������������� �
��� �����3������������������������!����������������� �� ������������� �� ���������������� � ������
����� ����� ����������3 � ���!�������������� � ����� ��������������� �!����#��"���������������������
����������� ������������������ ������������������������������ ������� ���#��"����st at i c���� ������������ �����
+������0�=����"�� ��������������������� ���������Char St ack��� ���������������� ������ "��� � ����������� �� ��������"������������������������ ��������� ������� ���Char St ack������������!������
Page 6 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
1(������0�2������������count er ������� � ���!������������������ �$0'��� �"������������� ���������� ����6��� �� ��������� �!�����0�"���� ��������������������1���� ������������ ���� ���Char St ack�������������� ���� ������� ��� ���� �$2'�����(��� ������������ ��� ����(����� ����������� �� ���
���� ������ ���������������� ����get I nst anceCount ( ) �� �$7'������� � ����� �������������� �� ����������� ��� ����� ������� ���!�����"������������
��������#���������������$%�&����$�������������������������
�
+������0�<����"�� ������������� ������� ��������������������Char St ack������� ��� �����������"����!���� ����������������������0�0�� � ���������� ������� ��������!������������������ ��� �����������
�������������������������������������
�����������$����������������������������������
/ / Sour ce Fi l ename Char St ack. j ava publ i c c l ass Char St ack { / / I nst ance var i abl es pr i vat e char [ ] s t ackAr r ay; / / The ar r ay i mpl ement i ng t he st ack. pr i vat e i nt t opOf St ack; / / The t op of t he st ack. / / St at i c var i abl e pr i vat e st at i c i nt count er ; / / ( 1) / / Const r uct or now i ncr ement s t he count er f or each obj ect cr eat ed. publ i c Char St ack( i nt capaci t y) { / / ( 2) s t ackAr r ay = new char [ capaci t y] ; t opOf St ack = - 1; count er ++; } / / I nst ance met hods publ i c voi d push( char el ement ) { s t ackAr r ay[ ++t opOf St ack] = el ement ; } publ i c char pop( ) { r et ur n st ackAr r ay[ t opOf St ack- - ] ; } publ i c char peek( ) { r et ur n st ackAr r ay[ t opOf St ack] ; } publ i c bool ean i sEmpt y( ) { r et ur n t opOf St ack < 0; } publ i c bool ean i sFul l ( ) { r et ur n t opOf St ack == st ackAr r ay. l engt h - 1; } / / St at i c met hod ( 3) publ i c st at i c i nt get I nst anceCount ( ) { r et ur n count er ; }
Page 7 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
}
��������'��������������������
�
,���� �������������� � �������������� ������������������ ������������������������"�����������!�#���
���get I nst anceCount ( ) ��� ������� ���������Char St ack��
i nt count = Char St ack. get I nst anceCount ( ) ; / / Cl ass name t o i nvoke st at i c met hod
3 � ��������������������������������!�������� �������������
Char St ack st ack1 = new Char St ack( 10) ; i nt count 1 = st ack1. get I nst anceCount ( ) ; / / Ref er ence i nvokes st at i c met hod
3 � ���������������������������������������� ����� ������������������!�������� ��������������� �
��� ������������������������������������������ �������������
(��������(���������)�������������������
��� �����
/�������
������������� �����!���������������� ������� ���������������� �������������������
��������������!�#��� ��������������� ������������
��� �����
>��������
*������� �� ���������� ���"���� ��������������� �� �� ���� �� �����"������������ '����
���������������� ����*�����������non-static field��
��� �����
/� ����
*��� ���� �� ��������� �������� �������� ����������%���� ����� ��������������������
� ���������� � �����
3 � ���
/�������
����������� � ���!�������������� � ����� �����������������������������������������
��!�#����� ������������� ����������������� ��������������� ������������
3 � ���
>��������
*������� �� ���������� ���"���� ��������������������� ��������� �� ��������������� � ��
��������� ���� ����������*�����������static field�����class variable��
3 � ���
/� ����
*��� ����"������������� �� ��������������� � ����������� ���� ����������*�����������
class method��
Page 8 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
�
����������� �
���������� �
1.6 Inheritance
���������� "���������� �����������������������������"���������������(�� ��������������������
����������������� ���#��������� ������������������(�� ����������Vehi c l e� �����������������Car�������������������!������������������Vehi c l e�������!����������?� ����������� ���#��������� �������������������������������������Vehi c l e� �� ������������������������������������������Mot or��Axl e������Gear Box��"�������#�������!��������
������ ������������� �� ����������(������ �� ��������� ����� ��#���������� ���� �� ��������� �� ��
������ ����� ��� ����������������"�� ��#��������� ��������� ������������!�������� ���Char St ack ���������� �� ���������� ������� �������������� ��������� ����� �������� ���;�!��� �� � �������� �����
� ��#������� ��#���������� ������ ������������!�������� ���Char St ack ����������������� ��������������"�����+������0�@������������Pr i nt abl eChar St ack ����������� ���������������� ���������Char St ack����������� ������������������Char St ack��������������������������������� ��#����������� �����"������� ���������Pr i nt abl eChar St ack ����������������������� ��#����������� ���� �� �������������� � ����������� ���
��������*�������������������������+�%���������,��������
�
��� �!�������!��������"����������������(�� ������������)������ ���������� ���ext ends����������� ������������������ �����*������������������������������������������������������������ �������������
������������������������"���������������� ��������� �� ���Pr i nt abl eChar St ack ��������
cl ass Pr i nt abl eChar St ack ext ends Char St ack { / / ( 1) / / I nst ance met hod publ i c voi d pr i nt St ackEl ement s( ) { / / ( 2) / / . . . i mpl ement at i on of t he met hod. . . } / / The const r uct or cal l s t he const r uc t or of t he super cl ass expl i c i t l y . publ i c Pr i nt abl eChar St ack( i nt capaci t y) { super ( capaci t y) ; } / / ( 3) }
����Pr i nt abl eChar St ack��������( ����� ���Char St ack�������� �$0'���������� ���� ���pr i nt St ackEl ement s( ) ��� ������� ���Pr i nt abl eChar St ack ���������)������������� �� ���������st ackAr r ay������ ��������������Char St ack��4�"�!���� ������������������������� ����������� ��������������� ������������������������������������� ��������������� �������������� ����� �������������
�������� ��������������� ���Char St ack��������1(������0�7��������!���������� ���������Char St ack��"�������������������������������������������� � ������� ���pr i nt St ackEl ement s( ) ��� �������
Page 9 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
�
���"��� �$2'���������� ��� ������ ���Pr i nt abl eChar St ack �������� �$7'������� ������� ��� ������ ��������������Char St ack���������� ����� ����6�� ���� ��#�����������
���������!������������$��������
/ / Sour ce Fi l ename: Char St ack. j ava publ i c c l ass Char St ack { / / I nst ance var i abl es pr ot ect ed char [ ] s t ackAr r ay; / / The ar r ay t hat i mpl ement s t he st ack. pr ot ect ed i nt t opOf St ack; / / The t op of t he st ack. / / The r est of t he def i ni t i on i s t he same as i n Exampl e 1. 2. } / / Sour ce Fi l ename: Pr i nt abl eChar St ack. j ava publ i c c l ass Pr i nt abl eChar St ack ext ends Char St ack { / / ( 1) / / I nst ance met hod publ i c voi d pr i nt St ackEl ement s( ) { / / ( 2) f or ( i nt i = 0; i <= t opOf St ack; i ++) Syst em. out . pr i nt ( st ackAr r ay[ i ] ) ; / / pr i nt each char on t er mi nal Syst em. out . pr i nt l n( ) ; } / / Const r uct or cal l s t he const r uct or of t he super cl ass expl i c i t l y . Pr i nt abl eChar St ack( i nt capaci t y) { super ( capaci t y) ; } / / ( 3) }
%���� ����� ���Pr i nt abl eChar St ack�������"��������������� ���#�� �������� ����� ���Char St ack ���������� � ����"�����������!�� ������� ���������� ������ ������������� �������������
Pr i nt abl eChar St ack aPr i nt abl eChar St ack = new Pr i nt abl eChar St ack( 3) ; aPr i nt abl eChar St ack. push( ' H' ) ; aPr i nt abl eChar St ack. push( ' i ' ) ; aPr i nt abl eChar St ack. push( ' ! ' ) ; aPr i nt abl eChar St ack. pr i nt St ackEl ement s( ) ; / / Pr i nt s " Hi ! " on t he t er mi nal
����������� �
���������� �
1.7 Aggregation
9���������������"���������������(�� ��������������������������������������� ������� �������� ������
� �������� � ��� ������ �� �� ������ ����� ���
�!�������� ��������� ������������ ��������������������������� ������� ���� ����� ��������� ��
�(����� ��������������������������� ����!�������������� �!���� �� ������������������� ��� ��������� ���
1��������� ���� ���Char St ack ������������������� ��� ���� ������������� ���������������� � �� ������� ��������� �����1����� ��#������ �������������������������� �!���� �� ����i nt � ��� ���� �������(�!����� �� ����� ��� ��� ������� ��#���������������� ������ ��������� ������� ���Char St ack ��������"�������� ����������� �����!�������������������� �������� ��������� ��� � �� ������� � ��� ������ ��"�����
���������������� ���������������� ���!�������������� �!���� �� ���������� �������� ������������� ���
������� ������� ��������������������� ���������������� ������ ���./�������������+������0�A��
���"���� �� ����������� ���� ���Char St ack�������"������!����������������� ����char �������� ���"� ��� ��
Page 10 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
�
�
��������-�������������������������"�����������
�
�
���������� �
���������� �
1.8 Tenets of Java
� ,������� �!����� ������������� ���������������
� ���������� "��#��������!��������� �!�������� ����������������� �����!�������������� �!�� ������
� 8�������������� ������� �� �� ��������� �����������������
� %���� ���������������������� ���!���������������
� %���� ����� �!������� ���� ����� ��������� �?� ���������������!������������� ��� ��������� ���
� 5��� ������������ ���������������� ������ ������� ����
����������� �
���������� �
Review Questions
�
�� 9������ � ���� ���� �������� ����� ���:�
3���� � ������������� ����"����
� *��� ������������������ � ������������� ��� �����
Page 11 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
� *��� ����������� ���� ����������� ��������� ���������� ���������� ��� �����
� *��� ����������� �������������� ���
. *��� ��������������� ������������� �������!������������� ���������� ��� �����
� *��� ����������������� �������#��������� ������
�� 9������ � ���� ���� �������� ��������� :�
3���� � ������������� ����"����
� *������� ����"�� ���������������� �� �� ���������
� *������� ���������� �����������������
� *������� �������������� ��������� ���������� �������6� ���������� ��� ������
. *������� ���������������� ������ ���� ���
� *������� ������!���������
�! 9������������� ����������� ��� ������ ���������������� ���:�
publ i c c l ass Count er { / / ( 1) i nt cur r ent , s t ep; publ i c Count er ( i nt st ar t Val ue, i nt st epVal ue) { / / ( 2) set ( st ar t Val ue) ; set St epVal ue( st epVal ue) ; } publ i c i nt get ( ) { r et ur n cur r ent ; } / / ( 3) publ i c voi d set ( i nt val ue) { cur r ent = val ue; } / / ( 4) publ i c voi d set St epVal ue( i nt st epVal ue) { st ep = st epVal ue; } / / ( 5)}
3���� � ������������� ����"����
� ,�������#���"� ��$0'���������� ��� ����
� ,�������#���"� ��$2'���������� ��� ����
� ,�������#���"� ��$7'���������� ��� ����
. ,�������#���"� ��$='���������� ��� ����
� ,�������#���"� ��$<'���������� ��� ����
�# ;�!��� �� �Thi ng���������������"����������� ��������"����������������!�������������
Page 12 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
���� ������ ��������"��������:�
Thi ng i t em, st uf f ; i t em = new Thi ng( ) ; Thi ng ent i t y = new Thi ng( ) ;
3���� � ��� "�������� ����"�����
� %�������� �������� ����
� �"������� ���������� ����
� ����������� ���������� ����
. %�������������!��������������� ����
� �"������������!����������������� ����
� ����������������!����������������� ����
�' 9������ � ���� ���� �������� ������� ������� ���:�
3���� � ������������� ����"����
� *����� ������������������������������ � �����������
� *����� �����������������"�������������
� *����� �����������������!������� �����
. *����� �������������������� �������� �������� � �� ��������������"������
� *����� ��������������"������������ ���������� �����
�* 4�"��������� ������������������� �!�:�
3���� � ������������� ����"����
� �������������������������������������� ���-����������
� �������������������������������� ���� � ���!������������������ ���-�����������
� ������������������������������������ ���-����� ������� ������
. ������������������������������� � ����� �������������� ���-�����������
�- ;�!��� ��������"����������"������ � ���� ������ ���:�
cl ass A { i nt val ue1; }
Page 13 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
�
�
c l ass B ext ends A { i nt val ue2; }
3���� � ��� "�������� ����"�����
� ,�����A��( �����������B��
� ,�����B���� �����������������������A��
� ,�����A������� �������������B��
. ,�����B������������������������A��
� %���� �����������A���!����������������val ue2��
� %���� �����������B���!����������������val ue1��
���������� �
���������� �
1.9 Java Programs
*� �!��������������������� ����������������������������"� ��������� ������� ������� ����������-��
�(��� ����� �� �������� ��*� �!��������� ���������� ��������� �������������������� ��������� �!��2�
35B���������� �������� �� �� � ������ �������������� �������������������publ i c������������ ��������������� ������������������������������� ����������� �����������������"� ��. j ava�����( ��������1���������������� ����������������������������������� ���������� ������� ������� ���������������������������
�������� ������������������������� ����������� ���������"� ��. c l ass ��������( ��������*��������������� �������������������� �������������������� �!��2�35B����!����� ��������� ����������������
�(�����������0�0C�3������ �!��*������ �����
����������� �
���������� �
1.10 Sample Java Application
*���������������"�� ������������������������������������������� �� ���������������������� ���
�(��� ������������� ������ ������������ ������� �!��� �������������� ���!���������� �� �����������
�� �����������mai n������mai n( ) ��� ������� ������������ ���� �� �������� ����� ����(��� ������������������ �����
Essential Elements of a Java Application
1(������0�=��������(������������������� �������"������������ ������ ���Char St ack ������� ����!�������� �������������� �����
���������#�"��"�����������
Page 14 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
/ / Sour ce Fi l ename: Cl i ent . j ava publ i c c l ass Cl i ent { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { / / Cr eat e a st ack Char St ack st ack = new Char St ack( 40) ; / / Cr eat e a st r i ng t o push on t he st ack St r i ng st r = " ! no t i s ot nuf er a skcat S" ; i nt l engt h = st r . l engt h( ) ; Syst em. out . pr i nt l n( " Or i gi nal st r i ng: " + st r ) ; / / Push t he st r i ng char by char ont o t he st ack f or ( i nt i = 0; i <l engt h; i ++) { s t ack. push( st r . char At ( i ) ) ; } Syst em. out . pr i nt ( " Rever sed st r i ng: " ) ; / / Pop and pr i nt each char f r om t he st ack whi l e ( ! st ack. i sEmpt y( ) ) { Syst em. out . pr i nt ( st ack. pop( ) ) ; } Syst em. out . pr i nt l n( ) ; } } / / Sour ce Fi l ename: Char St ack. j ava publ i c c l ass Char St ack { / / Same as i n Exampl e 1. 2. }
%� �� ������ ������������
Or i gi nal st r i ng: ! no t i s ot nuf er a skcat S Rever sed st r i ng: St acks ar e f un t o s i t on!
�����������������Cl i ent ������������� ����"� �� ��������mai n������ �� � ���������� ����� ���mai n( ) ��� ������� ����������������������!�#������ ��� �!���� ����� ���������������� ��� �!��>�� ����/�������
$ >/'������mai n( ) ��� �������������������������������"���
publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { / / . . . }
����mai n( ) ��� ��������publ i c������������ ��� �� ������ �����������������������������������#��"����st at i c������� ����� ������������ �� ��������������#��"����voi d������� ����� ����������� ��� ��������!���������������� ������ ��St r i ng[ ] ar gs������������������ ����������� �������������� ���� �� ���mai n( ) ��� ����"���� ���������� �������� �� ����
Compiling and Running an Application
�!������������������������������������� ��� �!����������� ����j avac��"����������� ���� ��� �!��2�35B��
����������������Cl i ent . j ava���� ����� ��������� ������� ���Cl i ent �������������������������������
Page 15 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
�
��������������!���� ��������"�������� �� � �����������������
>j avac Cl i ent . j ava
��������� ��� ���������������Cl i ent . c l ass����� ������� ��� �!���� ����������� ���Cl i ent ������������Cl i ent ������������ ���Char St ack��������������� ��������Char St ack. c l ass�������� ����������(�� �� ������������"����������������� ���������������Char St ack. j ava��
,������������������������(��� ������ ��� �!���� ����� ���j ava��"���������������� ���� ��� �!��2�35B��1(������0�=�����������������!���� ��������"�������� �� � �����������������
>j ava Cl i ent
D� �� �� ������ ����������� ���������������������������� �������� �� ���� ����(��� ������� ���mai n( ) ��� ��������� ������������������������������� �������1(������0�=� ������ ���"���� ����(��� �������
���mai n( ) ��� ������������� ����
���������� �
���������� �
Review Questions
�
�/ 9�� ������������ ��� �!��2�35B���������������� ���������� ��������"������������ �����������
������������Smal l Pr og. j ava:�
publ i c c l ass Smal l Pr og { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { Syst em. out . pr i nt l n( " Good l uck! " ) ; }}
3���� � ������������� ����"����
� j ava Smal l Pr og�
� j avac Smal l Pr og�
� j ava Smal l Pr og. j ava�
. j avac Smal l Pr og. j ava�
� j ava Smal l Pr og mai n�
�0 9�� ������������ ��� �!��2�35B���������������� ���(��� �� ���mai n( ) ��� ���������������������Smal l Pr og:�
3���� � ������������� ����"����
Page 16 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm
�
�
� j ava Smal l Pr og�
� j avac Smal l Pr og�
� j ava Smal l Pr og. j ava�
. j ava Smal l Pr og. c l ass�
� j ava Smal l Pr og. mai n( ) �
���������� �
���������� �
Chapter Summary
�
���������"���� ������"����������������� �������� ����
� ������������ �����%%&��������"� �������������� ������ �!��
� ����� ���������� ������� �!��������� ����
� ���������������������� �!��������� �����
����������� �
���������� �
Programming Exercises
�
�� /������ ����������������1(������0�=� ������ ���Pr i nt abl eChar St ack ���������� ���� ���� ���Char St ack��������. ���6�� ���pr i nt St ackEl ement s( ) ��� ��������� ���Pr i nt abl eChar St ack����������� �����"�������������!����"���������������� ������1(������0�=:�
���������� �
Page 17 of 17
6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hh82B4.htm