Thread: Triangle
View Single Post
  #5 (permalink)  
Old 01-14-2008, 02:40 AM
jkswebsite jkswebsite is offline
Member
 
Join Date: Nov 2007
Location: Illinois
Posts: 12
jkswebsite has a little shameless behaviour in the past
Send a message via AIM to jkswebsite
this is the most recent code I have. I'M SO CLOSE!

Quote:
/**
* Class triangle
*/

public class Triangle {

private int a;
private int b;
private int c;

/**
* Default constructor that creates an equilateral Triangle with side length
* 1.0
*/

public Triangle() {
a = (int) 1;
b = (int) 1;
c = (int) 1;
}

/**
* Another constructor that takes three int parameters that will be the
* three side lengths. It must validate the three parameters that they can
* actually form a triangle. (recall from Geometry: the sum of any two sides
* of a triangle must be greater than the third side) The constructor must
* throw an IllegalArgumentException if the parameters are invalid.
* @return
*
* @return
*/

public Triangle(int d, int e, int f) {
if (d + e > f && e + f > d && f + e > d) {
a = d;
b = e;
c = f;
} else {
throw new IllegalArgumentException("cannot form a triangle with values "+ a +","+ b +","+ c);
}


}

/**
* Method called getLongestSide that takes no parameters and returns the
* value of the longest side length. (i.e. the greatest of a,b, and c)
*/

public int longestSide() {
if (a >= b && a >= c)
return a;
else if (b >= a && b >= c)
return b;
else
return c;
}

/**
* Method called isRightTriangle that takes no parameters and returns true
* if the Triangle, (whose sides are a,b, and c) form a right triangle and
* returns false otherwise. (hint: use Pythagorean Theorem and recall the
* longest side is the hypotenuse)
*/

public boolean isRightTriangle() {
if (a * a + b * b == c * c)
return true;
else
return false;
}

/**
* Method called scalar that takes an int parameter and converts this
* Triangle to a similar Triangle by multiplying each side length by the
* parameter.
*/

public void scalar(int i) {
a = a * i;
b = b * i;
c = c * i;
}

/**
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* BONUS! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
*/

{
/**
* Method called isCongruentTo that takes a Triangle parameter and
* returns true if all three 'corresponding' sides are equal, and
* returns false otherwise. (example: 3,4,5 is congruent to 3,5,4 but
* not congruent to 3,3,5)
*/

// INSERT CODE HERE
}

{
/**
* Write a method called area that returns the area of this Triangle
*/

// INSERT CODE HERE
}

}
this is the output I get:

Quote:
1
5
6
false
true
false
10
50
60
false
true
false

this is the output you're SUPPOSED to get:

Quote:
1
5
6
false
true
false
10
50
60
false
true
false
Exception in thread "main" java.lang.IllegalArgumentException: cannot form a tri
angle with values 3,9,4
Quote:
Triangle t4 = new Triangle(3,9,4);
the problem is my t4 in my TriangleRunner.java class isn't being read for some reason.

any ideas?
__________________
Jonathan - AIM: JKK088

Last edited by jkswebsite : 01-14-2008 at 03:15 AM.
Reply With Quote