7 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.Scanner;
interface Shape{
 double length();  //定义接口

}
//定义圆的周长
class Circle implements Shape{
    double r;
    double PI=3.14;
    Circle(){
        r=0;
    }
    Circle(double r)
    {
        this.r=r;
        if(r<=0)
            this.r=0;
    }
    @Override
    public double length() {
        return 2*PI*r;
    }
}
//定义长方形
class Rectangle implements Shape{
    double length,weight;
    Rectangle(){
        length=0;
        weight=0;
    }
    Rectangle(double length,double weight)
    {
        this.length=length;
        this.weight=weight;
        if(length<=0||weight<=0)
        {this.length=0;this.weight=0;}
    }
    public double length()
    {
        return (length+weight)*2;
    }
}
//定义三角形
class Triangle implements Shape{
    double a,b,c;
    Triangle()
    {
        a=0;b=0;c=0;
    }
    Triangle(double a,double b,double c)
    {
        this.a=a;this.b=b;this.c=c;


    }
    public double length()
    {
        if(a<=0||b<=0||c<=0)
        {
            return 0;
        }
        if(a+b<=c||a+c<=b||b+c<=a)//是否能构成三角形
           return 0;
        return a+b+c;
    }
}
public class Main {
    public static void main(String[] args) {
        double a,b,c;
        Shape sh;
        Scanner input=new Scanner(System.in);
        String s;
        String []total;
        while(input.hasNextLine())
        {
            double[] Save=new double[5];
           s=input.nextLine();
           total=s.split(" ");
            int cnt = 0;
            for (String value : total) {
                Save[cnt++] = Double.parseDouble(value);}
           if(total.length==1)
           {
               sh=new Circle(Save[0]);
               System.out.printf("%.2f\n",sh.length());
           }
           else if(total.length==2)
           {
               sh=new Rectangle(Save[0],Save[1]);
               System.out.printf("%.2f\n",sh.length());
           }
           else if(total.length==3)
           {
               sh=new Triangle(Save[0],Save[1],Save[2]);
               System.out.printf("%.2f\n",sh.length());
           }
        }
    }
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆粉今天敲了吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值