글 목록

최신 글과 검색 결과
DEVELOPMENT/JAVA

[7번째 과제] 원 넓이 메소드 추가 ? 두개의 넓이 구하기

간지뽕빨리턴님

이 글의 목차

    반응형
      자바

    과제 일 : 19-09-30

    과제 내용

    [7번째 과제] 교재 269 Point class 를 활용한 Circle class 만들고, 아래와 같이 수정해 볼 것.

    1) Circle class 에 원의 면적을 구하는 method(예: double calcArea()) 를 추가할 것.
    2) 두개의 원을 생성하고, 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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    // 파일 명 : Circle.java
    public class Circle {
        private int radius;
        private Point center;
        double old = 3.14;
        
        public Circle(Point p, int r, int won1) {
            center = p;
            radius = r;
            
        }
        
        public double CircleArea() {
            return radius * radius * old;
        }
        
        
        @Override
        public String toString() {
            return "원 결과 : " + CircleArea() + ", " + "[반지름은 " + radius + ", 포인트 : " + center + "]";
        }
    }
     
     
    // 파일 명 : CircleTest.java
    import java.util.Scanner;
     
    public class CircleTest {
     
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int won1;
            int won2;
            Scanner CircleRt = new Scanner(System.in);
            System.out.println("첫번째 원 반지름은?");
            won1 = CircleRt.nextInt();
            
            Point p = new Point(2575);
            Circle c = new Circle(p, won1,won1);
            System.out.println(c);
            
            System.out.println("두번째 원 반지름은?");
            won1 = CircleRt.nextInt();
            
            Point o = new Point(3090);
            Circle e = new Circle(o, won1, won1);
            System.out.println(e);
     
     
        }
     
    }
     
    // 파일 명 : point.java
     
    public class Point {
        private int x,y;
        
        
        public Point(int a, int b) {
            x = a;
            y = b;
        }
        
        @Override
        public String toString() {
            return " [x=" +  x +  ", y= " + y + "]";
        }
     
    }
     
    //------------------------------------------------------------------------
    // 과제 
    Choe yeong hwan
    cs

     

     

    출력 화면