# Square Tester Class

• 05-28-2012, 03:52 AM
FOX427
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?
• 05-28-2012, 05:01 AM
hei1233212000
Re: Square Tester Class
Do you want this?
Code:

```private float side; public Square(final float side) {     this.side = side; } public float area() {     return side*side; } ....```
• 06-16-2012, 02:38 AM
FOX427
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
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
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 :(
• 06-16-2012, 03:18 AM
FOX427
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:
Code:

```public Square()         {         } ........ public double sideLength=5;```
Basically the problem asked for this solution:
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! ;)
• 06-16-2012, 03:22 AM
Norm
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.
Quote:

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.
• 06-16-2012, 03:34 AM
pbrockway2
Re: Square Tester Class
Quote:

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.