1. class method 클래스 메소드
(1) 문제
HowMethod에서 클래스 변수나 클래스 메소드로 변경할 수 있는 부분은 변경.
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
class SimpleMath // 단순 계산 클래스
{
public static final double PI=3.1415;
public double add(double n1, double n2){ return n1+n2; }
public double min(double n1, double n2){ return n1-n2; }
public double mul(double n1, double n2){ return n1*n2; }
}
class AreaMath // 넓이 계산 클래스
{
public double calCircleArea(double rad)
{
SimpleMath sm=new SimpleMath();
double result=sm.mul(rad, rad);
result=sm.mul(result, SimpleMath.PI);
return result;
}
public double calRectangleArea(double width, double height)
{
SimpleMath sm=new SimpleMath();
return sm.mul(width, height);
}
}
class PerimeterMath // 둘레 계산 클래스
{
public double calCirclePeri(double rad)
{
SimpleMath sm=new SimpleMath();
double result=sm.mul(rad, 2);
result=sm.mul(result, SimpleMath.PI);
return result;
}
public double calRectanglePeri(double width, double height)
{
SimpleMath sm=new SimpleMath();
return sm.add(sm.mul(width, 2), sm.mul(height, 2));
}
}
class HowMethod
{
public static void main(String[] args)
{
AreaMath am=new AreaMath();
PerimeterMath pm=new PerimeterMath();
System.out.println("원의 넓이: "+am.calCircleArea(2.4));
System.out.println("직사각형 둘레: "+pm.calRectanglePeri(2.0, 4.0));
}
}
|
cs |
(2) 풀이
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
33
34
35
36
37
38
39
40
41
42
43
44
|
class SimpleMath // 단순 계산 클래스
{
public static final double PI=3.1415;
public static double add(double n1, double n2){ return n1+n2; }
public static double min(double n1, double n2){ return n1-n2; }
public static double mul(double n1, double n2){ return n1*n2; }
}
class AreaMath // 넓이 계산 클래스
{
public static double calCircleArea(double rad)
{
double result=SimpleMath.mul(rad, rad);
result=SimpleMath.mul(result, SimpleMath.PI);
return result;
}
public static double calRectangleArea(double width, double height)
{
return SimpleMath.mul(width, height);
}
}
class PerimeterMath // 둘레 계산 클래스
{
public static double calCirclePeri(double rad)
{
double result=SimpleMath.mul(rad, 2);
result=SimpleMath.mul(result, SimpleMath.PI);
return result;
}
public static double calRectanglePeri(double width, double height)
{
return SimpleMath.add(SimpleMath.mul(width, 2), SimpleMath.mul(height, 2));
}
}
class HowMethod
{
public static void main(String[] args)
{
System.out.println("원의 넓이: "+AreaMath.calCircleArea(2.4));
System.out.println("직사각형 둘레: "+PerimeterMath.calRectanglePeri(2.0, 4.0));
}
}
|
cs |
'Java > Day7' 카테고리의 다른 글
[Java] access method ( getter & setter 게터앤세터) (0) | 2021.11.08 |
---|---|
[Java] 기본값 & 접근 제어 지시자와 접근 허용범위 (0) | 2021.11.08 |
[Java] class path 클래스 패스 & 패키지 (배치 프로그램 실행하기) (0) | 2021.11.07 |