java - 如何修复运行时错误并删除java中抽象类的 double ?

标签 java class

我的程序要求定义一个抽象类,其中包含:

  • 3 个 protected 实例变量:topLeft(java.awt.Point 类型)、width(int 类型)和 height(int 类型)
  • 默认(或“无参数”或“无参数”)构造函数,用于构造具有默认值的形状:topLeft = (0, 0)、width=0 和 height=0
  • 一个重载的构造函数,用于构造具有给定值的形状。
  • 重写的 toString() 方法,以“(x, y) width x height”格式返回实例的字符串描述
  • 访问器和修改器方法:getX()、getY()、setX()、setY()、getHeight()...
    • move() 方法,将左上点在 X 方向移动 1,在 y 方向移动 2
    • 一种抽象绘制方法,通过位置、宽度和高度“绘制”形状。

我尝试用“int”代替 double,但出现错误,提示无法转换。

到目前为止已经完成了

      import java.awt.*;

      abstract class SimpleMovingShape {

         protected Point topLeft;
         protected int width;
         protected int height;

         public SimpleMovingShape() {
            topLeft = new Point(0, 0);
            width = 0;
            height = 0;
         }


        public SimpleMovingShape(Point topLeft, int width, int height) {
            this.topLeft = topLeft;
            this.width = width;
            this.height = height;
         }

        public double getX() {
            return topLeft.getX();
        }

        public double getY() {
            return topLeft.getY();
       }

        public void setX(double x) {
            topLeft.setLocation(x, topLeft.getY());
        }

        public void setY(double y) {
            topLeft.setLocation(topLeft.getX(), y);
        }

        public void move() {
            topLeft.setLocation(getX() + 1, getY() + 2);
        }

        public int getHeight() {
            return height;
       }

        public int getWidth() {
            return width;
       }

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

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

       public void setTopLeft(Point topLeft) {
           this.topLeft = topLeft;
       }

       public Point getTopLeft() {
           return topLeft;
       }

       public abstract void draw();

       @Override
       public String toString() {
           return "(" + getX() + "," + getY() + ") " + getWidth() + " X " 
           + getHeight();
      }
      }

对于测试人员

    SimpleMovingShape r2 = new SimpleMovingRectangle(new Point(10, 20), 
    20, 20);
    System.out.println(r2.getX());
    System.out.println(r2.getY());

输出显示10.0 20.0 其中预期输出为 10 20

对于测试人员来说,这显示运行时错误SimpleMovingRectangle r1= new SimpleMovingRectangle();r1.move(); System.out.printf("(%d, %d)", r1.getX(), r1.getY());

   ***Runtime error***
   Exception in thread "main" java.util.IllegalFormatConversionException: 
   d != java.lang.Double
    at 
  java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
    at 
  java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
    at java.util.Formatter.format(Formatter.java:2520)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at __Tester__.runTests(__Tester__.java:86)
    at __Tester__.main(__Tester__.java:80)

最佳答案

        public int getX() {
            double dbl = topLeft.getX().doubleValue();
            return (int) dbl;
        }

        public int getY() {
            double dbl = topLeft.getY().doubleValue();
            return (int) dbl;
       }

        public void setX(int x) {
            topLeft.setLocation(x, topLeft.getY());
        }

        public void setY(int y) {
            topLeft.setLocation(topLeft.getX(), y);
        }

关于java - 如何修复运行时错误并删除java中抽象类的 double ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55630464/

相关文章:

java - 获取(并行)流的状态

Java2D距离碰撞检测

java - Jsoup 获取多个类的 li

java - ehcache 持续存在磁盘问题

java - 从csv文件读取的字符串不等于java中定义的字符串代码

c++ - 处理对继承的循环依赖

c# - 比较类的两个实例

objective-c - cocoa /Objective-C : Access a (Boolean) variable in different Classes

c++ - 是否可以在 C++ 类声明中模板化类名?

java - 我可以在创建 JPEG 时更改 Java 的 ImageWriter 使用的压缩算法吗?