CS 1063  Introduction to Computer Programming  Project 1

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:

Details

You have been asked to write a program that calculates the area of a triangle given the lengths of its three sides.  In addition, your program should be able to calculate the base and height of the triangle assuming that the first length is the base of the triangle.

Heron's Formula provides a way to calculate the area of a triangle given the lengths of its three sides.  If a, b and c are the lengths, then the area can be computed as follows.

First, compute s as:

s =  
 a + b + c 

 2 
Next, compute area as:
area = square root of (s * (s - a) * (s - b) * (s - c))
Assuming that a corresponds to the base of the triangle, and given that the area also equals base times height divided by 2, we can compute base and height as:
base = a
height =  
 2 * area 

 base 

Use the Math.sqrt method to compute the square root of a number.  For example, Math.sqrt(2) computes the square root of 2.

Writing the Triangle Class

Instance fields:
myA - a double value representing the first length
myB - a double value representing the second length
myC - a double value representing the third length
Constructors:
/* The constructor has three parameters: */
/* the lengths of the triangle's sides. */
public Triangle(double a, double b, double c)
Methods:

/* getArea returns the area */
public double getArea()

/* getBase returns the base */
public double getBase()

/* getHeight returns the height */
public double getHeight()

/* setA sets the first length to a new value */
public void setA(double a)

/* setB sets the second length to a new value */
public void setB(double b)

/* setC sets the third length to a new value */
public void setC(double c)

/* toString returns a string describing the triangle, */
/* including the lengths, area, base and height. */
public String toString()

Testing your Triangle class:

  1. Initialize and construct a Triangle object named triangle1 with lengths 3, 5 and 4.

  2. Create a variable named area1 that contains a double value.  In the variable, store the area for triangle1 obtained using the getArea method.

  3. Print the value of area1.  Be sure to use a descriptive label.

  4. Print triangle1 using the toString method.

  5. Change the first length of triangle1 to 1 using the setA method.

  6. Change the second length of triangle1 to 2.4 using the setB method.

  7. Change the third length of triangle1 to 2.6 using the setC method.

  8. Print triangle1 using the toString method.

  9. Print the base of triangle1 using the getBase method.  Be sure to use a descriptive label.

  10. Print the height of triangle1 using the getHeight method.  Be sure to use a descriptive label.