Upload
wongyos-keardsri
View
221
Download
0
Embed Size (px)
Citation preview
7/25/2019 Java Programming - Chapter 12 (in Thai)
1/24
157
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
CHAPTER
2
(Classes and Objects)
1. (Introduction to Classes and Objects)
1.
2.
Package
Constructor
Methods
Attributes (Attributes) (Constructors) (Methods)
(Package) (Classes)
Attributes
...
Method-1
Method-N
(Instance of Class) (Objects)
Attributes
...
Method-1
Method-N
Attributes
...
Method-1
Method-N
new new new
()
(Package) ()
(Class)
(Attribute)
Constructor
Methods
Attributes
Constructor
Methods
Attributes
(Constructor)
Class A Class B Class C
Object bObject a Object c
(Object-Oriented Programming)
(Method)
(Variable)
(Statement) (i f, f or , )
(Object) ()
7/25/2019 Java Programming - Chapter 12 (in Thai)
2/24
158
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
1 [] (10 )
1 Class Diagram
2 [] () () (15 )1) 1 ()2) mai n( ) 3)
4) ()
1234
567891011121314151617
181920
publ i c cl ass Num {pri vat e i nt x;pr i vat e doubl e y;publ i c Num( ) {
x = 0; y = 0. 0;}publ i c Num( i nt m, doubl e n) {
x = m; y = n;}publ i c i nt addNX( i nt n) {
r et ur n n + x;}publ i c doubl e addNY(doubl e n) {
r et ur n n + y;}publ i c voi d showXY() {
System. out . pr i nt l n( x) ;
System. out . pr i nt l n( y) ;}}
12345678910
publ i c cl ass RunNum {publ i c stati c voi d mai n( St r i ng[ ] ar gs) {
Num obj = new Num( 5, 7. 0) ;obj . showXY( ) ;i nt a = obj . addNX( 4) ;doubl e b = obj . addNY(13. 0) ;Syst em. out . pr i nt l n( a) ;Syst em. out . pr i nt l n( b) ;
}}
Num
Num( )Num( i nt m, doubl e n)
i nt xdoubl e y
i nt addNX( i nt n)doubl e addNY( doubl e n)voi d showXY( )
obj: Num
x = 5y = 7. 0
addNX( i nt n)addNY( doubl e n) showXY( )
RunNum
publ i c st at i c voi d mai n( ) {Num obj= new Num(5, 7.0);
obj. showXY() ;
i nt a = obj. addNX( 4) ;...
}
new
7/25/2019 Java Programming - Chapter 12 (in Thai)
3/24
159
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
5)
6)
7)
8)
9) 10) 11) 12) 13)
14) 15)
2. (Classes)
1. 3 1)
2) 2
[] class {
[()]
[()]
[()]}.
[] ;
1
2345678
public c l assNum {
i nt x;publ i c doubl e y;Num( ) {
x = 0; y = 0. 0;}. . .
}
(Modifier) publ i cf i nal
* . j ava1 publ i c ( ) publ i c( )
(Modifier)
+ publ i c
- pr i vate
# prot ect edst at i cf i nal
7/25/2019 Java Programming - Chapter 12 (in Thai)
4/24
160
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
3) (Constructor) ()
4) 8
[] ([]) {[]
}.
12345
publ i c cl ass Dat a {public static int n = 1;
private double data;
. . .}
stat i c-stat i c
[] ([]) {[]
}.
Return Type
123456789
10111213141516
publ i c cl assNumber {publ i c l ong x;pr i vat e i nt y;publ i cNumber() {
x = 0L;y = 0;
}publ i cNumber(long a, int b) {
x = a;
y = b;}publ i cNumber(double k) {
x = y = ( i nt ) k;}. . .
}
publ i c
Overload
(Default Constructor)
12345
678
publ i c cl ass Oper at i on {. . .publ i c i nt incrX( i nt x) {
x = x + 1;r et ur n x;
}. . .
}
publ i c, pri vat e, pr ot ect ed,
stat i c f i nal
8 ()
7/25/2019 Java Programming - Chapter 12 (in Thai)
5/24
161
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
3 [ ] Course (30 )
public class Course {
//3 () (3 )//idprivate(Course ID)//titlepublic (Course Title)//creditprotected(Credit)
// 3 (int0, String"" ) (3 )
// 3 3 (3 )
//setID()public id (3 )
7/25/2019 Java Programming - Chapter 12 (in Thai)
6/24
162
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
//setTitle()public -title (3 )
//setCredit()public -credit (3 )
//getLevel()public 3 2110191 1 2 Undergraduate ( 1-4) Graduated (5 )(3 )
//getFaculty()public 2110191 21
(3 )
7/25/2019 Java Programming - Chapter 12 (in Thai)
7/24
163
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
//getDepartment()public 3-4 2110191 10 (3 )
//toString()public 2110191 Innovative Thinking (3) (3 )
} //End of class
2.
1)
(Body) (Local Variable)
2) (Head)
3)
(Global Variable)
123456789101112
131415
publ i c cl ass Test {pr i vat e st at i c i nt x = 10;pr i vat e st at i c i nt y = 1;publ i c stat i c i nt get X( ) { r et ur n x; }publ i c st at i c i nt get X( i nt x) { r et ur n x; }publ i c st at i c i nt i ncX( i nt y) { r et ur n x + y; }publ i c stati c voi d mai n( St r i ng[ ] ar gs) {
i nt x = 5;System. out . pr i nt l n( x) ; 5System. out . pr i nt l n( y) ; 1Syst em. out . pr i nt l n( get X( ) ) ;
10Syst em. out . pr i nt l n( get X( 2) ) ;
2
System. out . pr i nt l n( i ncX( x) ) ; 15}
}
xy get X( ) xget X( i nt x)
xxyy
xx
yy
xmai n( ) xget X( i nt x)
7/25/2019 Java Programming - Chapter 12 (in Thai)
8/24
164
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
4)
4 [] (10 )
3. (Objects)
1.
1)
()
12345678910111213141516171819202122232425262728
29303132
publ i c cl ass Test {pr i vat e st at i c i nt x = 5;pr i vat e st at i c i nt y = 2;publ i c stati c voi d mai n( St r i ng[ ] ar gs) {
i nt x = 9;System. out . pr i nt l n( x) ;Syst em. out . pr i nt l n( get X( ) ) ;Syst em. out . pr i nt l n( showX( ) ) ;y = 1;System. out . pr i nt l n( y) ;System. out . pr i nt l n( get Y( ) ) ;Syst em. out . pr i nt l n( showY( ) ) ;i nt y = 11;System. out . pr i nt l n( y) ;System. out . pr i nt l n( get Y( ) ) ;Syst em. out . pr i nt l n( showY( ) ) ;
}publ i c st at i c i nt get X( ) {
r et ur n x;}publ i c st at i c i nt get Y( ) {
r et ur n y;}publ i c st at i c i nt showX( ) {
i nt x = 7;r et ur n x;
}publ i c stati c i nt showY( ) {
i nt y = 12;r et ur n y;
}}
1) y10 yy
2) y14 yy
publ i c cl ass Test Cl ass {
publ i c i nt add( ) {
return ;} / / End of method
} / / End of cl ass
Attribute1
Parameter2
Variable3
?
?
7/25/2019 Java Programming - Chapter 12 (in Thai)
9/24
165
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
2)
2. 1)
St udent st d; / Car car ; / Account obj ;
2) (new)
st d = new St udent ( 52300121) ; / car = new Car ( ) ; / obj = new Account ( 101) ;
3)
2
1
2
(
)
Student st d = new Student ( 52300121) ; / Account obj = new Account ( 101) ;
4)
;
= new ( ) ;
= new ( ) ;
a. at t r 1 = 0
a. method1( )a. met hod2( )
Attributes
...
Method-1
Method-N
New Object
Constructor
Methods
Attributes
Class A
Object a
= new ;
( ) (Constructor)
123456789101112
publ i c cl ass Digit {publ i c i nt x;Digit() {
x = 0;}Digit(int n) {
x = n;}publ i c i nt get X( ) {
r et ur n x;}
}
12345678910
publ i c cl ass TestDigit {publ i c st at i c voi dmain( St r i ng[ ] ar gs){
Di gi t d = new Digit();Di gi t e = new Digit(1);Di gi t f = new Digit(2);i nt di gi t 1 = d. get X( ) ;i nt di gi t 2 = e. get X( ) ;i nt di gi t 3 = f . get X( ) ;
}}
mai n+ +
7/25/2019 Java Programming - Chapter 12 (in Thai)
10/24
166
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
5 [] (8 )
6 [] TestNumber Number
Number(10 )
//TestNumberimport java.util.Scanner;
public static void main(String[] args) {Scanner kb = new Scanner(System.in);
//no1 Number(Number)
1234
56789101112
publ i c cl ass St udent {publ i c i nt i d;publ i c St r i ng name;Student ( ) { i d = 0; name = "" ; }
St udent ( i nt i , St r i ng n) { i d = i ; name = n; }publ i c St r i ng getName( ) { r etur n name; }publ i c i nt get I D( ) { r et ur n i d; }publ i c char cal Gr ad( i nt scor e) {
i f ( scor e > 60) { r et ur n ' S' ; }el se { r et ur n ' U' ; }
}}
123456789101112
publ i c cl ass Test St udent {publ i c stati c voi d mai n( St r i ng ar gs[ ] ) {
Student y = new St udent ( 101, "Taksi n" ) ;St udent z = new St udent ( 102, "Api si t " ) ;Student x = new Student ( ) ;Syst em. out . pr i nt l n( x. get I D( ) + ", " + x. get Name( ) ) ;Syst em. out . pr i nt l n( y. get I D( ) + ", " + y. get Name( ) ) ;Syst em. out . pr i nt l n( ( z. get I D( ) + 1) + ", " + z. get Name( ) ) ;System. out . pr i nt l n( y. get Name( ) + ": " + y. cal Gr ad( 49) ) ;System. out . pr i nt l n( z. get Name( ) + ": " + z. cal Gr ad( 79) ) ;
}}
1234567891011
publ i c cl ass Number {pr i vat e doubl e x;pr i vat e doubl e y;Number ( ) { x = y = 0; }Number ( doubl e a, doubl e b) { x = a; y = b; }publ i c doubl e add( ) { r et ur n x + y; }publ i c doubl e sub( ) { r et ur n x - y; }publ i c doubl e mul ( ) { r et ur n x * y; }publ i c doubl e di v( ) { r et ur n x / y; }publ i c doubl e mod( ) { r et ur n x % y; }
}
7/25/2019 Java Programming - Chapter 12 (in Thai)
11/24
167
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
// 2 no2 Number
// no2
} //End of main} //End of class
4.
1.
1) public,privateprotected (Where)
modifierme()(A)modifierme()
modifierme() modifierme()
2
3
4
Class BClass A
modifierme()
Class D
Package java.test1 Package java.test2
Class C Extends A
Methods in A callmodifierme()1
Methods in B call
modifier me()2
Methods in C callmodifier me()3
Methods in D callmodifier me()4
1
7/25/2019 Java Programming - Chapter 12 (in Thai)
12/24
168
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
( )
publ i c me( )
pri vate me( ) prot ect ed me( ) me( )
privatepublic
2) static
(How)
(stat i c) (stat i c)
7 [] (18 )1)
data
2) var10
3) check
4) stdName 351
7/25/2019 Java Programming - Chapter 12 (in Thai)
13/24
169
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
5) m8 x 5
6)
show show
7) search x num
8) mulMatrix
2
9)
union 2
2. 1) (
8-11 ) stat i cstat i c
2) (stat i c) (1)
(Class Variable)
Mat h. PI ; Col or . RED;
. ;
stat i c stat i c stat i c stat i c stat i c stat i c stat i c stat i c
7/25/2019 Java Programming - Chapter 12 (in Thai)
14/24
170
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
(2)(Class Method)
Mat h. pow( 2, 3) ; Ti me. st ar t ( ) ;
3) (stat i c) (1)
(Object Variable)(Instance Variable)
obj St d. i d; no. x; g. y;
(2)
(Object Method) (Instance Method)
st d. get ( 5230121) ; c. st op( ) ;
8 [] (20 )(A) Class Variable (B) Class Method (C) Object Variable (D) Object Method
1. Mat h. sqr t ( x) 11. st d. gr ad
2. p. col or Code( s) 12. Sqt . bor der s( a, b)
3. Ar r ay. equal s( a, b) 13. i n. r eadLi ne( )
4. v. x 14. Syst em. i n
5. System. get Pr oper t i es( ) 15. Math. r andom( )
6. I nt eger . MAX_VALUE 16. i n. hasNext ( )
7. kb. next I nt ( ) 17. i . i d_code
8. r ectangl e. set Si ze( w, h) 18. out . c l ose( )
9. Mat h. PI 19. St r i ng. f or mat ( "%4d", x)
10. a. appendAr r ays(x, y) 20. o. name
. ( ) ;
. ;
. ( ) ;
static
..
stat i c
7/25/2019 Java Programming - Chapter 12 (in Thai)
15/24
171
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
9 [] (15 )
10 [] Dice n 6 12 TestDiceDice(20 )
public class Dice {
//publicface //publicvalue
// 2
1234
56789101112
publ i c cl ass Val {publ i c stat i c i nt x;publ i c stat i c i nt y;publ i c st at i c St r i ng s;
publ i c Val ( ) { x = 0; y = 0; s = " " ; }publ i c Val ( i nt x, i nt y) { t hi s. x = x; t hi s. y = y; }publ i c st at i c i nt get X( ) { r et ur n x; }publ i c stat i c i nt get Y( ) { r et ur n y; }publ i c stat i c i nt get Y( i nt y) { r et ur n y; }publ i c st at i c St r i ng get S( ) { r et ur n s; }publ i c st at i c St r i ng get S( St r i ng s) { r et ur n s; }
}
1234
567891011121314151617
181920
publ i c cl ass Test {publ i c stati c voi d mai n( St r i ng[ ] ar gs) {
Syst em. out . pr i nt l n( Val . x + ", " + Val . y) ;Val . x = 6; Val . y = 10;
Syst em. out . pr i nt l n( Val . get X( ) + ", " + Val . get Y( ) ) ;Val . s = "Good By A" ;System. out . pr i nt l n( Val . get S( ) ) ;Syst em. out . pr i nt l n( Val . get S( "See You F") ) ;Val v = new Val ( ) ;Syst em. out . pr i nt l n( v. x + ", " + v. y) ;v. x = 5; v. y = 7;Syst em. out . pr i nt l n( v. get X( ) + ", " + v. get Y( ) ) ;Syst em. out . pr i nt l n( Val . get X( ) + ", " + Val . get Y( ) ) ;Val w = new Val ( 9, 7) ;Syst em. out . pr i nt l n( w. get X( ) + ", " + w. get Y( ) ) ;Syst em. out . pr i nt l n( v. get X( ) + ", " + v. get Y( ) ) ;v. s = "I l ove J ava";
Syst em. out . pr i nt l n( w. get S( ) ) ;}}
-stat i cGlobal
7/25/2019 Java Programming - Chapter 12 (in Thai)
16/24
172
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
//
//
//roll 1 value
//setValue value
//getFace
7/25/2019 Java Programming - Chapter 12 (in Thai)
17/24
173
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
//getValue
//show
} //End of class
public class TestDice {
/* 3 d1, d2d3d1 d2 13 d3 30 9
3 3 20 3 */
} //End of class
7/25/2019 Java Programming - Chapter 12 (in Thai)
18/24
174
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
11 [] RealNumber -TestRealNumberRealNumber(15 )
public class RealNumber {
//
public
num
//numDefault
// num
//plus
num
//diff
num
} //End of class
7/25/2019 Java Programming - Chapter 12 (in Thai)
19/24
175
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
public class TestRealNumber {
/* 1 r 70 RealNumber15 50 */
} //End of class
12 [] Account (10 )1) balance Object Variable 2) 2
0
3)
deposit() Object Method
4) withdraw() Object Method
5)
getbalance() Object Method
7/25/2019 Java Programming - Chapter 12 (in Thai)
20/24
176
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
TestAccount Account 1 2000 1500 800
() (10 )
7/25/2019 Java Programming - Chapter 12 (in Thai)
21/24
177
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
13 [] PiggyBank 1 2 5 10 (20 )
public class PiggyBank {
(1)publicone, two, fiveten
(2)
publicsize
(3)2 1) size100 02) sizesize
0
(4)clear() (0)
7/25/2019 Java Programming - Chapter 12 (in Thai)
22/24
178
2553 ( 7 2/2553) ( )
Computer Programming using Java 12 - Classes and Objects
(5)getTotal()
(6)full() truefalse
(7)addOne() 1
(8)addTwo() 2
7/25/2019 Java Programming - Chapter 12 (in Thai)
23/24
179
2553 ( 7 2/2553) ( )
Computer Programming using JavaClasses and Objects - 12
(9)
addFive()5
(10) addTen()10
}//End of class
7/25/2019 Java Programming - Chapter 12 (in Thai)
24/24
180 Computer Programming using Java 12 - Classes and Objects
TestPiggyBank PiggyBank 500 34 13
public class TestPiggyBank {public static void main(String[] args) {
}//End of main}//End of class