Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By pbrockway2

Thread: Square Tester Class

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Post 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. #2
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default 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. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default 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. #4
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default 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. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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.
    FOX427 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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 03:37 AM.
    FOX427 likes this.

Similar Threads

  1. Load Tester
    By java software in forum Java Software
    Replies: 0
    Last Post: 05-15-2012, 06:44 PM
  2. Question about tester
    By bside in forum New To Java
    Replies: 4
    Last Post: 09-08-2011, 06:37 PM
  3. Looking for SOFTWARE TESTER
    By global.jobsection in forum Jobs Offered
    Replies: 0
    Last Post: 07-21-2011, 10:48 AM
  4. http://localhost:8080/helloservice/HelloService?Tester HTTP Status 404 -
    By vietnamusa in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 03-13-2011, 12:02 AM
  5. moving square
    By blindfolded in forum New To Java
    Replies: 5
    Last Post: 01-22-2010, 05:58 PM

Tags for this Thread

Posting Permissions

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