You are on page 1of 24

Computer Programming using Java

Classes and Objects - 12

CHAPTER

157

(Classes and Objects)

12

1. (Introduction to Classes and Objects)


1.
Package
Class A

Class B

Class C

Attributes

Attributes

Attributes

Constructor

Constructor

Constructor

Methods

Methods

Methods

new

new

new

Object a

Object b

Object c

Attributes

Attributes

Attributes

Method-1
.
.
.

Method-1
.
.
.

Method-1
.
.
.

Method-N

Method-N

Method-N

(Package)
(Classes)

(Attributes)
(Constructors) (Methods)


(Instance of Class)
(Objects)


( )

2.
(Package)

()

(Class)

(Attribute)

(Constructor)

(Method)

(Variable)

(Statement)

(if, for, )

(Object)

( )

(Object-Oriented Programming)
2553 ( 7 2/2553) ()

158

Computer Programming using Java

12 - Classes and Objects

1 [ ] (10 )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

public class Num {


private int x;
private double y;
public Num() {
x = 0; y = 0.0;
}
public Num(int m, double n) {
x = m; y = n;
}
public int addNX(int n) {
return n + x;
}
public double addNY(double n) {
return n + y;
}
public void showXY() {
System.out.println(x);
System.out.println(y);
}
}

1
2
3
4
5
6
7
8
9
10

public class RunNum {


public static void main(String[] args){
Num obj = new Num(5, 7.0);
obj.showXY();
int a = obj.addNX(4);
double b = obj.addNY(13.0);
System.out.println(a);
System.out.println(b);
}
}

1 Class Diagram
Num
obj: Num

RunNum

int x
double y
x = 5
y = 7.0
Num()
Num(int m, double n)

int addNX(int n)
double addNY(double n)
void showXY()

public static void main() {


Num obj = new Num(5, 7.0);

new
addNX(int n)
addNY(double n)
showXY()

obj.showXY();
int a = obj.addNX(4);

...
}

2 [ ] (;) (:) (15 )


1)
1 ( )
2)
main()
3)

4)
( )
2553 ( 7 2/2553) ()

Computer Programming using Java

Classes and Objects - 12

5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)

159

2. (Classes)
1.
3
1)
[ ] class <> {

[ ( )]
[ ( )]
[ ( )]
}.



(Modifier)
public final

1
2
3
4
5
6
7
8

public class Num {


int x;
public double y;
Num() {
x = 0; y = 0.0;
}
...
}

*.java
public
1
( )
public (
)

2) 2
[ ] <
> < >;

(Modifier)

+ public
- private
# protected
static
final

2553 ( 7 2/2553) ()

160

Computer Programming using Java

12 - Classes and Objects

1
2
3
4
5

public class Data {


public static int n = 1;
private double data;
...
}

static
static

3)
(Constructor) ( )
[ ] < > ([ ]) {
[
]
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Return Type

public

public class Number {



public long x;
private int y;
Overload
public Number() {
x = 0L;

y = 0;
}
public Number(long a, int b) {
x = a;
y = b;

}
public Number(double k) {
(Default Constructor)
x = y = (int) k;

}
...

}

4) 8
[ ] <
> <> ([ ]) {
[]
}

1
2
3
4
5
6
7
8

public class Operation {


...
public int incrX(int x) {
x = x + 1;
return x;
}
...
}


public, private, protected,
static final
8 ( )

2553 ( 7 2/2553) ()

Classes and Objects - 12

Computer Programming using Java

161

3 [ ]
Course
(30 )
public class Course {

// 3 ( ) (3 )
// id private (Course ID)

// title public (Course Title)

// credit protected (Credit)

// 3
( int 0, String "" ) (3 )

// 3 3 (3 )

// setID() public
id (3 )

2553 ( 7 2/2553) ()

162

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 )

2553 ( 7 2/2553) ()

Classes and Objects - 12

Computer Programming using Java

163

// 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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

x y
getX() x
public class Test {
private static int x = 10;
getX(int x)
private static int y = 1;
public static int getX() { return x; }
public static int getX(int x) { return x; }
public static int incX(int y) { return x + y; } x x
public static void main(String[] args) {
y y
int x = 5;
System.out.println(x);
5
System.out.println(y);
1
x x
System.out.println(getX());
10
System.out.println(getX(2));
2
y y
System.out.println(incX(x));
15
}
}
x main() x getX(int x)

2553 ( 7 2/2553) ()

164

Computer Programming using Java

12 - Classes and Objects

