Lab 4 is tough! 5hrs of coding!
Wow! Lab is getting tougher. More mathematical thinking required. Read the full post to look at the question. This actually took me around 5 hours to code and troubleshoot and conform to the test data. Although I still failed 4 sub test data.
Well, limiting where the triangle should rotate need some thinking. Playing around with mathematical rules and functions. >.<
The Question
(ATTENTION: Please refer to the webpage for the full question, because the table and figures cannot be displayed here.)
1.1 Learning objectives
* Creating a Java class
* Instantiating objects
* Constructors and methods
* Overriding method toString()
* Static methods
* Iterations
1.2 Task
Some basic trigonometrical formulas are given in Figure 1 and Figure 2 below.
(See figure 1 and 2 on the webpage)
You are to write two programs: MyTriangle2.java and Trigonometry.java.
The class MyTriangle2, unlike the MyTriangle class that you created for lab #3, contains the following properties and methods:
* The 3 points representating its vertices: point1, point2, point3.
The points are stored in counter-clockwise order, and each point should be of the class Point2D.Double from the package java.awt.geom. Refer to API Specification: Point2D.Double for details. You may assume that the points are not co-linear.
* 2 constructors:
MyTriangle2();
MyTriangle2( Point2D.Double, Point2D.Double, Point2D.Double );
The default constructor creates a triangle with these 3 points: (0.0, 0.0), (1.0, 0.0), and (0.0, 1.0). The alternative constructor creates a triangle with the 3 points given in the parameters.
* Mutators to change the respective point of the triangle:
setP1( Point2D.Double);
setP2( Point2D.Double);
setP3( Point2D.Double);
* Overriding the toString() method to display a triangle in this format:
([point1.x, point1.y], [point2.x, point2.y], [point3.x, point3.y])
where the co-ordinates are accurate to 4 decimal places.
For example, for a triangle created by the default constructor, the string produce by this method would be:
([0.0000, 0.0000], [1.0000, 0.0000], [0.0000, 1.0000])
The class Trigonometry is to create a triangle object of class MyTriangle2 as described above, and perform some operations on the triangle object:
1. You are to request for user to input different triangles. For each triangle, perform the tasks listed below. After performing the tasks, the user is asked if he/she would like to try another triangle. For simplicity, the user may enter “Y” or “y” to continue with another triangle, or any other key to terminate. (See sample runs below.)
2. The user needs to enter values for point2 and point3 of the triangle. It is assumed that:
* point1 is fixed at the origin (0.0, 0.0).
* The line segment connecting point1 and point2 is the longest side of the triangle.
3. You need to check that the triangle entered is scalene before your proceed. If it is not scalene, request for another triangle.
4. If the triangle entered is scalene, you are to compute the area of the smallest rectangle that bounds the given triangle. See Figure 3 for an illustration.
(See figure 3 on the webpage)
5. You are also to rotate the triangle about point1 which is at the origin, such that the longest side of the triangle lies on the x-axis, with point2 on the right of point1. Hence, the positions of point2 and point3 will be changed as a result of the rotation. See Figure 4 below. You are to display the new locations of these points through the toString() method of the MyTriangle2 class you wrote earlier.
(See figure 4 on the webpage)
6. You are to ensure that the output conforms to the sample output shown below.
7. You may write additional methods that you deemed necessary. For example, a static method to compute the distance between points.
1.3 Sample runs
Sample run using interactive input (output shown in bold purple):
$ javac MyTriangle2.java
$ javac Trigonometry.java
$ java Trigonometry
Enter point 2: 3.2 1
Enter point 3: 0 2
Triangle: ([0.0000, 0.0000], [3.2000, 1.0000], [0.0000, 2.0000])
Triangle is not scalene.
Another triangle? y
Enter point 2: 5 2
Enter point 3: 1 1.5
Triangle: ([0.0000, 0.0000], [5.0000, 2.0000], [1.0000, 1.5000])
Area of enclosing rectangle = 10.0000
Rotated triangle: ([0.0000, 0.0000], [5.3852, 0.0000], [1.4856, 1.0213])
Another triangle? n
Bye bye!
Another sample run:
$ java Trigonometry
Enter point 2: -5.5 2.1
Enter point 3: -3.2 -2.2
Triangle: ([0.0000, 0.0000], [-5.5000, 2.1000], [-3.2000, -2.2000])
Area of enclosing rectangle = 23.6500
Rotated triangle: ([0.0000, 0.0000], [5.8873, 0.0000], [2.2048, 3.1967])
Another triangle? y
Enter point 2: 3.9 -2.9
Enter point 3: 2 1.1
Triangle: ([0.0000, 0.0000], [3.9000, -2.9000], [2.0000, 1.1000])
Area of enclosing rectangle = 15.6000
Rotated triangle: ([0.0000, 0.0000], [4.8600, 0.0000], [0.9486, 2.0761])
Another triangle? n
Bye bye!
1.4 Submission
Submit your programs MyTriangle2.java and Trigonometry.java through the autograder.
1.5 Important notes
* Test your program thoroughly with different inputs.
* Ensure that the outputs of your program comform to the sample outputs.
* Do not use array.
1.6 Extension
The task has been simplified with the assumptions that (1) the points are ordered in counter-clockwise direction, (2) point1 is fixed at the origin (0.0, 0.0), and (3) the line segment connecting point1 and point2 is the longest side of the triangle.
You may extend your program by lifting these assumptions for your own practice. The user will enter 3 points, either in the clockwise or counter-clockwise direction. The longest side of the triangle could be any side, not necessarily the side that connects point1 and point2.
Your program is required to rotate and translate the triangle such that the longest side of the triangle lies on the x-axis, with the left end of the longest side at the origin.
You do not need to submit this. This is only for your own practice.