#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Constructor problem among other things


    Hey guys,

    I have a few problems with the code I have been developing for my assignment. For starts I had to create two constructors to test string and integer parameters in my junit test cases all in the construction of a triangle.

    The integer constructor calls a validate method and that works fine in my test. However, when I test for the triangles area I get an error message I think my constructor is the problem.

    The second problem I'm having is with my setside# methods how do I call them? Could someone show me an example of a test case I could write.
    I will include all my code and just a sample of my test cases. Thank you


    [/CODE]
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I get an error message
    Please post the full text of the error message.

    Can you explain what the setSide# methods are each supposed to do? Have you looked at their code to see what the code in the methods do?
    It looks like you call them with an int value. For example: setSide(1234);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0
    Hi, thanks for your reply. The error for the area test is: Triangle.TriangleException: valid triangle

    I think it is incorrectly searching in the validate method when I should be finding the area from my setside# methods.

    This I how Ive been trying to test them methods.

    Code:
    public void test04Area() throws Exception {
    		
    
    		Triangle t = new Triangle(0,0,0);
    		//this area is correct
    		t.setside1(50);
    		t.setside2(50);
    		t.setside3(50);
    		
    		assertEquals(t.getside1(),50);
    		assertEquals(t.getside1(),50);
    		assertEquals(t.getside3(),50);
    		
    	}
    Thats where Im having problems with the constructor because it looks for 3 ints.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    What line in the source causes the error message?

    That's a strange error message: valid triangle
    If its valid, why the error?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0
    Code:
    if (side1 + side2 >= side3 && side2 + side3 >= side1 && side1 + side3 >= side2 && 
    				side1 > 0 && side2 > 0 && side3 > 0 && side1 < 500 && side2 < 500 && side3 <500){
    			throw new TriangleException("valid triangle");
    I think this is the offending code. I just want to throw an exception for pretty much everything in my program including the valid stuff, for the time being at least. So do you think you can help?
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Why should the code throw an exception for valid values?
    Remove the throw statement so the code does not throw an exception if the values are good.

    I just want to throw an exception for pretty much everything
    Why? That makes no sense to me. You should throw exceptions when something exceptional happens. Not for good data.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0
    Thanks a million that actually solved the problem I am getting the area fine now. Are my setside### methods relevant as I dont call them when getting the area the validate method seems to take care of that?
    Could I rewrite my validate method to throw individual errors for each side, rather than invalid triangle. I know that means more code but it seems it is a requirement of the assignment.

IMN logo majestic logo threadwatch logo seochat tools logo