2

だから私は、1 2 3 4などのコマンドラインから4つの引数を入力すると、次のように出力されるプログラムを作成しようとしています。

java TestRect 1 2 3 4
rectangle = (1.0, 2.0, 3.0, 4.0)
area = 12.0
perimeter = 14.0

これが私がこれまでに持っているものです:

public class TestRect {

  private double x;
  private double y;
  private double base;
  private double height;
  private double area;
  private double perimeter;

  public double getPerimeter () {
     perimeter = 2 * (base + height);
     return perimeter;
  }

  public double getArea () {
     area = (base * height);
     return area;
  } 

  @Override
  public String toString() {
  return "("+x+","+y+","+base+","+height+")"; 
  }

  public static void main(String[] args) {

      TestRect test = new TestRect((args[0]), (args[1]), (args[2]), (args[3]));
      System.out.println(test.toString());
      System.out.println("Area = " + area);
  System.out.println("Perimeter = " + perimeter);
   }

}

プログラムを実行すると、次のエラーが表示されます。

TestRect.java:27: error: constructor TestRect in class TestRect cannot be applied to       given types;
      TestRect test = new TestRect((args[0]), (args[1]), (args[2]), (args[3]));
                      ^
  required: no arguments
  found: String,String,String,String
  reason: actual and formal argument lists differ in length
TestRect.java:29: error: non-static variable area cannot be referenced from a static   context
      System.out.println("Area = " + area);
                                     ^
TestRect.java:30: error: non-static variable perimeter cannot be referenced from a static context
  System.out.println("Perimeter = " + perimeter);
                                      ^
3 errors

私は何を間違っていますか?Java に関する私の知識は非常に限られています。

*完全な開示: このプログラムは、課題や宿題のためのものではありません。それは純粋に私の知識のためです。

4

4 に答える 4

1

TestRectクラスのコンストラクターを呼び出して、 の新しいインスタンスを作成しています。良いチュートリアルはこちら

 TestRect test = new TestRect((args[0]), (args[1]), (args[2]), (args[3]));

コンストラクターを宣言する必要があります。

public class TestRect {
    // your fields here

     public TestRect(double x, double y, double base, double height) {
         this.x = x;
         this.y = y;
         this.base = base;
         this.height = height;             
     }

    // the rest of your class

そして、それを呼び出すことができます:

   TestRect test = new TestRect(Double.parseDouble(args[0]), Double.parseDouble(args[1]), Double.parseDouble(args[2]), Double.parseDouble(args[3]));
于 2013-08-15T08:32:45.423 に答える
0
  • 引数として 4 つの文字列を取るコンストラクターがありません。
new TestRect((args[0]), (args[1]), (args[2]), (args[3]))
  • area と perimeter は TestRect のインスタンス フィールドです。静的コンテキストで TestRect オブジェクトを介してのみアクセスできます。
test.getArea()
test.getPerimeter
于 2013-08-15T08:34:42.153 に答える
0

他のコメントが指摘したように、関連する引数を取るコンストラクターを定義する必要があります。デフォルトでは、コンパイラは引数のない空のコンストラクターのみを挿入します。

これはおそらく仕事をするはずです:

public TestRect(double x, double y, double base, double height){
    this.x = x;
    this.y = y;
    this.base = base;
    this.height = height;
}

areaまた、次のperimeterように参照する必要があります。

test.getArea();
test.getPrimeter();
于 2013-08-15T08:35:04.107 に答える
0

コメントは使用しませんでしたが、一目瞭然です。そうでない場合は、コメントしてください。

コードの重複を避けるために、double を読み取るメソッドを追加しました。文字列を double に変換しようとし、発生する可能性のある例外をキャッチします。

public class TestRect {

    private final double x;
    private final double y;
    private final double base;
    private final double height;

    private double area;
    private double perimeter;

    public TestRect(double x, double y, double base, double height) {
        this.x          = x;
        this.y          = y;
        this.base       = base;
        this.height     = height;
        this.perimeter  = 2 * (base + height);
        this.area       = base * height;
    }

    public double getPerimeter()    { return perimeter; }
    public double getArea()         { return area; }
    @Override
    public String toString()        { return "(" + x + ", " + y + ", " + base + ", " + height + ")"; }

    public static void main(String[] args) {

        double x = 0, y = 0, base = 0, height = 0;

        if (args.length == 4) {
                x       = readDoubleFromString(args[0]);
                y       = readDoubleFromString(args[1]);
                base    = readDoubleFromString(args[2]);
                height  = readDoubleFromString(args[3]);
        }

        TestRect test = new TestRect(x, y, base, height);

        System.out.println(test.toString());
        System.out.println("Area = " + test.getArea());
        System.out.println("Perimeter = " + test.getPerimeter());
    }

    private static double readDoubleFromString(String d) {
        double n = 0;
        try {
            n = Double.parseDouble(d);
        } catch (NumberFormatException e) {
            System.out.println(d + " is not a valid double. 0.0 is used instead!");
        }
        return n;
    }
}
于 2013-08-15T08:45:28.830 に答える