2018 Java Chapter 11 (Week 11)

From: 2018-09-01 00:00:00 To: 2018-12-26 23:59:59 Now: 2024-11-23 17:21:59 Status: Public

D - Triangle 클래스

Time Limit: 1s Memory Limit: 128MB

Snippet Judge Submissions: 784 Solved: 340
Description

GeometricObject 클래스를 상속받은 Triangle이라는 이름의 클래스를 만드시오. 이 클래스는 다음을 포함한다 :

  • 변수 side1, side2, side3. 삼각형의 세 변을 의미하는 double형 변수이며, 디폴트 값은 1.0이다.
  • 인자를 받지 않는 생성자(constructor). 디폴트 값을 갖는 삼각형을 만든다.
  • 인자를 받는 생성자(constructor). side1, side2, side3를 지정하며 삼각형을 만든다.
  • 세 개의 변수에 접근하는 접근자(accessor)
  • 삼각형의 넓이를 반환하는 메소드 getArea()
  • 삼각형의 둘레의 길이를 반환하는 메소드 getPerimeter()
  • 삼각형에 대한 묘사를 반환하는 메소드 toString(). 삼각형의 넓이를 계산하는 공식은 연습문제 2.19를 참고하시오.

toString() 메소드는 다음과 같이 구한한다 : return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;

 

(The Triangle class) Design a class named Triangle that extends GeometricObject. The class contains:

  • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.
  • A no-arg constructor that creates a default triangle.
  • A constructor that creates a triangle with the specified side1, side2, and side3.
  • The accessor methods for all three data fields.
  • A method named getArea() that returns the area of this triangle.
  • A method named getPerimeter() that returns the perimeter of this triangle.
  • A method named toString() that returns a string description for the triangle. For the formula to compute the area of a triangle, see Programming Exercise 2.19.

The toString() method is implemented as follows: return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;

Input

* Line 1 : 테스트케이스 T (1~1,000)

* Line 2 ~ T+1 : side1 side2 side3 color filled

 - side1, side2, side3 는 0~10사이의 실수

 - color는 공백없이 길이가 50을 넘지 않는 문자열

 - filled는 true 또는 false

Output

Sample Output 형식으로 출력

숫자는 소숫점 둘째자리 까지 출력하시오. (DecimalFormat("##.00") 참고)

Sample Code
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int T = sc.nextInt();
        for (int t = 0; t < T; t++) {
            double side1 = sc.nextDouble();
            double side2 = sc.nextDouble();
            double side3 = sc.nextDouble();

            GeometricObject triangle = new Triangle(side1, side2, side3);
            String color = sc.next();
            triangle.setColor(color);

            boolean filled = sc.nextBoolean();
            triangle.setFilled(filled);
            DecimalFormat df = new DecimalFormat("##.00");
            System.out.println("The area is " + df.format(triangle.getArea()));
            System.out.println("The perimeter is " + df.format(triangle.getPerimeter()));
            System.out.println(triangle);
        }
    }
}

abstract class GeometricObject {
    private String color = "white";
    private boolean filled;

    /**
     * Default construct
     */
    protected GeometricObject() {
    }

    /**
     * Construct a geometric object
     */
    protected GeometricObject(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
    }

    /**
     * Getter method for color
     */
    public String getColor() {
        return color;
    }

    /**
     * Setter method for color
     */
    public void setColor(String color) {
        this.color = color;
    }

    /**
     * Getter method for filled. Since filled is boolean,
     * so, the get method name is isFilled
     */
    public boolean isFilled() {
        return filled;
    }

    /**
     * Setter method for filled
     */
    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    /**
     * Abstract method findArea
     */
    public abstract double getArea();

    /**
     * Abstract method getPerimeter
     */
    public abstract double getPerimeter();
}

YOUR_CODE
Sample Input
5
5.22856359077 8.64369661889 7.81208777215 red false
4.73859498495 5.28984483026 6.43911724765 blue true
9.85370908953 7.66252345724 4.89050910369 orange true
6.28459944776 9.74426851916 4.98337330199 green false
7.63836147698 7.61050092554 7.24533208254 violet true
Sample Output
The area is 20.14
The perimeter is 21.68
Triangle: side1 = 5.23 side2 = 8.64 side3 = 7.81
The area is 12.33
The perimeter is 16.47
Triangle: side1 = 4.74 side2 = 5.29 side3 = 6.44
The area is 18.38
The perimeter is 22.41
Triangle: side1 = 9.85 side2 = 7.66 side3 = 4.89
The area is 13.66
The perimeter is 21.01
Triangle: side1 = 6.28 side2 = 9.74 side3 = 4.98
The area is 24.30
The perimeter is 22.49
Triangle: side1 = 7.64 side2 = 7.61 side3 = 7.25
Source

JAVA2015 PE11.1