Calculate the area of Triangle
Calculate the Area of Right angled Triangle
Parameterized Methods are defined to calculate the area of a simple triangle and the Right angled triangle and the values are passed as an argument from the main function. Values are taken from the user with the help of Scanner (built-in class of util package in java project ).
public class TriangleArea
{
//Declaring Variables
private int height;
private int base;
private int a;
private int b;
private int c;
private int s;
private boolean T;
public void Area(int height, int base) //Method for calculating area of Right-angled Triangle
{
double area= (height*base)/2; //Processing
System.out.println("Area of right angled triangle is "+area); //Printing Result
}
public void Area(int a, int b, int c) //Method for calculating area of Scalene Triangle
{
s= (a+b+c)/2; //Processing
double area= Math.sqrt(s*((s-a)*(s-b)*(s-c))); //Processing
System.out.println("Area of triangle is "+area); //Printing Result
}
public static void main(String []args)
{
System.out.println("To calculate area of triangle");
Scanner area1= new Scanner(System.in); //Creating object
System.out.println("Enter Side 1"); //Instruction for User
int s1=area1.nextInt(); //Getting value from Key-Board
System.out.println("Enter Side 2"); //Instruction for User
int s2=area1.nextInt(); //Getting value from Key-Board
System.out.println("Enter Side 3"); //Instruction for User
int s3=area1.nextInt(); //Getting value from Key-Board
TriangleArea obj= new TriangleArea(); //Creating object
obj.Area(s1, s2, s3); //Calling Area method which calculates Area of scalene Triangle
System.out.println("To calculate area of Right angled triangle");
System.out.println("Enter height"); //Instruction for User
int h= area1.nextInt(); //Getting value from Key-Board
System.out.println("Enter base"); //Instruction for User
int b=area1.nextInt(); //Getting value from Key-Board
obj.Area(h, b); //Calling Area method which calculates Area of Right-Angled Triangle
}
}
Output:
Comments
Post a Comment