In: Computer Science
Write a simple java class that contains the following three methods:
1. isosceles -- accepts 3 integers which represent the sides of a triangle. Returns true if the triangle is isosceles and false otherwise.
2. perimeter - accepts 3 integers that represent the sides of a triangle and returns the perimeter of the triangle.
3. area -- accepts 3 integers, which represent the sides of a triangle and calculates and returns the area of the triangle. Hint: use Heron's formula. You must call the perimeter method from this method.
4. maxSide -- accepts 3 integers, which represent the sides of a triangle. Returns the maximum side.
Code:
import java.util.Scanner;
import java.lang.Math;
class triangle
{
public boolean isosceles(int a,int b,int c)
{
if(a==b||b==c||c==a)
{/*If 2 sides are equal then we
return true*/
return
true;
}
else
{/*Else we return false*/
return
false;
}
}
public int perimeter(int a,int b,int c)
{/*Return the perimeter which is sum of sides*/
return a+b+c;
}
public double area(int a,int b,int c)
{/*Area */
double
s=perimeter(a,b,c)/2;/*Calculating s*/
return
Math.sqrt(s*(s-a)*(s-b)*(s-c));/*Returning the area*/
}
public int maxSide(int a,int b,int c)
{
return (a>b &&
a>c)?a:(b>c)?b:c;/*Returning the max side*/
}
public static void main(String[] args)
{
int a,b,c;
Scanner scnr=new
Scanner(System.in);/*Scanner object*/
System.out.print("Enter the three
sides of the triangle:");
a=scnr.nextInt();
b=scnr.nextInt();
c=scnr.nextInt();/*Reading the
three sides of the triangle*/
triangle tr=new
triangle();/*Triangle object tr*/
if(tr.isosceles(a,b,c))
{/*If isosceles we print isoscles
triangle*/
System.out.println("The triangle is a isosceles triangle");
}
else
{/*else we print not a isoscles
triangle*/
System.out.println("The triangle is not a isosceles
triangle");
}
System.out.println("Area of the
triangle:"+tr.area(a,b,c));/*Printing the area*/
System.out.println("Maximum
side:"+tr.maxSide(a,b,c));/*Printing the maximum side*/
}
}
Output:
Indentation: