Assignment #3

Triangle Class

Semiperimeter, what is that?

The semiperimeter is just ½ the perimeter of a triangle (it is denoted by the letter s; s = ½(a+b+c)). Besides just being a really nice proof (because all computer scientists also love math!), it is actually useful to calculate the area of a triangle. Normally we use the formula (A = ½ bh). But in a lot of triangles, we do not know what the height is and don’t want to calculate it; we only know the length of the three sides. It can be proven (using Heron's formula) that the area of a triangle can also be calculated using ... (see image below).

Here is the problem

Create a program that lets the user enter 3 side lengths. The user can then display information about the semi-perimeter, area, perimeter, angles and type. Since the application is about triangles, which have a clear set of related calculations and properties (see link!), a Triangle class will be implemented. It will have at least the following:

  • private data members (fields) - the 3 side lengths

  • 1 x constructor (because when you create a triangle, the user will provide 3 pieces of information)

  • 3 x getters (the 3 side lengths)

  • 1 x private function: perimeter

  • many x public function (if not valid (because t is not a triangle!) then return -1):

    • public isValid(): boolean

      • the sum of any 2 sides MUST be greater than the length of the 3rd side

      • return a boolean

    • public area(): number

      • MUST use the semi-perimeter to do this calculation

    • public getType(): string

      • equilateral triangle

      • right angle triangle

      • isosceles triangle

      • scalene triangle (if it is this and right angle, just return right angle triangle)

      • we will ignore obtuse and acute triangles!

    • public semiPerimeter(): number

    • public angle(angleNumber: number): number

      • (leave in radians, it is easier!)

Things to note

  • If you are not sure on any part of the problem, just ask me and I will give you additional information

  • You must do the program in your primary language first (get a 3+). You can get a 4+ by doing the program in your secondary language

  • Remember create you class diagram first. All of the brains for the program must be in the class; the program is just a stub to use the class. It should only be checking for valid input from the “village idiot” and accepting or rejecting it.

  • Higher mark

    • Get a 4- if you can create another method that calculates the height of the triangle (Note: there are actually 3 heights!)

      • public height(sideNumber: number): number

    • Get a 4* if you can calculate the radius of the largest inscribed circle that can fit inside the triangle

      • public innerCircleRadius(): number

    • Get a 4+ if you do the above BUT ALSO calculate the area of the Circumcircle of the triangle

      • public circumsicleRadius(): number

index.ts & Test Cases for TypeScript