1. 程式人生 > >3338-計算各種圖形的周長(介面與多型)-JAVA

3338-計算各種圖形的周長(介面與多型)-JAVA

計算各種圖形的周長(介面與多型)

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

定義介面Shape,定義求周長的方法length()。

定義如下類實現介面Shape的抽象方法:

(1)三角形類Triangle (2)長方形類Rectangle (3)圓形類Circle等。

定義測試類ShapeTest,用Shape介面定義變數shape,用其指向不同類形的物件,輸出各種圖形的周長。併為其他的Shape介面實現類提供良好的擴充套件性。

Input

輸入多組數值型資料(double);

一行中若有1個數,表示圓的半徑;

一行中若有2個數(中間用空格間隔),表示長方形的長度、寬度。

一行中若有3個數(中間用空格間隔),表示三角形的三邊的長度。

若輸入資料中有負數,則不表示任何圖形,周長為0。

Output

行數與輸入相對應,數值為根據每行輸入資料求得的圖形的周長(保留2位小數)。

Sample Input

1
2 3
4 5 6
2
-2
-2 -3

Sample Output

6.28
10.00
15.00
12.56
0.00
0.00

Hint

構造三角形時要判斷給定的三邊的長度是否能組成一個三角形,即符合兩邊之和大於第三邊的規則;

計算圓周長時PI取3.14。

Source

import java.util.*;
import java.text.*;

public class Main {
    public static void main(String[] args) {
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        Scanner scanner = new Scanner(System.in);
        ShapeTest shapetest = new ShapeTest();
        while (scanner.hasNext()) {
            Shape shape = null;
            String string = scanner.nextLine();
            String value[] = string.split(" ");
            if (value.length == 3) {
                double a = Double.parseDouble(value[0]);
                double b = Double.parseDouble(value[1]);
                double c = Double.parseDouble(value[2]);
                shape = new Triangle(a, b, c);
            } else if (value.length == 2) {
                double a = Double.parseDouble(value[0]);
                double b = Double.parseDouble(value[1]);
                shape = new Rectangle(a, b);
            } else if (value.length == 1) {
                double r = Double.parseDouble(value[0]);
                shape = new Circle(r);
            }
            double length = 0;
            if (shape != null) {
                shapetest.setShape(shape);
                length = shapetest.length();
            }
            System.out.println(decimalFormat.format(length));
        }
    }
}

interface Shape {
    double length();
}

class Triangle implements Shape {
    double a, b, c;

    public Triangle(double a, double b, double c) {
        if (a < 0 || b < 0 || c < 0) {
            a = 0;
            b = 0;
            c = 0;
        }
        if (a + b > c && a + c > b && b + c > a) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    }

    public double length() {
        return a + b + c;
    }
}

class Rectangle implements Shape {
    double a, b;

    public Rectangle(double a, double b) {
        if (a < 0 || b < 0) {
            a = 0;
            b = 0;
        } else {
            this.a = a;
            this.b = b;
        }
    }

    public double length() {
        return (a + b) * 2;
    }
}

class Circle implements Shape {
    double r;

    public Circle(double r) {
        if (r < 0) {
            r = 0;
        } else {
            this.r = r;
        }
    }

    public double length() {
        return 2 * r * 3.14;
    }
}

class ShapeTest {
    Shape shape;

    public void setShape(Shape shape) {
        this.shape = shape;
    }

    public double length() {
        return shape.length();
    }
}