# Thread: rectangles and square constructors

1. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7

## rectangles and square constructors

i have another problem with constructors. here is the problem:
Complete the class, named Rectangle, that creates a new rectangle, given the integer height (h) and width (w) of the rectangle. This means that the constructor has two parameters.
Recall that a square is a special case of a rectangle, with its height equal to its width. Hence, a square can be constructed as a Rectangle, with only a single parameter. This is a case of overloading the constructor, by having two constructor methods, each with different parameters than the other.
Besides the constructors, this class should include a method named perimeter, to compute the perimeter of the rectangle (2 x width + 2 x height), as well as a method named area to compute the area of the rectangle (width x height).
I wasn't sure what they meant by "constructs".
here is my code:

Java Code:
```/**
A rectangle with height and width.
*/
public class test
{
private int height;
private int width;

/**
Constructs a rectangle.
@param aHeight: the height of the rectangle
@param aWidth: the width of the rectangle
*/
public void Rectangle(int aHeight, int aWidth)
{
int perimeter = (2*aWidth) + (2*aHeight);
}

/**
Constructs a rectangle that is a square.
@param aSide: the length of a side of the square
*/
public void Rectangle(int aSide)
{
int area;
area = aSide*aSide;
}

/**
Computes the perimeter.
@return the perimeter of the rectangle
*/
public int perimeter()
{
return perimeter;
}

/**
Computes the area.
@return the area of the rectangle
*/
public int area()
{
return area;
}
}```
and the code they will use to check it:

Java Code:
```public static String check(int h, int w, int sideSq)
{
Rectangle aRect = new Rectangle(h, w);
Rectangle aSquare = new Rectangle(sideSq);
return aRect.perimeter() + " " + aRect.area() + " "
+ aSquare.perimeter() + " " + aSquare.area();
}
}```

2. Senior Member
Join Date
Mar 2011
Posts
103
Rep Power
0
For starters, your constructors should simply create the Rectangle object. So take the constructor parameter(s) and set the Rectangle's properties (height and width).

3. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
aren't they passed in, such as the height and width, and the length of one side? then you just do the math to create the object?

4. Problems:
• Your code has no constructors. Constructors have no return type, not void not anything, and are named the same as the class.
• Your class fields are never set by any of your methods. All you're doing is changing variables that are local to your methods, and as soon as the methods end, your local variables disappear.
• You're trying to return a variable, perimeter, that doesn't exist in the class.

Solutions:
• Create true constructor(s).
• Set your class fields in the constructor(s)
• Calculate the perimeter only when you need it, only in the getPerimeter() method, and then return it.

5. Senior Member
Join Date
Mar 2011
Posts
103
Rep Power
0
Yes, they are passed in. But in object-oriented programming, you create your objects first, then you invoke methods on them. So first create an object of Rectangle (with a particular height and width). Then, invoke methods like perimeter() and area() on it.

You have to think object-oriented, not procedural (like C). You may not see the benefit of doing that in a small Rectangle class like this. But for a true OO program, you need to think in terms of classes, objects, and methods.

6. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
so if i am trying to create a rectangle, would this be good code for it?:
Java Code:
```public void Rectangle(int aHeight, int aWidth)
{
int rectangle = (2*aHeight) + (2*aWidth);
}```

7. Originally Posted by droidus
so if i am trying to create a rectangle, would this be good code for it?:
Java Code:
```public void Rectangle(int aHeight, int aWidth)
{
int rectangle = (2*aHeight) + (2*aWidth);
}```
No. You're just creating a temporary int variable named rectangle that exists only in the method and disappears once the method ends. Again, your class needs constructors, needs to set the class fields in the constructors. You really should read the book and tutorials that you're studying as it's all described there how to do this.

e.g., If I wanted to create a class Foo with a class field, bar, I'd do:

Java Code:
```public class Foo {
private int bar;

// my constructor -- same name as class, no return type
public Foo(int bar) {
this.bar = bar;  // set the class field
}
}```
Edit: I see that you've been told most of this in your previous threads!
Last edited by Fubarable; 04-28-2011 at 10:03 PM.

8. Senior Member
Join Date
Feb 2011
Posts
259
Rep Power
7
so when i have
public Rectangle(int aHeight, int aWidth)
{
}
why does it want a return type if void can't be there since it is a constructor?
and i guess i have to use this? im not familiar with it, but i will see what i can do
Last edited by droidus; 04-28-2011 at 10:09 PM.

9. Originally Posted by droidus
so when i have
public Rectangle(int aHeight, int aWidth)
{
}
why does it want a return type if void can't be there since it is a constructor?
What is the name of your class? What is the name of the constructor? Again, please review your book on classes and their constructors.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•