Write an applet that will calculate the perimeter and area of certain
types of triangles and draw them. The user will be prompted to
indicate which type of triangle to work with. Types are right
triangle, isosceles triangle, and equilateral triangle. The user will
input R or right to mean right triangle, I or isoceles to mean
isosceles triangle, or E or equilateral to mean equilateral triangle.
For a right triangle the user will then enter the lengths of the two
non-hypotenuse sides.
For an isosceles triangle the user will then enter the length of the
base and the length of a side (which equals the length of the other
side).
For an equilateral triangle the user will then enter the length of a
side (which equals the length of the other two sides).
The program must check that no length is less than or equal to zero.
Also, for the isosceles triangle the base must be less than the
sum of the two sides.
Perimeter of any triangle is the sum of the lengths of all three sides.
Area of any triangle is (1/2)bh, where b is the base and h is the
height.
In a right triangle, either non-hypotenuse side can be the base and
the other the height.
In an isosceles triangle, the height is:
square root of (side squared minus (base squared divided by 4))
In an equilateral triangle, the height is:
((square root of 3) divided by 2) times side
Draw the triangle in the paint method.
Right triangle: (x,y)
go up: (x,y-s1)
go right: (x+s2,y)
Isosceles: (x,y)
go up: (x+b/2,y-h)
go right:(x+b,y)
Equilateral (special case of Isoclese, b=s)
(x,y)
go up: (x+s/2,y-h)
go right:(x+s,y)
12 years ago
Purchase the answer to view it
- shapeapplet.java