7-4 sdut-oop-6 计算各种图形的周长(多态)

定义接口或类 Shape,定义求周长的方法length()。

定义如下类,实现接口Shape或父类Shape的方法。

(1)三角形类Triangle (2)长方形类Rectangle (3)圆形类Circle等。

定义测试类ShapeTest,用Shape接口(或类)定义变量shape,用其指向不同类形的对象,输出各种图形的周长。并为其他的Shape接口实现类提供良好的扩展性。

提示: 计算圆周长时PI取3.14。

输入格式:

输入多组数值型数据(double);

一行中若有1个数,表示圆的半径;

一行中若有2个数(中间用空格间隔),表示长方形的长度、宽度。

一行中若有3个数(中间用空格间隔),表示三角形的三边的长度。(需要判断三个边长是否能构成三角形)

若输入数据中有0或负数,则不表示任何图形,周长为0。

输出格式:

行数与输入相对应,数值为根据每行输入数据求得的图形的周长。

样例">输入样例:

在这里给出一组输入。例如:

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

输出样例:

在这里给出相应的输出。例如:

6.28
10.00
15.00
12.56
0.00
0.00

代码示例:

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String str=sc.nextLine();
            String[] str1=str.split(" ");
            if(str1.length==1){
                double r=Double.parseDouble(str1[0]);
                if(r>0){
                    Shape shape=new Circle(r);
                    System.out.printf("%.2f\n",shape.length());
                }
                else{
                    System.out.println("0.00");}
            }
            if(str1.length==2){
                double a=Double.parseDouble(str1[0]);
                double b=Double.parseDouble(str1[1]);
                if(a>0&&b>0){
                    Shape shape=new Rectangle(a,b);
                    System.out.printf("%.2f\n",shape.length());
                }
                else{
                    System.out.println("0.00");
                }
            }
            if(str1.length==3){
                double a=Double.parseDouble(str1[0]);
                double b=Double.parseDouble(str1[1]);
                double c=Double.parseDouble(str1[2]);
                if(a>=0&&b>=0&&c>=0){
                    Shape shape=new Triangle(a,b,c);
                    System.out.printf("%.2f\n",shape.length());
                }
                else{
                    System.out.println("0.00");
                }
            }
        }
    }
}
abstract class Shape{
    abstract double length();

}

class Circle extends Shape{
    double r;
    public Circle(double r){
        this.r=r;
    }
    public void setR(double r){
        this.r=r;
    }
    public double getR(double r){
        return r;
    }
    public double length(){
        return 2*3.14*r;
    }
}

class Rectangle extends Shape{
    double a;
    double b;
    public Rectangle(double a,double b){
        this.a=a;
        this.b=b;
    }
    public void setA(double a){
        this.a=a;
    }
    public void setB(double b){
        this.b=b;
    }
    public double length(){
        return 2*(a+b);
    }
}

class Triangle extends Shape{
    double a;
    double b;
    double c;
    public Triangle(double a,double b,double c){
        this.a=a;
        this.b=b;
        this.c=c;
    }
    public void setA(double a){
        this.a=a;
    }
    public void setB(double b){
        this.b=b;
    }
    public void setC(double c){
        this.c=c;
    }

    public double length(){
        if(a+b>c&&a+c>b&&b+c>a){
            return a+b+c;
        }
        else{
            return 0;
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值