?= é ?_`?? !9 ?c P¨`? ?I?? ?c ?¥??? ?c ?F?_  ?c P¨` ? ?I?? ) ?3`? ?# ? ì-W¥1" D ?? ?l ??7y ?¥` ?3/ZE¥T¨ ) 3`¥?¨? ??_ZE.?` ) ?3 ?M a ?ZEa L èM ? L èZE )M ¥T¨× ) P¨4USJOHa4USJOH#VGGFS) ?3?1 K a¨¥?y? ^Kz¥?y K a¨¥?y? ^Kz¥?y K a¨¥?y? ^Kz¥?y ú)?? !9?y¥?Z ? N 1 ??l BCPL C L?l C i?l Modula-2 ???l Ada ?C L?l C++ ??l Fortran * Y?l Algol L?l Pascal VC?l Java ? ¥?l Simula · ??l SmallTalk 3?l C# ? ? ? Y I??y¥+??Z¨ I??y¥+??Z¨ ) ?_ ?y  ??I?yb ) ?_V???y  ? CaFortran?bV??? y _ 9 ?3?· 7¥Z T?g? ? ? |ù5s3?B ?1¥e???b )2??? !9  àμ?M P¨¥?y 7 ^ 4 B??¥) ?ZEb ) ?_`I? 9 ???$à Q??x ] y Ta ]?? ?3 ?¥BF`"bü ? 7 y¥`?Xμ¥`?i  ??1- ¥ ? ?b ??`?????ò"B?+ ? ¥ ? ?b 5.1 ` )`}VC L W?? V[ ü ?S M¥ ?? Ytb )`¥ ?? ? ???1b )`¥ ? ^M ×[# ? ì¥? -′ ?¥"?b )`¥?1 ^ZE¥"?b `  è `  è ? `¥ ?9 ? =?¨M  ?V U`¥ ?9 ? =?¨M  ?V U ? `¥?19 ? =?¨ZE ?V U`¥?19 ? =?¨ZE ?V U ? ZE L=  ? ?? ?_V??¥f ?`¥?1? L=  ? ?? ?_V??¥f ?`¥?1? T?l ZE¥ =?b T?l ZE¥ =?b ` ? `¥M   ? `¥ZE ?1 ú ?a2 a ?)ê? …… F ?a ?úaD? …… ? o 2a ? a oi …… ? oa oa ?? …… SE ü6 ?a | ga1 ?ê? …… ?Zü6a^?a ? …… | ?2a? ?a ?B …… ?ayTaD …… ˉ¥ ?ü ^?B" e?¥`MoT¨ ??¥b `m U `m U M    ? ZE  ?1 M V ü`¥ ?b ZEV ü`¥?1b   `¥ZEü`¥ =?M  Ua a 1  ? P¤oμ`1-¥ ? P¤oμ`1-¥ ZE? ??T?t =?M b `-WY?ZE `-WY?ZE ` ¥Z E ^`D??ìa?  e`?oaY?¥ ¥Z ¤ g  ` ¥ìa?  e` V[YV??¤ g ??¨ ¤ g ¥ìa?  e` V[YV??¤ g ??¨ `¥ZE?:`¥?1??M`¥ ?b `¥ZE?:`¥?1??M`¥ ?b h.? ' ·?¥ ?`¥ZE[?M  ? ' ·?¥ ?`¥ZE[?M  ? ? ?á 3B?¥?1b ? ?á 3B?¥?1b ` B ` A message 5.2 ? ) ? ^?l`¥2bM ¨ ? í ? ?  ZE¨ ??l?1b`¥ ?cB"  ? ?ZE¥?lb ) ? ^?lB?`¥ ? ?ZE¥ ?'b )` ^ ?¥ L èb V[VB ??7y? L èb7yB? L è$?1 L è?b )`? L è ?MYè V[oDb ?¥?l < ?? e? >DMBTT ? ? <FYUFOET?? ? ? > <JNQMFNFOUT¤ g ? ¤ g ? > \ ??8 ??8 ^ ) ?? e? ?-W !?]¥i | ?Kb ?? e? ?-W !?]¥i | ?Kb &? &? QVCMJDaBCTUSBDUaGJOBM ) 1o3???y?Xü?lz+?¨o¥3M?t =1o3???y?Xü?lz+?¨o¥3M?t = 1o3|? ?$¨ ?T1M  ??1?¨o?5??Y 1o3|? ?$¨ ?T1M  ??1?¨o?5??Y ??b ) +BWB? ?μ¥ ?? ^YV°¤W¤1 ? ? KBWBMBOH0CKFDU¤?¥b ?¥ U è 1 class Zú Car Vehicle subclass  ú object ?ì ?ì  ú ?? Zú Zú Dn Zú Zú myCar ù  ù  ?ú !? !?  ú  ú ?ú  ú public class Vehicle extends Object{ public int WheelNum = 4; //: cú } ? "¥ ? ?? ......//  ? ??8} ? } import Vehicle; public class Car extends Vehicle{ public int DoorNum = 4 ; public String EngineType = "V6? c " ; ......//  ? ??8} ? } import java.io.*; import Car; public class Me{ public static void main(String args[]){ Car mycar = new Car(); System.out.printlin("ú } ?  "+mycar.WheelNum); System.out.printlin("? c ?? "+mycar.EngineType); } } ??` Circle radius findArea circle1: Circle radius = 2 new Circle() circlen: Circle radius = 5 new Circle() ... Graphical notation for classes Graphical notation for objects ?¥m? ?| `¥m? ?| ?¥ U è 2 class Circle { double radius = 1.0; double findArea() { return radius*radius*3.14159; } } 5.3 L8a`D ? ` ? ??` ? ?? à Q W? L8 L8 C L W? ` ? ? 9 ? ? Vr 9 ? ? Vr ?  `  ?  L8?D3 ?  L8?D3 13 %¥ù5M113 %¥ù5M1 ¥ ? ? ??¥ ? ? ?? /9 ? ? ?/9 ? ? ? 3?) ?¥ ?3?) ?¥ ? L è? ` ¨ ?_`¥ èqZE3 %C L W?¥ù5 ¨ ?_`¥ èqZE3 %C L W?¥ù5 ? ` ?? C L 3 ?¥` 9 ??¥`¥e? class Car { int color_number; int door_number; int speed; void brake() { … } void speedUp() {…}; void slowDown() { … } } 5.4 `¥ 2 üD7y 2 ü`¥?E1 ClassName objectName; Example: Car myCar; Circle myCircle; 7y`¥?E objectName = new ClassName(); Example: myCar = new Car(); myCircle = new Circle(); `¥ 2 üD7y VB??? ClassName objectName = new ClassName(); Example: Car myCar = new Car(); Circle myCircle = new Circle(); 5.5 e? ??M ?` ??M ¥ uY 1 c: Circle radius = 5 Primitive type int i = 1 i Object type Circle c c reference Created using new Circle(5) ′M  ?¨M  ??M }VB?i %′¥ =i1?b ?e? ??M  ? =i ?i%¥′ ^e? ??¥ ?` ??M  ? =i¥′ ^B? ?¨ P`¥i% 1?b M ?′ 1 c1: Circle radius = 5 Primitive type assignment i = j Before: i 2j 2 After: i 2j Object type assignment c1 = c2 Before: c1 c2 After: c1 c2 c2: Circle radius = 9 M ?′ 1 c1: Circle radius = 5 Primitive type assignment i = j Before: i 2j 2 After: i 2j Object type assignment c1 = c2 Before: c1 c2 After: c1 c2 c2: Circle radius = 9 ? ` 5.6 `¥ P¨ )?¨`¥ ? : objectName.data ? myCircle.radius )?¨`¥ZE : objectName.method ? myCircle.findArea() L èM  L èZE Example 5.1 P¨` )'?? P¨ Circle ?7yB?? ?¥` i?¨`¥ ? ?ZEb TestCircle Run 5.7 /ZE )/ZE ^B?+ y¥ZE ?DB? ZEμ ??] –/ZE àμRí ?? .à ? void 9 àμ –/ZE¥?¨ ^7yB?` H P¨ new?T?é?¥b )/ZE¥T¨ ^ S?`b 5.7 /ZE Circle(double r) { radius = r; } Circle() { radius = 1.0; } myCircle = new Circle(5.0); ?¨/ZE ? S? `¥ ?  a/ZE ?T ?? àμ ü ??l ??/Z Eü??c1?lB?  a/ZEb Example 5.2 P¨/ZE )'?? P¨ Circle ??¥/ZE7 y ????]¥`b TestCircleWithConstructors Run 5.8 óZE.?` ).?e? ??¥M ′D.?`¥ uY ).?e? ??M ü ^| ′.?ó??b?MZ E =?¥? ????YZE??¥M ′b ).?`i?"|`¥?¨.?ó??bZE8 = ?`¥ ??M????YT1? ?.?¥e `b??.??1?¨.?b Example 5.3 ü`T1? ?.? TestPassingObject Run è0 a ü )YV.?` myCircle?? ? n mainZ E?¨ printAreasZEb 5 main 5 times printAreas n c myCircle myCircle Circle radius=1 5.9 Vn?? e? Java4 +?? e?¨ ? e? ? aZ E? ?¥`ùb ? public ¨ ??l¥ ?aZE? ?  ?μ¥??? V[ `ù . ? private ¨ ??l¥ZE ? o ???l?ZE? ? ¥ ?`ù7  e ?? ?`ù . ? `? ?YV°¤?¨`ù 2μ ? b V[Y V@F getter and setter ?tZE ?1`ù ZE_ ì??? 2μ ? b Example 5.4 P¨ private? e??`ù ZE ' è 2 üB?? ? Circle !??1 2μ M ¨`ù ZE getRadius and setRadius  |?????b RunTestCircleWithPrivateModifier 5.10 ?M a ?è ? ?ZE ) ?M B? ?? ?μ L è Z ?  i%? ?¥¨ =ib ) 2 ü ?M a ?ZE3¨? e? staticb ) ?è  2 ü H@F? e? final staticb ?M a ?è ? ?ZE Circle -radius -numOfObjects +getRadius +setRadius +getNumOfObjects +findArea 1 radiuscircle1:Circle -radius = 1 -numOfObjects = 2 instantiate instantiate Memory 2 5 radius numOfObjects radius is an instance variable, and numOfObjects is a class variable Notation: +: public variables or methods -: private variables or methods underline: static variables or metods circle2:Circle -radius = 5 -numOfObjects = 2 Example 5.5 P¨ L èM a ?M # L èZEa ?ZE ' è@F ?M  numOfObjects ¨ ??6 7y Circle `¥? ?b Circle private double radius private static int numOfObjects public double getRadius( ) public void setRadius(double newRadius) public static int getNumOfObjects( ) public double findArea( ) TestInstanceAndClassVariable Run 5.11M T¨× )  Java?¨? V[ ??1Z 2 üM b?¨? V[ ??1Z 2 üM b ) M ¥T¨× ^VM ¥?l? 7 S?KíB? v¥2 ?bM ¥T¨× ^VM ¥?l? 7 S?KíB? v¥2 ?b B? v ^·¨ B? v ^·¨ { } ?  ? ¥ u Wb ? =a vD?a v?l  ?  ? ¥ u ? =a vD?a v?l  ] M  H =a v?| P¨'a??l7?a?l ] M  H =a v?| P¨'a??l7?a?l $??b ) M ¥T¨× ^ T¨×- = 2 üT¨×- = 2 ü ¥M ?¥M ? ?T¨×?T¨× -?¥} ?? VnbT¨×?54  ¥ -?¥} ?? VnbT¨×?54  ¥ $b ) ???M  2 ü¥ê?y ? ?¥T¨×???M  2 ü¥ê?y ? ?¥T¨× – ??f ?T¨× ?¥? ??l ??f ?T¨× ?¥? ??l – ?M T¨×V 2 ü ?Z? ?$?l¥} ? v2 ? ?M T¨×V 2 ü ?Z? ?$?l¥} ? v2 ? – ZE? ?T¨×ZE? ? ^ZE/f ?¥? T? ZE? ?T¨×ZE? ? ^ZE/f ?¥? T? ?¨?.? ?′óZE?/f ?b T¨× ^?? ?¨?.? ?′óZE?/f ?b T¨× ^?? ZE?/f ?b ZE?/f ?b – sè) ?? ?T¨×DZE? ?M ?μY? -? ^.?? ?ósè) ?b ^.?? ?ósè) ?b M T¨× Uim M T¨× Uim ??M ? ?M  ) ZE8??l¥M ?ZE¥? ??1 ?M bZE8??l¥M ?ZE¥? ??1 ?M b ) ??M ?? ???μr ?M o?l ?¥??M ?? ???μr ?M o?l ?¥ ZE =μrb class A{ int x; int f() { int a=5; x=a; //?E x?? ? =μr } int g() { int y; y=a;//dE aX >rZE g =? àμ?lM  a } } ??M ? ?M  )M ¥ 3D??M ¥ 3M]5??MM ¥ 3D??M ¥ 3M]5??M $??'????M ??ZE = H $??'????M ??ZE = H >rb class Tom{ int x=98,y; void f() { int x=3; y=x; //y¤?¥′ ^ 3? ^ 98 } } 5.12 1o3 this? super ) this¨ ? ?¨ ? -`1 &b ? ?B? ?31ü1 &¨ ? ?¨ ? -`1 &b ? ?B? ?31ü1 & ¥B ?? ¨. ?ó 6B?ZEü V[¨ ?? ¨. ?ó this?¨ ??¨ ?¨ ??¨ 1 &b ) UIJT¨B?ZE?¨ ??¨? -` ?¥′ ^?¨?¨B?ZE?¨ ??¨? -` ?¥′ ^?¨? ZE¥`bYè S?ZE¥ 2 ü? ? |¥? ? ZE¥`bYè S?ZE¥ 2 ü? ? |¥? ? ? ?¥??M  M]? H1¨? ? ?¥??M  M]? H1¨? UIJT ?· ü??M  ?· ü??M  ???^?á 3=l?¥1Z9? P¨ ???^?á 3=l?¥1Z9? P¨ UIJT· ü? - · ü? - `[ P} ?÷ b b ) super·} ?¥? ?¥?¨ LC3 ???¥`ùb·} ?¥? ?¥?¨ LC3 ???¥`ùb ) 0 ??? 3 ?¥??M × 3 ?¥ZEaè è?1¨?3 ?¥??M ×¥ZE? P¨3 ? ?$×¥ZE[e?} ?¥I? Hü1`ù3 ?¥ ??M ?¨3 ?¥ZE Java?YV super ? LC 3 ???¥`ùb Example 5.6 P¨ this? super class B extends A { int b, c; B() { this(2, 3); } B(int new_b, int new_c) { super(1); b = new_b; c = new_c; } } class A { int a; A(int new_value) { a = new_value; } } class Demo { public static void main(String[] args) { B obj_b = new B(); System.out.println("a= "+obj_b.a + "; b= "+obj_b.b + "; c= "+ obj_b.c); } } ?2T a= 1; b= 2; c= 3 5.13 `1"s )5.13.1 1 ó )5.13.2 " )5.13.3 ?? 5.13.1 1 ó 1 ó ^ í ? ? ?MoT¨¥B? =í1"b1 ? Student Course Faculty ê [ ? q 5.60 0.3 * 1 ? = B?D 3 V[ê ?i¥ [?B?? =à V? q 3 ê [?B ê [? V[μ 5? 60 D 3oμB ? = ?? qb 5.13.1 1 ó 1 ó ^ í ? ? ?MoT¨¥B? =í1"b1 ? Student Course Faculty ê [ ? q 5.60 0.3 * 1 ? = 1" B?D 3 V[ê ?i¥ [?B?? =à V? q 3 ê [?B ê [? V[μ 5? 60 D 3oμB ? = ?? qb 5.13.1 1 ó 1 ó ^ í ? ? ?MoT¨¥B? =í1"b1 ? Student Course Faculty ê [ ? q 5.60 0.3 * 1 ? = VêS & B?D 3 V[ê ?i¥ [?B?? =à V? q 3 ê [?B ê [? V[μ 5? 60 D 3oμB ? = ?? qb 5.13.1 1 ó 1 ó ^ í ? ? ?MoT¨¥B? =í1"b1 ? Student Course Faculty ê [ ? q 5.60 0.3 * 1 ? = S ü1"Z_ B?D 3 V[ê ?i¥ [?B?? =à V? q 3 ê [?B ê [? V[μ 5? 60 D 3oμB ? = ?? qb 5.13.1 1 ó 1 ó ^ í ? ? ?MoT¨¥B? =í1"b1 ? Student Course Faculty ê [ ? q 5..60 0..3 * 1 ? = B?D 3 V[ê ?i¥ [?B?? =à V? q 3 ê [?B ê [? V[μ 5? 60 D 3oμB ? = ?? qb ?¥? ? ? 5.13.1 1 ó 1 ó ^ í ? ? ?MoT¨¥B? =í1"b1 ? Student Course Faculty ê [ ? q 5..60 0..3 * 1 ? = B?D 3 V[ê ?i¥ [?B?? =à V? q 3 ê [?B ê [? V[μ 5? 60 D 3oμB ? = ?? qb `¥? ?¨ uWV U 5.13.2 " " ^B?+ y¥1 ó? TV U ? ? -W¥ ? ?1" ? μ (has-a)ac (part-of) a?μ (own) a&? (employed- by)?b1 ? Magazine Publisher Consultant $]μ $&? 1 * $ù ** B?? ?μB??B?$ù$?? &? 5.13.2 " " ^B?+ y¥1 ó? TV U ? ? -W¥ ? ?1" ? μ (has-a)ac (part-of) a?μ (own) a&? (employed- by)?b1 ? Magazine Publisher Consultant $]μ $&? 1 * $ù ** "1" ?TB?`$B? "` ?μ ? ì-W¥1"ü ^ ?1" B?? ?μB??B?$ù$??  &? 5.13.3 ?? ??  E ? ?-W “ ^ (is-a)”¥1"b Person Student Faculty ??1" 5.14 L èD è 5.7 ·?~ { ?? è0 P¨ ?y ?1~ ?b 1y ?B? ?_`¥"dYèμ/ ?? 1. !9aS M"d¥ ? 2. í ? ?? ?¥ ???ZE 3.y ? ?-W¥1" 4.7y ? Example 5.6 ·?~ { μ t 1 Name 1 Borrower Address Mortgage 1 1 Example 5.6 ·?~ { The following is a test program that uses the classes Name, Address, Borrower, and Mortgage. Run BorrowMortgage 5.15 Java API and Core Java classes )java.lang Contains core Java classes, such as numeric classes, strings, and objects. This package is implicitly imported to every Java program. )java.awt Contains classes for graphics. )java.applet Contains classes for supporting applets. 5.15 Java API and Core Java classes, cont. )java.io Contains classes for input and output streams and files. )java.util Contains many utilities, such as date. )java.net Contains classes for supporting network communications. 5.15 Java API and Core Java classes, cont. )java.awt.image Contains classes for managing bitmap images. )java.awt.peer Platform-specific GUI implementation. )Others: java.sql java.rmi 5.16 The String Class )Declaring a String: –String message = "Welcome to Java!" –String message = new String("Welcome to Java!“); –String s = new String(); )String 1? )String ?¤ )01 )String Length )_ ì String?¥3? String 1? )equals String s1 = "Welcome"; String s2 = "welcome"; if (s1.equals(s2)) { ... } if (s1 == s2) { … } 01 String is an immutable class; its values cannot be changed individually. String s1 = "Welcome to Java"; String s2 = s1.substring(0,10) + "HTML"; Finding String Length Finding string length using the length() method: message = "Welcome"; message.length() (returns 7) Retrieving Individual Characters in a String )Do not use message[0] )Use message.charAt(index) )Index starts from 0 5.17 The StringBuffer Class StringBuffer ? ^ String ?¥9} ? . ? 5B?3?1¨?)?31 P¨3??  u . StringBuffer1 String ÷ 2 V[3 ?? u?@Fa? ?F? = ? . ?7B ?3?1B?7y ? ì¥′ü%?  . StringBuffer Constructors )public StringBuffer() No characters, initial capacity 16 characters. )public StringBuffer(int length) No characters, initial capacity specified by the length argument. )public StringBuffer(String str) Represents the same sequence of characters as the string argument. Initial capacity 16 plus the length of the string argument. Appending New Contents into a String Buffer StringBuffer strBuf = new StringBuffer(); strBuf.append("Welcome"); strBuf.append(' '); strBuf.append("to"); strBuf.append(' '); strBuf.append("Java"); 5.18 The StringTokenizer Class Constructors ) P¨?? ? V[|B?1s?l ?[L4 |?) ? ?¥?b V[·????b )StringTokenizer(String s, String delim, boolean returnTokens) )StringTokenizer(String s, String delim) )StringTokenizer(String s) The StringTokenizer Class Methods )boolean hasMoreTokens() )String nextToken() )String nextToken(String delim) ˉ5 ) í ?`??l ?¥ ?-W¥1"b8 " 2 ü ?$8" 2 ü`$8"7y `$8"¨BH?  2 üi7y`$ )/ZE? ?YZE-W¥ uY ^ I 1 ? ) í ??¨.??′.?b )? ?1o3 this¥T¨b