༄༊࿆水下月ོྂཾ࿆࿐ 2023-03-26 16:42 采纳率: 98%
浏览 99
已结题

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

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

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

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

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

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

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

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

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

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

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

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");
                    continue;
                }
                else {
                    shape = new Rectangle(a,b);
                    System.out.printf("%.2f",shape.length());
                }
            } else if (sc.hasNextDouble()) {//三角形
                double b = sc.nextDouble();
                double c = sc.nextDouble();
                shape = new Triangle(a,b,c);
                System.out.printf("%.2f",shape.length());
            }
            else {//圆形
                shape = new Circle(a);
                System.out.printf("%.2f",shape.length());
            }
        }
            sc.close();
        }
        }
    interface Shape {
        double length();
    }
    class Triangle implements Shape{//三角形
        private double a,b,c;
        public void 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 void 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 void Circle (double radius){
            this.radius=radius;
        }
        public double length() {
            if(radius<=0){
                return 0;
            }
            else {
                return 2*3.14*radius;
            }
        }
    }

img


为什么会有这样的提示?应该怎么更改?

  • 写回答

3条回答 默认 最新

  • 心寒丶 全栈领域优质创作者 2023-03-28 09:48
    关注
    public interface Shape {
         double length();
    }
    
    public class Circle implements Shape {
        private double radius;
        public Circle(double radius) {
            this.radius = radius;
        }
        public double length() {
            if (radius <= 0) {
                // 圆不合法
                return 0;
            }
            return 2 * 3.14 * radius;
        }
    }
    
    
    public class Rectangle implements Shape {
        private double length;
        private double width;
        public Rectangle(double length, double width) {
            this.length = length;
            this.width = width;
        }
        public double length() {
            if (length <= 0 || width <= 0) {
                // 长方形不合法
                return 0;
            }
            return 2 * (length + width);
        }
    }
     
    
    
    public class Triangle implements Shape {
        private double a;
        private double b;
        private double c;
        public Triangle(double a, double b, double c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
        public double length() {
            if (a + b <= c || a + c <= b || b + c <= a) {
                // 三角形不合法
                return 0;
            }
            return a + b + c;
        }
    }
    
    
    public class ShapeTest {
         public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                while (sc.hasNext()) {
                    double a = sc.nextDouble();
                    if (a <= 0) {
                        // 不合法的输入,周长为0
                        System.out.println(0);
                        continue;
                    }
                    Shape shape;
                    if (sc.hasNextDouble()) {
                        // 长方形
                        double b = sc.nextDouble();
                        shape = new Rectangle(a, b);
                    } else if (sc.hasNextDouble()) {
                        // 三角形
                        double b = sc.nextDouble();
                        double c = sc.nextDouble();
                        shape = new Triangle(a, b, c);
                    } else {
                        // 圆形
                        shape = new Circle(a);
                    }
                    System.out.println(shape.length());
                }
                sc.close();
            }
    }                  
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月7日
  • 已采纳回答 3月30日
  • 修改了问题 3月30日
  • 修改了问题 3月28日
  • 展开全部

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度