1. Member
Join Date
Jun 2011
Posts
54
Rep Power
0

## Square Tester Class

Hi! there is an easy code to calculate the area, perimeter and diagonal of square. I did it and it works well, but the task asks to put side length value of the square inside constructor, but I did it as and instance field. how to implement the same class using parameter inside that constructor?

2. Member
Join Date
Jul 2010
Posts
36
Rep Power
0

## Re: Square Tester Class

Do you want this?
Java Code:
```private float side;

public Square(final float side) {
this.side = side;
}

public float area() {
return side*side;
}

....```

3. Member
Join Date
Jun 2011
Posts
54
Rep Power
0

## Re: Square Tester Class

don't really know... I am studying java from the book and doing some programming, one of the tasks asks me: "Write a class Square whose constructor receives the length of the sides. Then supply methods to compute:
1) The area and perimeter of the square
2) The length of the diagonal(use the Pythagorean theorem)"

Here is the code I did:
Square Class
Java Code:
```public class Square {
public Square()
{

}
public double getArea()
{
double area = sideLength*sideLength;
return area;
}
public double getPerimeter()
{
double perimeter = sideLength+sideLength;
return perimeter;
}
public double getDiagonal()
{
double diagonal = Math.sqrt((sideLength*sideLength)+(sideLength*sideLength));
return diagonal;
}
public double sideLength = 5;
}```
SquareTester Class
Java Code:
```public class SquareTester {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Square square1 = new Square();
System.out.println(square1.getArea());
System.out.println(square1.getPerimeter());
System.out.println(square1.getDiagonal());
}

}```
everything is works good, but look at constructor(public Square()) is left blank, but the task is asking to put length into constructor....I just wanna now how you can put parameters inside that constructor and run it and have correct results....I guess I don't fully understand the function of the constructor :(

4. Member
Join Date
Jun 2011
Posts
54
Rep Power
0

## Re: Square Tester Class

I just got it! basically I my solution is correct also, but instead of putting the sideLength value inside the constructor I put it at instance field:
Java Code:
```public Square()
{

}
........

public double sideLength=5;```
Basically the problem asked for this solution:
Java Code:
```public Square()
{
sideLength=5;
}
.......
public double sideLength;```
so as you can see my way is correct too(I think)....Anyway, thank you for trying to help me! ;)

5. ## Re: Square Tester Class

The constructor is like a method in some ways. You can pass arguments to it when you call it and it can save the values of those arguments in class variables.
class Square whose constructor receives the length of the sides
Add an argument to the constructor definition (inside the ()s)and in the body of the constructor save its value in the class variable that holds the length of the sides.

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,715
Rep Power
15

## Re: Square Tester Class

I guess I don't fully understand the function of the constructor
The job of a constructor is to get an instance of a class into a fully initialised state that "makes sense". (The function of a building constructor is precisely analogous: without one you would be left sitting amid a pile of building materials.)

If you look at your original code you will see that the square is left without a side value. A square without a side is rather like a building without walls! And it is the constructor's job to do something about this. Your amended code, with its "sideLength=5" in the constructor, at least gives us a fully functional square, but it has the odd side effect that all squares are the same size. So I second Norm's #5 and something sorta/kinda like the code that was (too hastily) posted in #2.

-----

In your revised code you have "public double sideLength;". As rules of thumb you are right to prefer double to float. And I would suggest you also prefer private to public: resist the temptation to use keywords like "public" and "static" just to keep the compiler quiet. Instead address the compiler's messages and ask about them if they are unclear.
Last edited by pbrockway2; 06-16-2012 at 04:37 AM.