(JAVA)
Implement a Triangle class. Any triangle can be represented by its THREE sides.
Therefore, your class will have THREE private member variables → side1, side2 and side3. Use the double data type to represent the triangle sides.
In addition, please provide public methods that perform the following FIVE tasks:
▪ An input method that obtains the appropriate values for the three sides from the user. While entering the values of the three sides, please remember the triangle property that the sum of ANY two sides needs to be greater than the third side.
▪ A method that calculates and returns the Perimeter of the Triangle: The perimeter of a triangle is simply the sum of all three sides of the triangle. ▪ An output method that prints all three sides of the Triangle and other information (as show in the sample I/O) on the user screen. (No other methods should print out any information)
▪ A boolean method that determines whether the triangle is Equilateral: A triangle is equilateral if and only if all the THREE sides of the triangle are equal. If the triangle is equilateral, a message conveying this should be sent to the screen. If the triangle is not equilateral, then the corresponding message should be sent to the screen (for example, if a = 3.4, b = 5.6 and c = 23.7, then the output message should read → the triangle IS NOT equilateral).
• A method that calculates and returns the area of the triangle: The area of a triangle is given by the following formula: Given the three sides of the triangle (say a, b and c), S = (a + b + c)/2. Then, Area = square root (S * (S – a) * (S – b) * (S – c)).
Write the corresponding main method that tests the Triangle class and produce the following outputs:
Here is some sample output:
Please enter the value for side 1: 12.1
Please enter the value for side 2: 14.1
Please enter the value for side 3: 15. 1
The three sides that you entered are: 12.1, 13.1 and 14.1
The perimeter of the triangle is: 41.3 T
he triangle IS NOT equilateral.
The area of the triangle is: 80.11.
import java.util.Scanner;
public class Triangles {
public static double perimeter(double lengths[]) {
return lengths[0] + lengths[1] + lengths[2];
}
public static boolean isEquilateral(double lengths[]) {
return (lengths[0] == lengths[1]) && (lengths[1] == lengths[2]);
}
public static double area(double lengths[]) {
double p = (lengths[0] + lengths[1] + lengths[2]) / 2.0;
return Math.sqrt(p * (p - lengths[0]) * (p - lengths[1]) * (p - lengths[2]));
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double lengths[] = new double[3];
for (int i = 0; i < 3; i++) {
System.out.print("Enter length of side " + (i + 1) + ": ");
try {
lengths[i] = Double.parseDouble(in.next());
if (lengths[i] <= 0) {
System.out.println("Error: Length can not be 0 or lesser.");
i--;
}
} catch (NumberFormatException e) {
System.out.println("Error: Invalid number");
i--;
}
}
System.out.println("Three sides that you enetered are: ");
System.out.println(lengths[0] + " " + lengths[1] + " " + lengths[2]);
System.out.println("The perimeter of the triangle is: " + perimeter(lengths));
if (isEquilateral(lengths)) {
System.out.println("The triangle IS equilateral.");
} else {
System.out.println("The triangle IS NOT equilateral.");
}
System.out.printf("The area of the triangle is: %.2f\n", area(lengths));
in.close();
}
}
![) return (lengths|θ] lengths!!]) && (lengths | 1] lengths[2]); := -= <terminated»Triangles (2) Dava Application CProgram File](http://img.homeworklib.com/images/765493f5-d463-46a6-b13a-02743eb9bbc6.png?x-oss-process=image/resize,w_560)
Please upvote, as i have given the exact answer as
asked in question. Still in case of any issues in code, let me know
in comments. Thanks!
(JAVA) Implement a Triangle class. Any triangle can be represented by its THREE sides. Therefore,...
(The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...
Must be in Python 3.6 (please have a screen shot on the
code)
ex 2.14 :
# Enter three points for a triangle
x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a
triangle: "))
# Compute the length of the three sides
side1 = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) **
0.5
side2 = ((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 -...
Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated; public GeometricShapes() { dateCreated = new java.util.Date(); } public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public void setColor (String color)...
Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...
Why is my program returning 0 for the area of a triangle? public class GeometricObjects { private String color = " white "; private boolean filled; private java.util.Date dateCreated; public GeometricObjects() { dateCreated = new java.util.Date(); } public GeometricObjects(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor() { return color; } public void setColor(String color)...
Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3: 5 Enter the base: 5 Enter the height: 8 --------------------- The triangle is Isosceles since it has two equal length sides. Isosceles triangle also has two equal angles The area is: 20 The permimeter is: 13 Continue? (y/n): y Enter length 1: 10 Enter length 2: 10 Enter length 3: 10 Enter the base: 10 Enter the height: 7 --------------------- The triangle is Equilateral...
Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...
(Need to complete the methods and pass a Junit test i can email them to you.) public class Triangle implements Comparable { /** * Stores the number of objects instantiated from the {@code Triangle} class */ private static int numTriangles; /** * The shortest side of the triangle */ private final double a; /** * The side of medium length of the triangle */ private final double b;...
Java Please - Design and implement a class Triangle. A constructor should accept the lengths of a triangle’s 3 sides (as integers) and verify that the sum of any 2 sides is greater than the 3rd(i.e., that the 3 sides satisfy the triangle inequality). The constructor should mark the triangle as valid or invalid; do not throw an exception. Provide get and set methods for the 3 sides, and recheck for validity in the set methods. Provide a toString method...
Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...