CS 1073 Introductory Programming
for Scientific Applications
Homework 1: Triangles


Due Date:


Overview: For this homework, you are to write a Java program that analyzes an input triangle, determining various values associated with the triangle. Values to print are in red.


Details:
  1. The input triangle should be specified by the double coordinates of the three vertices:

    Use simple assignment statements to determine these values.
    Print the six values that were input.

  2. Calculate the lengths of the three sides: a, b, and c using the formulas:

    Print the values of these three sides.

  3. Rearrange the numbers a, b, and c so that c is the largest:

    Note: to interchange the values a, and c, use the statements:

    Print the rearranged values of these three sides.

  4. Check if c >= a + b or if a == 0 or if b == 0 or if c == 0, in which case the triangle is degenerate. Don't do any more with it.

    Print a message in case of a degenerate triangle.
    Then execute System.exit(1) to quit the program.

  5. Calculate the three angles A, B, and C using the formulas:

    Note: arccos in Java is Math.acos

    Print the values of these three angles in degrees.
    (Must multiply by 180/PI to convert radians to degrees.)

  6. Check that A + B + C is close to 180 degrees, that is, pi/2 radians.

    Print the value of the sum of these angles in degrees.

  7. Calculate the area of the triangle using the formulas:

    Print the value of this area.


Sample input triangles:
  1. Points: (2, 1), (6, 1), (6, 4)

    Sample output (your output can be different, but you should have these values):

  2. Points: (2, 4), (5, 4), (5, 1)

  3. Points: (2, 1), (6, 3), (3, 1.5)

  4. Points: (-3, 4), (-2, -1), (-1, 4)

  5. Points: (0, 0), (3, 5.196152422706632), (-3, 5.196152422706632)

    (Here 5.196152422706632 is approximately 3*sqrt(3).)


What to turn in: Turn in computer printed copy containing:
  1. A source listing (the Java code) for the program under part I.

  2. Results of runs for each of the programs in parts I - V. (The Java programs should be the same except for the initial values of the vertices. The results will be different.)

    Note: You should use netBeans, though it is permissible to use Java from another source. In netBeans, the simplest way to make up the material to print is to just copy from netBeans (using ctrl-C) and paste into Word or WordPad (using ctrl-V). Then print the resulting document.


Extras for bored students: Add code that will say what type of triangle it is: right, isosceles, equilateral, or scalene. (A triangle could be both right and isosceles.) Because we are working with floating point numbers on a computer (and not "real" real numbers), results are only approximate, so you should (for example) say that a triangle is isosceles if two sides are close to within some threshold, such as 1.0e-12.