## CS
1401 Introduction to Computer Science

Fall 2014, Lab 2

**Assignment.** This assignment is about triangles. The program
should: - ask the user for the coordinates x1, y1, x2, y2,
x3, and y3 of the three vertices, and
- compute the three sides
s1, s2, and s3, the perimeter p, the half-perimeter s, and the
area a of the triangle.

For these computations, use the
following formulas: - to compute the sides, use Pythagoras
theorem:

s1 = √(x2 − x3)^{2} + (y2 −
y3)^{2};

s2 = √(x1 − x3)^{2} + (y1 −
y3)^{2};

s3 = √(x1 − x2)^{2} + (y1 −
y2)^{2};
- perimeter is the sum of all three sides: p = s1 + s2 + s3;
- the
half-perimeter is literally the half of the perimeter: s = 0.5 *
p;
- finally, the area can be computed by using Heron's formula:

a =
√s * (s − s1) * (s − s2)
* (s − s3).

**How to compute square root.** Use Math.sqrt(x) to compute the
square root of x. For example, the instruction

double a = Math.sqrt(4.0);

will assign the value 2.0 to the variable a.
To use Math.sqrt, you probably need to import java.lang.Math

**Example.** Test your program on the example of the following
triangle:

- its first vertex has coordinates x1 = 0.0 and
y1 = 0.0;
- its second vertex has coordinates x2 = 4.0 and y2 =
0.0;
- its third vertex has coordinates x3 = 0.0 and y3 = 3.0.

(This is a right triangle which is often used to illustrate the
Pythagoras theorem.)
For this triangle, the above formulas lead to

- s1 = √(4.0 − 0.0)
^{2} + (0.0 −
3.0)^{2} = √4.0^{2} + (−3.0)^{2} =
√16.0 + 9.0 = √25.0 = 5.0;
- s2 = √(0.0 − 0.0)
^{2} + (0.0 −
3.0)^{2} = √9.0 = 3.0;
- s3 = √(0.0 − 4.0)
^{2} + (0.0 −
0.0)^{2} = √16.0 = 4.0.

(So, we indeed
have s1^{2} = s2^{2} + s3^{2}.)
Here,

- p = 5.0 + 4.0 + 3.0 = 12.0,
- s = 0.5 * 12.0 = 6.0, and
- a = √6.0 * (6.0 − 5.0) * (6.0 − 3.0)
* (6.0 − 4.0) = √6.0 * 1.0 * 3.0 * 2.0 = √36.0 = 6.0.

**When it is due.** The program is due at the beginning of the
first lab section on the week of September 15, i.e.:

- on
Monday September 15 for those who attend Monday-Wednesday labs,
and
- on Tuesday September 16 for those who attend
Tuesday-Thursday labs.

**How to submit:** Your TAs will instruct you how to submit the
programs.