CPW 143 Programming Assignment 3: Java Triangle and TriangleTester Classes


This assignment is primarily about Java Classes. Suppose we wish to make a class that can represent a triangle in a graphics system. Each corner of the triangle will need to be a point in the graphics system. We will also need some methods to make the class useful.

Complete the class, Triangle.java, providing the following constructors and methods.

Constructors:

A no-arg constructor that sets the three corners to be the points (0, 0), (1, 1), and (2, 5).

A constructor that takes 3 Points as parameters and sets the object’s corners to be those.

Methods:

public String toString()

Method returns a Stringrepresenting the triangle. The format should be like this example: {(0, 0), (1, 1), (2, 5)}

public double getPerimeter()

Method returns the length of the perimeter of the triangle. You can compute this easily since the Point class has a distance method that will return the distance between two Points. The perimeter of a triangle is the sum of the distances between the corners.

public double getArea()

Method returns the area contained by the triangle. You can compute the area most accurately by using Heron’s formula. First compute s = ( a + b + c ) / 2.0, where a, b, and c are the lengths of the three sides. Then, the area of the triangle is : A = Math.sqrt( s * (s – a) * (s – b) *(s – c) ) For more info, see . http://en.wikipedia.org/wiki/Heron%27s_formula.

public boolean contains( Triangle t )

Method that returns true if Triangle t is contained in the triangle, false if it is not. You can determine if t is inside by checking to see if its corners are in the triangle. A method, public boolean contains( Point p ), is provided that will determine if a Pointis in the triangle.

Use the provided test code, TriangleTester.java, to develop your Triangleclass.

You will also need to include the class Point.javain your project.

You are not allowed to use any class in java.awt.

Your code must conform to the coding standard as posted to Canvas in Java Language Coding Guidelines.

Submit: Triangle.java with methods completed.

Expected output from TriangleTester.java:

no-arg construction:

Triangle should have points {(0, 0), (1, 1), (2, 5)}

Triangle has these points {(0, 0), (1, 1), (2, 5)}

general construction:

Triangle should have points {(1, 1), (14, 6), (1, 6)}

Triangle has these points {(1, 1), (14, 6), (1, 6)}

testing getArea, answer should be 32.5 32.5

testing getPerimeter, answer should be 31.92838827718412

31.92838827718412

testing contains, answer should be true

{(1, 1), (14, 6), (1, 6)} contains {(1, 1), (14, 6), (1, 6)} is true

testing contains, answer should be true

{(1, 1), (14, 6), (1, 6)} contains {(1, 1), (2, 3), (1, 6)} is true

testing contains, answer should be false

{(1, 1), (14, 6), (1, 6)} contains {(0, 0), (1, 1), (2, 5)} is false

Leave a Reply

Your email address will not be published. Required fields are marked *



  • File Format: .java
  • Lines of Code: 141