The formula to find area of circle :: 3.14 * radius * radius
The formula to find area of triangle :: 0.5 * base * height
Here, We are using two classes, one which contains main method and another one which contains the sub code.
The area class contains two method one is circle() that is used to find the area of circle. It takes the radius from user using Scanner class, and the find the area as per the formula giver above. It takes value of PI is defined globally as Constant using final keyword. This method is called by object of area class in main() method.
The another method is triangle() that is used to find the area of triangle. It takes the value of base and height from user Using Scanner class, and then find the area of triangle using the formula given above. It takes the value 0.5 by using Constant variable declared in global by using final keyword. This method is called by main() method by using the object of area class.
import java.util.*; class area { float r,a,b; double aa; final double PI=3.14; final double H=0.5; Scanner sc=new Scanner(System.in); void circle() { System.out.print("\n*** AREA OF CIRCLE ***\n"); System.out.print("Enter the value of radius = "); r=sc.nextFloat(); aa=PI*r*r; System.out.print("\nArea of circle is = "+aa); } void triangle() { System.out.print("\n\n*** AREA OF TRIANGLE ***\n"); System.out.print("\nEnter the value of base = "); b=sc.nextFloat(); System.out.print("Enter the value of Altitude = "); a=sc.nextFloat(); aa=H*a*b; System.out.print("\nArea of triangle = "+aa); } } class areademo { public static void main(String args[]) { area ob1=new area(); ob1.circle(); ob1.triangle(); } }
Here is the OutPut
*** AREA OF CIRCLE *** Enter the value of radius = 2 Area of circle is = 12.56 *** AREA OF TRIANGLE *** Enter the value of base = 5 Enter the value of Altitude = 6 Area of triangle = 15.0