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

义接口或类 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


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

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();
        }
}                  


java代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            double[] input = new double[3];
            for (int i = 0; i < input.length; i++) {
                input[i] = scanner.nextDouble();

                // 如果输入中有0或负数,即不构成任何图形
                if (input[i] <= 0) {
                    System.out.println("0.00");
                    continue;
                }
            }

            if (input[0] == 0 && input[1] == 0 && input[2] == 0) {
                break;
            }

            Shape shape;
            if (input[1] == 0 && input[2] == 0) { // 圆形
                shape = new Circle(input[0]);
                System.out.printf("%.2f\n", shape.length());
            } else if (input[2] == 0) { // 长方形
                shape = new Rectangle(input[0], input[1]);
                System.out.printf("%.2f\n", shape.length());
            } else { // 三角形
                if (isValidTriangle(input[0], input[1], input[2])) {
                    shape = new Triangle(input[0], input[1], input[2]);
                    System.out.printf("%.2f\n", shape.length());
                } else {
                    System.out.println("0.00");
                }
            }
        }
        scanner.close();
    }

    private static boolean isValidTriangle(double a, double b, double c) {
        if (a + b <= c || a + c <= b || b + c <= a) {
            return false;
        }
        return true;
    }
}

您可以使用以上代码进行运行和验证。

你的 shape 接口 y 应该单独写一个类吧