Building Triangles
From Huben's Wiki
(Created page with "Your assignment is to build three classes: Point, Triangle, and TestTriangle. Two of them will have toString() methods. These methods are used by println() if you try to displa...") |
|||
Line 4: | Line 4: | ||
Class Point will have: | Class Point will have: | ||
− | * two double | + | * two final double fields, x and y. |
− | * nobody else should be able to look at or change those variables except with accessor | + | * a two argument constructor for setting the fields. |
− | * a method named distance that takes another point as an argument and finds the distance between the two points. | + | * nobody else should be able to look at or change those variables except with accessor methods (that you must write.) |
− | * a toString() method that has no arguments and returns a String showing the point as a coordinate pair. | + | * a method named distance() that takes another point as an argument and finds the distance between the two points. |
+ | * a toString() method that has no arguments and returns a String showing the point as a coordinate pair such as (9,3). | ||
Class Triangle will have: | Class Triangle will have: | ||
− | * three Point variables | + | * an array of three Point variables. They represent the vertex points. |
− | * | + | * a constructor that takes 3 Points as arguments. |
− | * a | + | * nobody should be able to look at or change those points. |
− | * a perimeter method that anybody can use which returns the perimeter of the Triangle. | + | * a sideLength() method that takes two integer arguments which are the indexes of two points in the array. Example: sideLength(0, 2). It should use the Point.distance() method to calculate the answer. |
− | * an area method that anybody can use, | + | * a perimeter() method that anybody can use which returns the perimeter of the Triangle. |
+ | * an area() method that anybody can use, [http://www.mathopenref.com/coordtrianglearea.html which uses this formula] or [http://en.wikipedia.org/wiki/Heron%27s_formula Heron's formula]. Remember that the functions you'll need are in class Math. | ||
+ | * a toString() method that has no arguments and returns a String describing the Triangle by its three points, its side lengths, its perimeter and its area. | ||
− | Class TestTriangle will have | + | Class TestTriangle will have a main() that should: |
− | + | * create at least three triangles, all with different points. | |
− | + | * print the triangles. | |
− | + | * print the sum of the perimeters and the sum of the areas of the triangles. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | ** print the sum of the perimeters and the sum of the areas of the | + |