Question: II . Enhancing a Triangle Class Part A In Desire 2 Learn you will find a . zip archive containing two Java classes: CartesianPoint.java and
II Enhancing a Triangle Class
Part A
In DesireLearn you will find a zip archive containing two Java classes: CartesianPoint.java and Triangle.java. Download and unzip that file. Compile both classes.
Part B
Add the following methods to the Triangle class:
public double getPerimeter returns the perimeter of the triangle.
public boolean isEquilateral returns true if the triangle is an equilateral triangle, false otherwise.
public boolean isRight returns true if the triangle is a rightangle triangle, false otherwise. Hint: C an use the Pythagorean Formula
Note: make use of methods in the CartesianPoint.java
Consult math texts or online sources if you forget the definitions of "equilateral" or "rightangle", or if you can't recall a formula.
IMPORTANT NOTE regarding equality comparisons: When working with floatingpoint numbers in Java or any programming language calculations don't work out exactly as we might expect. For instance, we may perform some calculations and end up with an answer like: instead of the expected answer of This happens because of the limited precision used to storerepresent floatingpoint numbers in a computer.
Therefore, rather than comparing two double values using:
you should instead calculate the difference between the two numbers and then compare that to some really, really small value. If the difference is less than that re ally small value, then the two numbers are, essentially, equal.
For example, use:
if Mathabs numnum TOLERANCE
where double TOLERANCE or some other really small number
Aside: Math.abs numnum simply gives the absolute value of the difference betwe n num and num
Continued on the next page...
See also: Section of the course textbook.
Part C
In a separate file, create a test driver that exercises the two new methods in your Triangle class; name this class TestTriangle. In your test driver, create at least Triangle objects one that is an equilateral triangle, and another that is a right triangle Call both methods on each of these objects and print out the results in a meaningful way, such as "The triangle t is not an equilateral triangle" or "The triangle t is a rightangle triange." Compile and run this program. Examine the output and return to part b if you notice any problems. Once it is running correctly, please save sample output.
Hint if you're having trouble coming up with test data: For an equilateral triangle you can use the points ath.sqrt and for a right triangle you can use
This class represents a triangle shape using points.
@author Natalie Webber
public class Triangle
private CartesianPoint pointA;
private CartesianPoint pointB;
private CartesianPoint pointC;
public Triangle double double
double double y
double x double y
point new CartesianPoint ;
pointB new CartesianPoint ;
pointC new CartesianPoint ;
public Triangle CartesianPoint p
CartesianPoint p
CartesianPoint p
pointA ;
pointB p;
pointC p;
public class CartesianPoint
The value for the point.
private double
The value for the point.
private double ;
Constructs a CartesianPoint object with the
given and values.
@param xVal the initial x value.
@param yVal the initial y value.
public CartesianPoint double xVal, double yVal
xVal;
yVal;
Calculates the distance between this
CartesianPoint and the specified one.
@param the value for the other point.
@param y the value for the other point.
@return the distance between the two points.
public double distance double double y
double ;
double dy ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
