0

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
4

2 に答える 2

5

クリス、グラフィックを拡張しないでください。それは非常に間違っています。何も拡張しないでください。

コンストラクターを修正する必要があります。スクエアを作成しようとしている方法と一致していません。

また、複数の変数がありません。

この混乱をスタックオーバーフローの誰かに整理してもらうのではなく、教科書を開くか、オンラインでいくつかのチュートリアルを読むことをお勧めします。これは数分で解決できますが、使用方法を理解していただけるかどうかは疑わしいため、役に立ちません。

勉強してください。あなたはそれで良くなるでしょう。

于 2013-07-06T03:48:35.743 に答える
0

エラーに従って説明されたコード:

エラー

  • エラー 1 [行: 15] : コンストラクターが未定義です。私が想定するsuper(origin, side);のではなく、あなたが望むのは です。super(origin, side, side);それか、コンストラクターを次のように定義する必要がありますsuper(origin, side, side);

  • エラー 2,3,4,5 [行: 17, 18, 26, 27] :メソッド パラメーターとして使用せずに、コンストラクター内で変数を使用wしています。hこれは必要な正方形なので、両方のコンストラクターで and を to および inwidth = wheight = h変更width = sideします。メソッドのパラメーターでheight = side変数を渡しているため、これは問題になりません。side

  • 最後のエラー [行: 32]side : getter メソッドで変数を返していますpublic double getSide()sideはクラスの変数ではないため、エラーが表示されます。return widthそれをまたはに変更しreturn heightます。これは正方形なので、どちらも等しくなります。

于 2013-07-06T03:56:14.047 に答える