CS 1063  Introduction to Computer Programming  Project 3

Extending the Triangle Class

Objectives

Hand-in Requirements

The project will be submitted electronically through WebCT.  Zip up your entire project directory for submission.  (Right click on the project folder, follow the Send To link and select Compressed (zipped) Folder.)  Once the compression process is done, you will have a zipped file with the same name as your project folder and an extension of .zip.  This zipped file will be uploaded to the WebCT system during project submission.  The project directory should include the following files:

Overview

In the previous projects, you have written programs that calculate the area of a triangle and classify the type of triangle given the length of its sides.  In this project, you will use loops for data entry and to calculate coordinates for a triangle.

New Methods for the Triangle Class

Methods:

In addition to the methods from Projects 1 and 2, you should have these methods:

/* Print coordinates for the triangle consistent with the lengths of its sides */
public void printCoordinates()

/* Return the distance between the points (x1,y1) and (x2,y2) */
private double distance(double x1, double y1, double x2, double y2)

Given lengths a, b, and c, the printCoordinates method should print the coordinates (0,0), (a,0), and (x,h), where a, x, and h should be replaced with the following values.

  1. a is the first length of the triangle.
  2. h is the height of the triangle as calculated by the getHeight method.
  3. x is calculated by a loop so that the distance between (0,0) and (x,h) is b and the distance between (a,0) and (x,h) is c.

For example, if 5, 4, and 3 are the lengths of the triangle, then the three coordinates should be (0,0), (5,0), and (3.2, 2.4), as illustrated by the following figure.

showing the coordinates of a triangle

The h = 2.4 value is returned by the getHeight method, so the key value left to determine is the x = 3.2 value.  The printCoordinates method will find this value with a loop that isolates the value to within a small interval.

Before the loop, assign 0 to a local variable named left and assign the b length to a local variable named right.  The idea of the loop is to move left and right toward each other while ensuring that the distance between (0,0) and (left, h) is less than or equal to b and the distance between (0,0) and (right, h) is greater than or equal to b.

Within the loop, assign the average of left and right to a local variable named middle.  Use the distance method to calculate the distance between (0,0) and (middle, h).  If this distance is less than or equal to b, then assign middle to left, else assign middle to right.  Keep performing the loop as long as right - left is greater than 0.000001.

To calculate the distance between two points (x1, y1) and (x2, y2), the private distance method should return the value of the following expression:

(x2 - x1)2 + (y2 - y1)2

For the above triangle with lengths 5, 4 and 3, left, right and middle should go through the following sequence of values (rounded to 6 digits):

    left     middle    right
  0.000000  2.000000  4.000000
  2.000000  3.000000  4.000000
  3.000000  3.500000  4.000000
  3.000000  3.250000  3.500000
  3.000000  3.125000  3.250000
  3.125000  3.187500  3.250000
  ...       ...       ...
  3.199982  3.199997  3.200012
  3.199997  3.200005  3.200012
  3.199997  3.200001  3.200005
  3.199997  3.199999  3.200001
  3.199999  3.200000  3.200001

Either middle or -middle is the desired x value for the (x,h) coordinate.  If c*c is greater than a*a + h*h, then x = -middle, else x = middle.

Testing your Triangle class:

Use a do-while loop to ask the user if they would like to continue.  In this loop do the following.
  1. Ask the user to enter the first side as a double from the keyboard.  Store the response in side1.

  2. Ask the user to enter the second side as a double from the keyboard.  Store the response in side2.

  3. Ask the user to enter the third side as a double from the keyboard.  Store the response in side3.

  4. Construct a Triangle object named triangle1 using the input from above.

  5. Print a message indicating what kind of triangle has been entered.  For example, "You have entered a right triangle."

  6. Use a while loop to ensure that the triangle is valid.  If the triangle is invalid, the loop will need to get new values for the sides from the user.  Use the setA, setB and setC methods to set the sides of triangle1 to the new values.  The loop should also print what kind of triangle has been entered.

  7. Print triangle1 implicitly using the toString method.  This should list the sides, the height, and classification of the triangle.

  8. Invoke triangle1.printCoordinates() to print out possible coordinates for triangle1.