༄༊࿆水下月ོྂཾ࿆࿐ 2023-03-30 21:10 采纳率: 98%
浏览 28
已结题

计算各种图形的周长(多态)更

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

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

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

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

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

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

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

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

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

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

img


我的代码:

package com.sxt;
import java.util.Scanner;
public class ShapeTest {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner sc=new Scanner(System.in);
        Shape shape;
        while(sc.hasNextDouble()) {
            double a = sc.nextDouble();
            if (a <= 0) {
                System.out.println("0.00");
                continue;
            }
            if (sc.hasNextDouble()) {//长方形
                double b = sc.nextDouble();
                if (b <= 0) {
                    System.out.println("0.00");
                }
                else {
                    shape = new Rectangle(a,b);
                    System.out.printf("%.2f",shape.length());
                    System.out.println();
                }
            } else if (sc.hasNextDouble()) {//三角形
                double b = sc.nextDouble();
                double c = sc.nextDouble();
                shape = new Triangle(a,b,c);
                System.out.printf("%.2f",shape.length());
                System.out.println();
            }
            else {//圆形
                shape = new Circle(a);
                System.out.printf("%.2f",shape.length());
                System.out.println();
            }
        }
        sc.close();
    }
}
interface Shape {
    double length();
}
class Triangle implements Shape{//三角形
    private double a,b,c;
    public 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;
        }
        else if(a+b<=c||a+c<=b||b+c<=a) {
            return 0;
        }
        else {
            return a+b+c;
        }
    }
}
class Rectangle implements Shape{//长方形
    private double a,b;
    public Rectangle(double a,double b){
        this.a=a;
        this.b=b;
    }
    public double length(){
        if(a<=0||b<=0) {
            return 0;
        }
        else {
            return 2*(a+b);
        }
    }
}
class Circle implements Shape{//圆形
    private double radius;
    public Circle (double radius){
        this.radius=radius;
    }
    public double length() {
        if(radius<=0){
            return 0;
        }
        else {
            return 2*3.14*radius;
        }
    }
}

我的运行结果怎么是这样?

img


好像只执行了长方形类,怎么修改?
怎么让它实现,我输入几个数字就输出对应图形的周长?

  • 写回答

1条回答 默认 最新

  • threenewbee 2023-03-30 21:18
    关注
    import java.util.Scanner;
    class Circle implements Shape{
        double dmyr;
        Circle(){
            dmyr = 0;
        }
        Circle(double dmyr){
            this.dmyr = dmyr;
            if(dmyr <= 0) this.dmyr = 0;
        }
        public double length() {
            return 2 * 3.14 * dmyr;
        }
    }
    class Rectangle implements Shape{
        double da, db;
        Rectangle(){
            this.da = this.db = 0;
        }
        Rectangle(double da){
            this.da = this.db = 0;
        }
        Rectangle(double da, double db){
            this.da = da;
            this.db = db;
            if(da <= 0 || db <= 0){
                this.da = this.db = 0;
            }
        }
        public double length() {
            return 2 * (da + db);
        }
    }
    class Triangle implements Shape{
        double da;
        double db;
        double dc;
        Triangle(){
            this.da = this.db = this.dc = 0;
        }
        Triangle(double da){
            this.da = this.db = this.dc = 0;
        }
        Triangle(double da, double db){
            this.da = this.db = this.dc = 0;
        }
        Triangle(double da, double db, double dc){
            this.da = da;
            this.db = db;
            this.dc = dc;
            if(da <= 0 || db <= 0 || dc <= 0 || !(da + db > dc && da + dc > db && db + dc > da)){
                this.da = this.db = this.dc = 0;
            }
        }
        public double length() {
            return da + db + dc;
        }
    }
    interface Shape{
        double length();
    }
    public class Main {
        public static void main(String[] args) {
            Scanner dmy = new Scanner(System.in);
            while(dmy.hasNextInt()) {
                double[] l = new double[5];
                String s;
                s = dmy.nextLine();
                String[] str = s.split(" ");
                int cnt = 0;
                for (String value : str) {
                    l[cnt++] = Double.parseDouble(value);
                }
                if(cnt == 1){
                    Circle C = new Circle(l[0]);
                    System.out.printf("%.2f\n", C.length());
                }
                else if (cnt == 2){
                    Rectangle r = new Rectangle(l[0], l[1]);
                    System.out.printf("%.2f\n", r.length());
                }
                else{
                    Triangle t = new Triangle(l[0], l[1], l[2]);
                    System.out.printf("%.2f\n", t.length());
                }
            }
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月6日
  • 已采纳回答 4月6日
  • 修改了问题 4月1日
  • 创建了问题 3月30日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。