To find out the area of a triangle using class.


To find out the area of a triangle using class.

Methodology: At first we have to declared a class outside of public class and inside of package .The class name is area .In the class I have to declared a veriable .In this veriable I have to take two parameter . I apply the law area of a triangle in a variable that name is area .Hence in the main function I write the class name and a obj and this new area () is the memory size of the object and assignes it .Then I take input by user ,using Scanner input=new Scanner(System.in).And print the result .
Source code :
                          package beginnerjava;


import java.util.Scanner;

class area {
    
    float a(int  l,int h){
        float area =(float )(0.5*l*h);
        return area ;
    }
    
}
public class AreaOfTriangleUsingClass {
    public static void main(String[] args) {
        
        area obj=new area () ;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the length :");
        int x=input.nextInt();
        System.out.print("Enter the hight :");
        int y=input.nextInt();
        System.out.println("Area of triangle is :"+obj.a(x,y));
        
    }
    
    
    
}




No comments

Powered by Blogger.