The project will be submitted electronically through WebCT. Zip up your entire project directory for submission. (Right click on the project folder, follow the Send To link and select Compressed (zipped) Folder.) Once the compression process is done, you will have a zipped file with the same name as your project folder and an extension of .zip. This zipped file will be uploaded to the WebCT system during project submission. The project directory should include the following files:
In the previous projects, you have written programs that calculate the area of a triangle and classify the type of triangle given the length of its sides. In this project, you will use loops for data entry and to calculate coordinates for a triangle.
Methods:
In addition to the methods from Projects 1 and 2, you should have these methods:
/* Print coordinates for the triangle consistent with the lengths of its sides */
public void printCoordinates()/* Return the distance between the points (x1,y1) and (x2,y2) */
private double distance(double x1, double y1, double x2, double y2)
Given lengths a, b, and c, the printCoordinates method should print the coordinates (0,0), (a,0), and (x,h), where a, x, and h should be replaced with the following values.
For example, if 5, 4, and 3 are the lengths of the triangle, then the three coordinates should be (0,0), (5,0), and (3.2, 2.4), as illustrated by the following figure.
The h = 2.4 value is returned by the getHeight method, so the key value left to determine is the x = 3.2 value. The printCoordinates method will find this value with a loop that isolates the value to within a small interval.
Before the loop, assign 0 to a local variable named left and assign the b length to a local variable named right. The idea of the loop is to move left and right toward each other while ensuring that the distance between (0,0) and (left, h) is less than or equal to b and the distance between (0,0) and (right, h) is greater than or equal to b.
Within the loop, assign the average of left and right to a local variable named middle. Use the distance method to calculate the distance between (0,0) and (middle, h). If this distance is less than or equal to b, then assign middle to left, else assign middle to right. Keep performing the loop as long as right - left is greater than 0.000001.
To calculate the distance between two points (x1, y1) and (x2, y2), the private distance method should return the value of the following expression:
√(x2 - x1)2 + (y2 - y1)2
For the above triangle with lengths 5, 4 and 3, left, right and middle should go through the following sequence of values (rounded to 6 digits):
left middle right
0.000000 2.000000 4.000000
2.000000 3.000000 4.000000
3.000000 3.500000 4.000000
3.000000 3.250000 3.500000
3.000000 3.125000 3.250000
3.125000 3.187500 3.250000
... ... ...
3.199982 3.199997 3.200012
3.199997 3.200005 3.200012
3.199997 3.200001 3.200005
3.199997 3.199999 3.200001
3.199999 3.200000 3.200001
Either middle or -middle is the desired x value for the (x,h) coordinate. If c*c is greater than a*a + h*h, then x = -middle, else x = middle.