I. Implement a TriangleAnalyzer Class (Triangalyzer?) Your class will have 1. pr
ID: 3762983 • Letter: I
Question
I. Implement a TriangleAnalyzer Class (Triangalyzer?)
Your class will have
1. private instance variables that store the integer lengths of the 3 sides
2. a constructor that initializes the sides to parameters passed by the user
3. accessor methods that return each of the side lengths (optional: instead, you may have one accessor method that returns a string containing all three side lengths)
4. a method that returns a String indicating the type of triangle (Equilateral, Isosceles, etc). If the three side lengths do not form a triangle, an appropriate message should be returned. See “Additional Specifications,” below
5. a method that computes and returns the area using Heron’s formula
area = s (s-a) (s-b) (s-c)
where a,b, and c are the lengths of the sides, and s = ½ the perimeter
II. Write a Class to Test Your TriangleAnalyzer Class
1. have the user enter the lengths of the 3 sides
2. create an object
3. call the accessor method(s) to get the side lengths and print them
4. call the method that returns the type of triangle (or an appropriate message if the sides do not form a triangle) and print the String returned
5. if the side lengths do form a triangle, call the method that computes and returns the area and print it
III. Additional Specifications
1. As stated above, the side lengths are to be type int
2. Assume valid input
3. If the triangle formed is Isosceles, then the string returned should be either Isosceles Right, Acute Isosceles, or Obtuse Isosceles, as the case may be
(If all three sides are the same length, then the String returned should be just Equilateral, since by definition every Equilateral triangle is also acute)
IV. Data to be Used
Test your program with the following sets of data and hand in the output for all 7 sets:
9 9 9 4 3 5 13 5 12 3 7 3 2 6 3 9 8 8 5 8 4
V. Due Date: Thursday, November 19th
VI. "Extra Credit" Opportunity
To gain valuable programming experience, expand your programming knowledge, simplify your class by eliminating duplicate code, and earn 5 extra credit programming points, add boolean methods to your TriangleAnalyzer class to indicate whether or not a triangle is Equilateral, Isosceles, etc. These methods will be called from the method that returns the String containing the type of triangle formed.
Really need help with this program, thank you.
I. Implement a TriangleAnalyzer Class (Triangalyzer?)
Your class will have
1. private instance variables that store the integer lengths of the 3 sides
2. a constructor that initializes the sides to parameters passed by the user
3. accessor methods that return each of the side lengths (optional: instead, you may have one accessor method that returns a string containing all three side lengths)
4. a method that returns a String indicating the type of triangle (Equilateral, Isosceles, etc). If the three side lengths do not form a triangle, an appropriate message should be returned. See “Additional Specifications,” below
5. a method that computes and returns the area using Heron’s formula
area = s (s-a) (s-b) (s-c)
where a,b, and c are the lengths of the sides, and s = ½ the perimeter
II. Write a Class to Test Your TriangleAnalyzer Class
1. have the user enter the lengths of the 3 sides
2. create an object
3. call the accessor method(s) to get the side lengths and print them
4. call the method that returns the type of triangle (or an appropriate message if the sides do not form a triangle) and print the String returned
5. if the side lengths do form a triangle, call the method that computes and returns the area and print it
III. Additional Specifications
1. As stated above, the side lengths are to be type int
2. Assume valid input
3. If the triangle formed is Isosceles, then the string returned should be either Isosceles Right, Acute Isosceles, or Obtuse Isosceles, as the case may be
(If all three sides are the same length, then the String returned should be just Equilateral, since by definition every Equilateral triangle is also acute)
IV. Data to be Used
Test your program with the following sets of data and hand in the output for all 7 sets:
9 9 9 4 3 5 13 5 12 3 7 3 2 6 3 9 8 8 5 8 4
V. Due Date: Thursday, November 19th
VI. "Extra Credit" Opportunity
To gain valuable programming experience, expand your programming knowledge, simplify your class by eliminating duplicate code, and earn 5 extra credit programming points, add boolean methods to your TriangleAnalyzer class to indicate whether or not a triangle is Equilateral, Isosceles, etc. These methods will be called from the method that returns the String containing the type of triangle formed.
Really need help with this program, thank you.
I. Implement a TriangleAnalyzer Class (Triangalyzer?)
Your class will have
1. private instance variables that store the integer lengths of the 3 sides
2. a constructor that initializes the sides to parameters passed by the user
3. accessor methods that return each of the side lengths (optional: instead, you may have one accessor method that returns a string containing all three side lengths)
4. a method that returns a String indicating the type of triangle (Equilateral, Isosceles, etc). If the three side lengths do not form a triangle, an appropriate message should be returned. See “Additional Specifications,” below
5. a method that computes and returns the area using Heron’s formula
area = s (s-a) (s-b) (s-c)
where a,b, and c are the lengths of the sides, and s = ½ the perimeter
II. Write a Class to Test Your TriangleAnalyzer Class
1. have the user enter the lengths of the 3 sides
2. create an object
3. call the accessor method(s) to get the side lengths and print them
4. call the method that returns the type of triangle (or an appropriate message if the sides do not form a triangle) and print the String returned
5. if the side lengths do form a triangle, call the method that computes and returns the area and print it
III. Additional Specifications
1. As stated above, the side lengths are to be type int
2. Assume valid input
3. If the triangle formed is Isosceles, then the string returned should be either Isosceles Right, Acute Isosceles, or Obtuse Isosceles, as the case may be
(If all three sides are the same length, then the String returned should be just Equilateral, since by definition every Equilateral triangle is also acute)
IV. Data to be Used
Test your program with the following sets of data and hand in the output for all 7 sets:
9 9 9 4 3 5 13 5 12 3 7 3 2 6 3 9 8 8 5 8 4
V. Due Date: Thursday, November 19th
VI. "Extra Credit" Opportunity
To gain valuable programming experience, expand your programming knowledge, simplify your class by eliminating duplicate code, and earn 5 extra credit programming points, add boolean methods to your TriangleAnalyzer class to indicate whether or not a triangle is Equilateral, Isosceles, etc. These methods will be called from the method that returns the String containing the type of triangle formed.
Really need help with this program, thank you.
Explanation / Answer
//TriangleAnalyzer.java
public class TriangleAnalyzer
{
//instance variables
private int side1;
private int side2;
private int side3;
//Constructor to set three sides of the triangle
public TriangleAnalyzer(int side1,int side2,int side3)
{
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
//Accessor methods
public int getSide1()
{
return side1;
}
public int getSide2()
{
return side2;
}
public int getSide3()
{
return side3;
}
/*Method that returns the area of triangle*/
public double getarea()
{
double s=0;
s=(side1+side2+side3)/2.0;
double val=s*(s-side1)*(s-side2)*(s-side3);
if(val<0)
val=val*(-1.0);
return Math.sqrt(val);
}
/**Method getType returns the type of triangle*/
public String getType()
{
String type="";
//Check if all sides are equal
if((side1==side2) && (side2==side3) && (side1==side3))
type="Equilateral";
//Check any two sides are equal
else if((side1==side2) ||(side1==side3) || (side2==side3))
type= "Isosceles";
//Check if three sides are different
else if((side1!=side2) && (side2!=side3) &&((side1!=side3)))
type= "Scalane";
//return type string
return type;
}
}
-------------------------------
/**Test program of class TriangleAnalyzer.
* The program promps user to enter three sides
* of triangle and then print the triangle area and type
* and sides of the triangle calling accessor methods
* */
//TriangleDriver.java
import java.util.Scanner;
public class TriangleDriver
{
public static void main(String[] args)
{
//declare three sides
int side1;
int side2;
int side3;
//Create an object of Scanner
Scanner scanner=new Scanner(System.in);
//read three sides from user
System.out.println("Enter side1");
side1=scanner.nextInt();
System.out.println("Enter side2");
side2=scanner.nextInt();
System.out.println("Enter side3");
side3=scanner.nextInt();
//Create an instance of TriangleDriver
TriangleAnalyzer anylyzer=
new TriangleAnalyzer(side1, side2,side3);
//print method results
System.out.println("Triangle");
//call getType to print type of triangle
System.out.println("Type of Triangle : "+anylyzer.getType());
//Print sides of triangle
System.out.println("Side ,a ="+anylyzer.getSide1());
System.out.println("Side ,b="+anylyzer.getSide2());
System.out.println("Side ,c ="+anylyzer.getSide3());
//print area of the triangle
System.out.printf("Area = %.2f ",anylyzer.getarea());
}
}//end of the class
--------------------------
Sample outputs
sample run1
Enter side1
9
Enter side2
9
Enter side3
9
Triangle
Type of Triangle : Equilateral
Side ,a =9
Side ,b=9
Side ,c =9
Area = 35.074028853269766
sample run2
Enter side1
4
Enter side2
3
Enter side3
5
Triangle
Type of Triangle : Scalane
Side ,a =4
Side ,b=3
Side ,c =5
Area = 6.0
sample run3
Enter side1
13
Enter side2
5
Enter side3
12
Triangle
Type of Triangle : Scalane
Side ,a =13
Side ,b=5
Side ,c =12
Area = 30.0
sample run4
Enter side1
3
Enter side2
7
Enter side3
3
Triangle
Type of Triangle : Isosceles
Side ,a =3
Side ,b=7
Side ,c =3
Area = 6.31
sample run5
Enter side1
2
Enter side2
6
Enter side3
3
Triangle
Type of Triangle : Scalane
Side ,a =2
Side ,b=6
Side ,c =3
Area = 4.91
sample run6
Enter side1
9
Enter side2
8
Enter side3
8
Triangle
Type of Triangle : Isosceles
Side ,a =9
Side ,b=8
Side ,c =8
Area = 29.76
sample run7
Enter side1
5
Enter side2
8
Enter side3
4
Triangle
Type of Triangle : Scalane
Side ,a =5
Side ,b=8
Side ,c =4
Area = 8.18