4)
public class TestClass {

Attribute

public int add(


3

Parameter

) {

Variable

return ;
} //End of method
} //End of class

4 [ ] (10 )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

public class Test {


private static int x = 5;
private static int y = 2;
public static void main(String[] args) {
int x = 9;
System.out.println(x);
System.out.println(getX());
System.out.println(showX());
y = 1;
System.out.println(y);
System.out.println(getY());
System.out.println(showY());
int y = 11;
System.out.println(y);
System.out.println(getY());
System.out.println(showY());
}
public static int getX() {
return x;
}
public static int getY() {
return y;
}
public static int showX() {
int x = 7;
return x;
}
public static int showY() {
int y = 12;
return y;
}
}

1) y 10
y y

2) y 14
y y

3. (Objects)
1.
1)



( )
2553 ( 7 2/2553) ()

Computer Programming using Java

Classes and Objects - 12

2)
Class A

Object a

New Object

Constructor

Method-1
.
.
.

Methods

Method-N

2.
1)

Student std;

Car car;

a.attr1 = 0
a.method1()
a.method2()

<> < >;

Attributes

Attributes

165

Account obj;

2) ( new)
< > = new <>();

std = new Student(52300121); / car = new Car(); / obj = new Account(101);

3)

1 2 ( )
<> < > = new <>();
<> < > = new < >;

<>()
(Constructor)

Student std = new Student(52300121); / Account obj = new Account(101);




4)
1
2
3
4
5
6
7
8
9
10
11
12

public class Digit {


public int x;
Digit() {

x = 0;
}
Digit(int n) {
x = n;
}
public int getX(){
return x;
}
}

1
2
3
4
5
6
7
8
9
10

public class TestDigit {


public static void main(String[] args){
Digit d = new Digit();
Digit e = new Digit(1);

Digit f = new Digit(2);



int digit1 = d.getX();
int digit2 = e.getX();

int digit3 = f.getX();
}
}
main

+ +

2553 ( 7 2/2553) ()

166

Computer Programming using Java

12 - Classes and Objects

5 [ ] (8 )
1
2
3
4
5
6
7
8
9
10
11
12

public class Student {



public int id;
public String name;
Student() { id = 0; name = ""; }
Student(int i, String n) { id = i; name = n; }
public String getName() { return name; }
public int getID() { return id; }
public char calGrad(int score) {
if(score > 60) { return 'S'; }
else { return 'U'; }
}
}

1
2
3
4
5
6
7
8
9
10
11
12

public class TestStudent {


public static void main(String args[]){
Student y = new Student(101,"Taksin");
Student z = new Student(102,"Apisit");
Student x = new Student();
System.out.println(x.getID() + "," + x.getName());
System.out.println(y.getID() + "," + y.getName());
System.out.println((z.getID() + 1) + "," + z.getName());
System.out.println(y.getName()+ ":" + y.calGrad(49));
System.out.println(z.getName()+ ":" + z.calGrad(79));
}
}

6 [ ] TestNumber
Number
Number (10 )
1
2
3
4
5
6
7
8
9
10
11

public class Number {


private double x;
private double y;
Number() { x = y = 0; }
Number(double a, double b) {
public double add() { return
public double sub() { return
public double mul() { return
public double div() { return
public double mod() { return
}


x
x
x
x
x
x

=
+
*
/
%

a;
y;
y;
y;
y;
y;

y = b; }
}
}
}
}
}

// TestNumber
import java.util.Scanner;

public static void main(String[] args) {


Scanner kb = new Scanner(System.in);

// no1 Number ( Number)

2553 ( 7 2/2553) ()

Computer Programming using Java

Classes and Objects - 12

167

// 2 no2 Number

//
no2

} //End of main
} //End of class

4.
1.
1) public, private
protected
(Where)
Package java.test1
1

Methods in A call modifier me()


4

Methods in D call modifier me()

Methods in B call
modifier me()

Class A

2
3
4

Class D

Class B

modifier me()

Package java.test2

Class C Extends A

Methods in C call modifier me()

modifier
modifier
modifier
modifier

me() ( A)
me()

me()
me()

2553 ( 7 2/2553) ()

168

Computer Programming using Java

12 - Classes and Objects


( )
public me()
9
9
9
9
private me()
9
8
8
8
protected me()
9
9
9
8
me()
9
9
8
8
private public


2) static
(How)




( static)
9
9
( static)
9
8
7 [ ] (18 )
1) data

2) var 10

3) check

4) stdName 351

2553 ( 7 2/2553) ()

Computer Programming using Java

Classes and Objects - 12

169

5) m 8 x 5

