Skip to content

Commit 2f1ffcb

Browse files
committed
Add Triangle exercise
1 parent 1f0411d commit 2f1ffcb

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.codefortomorrow.intermediate.chapter11.practice;
2+
3+
/*
4+
Create a class named Triangle that contains the following two methods:
5+
6+
Return true if the sum of any two sides is greater than the third side.
7+
(This is the definition of a valid triangle.)
8+
public static boolean isValid(double side1, double side2, double side3)
9+
10+
Return the area of the triangle.
11+
public static double area(double side1, double side2, double side3)
12+
13+
Write a test program that reads three sides for a triangle
14+
and computes the area if the sides form a valid triangle.
15+
Otherwise, it displays that the input is invalid.
16+
Use Heron's Formula to calculate the area of a triangle given 3 sides.
17+
18+
Adapted from Exercise 6.19, Introduction to Java Programming (Comprehensive),
19+
10th ed. by Y. Daniel Liang
20+
*/
21+
22+
public class Triangle {
23+
public static void main(String[] args) {
24+
// test methods here
25+
}
26+
27+
// write methods here
28+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.codefortomorrow.intermediate.chapter11.solutions;
2+
3+
import java.util.Scanner;
4+
5+
/*
6+
Create a class named Triangle that contains the following two methods:
7+
8+
Return true if the sum of any two sides is greater than the third side.
9+
(This is the definition of a valid triangle.)
10+
public static boolean isValid(double side1, double side2, double side3)
11+
12+
Return the area of the triangle.
13+
public static double area(double side1, double side2, double side3)
14+
15+
Write a test program that reads three sides for a triangle
16+
and computes the area if the sides form a valid triangle.
17+
Otherwise, it displays that the input is invalid.
18+
Use Heron's Formula to calculate the area of a triangle given 3 sides.
19+
20+
Adapted from Exercise 6.19, Introduction to Java Programming (Comprehensive),
21+
10th ed. by Y. Daniel Liang
22+
*/
23+
24+
public class Triangle {
25+
public static void main(String[] args) {
26+
// prompt user to enter triangle side lengths
27+
Scanner input = new Scanner(System.in);
28+
29+
System.out.print("Enter side 1: ");
30+
double side1 = input.nextDouble();
31+
32+
System.out.print("Enter side 2: ");
33+
double side2 = input.nextDouble();
34+
35+
System.out.print("Enter side 3: ");
36+
double side3 = input.nextDouble();
37+
38+
input.close();
39+
40+
// if sides form a valid triangle, print area
41+
// else, print error message
42+
if (isValid(side1, side2, side3)) {
43+
double area = area(side1, side2, side3);
44+
System.out.println("The area of the triangle is: " + area);
45+
} else {
46+
System.out.println("Invalid input. Those sides don't form a triangle.");
47+
}
48+
}
49+
50+
/**
51+
* Returns whether the given side lengths form a valid triangle
52+
* @param side1 triangle's first side
53+
* @param side2 triangle's second side
54+
* @param side3 triangle's third side
55+
* @return true if the given side lengths form a valid triangle
56+
*/
57+
public static boolean isValid(double side1, double side2, double side3) {
58+
return (side1 + side2 > side3) &&
59+
(side1 + side3 > side2) &&
60+
(side2 + side3 > side3);
61+
}
62+
63+
/**
64+
* Returns the area of a triangle given its three side lengths
65+
* @param side1 triangle's first side
66+
* @param side2 triangle's second side
67+
* @param side3 triangle's third side
68+
* @return the area of the triangle
69+
*/
70+
public static double area(double side1, double side2, double side3) {
71+
// calculate semiperimeter
72+
double s = (side1 + side2 + side3) / 2;
73+
74+
// return area
75+
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
76+
}
77+
}

0 commit comments

Comments
 (0)