使用tostring的Java程序错误

时间:2013-08-15 08:28:55

标签: java string class tostring println

所以我正在尝试创建一个程序,当你通过命令行输入4个参数时,例如1 2 3 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 个答案:

答案 0 :(得分:1)

您正在通过调用类的构造函数来创建TestRect的新实例。好tutorial is here

 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]));

答案 1 :(得分:0)

  • 你没有一个带有4个字符串作为参数的constrcutor:
new TestRect((args[0]), (args[1]), (args[2]), (args[3]))
  • 区域和周长是TestRect的实例字段。它们只能通过静态上下文中的TestRect对象进行访问。
test.getArea()
test.getPerimeter

答案 2 :(得分:0)

与其他注释一样,您需要定义一个带有相关参数的构造函数。默认情况下,编译器仅插入一个空的无参数构造函数。

这应该可以完成这项工作:

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

您还需要引用areaperimeter,例如:

test.getArea();
test.getPrimeter();

答案 3 :(得分:0)

我没有使用任何评论,但它应该是不言自明的。如果没有,请发表评论。

我添加了一种方法来读取双打以避免代码重复。它尝试将字符串转换为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;
    }
}