Question

So I'm trying to create a program that When you enter 4 arguments through command line such as 1 2 3 4. It outputs:

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

Here is what I have so far:

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

}

When I run the program I get an error that reads:

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

What am I doing wrong? My knowledge in java is very limited.

*Full disclosure: This program is not for any assignments or homework. It is purely for my knowledge.

Was it helpful?

Solution 2

I didn't use any comments, but it should be self-explanatory. If not, please comment.

I added a method to read the doubles to avoid code duplication. It tries to convert the string into a double and catches the exception which may occur.

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;
    }
}

OTHER TIPS

You're creating a new instance of TestRect by calling class's constructor. A good tutorial is here

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

You need to declare constructor:

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

And then you can call it:

   TestRect test = new TestRect(Double.parseDouble(args[0]), Double.parseDouble(args[1]), Double.parseDouble(args[2]), Double.parseDouble(args[3]));
  • You don't have a constrcutor with takes 4 strings as arguments:
new TestRect((args[0]), (args[1]), (args[2]), (args[3]))
  • area and perimeter are instance fields of TestRect. They can only be accessed via a TestRect object in a static context.
test.getArea()
test.getPerimeter

Like other comments have pointed out, you need to define a constructor that takes the relevant arguments. By default, the compiler only inserts an empty, no-argument constructor.

This should probably do the job:

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

And you also need to reference the area and perimeter like such:

test.getArea();
test.getPrimeter();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top