Created
November 3, 2017 13:20
-
-
Save joek13/8bbb78f1a80a44f4c890532b532b4cd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyProgram | |
{ | |
public static void main(String[] args) throws RuntimeException | |
{ | |
Triangle[] triangles = new Triangle[] { | |
new Triangle(1,1,1), | |
new Triangle(3,3,5), | |
new Triangle(8,9,8), | |
new Triangle(2,2,2), | |
}; | |
Triangle referenceTri = new Triangle(1,1,1); | |
for(Triangle t : triangles) { | |
System.out.println("== Start new triangle =="); | |
System.out.println(t); | |
System.out.println("Triangle area: "+t.getArea()); | |
System.out.println("Triangle perimeter: "+t.getPerimeter()); | |
System.out.println("Triangle's largest angle: "+t.getLargestAngle()); | |
System.out.println("Triangle's area (using law of cosines): "+t.getAreaTrig()); | |
System.out.println("Triangle's area greater than or equal to a 1,1,1 triangle: "+t.areaGreaterThan(referenceTri)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Triangle { | |
private int a; //smallest side | |
private int b; //middle side | |
private int c; //longest side | |
//Creates a new triangle with the sicdes specified. | |
public Triangle(int a, int b, int c) throws RuntimeException { | |
int s,m,l; | |
//We need to find out which sides are small, medium, and large. | |
//This chain of if-elses does that, and sets s, m, and l, to those sides respectively. | |
if(a <= b && a <= c) { | |
//a is the smallest. | |
s = a; | |
//The medium option is now between b and c, so compare them to find out. | |
if (b <= c) { | |
m = b; | |
l = c; | |
} else { | |
l = b; | |
m = c; | |
} | |
} else if (b <= c) { | |
//b is the smallest. | |
s = b; | |
//between a and c now. | |
if(a <= c) { | |
m = a; | |
l = c; | |
} else { | |
m = c; | |
l = a; | |
} | |
} else { | |
//c is the smallest. | |
s = c; | |
//between a and b now. | |
if(a <= b) { | |
m = a; | |
l = b; | |
} else { | |
m = b; | |
l = a; | |
} | |
} | |
if(s + m <= l) { | |
//The sum of the two smallest sides must exceed the longer side in order to be valid. | |
throw new RuntimeException("Invalid values for triangle"); | |
} | |
this.a = s; | |
this.b = m; | |
this.c = l; | |
} | |
public Triangle() { | |
this(1, 1, 1); | |
} | |
public String toString() { | |
return String.format("Triangle with sides %d, %d, %d", a, b, c); | |
} | |
//Returns the triangle's perimeter. | |
public int getPerimeter() { | |
return a + b + c; | |
} | |
//Returns the triangle's area. | |
public double getArea() { | |
double s = getPerimeter() / 2.0; | |
return Math.sqrt(s * (s - a) * (s - b) * (s - c)); | |
} | |
//Returns the triangle's area, using law of cosines to calculate. | |
public double getAreaTrig() { | |
return 0.5 * a * b * Math.sin(Math.toRadians(this.getLargestAngle())); | |
} | |
//Returns whether this triangle is greater than or equal to another in area. | |
public boolean areaGreaterThan(Triangle other) { | |
return this.getArea() >= other.getArea(); | |
} | |
//Returns the triangle's largest angle in degrees. | |
public double getLargestAngle() { | |
return Math.toDegrees(Math.acos((Math.pow(c, 2) - Math.pow(b, 2) - Math.pow(a, 2)) / (-2 * a * b))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment