计算各种图形的周长(接口与多态)(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
zhouxq

import java.text.DecimalFormat;
import java.util.Scanner;
interface Shape{
    double length();
}
// 三角形继承接口
class Triangle implements Shape{
    double x,y,z;
    Triangle(double x,double y,double z){
        if(x > 0 && y > 0 && z > 0) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        else {
            x = y = z = 0;
        }
    }
    public double length() {    //重写接口必须明显声明public权限
        return x+y+z;
    }
}
//长方形实现接口
class Rectangle implements Shape{
    double x,y;
    Rectangle(double x,double y){
        if(x > 0 && y > 0) {
            this.x = x;
            this.y = y;
        }
        else {
            x = y = 0;
        }
    }
    public double length() {
        return (x+y)*2;
    }
}
//圆形实现接口
class Circle implements Shape{
    double r;
    final double PI = 3.14;
    Circle(double r){
        if(r > 0) {
            this.r = r;
        }
        else r = 0;
    }
    public double length() {
        return PI*2*r;
    }
}
class ShapeTest{
    public double length(Shape shape) {
        return shape.length();
    }
}
public class Main {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        ShapeTest shapetest = new ShapeTest();
        DecimalFormat df = new DecimalFormat("0.00");
        while(input.hasNext()) {
            String str = input.nextLine();
            Shape shape = null;
            String[] ch = str.split(" ");
            int i;
            double[] a = new double[50];
            for(i = 0;i < ch.length;i++) {
                a[i] = Double.parseDouble(ch[i]);
            }
            if(i == 1) {
                shape = new Circle(a[0]);
            }
            else if(i == 2) {
                shape = new Rectangle(a[0],a[1]);
            }
            else if(i == 3) {

                for(int j = 0;j < i;j++) {
                    for(int k = 0;k < j;k++) {
                        if(a[k] > a[k+1]) {
                            double t = a[k];
                            a[k] = a[k+1];
                            a[k+1] = t;
                        }
                    }
                }
                if((a[0] + a[1] > a[2]) && a[0] > 0) {
                    shape = new Triangle(a[0],a[1],a[2]);
                }

            }
            if(shape == null) {
                System.out.println("0.00");
            }
            else {
                System.out.println(df.format(shapetest.length(shape)));
            }
        }
        input.close();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40014462/article/details/80087619