6) show
show

7) search x
num

8) mulMatrix

9) union
2

2.
1) (
8-11 ) static static


static static


static static
static static

9
8
static static

9
9
2) ( static)
(1) (Class Variable)
<>.< >;

Math.PI;

Color.RED;

2553 ( 7 2/2553) ()

170

Computer Programming using Java

12 - Classes and Objects

(2) (Class Method)


<>.<>();

Math.pow(2,3);

Time.start();

3) ( static)
(1) (Object Variable)
(Instance Variable)
< >.< >;

objStd.id; no.x; g.y;


(2) (Object Method)
(Instance Method)
< >.<>();

std.get(5230121);

c.stop();

static
static
..
8 [ ] (20 )
(A) Class Variable (B) Class Method
(C) Object Variable (D) Object Method

1. Math.sqrt(x)
11. std.grad
2.

p.colorCode(s)

12.

Sqt.borders(a,b)

3.

Array.equals(a, b)

13.

in.readLine()

4.

v.x

14.

System.in

5.

System.getProperties()

15.

Math.random()

6.

Integer.MAX_VALUE

16.

in.hasNext()

7.

kb.nextInt()

17.

i.id_code

8.

rectangle.setSize(w, h)

18.

out.close()

9.

Math.PI

19.

String.format("%4d", x)

10.

a.appendArrays(x,y)

20.

o.name

2553 ( 7 2/2553) ()

Classes and Objects - 12

Computer Programming using Java

171

9 [] (15 )
1
2
3
4
5
6
7
8
9
10
11
12

public class Val {


public static int x;
public static int y;
public static String s;
public Val() { x = 0; y = 0; s = ""; }
public Val(int x, int y) { this.x = x; this.y = y; }
public static int getX() { return x; }
public static int getY() { return y; }
public static int getY(int y) { return y; }
public static String getS() { return s; }
public static String getS(String s) { return s; }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

public class Test {


public static void main(String[] args) {
System.out.println(Val.x + "," + Val.y);
Val.x = 6; Val.y = 10;
System.out.println(Val.getX()+ "," + Val.getY());
Val.s = "Good By A";
System.out.println(Val.getS());
System.out.println(Val.getS("See You F"));
Val v = new Val();
System.out.println(v.x + "," + v.y);
v.x = 5; v.y = 7;
System.out.println(v.getX() + "," + v.getY());
System.out.println(Val.getX() + "," + Val.getY());
Val w = new Val(9, 7);
System.out.println(w.getX() + "," + w.getY());
System.out.println(v.getX() + "," + v.getY());
v.s = "I love Java";
System.out.println(w.getS());
}
}


static

Global

10 [] Dice
n
6

12 TestDice Dice (20 )
public class Dice {

// public face

// public value

//
2

2553 ( 7 2/2553) ()

172

Computer Programming using Java

12 - Classes and Objects

//


//


// roll


1

value

// setValue


value

// getFace

2553 ( 7 2/2553) ()

Classes and Objects - 12

Computer Programming using Java

173

// getValue

// show

} //End of class
public class TestDice {

/*
d1, d2 d3
3

d1

d2
13
d3
30 9

3

3
20

3 */

} //End of class

2553 ( 7 2/2553) ()

174

Computer Programming using Java

12 - Classes and Objects

11 [] RealNumber TestRealNumber RealNumber (15 )


public class RealNumber {

// public num

// num Default

// num

// plus
num

// diff
num

} //End of class

2553 ( 7 2/2553) ()

Classes and Objects - 12

Computer Programming using Java

175

public class TestRealNumber {

/* 1
r 70 RealNumber

15 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

2553 ( 7 2/2553) ()

176

Computer Programming using Java

12 - Classes and Objects

TestAccount Account 1
2000 1500 800
( ) (10 )

2553 ( 7 2/2553) ()

Classes and Objects - 12

Computer Programming using Java

177

13 [] PiggyBank


1 2 5 10 (20 )
public class PiggyBank {

(1) public one, two, five ten



(2) public size

(3) 2
1) size 100 0
2) size size
0

(4) clear()
( 0)

2553 ( 7 2/2553) ()

178

Computer Programming using Java

12 - Classes and Objects

(5) getTotal()

(6) full() true


false

(7) addOne() 1

(8) addTwo() 2

2553 ( 7 2/2553) ()

Classes and Objects - 12

Computer Programming using Java

179

(9) addFive() 5

(10) addTen() 10

}//End of class

2553 ( 7 2/2553) ()

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

2553 ( 7 2/2553) ()

You might also like