0

I know there is an easy solution to this problem but it's driving me crazy. Why is there an error when I want to print the new Rectangle? any help appreciated!

public class Rectangle {

    public Rectangle(int x, int y, int width, int length) {

        x = 5;
        y = 10;
        width = 20;
           length = 30;

        Rectangle box = new Rectangle(5, 10, 20, 30);
        System.out.println(new Rectangle());
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Could you post the error that you are receiving? – kjh Jan 06 '14 at 18:20
  • 7
    You may be mixing up languages -- at least, in how you tagged your post. `public` and `class` aren't currently [JavaScript](http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) keywords. – Jonathan Lonowski Jan 06 '14 at 18:20
  • 7
    You are calling the constructor of Rectangle inside the constructor of Rectangle!!! That is infinite recursion which will result in a StackOverflow exception – Vincent Ramdhanie Jan 06 '14 at 18:22
  • Also you do not have a Rectangle constructor with no arguments...you have one that accepts 4 integers...you can only call the one that accepts the 4 integers. – Vincent Ramdhanie Jan 06 '14 at 18:23
  • You are passing the values of the variables( 5, 10, 20, 30); Instead of passing the variables (x,y,width,length) which you have declared and assigned values to. – z atef Jan 06 '14 at 18:24

4 Answers4

1

There are several problems with your code. First, you may not want to instantiate a Rectangle in the constructor of Rectangle as will lead to infinite recursion. The second problem is that you are calling a constructor that does not exist.

When you write:

   new Rectangle()

the Java compiler will look for a constructor in the Rectangle class that accepts no arguments. But your code does not have such a constructor. You can add one like this:

   public Rectangle(){
          //Your code here to instantiate a default rectangle
   }

Usually a constructor is used to set the values of the instance variables in a class rather than to execute code the way you have written it. You can move those lines that are creating rectangles into a main method to test the code.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
0

Here is some code that does what I think you want it to:

public class Rectangle
{
    int x, y, width, length; //declares the class's fields
    public Rectangle(int x, int y, int width, int length)
    {
        this.x = x; //initializes the field x to the value of the local variable x
        this.y = y; //initializes the field y to the value of the local variable y
        this.width = width; //initializes the field width to the value of the local variable width
        this.length = length; //initializes the field length to the value of the local variable length
        System.out.println(this); //prints this object. should look similar to "Rectangle@1246889"
    }
}

Please take a basic java tutorial (e.g. Providing Constructors for Your Classes), it will make your life easier.

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
0

You are calling a non-parameterized/default constructor from a parameterized constructor. The JVM in this case unable to create the default constructor. Hence in this case you need to include non-parameterized constructor explicitly into your class.

public class Rectangle {

public Rectangle(int x, int y, int width, int length) {

    x = 5;
    y = 10;
    width = 20;
       length = 30;

    Rectangle box = new Rectangle(5, 10, 20, 30);
    System.out.println(new Rectangle());
}
public Rectangle(){}
}

This will be error free.

Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26
0

First, the code (as you have provided it) can not possibly compile: you haven't declared x, y, width and height as member variables (fields) of your Rectangle. E.g.

// I'm assuming you want these private and final (I would)
private final int x, y, width, height;

Alternative, for a quick hack:

int x, y, width, height;

You are also trying to call a 0-argument constructor on your println line. Your class doesn't have a 0-argument constructor; it has a 4-argument constructor. I suspect (as noted above) you really want to print this.

But that wouldn't help much, on its own, unless you add an appropriate toString method to your class. E.g.:

public String toString() {
    StringBuilder sb = new StringBuilder("Rectangle: ");
    sb.append("x=").append(x);
    sb.append(", y=").append(y);
    sb.append(", width=").append(width);
    sb.append(", height=").append(height);
    return sb.toString();
}

You might want to think about implementing equals() and hashCode() too, if you choose to make this class immutable (I would). You can ixquick* or duckduckgo* this - there are plenty of explanations around.

[*] They are search engines: I don't use google.

Paul
  • 3,009
  • 16
  • 33