서노썬
sun noes sun
서노썬
전체 방문자
오늘
어제
  • 카테고리 (142)
    • Java (89)
      • Day1 (20)
      • Day2 (16)
      • Day3 (4)
      • Day4 (5)
      • Day5 (2)
      • Day6 (2)
      • Day7 (4)
      • Day8 (6)
      • Day9 (3)
      • Day10 (0)
      • Day11 (0)
      • Day12 (0)
      • Day13 (3)
      • Day14 (0)
      • Day15 (0)
      • Day16 (0)
      • Day19 (0)
      • Day20 (0)
      • Day21 (2)
      • Day22 (4)
      • Day23 (2)
      • Day24 (5)
      • Day25 (4)
      • Day27 (2)
      • Day28 (3)
      • Day29 (1)
      • Day30 (1)
      • Day31 (0)
      • Day32 (0)
      • Dat33 (0)
      • Day34 (0)
      • Day35 (0)
      • Day36 (0)
    • HTML (37)
      • Day36 (20)
      • Day37 (3)
      • Day38 (2)
      • Day39 (8)
      • Day40 (3)
    • SQL (4)
      • Day40 (4)
      • Day41 (0)
      • Day42 (0)
      • Day43 (0)
      • Day44 (0)
      • Day45 (0)
    • JSP (0)
      • Day46 (0)
      • Day75 (0)
    • PYTHON (0)
      • Day75 (0)
      • Day76 (0)
    • Photo (12)

블로그 메뉴

  • 홈
  • 방명록

인기 글

최근 글

태그

  • java데이터타입
  • java자료형
  • 자바자료형
  • 자바
  • java메뉴입력
  • 논리연산자
  • 자바break
  • Java
  • 자바continue
  • 자바연산자

티스토리

hELLO · Designed By 정상우.
서노썬

sun noes sun

Java/Day7

[Java] class method 클래스 메소드

2021. 11. 8. 00:14

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));
    }
}
Colored by Color Scripter
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));
    }
}
Colored by Color Scripter
cs

 

'Java > Day7' 카테고리의 다른 글

[Java] access method ( getter & setter 게터앤세터)  (0) 2021.11.08
[Java] 기본값 & 접근 제어 지시자와 접근 허용범위  (0) 2021.11.08
[Java] class path 클래스 패스 & 패키지 (배치 프로그램 실행하기)  (0) 2021.11.07
    'Java/Day7' 카테고리의 다른 글
    • [Java] access method ( getter & setter 게터앤세터)
    • [Java] 기본값 & 접근 제어 지시자와 접근 허용범위
    • [Java] class path 클래스 패스 & 패키지 (배치 프로그램 실행하기)

    티스토리툴바