Results 1 to 6 of 6
Thread: Square Tester Class
- 05-28-2012, 03:52 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 40
- 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?
- 05-28-2012, 05:01 AM #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; } ....
- 06-16-2012, 02:38 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 40
- 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
SquareTester ClassJava 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; }
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 :(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()); } }
- 06-16-2012, 03:18 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 40
- 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:
Basically the problem asked for this solution:Java Code:public Square() { } ........ public double sideLength=5;
so as you can see my way is correct too(I think)....Anyway, thank you for trying to help me! ;)Java Code:public Square() { sideLength=5; } ....... public double sideLength;
- 06-16-2012, 03:22 AM #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.
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.class Square whose constructor receives the length of the sidesIf you don't understand my response, don't ignore it, ask a question.
- 06-16-2012, 03:34 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Square Tester Class
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.)I guess I don't fully understand the function of the constructor
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 03:37 AM.
Similar Threads
-
Load Tester
By java software in forum Java SoftwareReplies: 0Last Post: 05-15-2012, 06:44 PM -
Question about tester
By bside in forum New To JavaReplies: 4Last Post: 09-08-2011, 06:37 PM -
Looking for SOFTWARE TESTER
By global.jobsection in forum Jobs OfferedReplies: 0Last Post: 07-21-2011, 10:48 AM -
http://localhost:8080/helloservice/HelloService?Tester HTTP Status 404 -
By vietnamusa in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 03-13-2011, 12:02 AM -
moving square
By blindfolded in forum New To JavaReplies: 5Last Post: 01-22-2010, 05:58 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks