输入不匹配异常错误?

时间:2018-05-15 09:01:29

标签: java constructor

我正在写这段代码:

我们专注于三种几何形状:圆形,矩形和方形。 这些形状中的每一个都是几何形状,因此,这些几何形状可以具有包括这些形状的共同特征的父类(geometryShape)。

同时,每个几何形状都是可打印且可比较的对象,以及其他可以打印和比较的对象,因此,这些形状将继承两个界面的可印刷性和可比性:Printable和Comparable。

最后,由于Square是Rectangle的一个特例,Square将从我们设计中的Rectangle扩展。

我的问题是让正方形只接受一个输入值(侧面),而它应该继承矩形的属性(接受2个输入值,宽度和高度)

我尝试在Rectangle类中使用无参数构造函数和超级构造函数,但都没有解决这个问题。

问题是否可能与构造函数以外的东西有关?

矩形类代码:

class Rectangle extends geometricShape {

     private double width, height;
     private double side;

     Rectangle(){
        super();
     }

     Rectangle (double side){
        this.side=side;
     }


     Rectangle(double width, double height) {
          super.setShapeName("Rectangle");
          this.width = width;
          this.height = height;
     }

    public void setSide(double side) {
        this.side = side;
    }

    public double getSide() {
        return side;
    }

    @Override
    public double gerPerimeter() {
        return 2 * (width + height);
    }


    @Override
    public double getArea() {
        return width * height;
    }

    public double getWidth() {
         return width;
    }

    public void setWidth(double width) {
         this.width = width;
    }

    public double getHeight() {
         return height;
    }

    public void setHeight(double height) {
          this.height = height;
    }
}

广场代码:

class Square extends Rectangle {

     double side;

     Square(double side) {
          super.setShapeName("Square");
          this.side = side;
     }

     public double getside() {
           return side;
     }

     public void setside(double s) {
           side = s;
     }

}

这是代码的其余部分(没有方形和矩形)

             public class Inheritance {

public static void main(String[] args) {
    int Select;
    double Value1, Value2;
    String s;
    geometricShape Shapes[] = new geometricShape[2];
    Scanner input = new Scanner(System.in);
    for (int i = 0; i < 2; i++) {
        s = (i < 1) ? "first " : "second ";
        System.out.println("Choose the " + s + "shape type:\n" + "(1) for a circle\n" + "(2) for a rectangle\n" + "(*) for a square");         // "*" means any other number 

        Select = input.nextInt();

        switch (Select) {
            case 1:
                System.out.print("Enter the radius: ");
                Value1 = input.nextDouble();
                Shapes[i] = new Circle(Value1);
                break;
            case 2:
                System.out.print("Enter the width: ");
                Value1 = input.nextDouble();
                System.out.print("Enter the height: ");
                Value2 = input.nextDouble();
                Shapes[i] = new Rectangle(Value1, Value2);
                break;
            default:
                System.out.print("Enter the side length: ");
                Value1 = input.nextDouble();
                Shapes[i] = new Square(Value1);
                break;
        }
    }

    System.out.println("The larger shape is:");
    if (Shapes[0].isLargerThan(Shapes[1])) {
        Shapes[0].print();
    } else {
        Shapes[1].print();
    }
}

public Inheritance() {
}

}

interface Comparable {

public boolean isLargerThan(E obj);     // The method returns true if the self object is larger than obj } 

interface Printable {

    public void print();
    /* For simplicity, let this method prints "A " + object name        + "with area: " + shape area */ }

abstract class geometricShape implements Comparable<geometricShape>, Printable {

    private String shapeName;

    abstract public double gerPerimeter();

    abstract public double getArea();

    public String getShapeName() {
        return shapeName;
    }

    public void setShapeName(String Name) {
        shapeName = Name;
    }

    @Override
    public boolean isLargerThan(geometricShape obj) {
        return this.getArea() > obj.getArea();
    }

    @Override
    public void print() {
        System.out.println("A " + this.getShapeName()
                + " with area: " + this.getArea());
    }
}

/*part 1*/
class Circle extends geometricShape {

    private double radius;

    Circle(double newRadius) {
        super.setShapeName("Circle");
        radius = newRadius;
    }

    @Override
    public double gerPerimeter() {
        return 2 * 3.14 * radius;
    }

    @Override
    public double getArea() {
        return 3.14 * radius * radius;
    }

    public void setradius(double r) {
        radius = r;
    }

    public double getradius() {
        return radius;
        }}

2 个答案:

答案 0 :(得分:1)

通过将单个(侧)参数传递给Square构造函数并将该参数两次(作为宽度和高度)传递给Rectangle构造函数,可以轻松处理Square和Rectangle所描述的问题:

public class Square extends Rectangle {
    ...
    public Square (double side) {
        super (side, side);
    }
    ...
    public double getSide () {
        return getWidth (); // a Rectangle method
    }

    public void setside(double s) {
        setWidth(s); // a Rectangle method
        setHeight(s); // a Rectangle method
    }
    ...
}

您的Square类不需要side实例变量。您的Rectangle课程也不需要它(它有widthheight)。

但请注意,这并不妨碍您的班级用户将Square的宽度和高度设置为不同的值。为了防止这种情况,您可以覆盖setWidth()类中setHeight()类的RectangleSquare方法。行为可以如下:每当您更改Square的宽度(通过调用setWidth())时,其高度将更改为相同的值,反之亦然。

public class Square extends Rectangle {
    ...
    public Square (double side) {
        super (side, side);
    }
    ...
    public double getSide () {
        return getWidth (); // a Rectangle method
    }

    public void setside(double s) {
        super.setWidth(s);
        super.setHeight(s);
    }

    @Override
    public void setWidth(double w) {
        setSide(w);
    }

    @Override
    public void setHeight(double h) {
        setSide(h);
    }
    ...
}

P.S。这个答案并没有解释你提到的异常,只是因为你没有包含产生该异常的代码。

答案 1 :(得分:0)

您可以设计类似的课程 具有geometricShapearea()等抽象方法的perimeter()类 例如Rectangle的子类将包含宽度,长度字段,并将覆盖area()perimeter()