Area of Shapes with Method 1
import java.util.*;
class AreaOfShapes
{
float ar,at,ac,l,b,bh,h,r;
Scanner sc=new Scanner(System.in);//Input
void areaOfRectangle()
{
System.out.print("Enter the Length of the Rectangle " );
l=sc.nextFloat();
System.out.print("Enter the Breadth of the Rectangle " );
b=sc.nextFloat();
ar=l*b;
System.out.println("The Area of the Rectangle is " +ar);
}
void areaOfTriangle()
{
System.out.print("Enter the Base of the Triangle " );
bh=sc.nextFloat();
System.out.print("Enter the Height of the Triangle " );
h=sc.nextFloat();
at=(bh*h)/2;
System.out.println("The Area of the Triangle is " +at);
}
void areaOfCircle()
{
System.out.print("Enter the Radius of the Circle " );
r=sc.nextFloat();
ac=3.14f*r*r;
System.out.println("The Area of the Circle is " +ac);
}
public static void main()
{
AreaOfShapes ob=new AreaOfShapes();
ob.areaOfRectangle(); /**Calling/Invoking a method or calling a method*/
ob.areaOfTriangle();
ob.areaOfCircle();
}
}
Area of Shapes with Method 2
import java.util.*;
class AreaOfShapesM2
{
float at,ac;
int ar;
void areaOfRectangle(int l, int b)
{
ar=l*b;
System.out.println("The Area of the Rectangle is " +ar);
}
void areaOfTriangle(float bh, float h)
{
at=(bh*h)/2;
System.out.println("The Area of the Triangle is " +at);
}
void areaOfCircle(float r)
{
ac=3.14f*r*r;
System.out.println("The Area of the Circle is " +ac);
}
public static void main()
{
Scanner sc=new Scanner(System.in);/**For taking Input from user Scanner class to be included */
AreaOfShapesM2 ob=new AreaOfShapesM2();
float r1,ba,h1;
int l1,b1;
System.out.print("Enter the Length of the Rectangle " );
l1=sc.nextInt();
System.out.print("Enter the Breadth of the Rectangle " );
b1=sc.nextInt();
System.out.print("Enter the Base of the Triangle " );
ba=sc.nextFloat();
System.out.print("Enter the Height of the Triangle " );
h1=sc.nextFloat();
System.out.print("Enter the Radius of the Circle " );
r1=sc.nextFloat();
ob.areaOfRectangle(l1,b1); /**Calling/Invoking a method or calling a method*/
ob.areaOfTriangle(ba,h1);
ob.areaOfCircle(r1);
}
}
Area of Shapes with Method 4
import java.util.*;
class AreaOfShapesM4
{
float at,ac;
int ar;
int areaOfRectangle(int l, int b)
{
ar=l*b;
return ar;
}
float areaOfTriangle(float bh, float h)
{
at=(bh*h)/2;
return at;
}
float areaOfCircle(float r)
{
ac=3.14f*r*r;
return ac;
}
public static void main()
{
Scanner sc=new Scanner(System.in);/**For taking Input from user Scanner class to be included */
AreaOfShapesM4 ob=new AreaOfShapesM4();
float r1,ba,h1,aot,aoc;
int l1,b1,aor;
System.out.print("Enter the Length of the Rectangle " );
l1=sc.nextInt();
System.out.print("Enter the Breadth of the Rectangle " );
b1=sc.nextInt();
System.out.print("Enter the Base of the Triangle " );
ba=sc.nextFloat();
System.out.print("Enter the Height of the Triangle " );
h1=sc.nextFloat();
System.out.print("Enter the Radius of the Circle " );
r1=sc.nextFloat();
aor=ob.areaOfRectangle(l1,b1); /**Calling/Invoking a method or calling a method*/
aot=ob.areaOfTriangle(ba,h1);
aoc=ob.areaOfCircle(r1);
System.out.println("The Area of the Rectangle is " +aor);
System.out.println("The Area of the Triangle is " +aot);
System.out.println("The Area of the Circle is " +aoc);
}
}
Area of Shapes with Switch Case Method 2
import java.util.*;
class AreaOfShapesWithSwitchCase
{
float at,ac;
int ar;
void areaOfRectangle(int l, int b)
{
ar=l*b;
System.out.println("The Area of the Rectangle is " +ar);
}
void areaOfTriangle(float bh, float h)
{
at=(bh*h)/2;
System.out.println("The Area of the Triangle is " +at);
}
void areaOfCircle(float r)
{
ac=3.14f*r*r;
System.out.println("The Area of the Circle is " +ac);
}
public static void main()
{
Scanner sc=new Scanner(System.in);/**For taking Input from user Scanner class to be included */
AreaOfShapesWithSwitchCase ob=new AreaOfShapesWithSwitchCase();
float r1,ba,h1;
int l1,b1,choice;
System.out.println("1. Area of Rectangle ");
System.out.println("2. Area of Triangle ");
System.out.println("3. Area of Circle ");
System.out.print("Enter your Choice: " );
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter the Length of the Rectangle " );
l1=sc.nextInt();
System.out.print("Enter the Breadth of the Rectangle " );
b1=sc.nextInt();
ob.areaOfRectangle(l1,b1);
break;
case 2:
System.out.print("Enter the Base of the Triangle " );
ba=sc.nextFloat();
System.out.print("Enter the Height of the Triangle " );
h1=sc.nextFloat();
ob.areaOfTriangle(ba,h1);
break;
case 3:
System.out.print("Enter the Radius of the Circle " );
r1=sc.nextFloat();
ob.areaOfCircle(r1);
break;
default:
System.out.println("You have only choice from 1 to 3 ");
}
}
}
Area of Shapes with Switch Case Method 4
import java.util.*;
class AreaOfShapesSwitchCase
{
float at,ac;
int ar;
int areaOfRectangle(int l, int b)
{
ar=l*b;
return ar;
}
float areaOfTriangle(float bh, float h)
{
at=(bh*h)/2;
return at;
}
float areaOfCircle(float r)
{
ac=3.14f*r*r;
return ac;
}
public static void main()
{
Scanner sc=new Scanner(System.in);/**For taking Input from user Scanner class to be included */
AreaOfShapesSwitchCase ob=new AreaOfShapesSwitchCase();
float r1,ba,h1,aot,aoc;
int l1,b1,aor,choice;
System.out.println("1. Area of Rectangle ");
System.out.println("2. Area of Triangle ");
System.out.println("3. Area of Circle ");
System.out.print("Enter your Choice: " );
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter the Length of the Rectangle " );
l1=sc.nextInt();
System.out.print("Enter the Breadth of the Rectangle " );
b1=sc.nextInt();
aor=ob.areaOfRectangle(l1,b1);
System.out.println("The Area of the Rectangle is " +aor);
break;
case 2:
System.out.print("Enter the Base of the Triangle " );
ba=sc.nextFloat();
System.out.print("Enter the Height of the Triangle " );
h1=sc.nextFloat();
aot=ob.areaOfTriangle(ba,h1);
System.out.println("The Area of the Triangle is " +aot);
break;
case 3:
System.out.print("Enter the Radius of the Circle " );
r1=sc.nextFloat();
aoc=ob.areaOfCircle(r1);
System.out.println("The Area of the Circle is " +aoc);
break;
default:
System.out.println("You have only choice from 1 to 3 ");
}
}
}