制作方形课程需要很多帮助

时间:2013-07-06 03:40:28

标签: java

我正在尝试使用Dr.Java进行方形课程。我从矩形类中获取了大部分代码,但它给我留下了一团糟。我现在是Java的初学者,所以我现在真的迷失了。如果您有任何关于如何纠正方形课程的更正或提示,请告诉我。感谢

    package graphics2;

/**
 * A class that represents a square with given origin, width, and height.
 */
public class Square extends Graphics {
  // The private width and height of this square.
  private double width = 0;
  private double height = 0;  

  /**
   * Constructs a square with given origin, width, and height.
   */
  public Square(Point origin, double side) {
    super(origin, side, side);
    setOrigin(new Point(0, 0));
    width = height = side;
}

  /**
   * Constructs a square with given width and height at origin (0, 0).
   */
  public Square(double side) {
    setOrigin(new Point(0, 0));
    width = height = side;
  }
  /**
   * Returns the square's side of this square.
   */
  public double  getSide() {return width;}

  /**
   * Returns the width coordinate of this square.
   */
  public double getWidth() {return width; }

  /**
   * Returns the height coordinate of this square.
   */
  public double getHeight() {return height; }

  /**
   * Returns the area of this square.
   */
  public double area() {
    return width * height;
  }
}

这也是我收到的错误:

    1 error found:
File: C:\Users\GreatOne\Desktop\06Labs-\graphics2\Square.java  [line: 15]
Error: The constructor graphics2.Graphics(graphics2.Point, double, double) is undefined

2 个答案:

答案 0 :(得分:5)

克里斯,不要扩展图形。那是非常错的。不要做任何扩展。

你的构造者需要纠正,他们与你试图创建Squares的方式不符。

你也缺少多个变量。

我建议而不是让stackoverflow上的某人解决这个混乱,你打开你的教科书或在线阅读一些教程。我可以在几分钟内为你解决这个问题,但它对你没有帮助,因为我对你会理解如何使用它持怀疑态度。

请学习。你会更好。

答案 1 :(得分:0)

根据错误解释您的代码:

<强>错误

  • 错误1 [第15行] :构造函数未定义。你想要的是super(origin, side);,而不是我假设的super(origin, side, side);。或者,或者您应该使用super(origin, side, side);

  • 定义构造函数
  • 错误2,3,4,5 [行:17,18,26,27] :您正在使用变量wh构造函数,没有它们作为方法参数。由于这是您想要的正方形,因此请在两个构造函数中将width = wheight = h更改为width = sideheight = side。当您在方法参数中传递变量side时,这不会产生问题。

  • 上一个错误[第32行] :您将在getter方法side中返回变量public double getSide()。由于side不是类的变量,因此显示错误。将其更改为return widthreturn height。由于这是一个正方形,两者都是平等的。

相关问题