Building Triangles

From Huben's Wiki
Jump to: navigation, search

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 display an object with println. Page 142 of the Book Of Humans (your text) describes toString().

Class Point will have:

  • two final double fields, x and y.
  • a two argument constructor for setting the fields.
  • nobody else should be able to look at or change those variables except with accessor methods (that you must write.)
  • 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:

  • an array of three Point variables. They represent the vertex points.
  • a constructor that takes 3 Points as arguments.
  • nobody should be able to look at or change those points.
  • 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.
  • a perimeter() method that anybody can use which returns the perimeter of the Triangle.
  • an area() method that anybody can use, which uses this formula or 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 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.
Personal tools
translate