You are on page 1of 2

Ex No.

12
Date:
Objects and Classes
Aim:
To write a java program to calculate the area and perimeter of shapes.
Source Code:
class square{
int sqarea(int side){
int area = side * side;
return(area);
}
int sqpari(int side){
int pari = 4 * side;
return(pari);
}
}
class rectangle{
int rectarea(int length,int breadth){
int area = length * breadth;
return(area);
}
int rectpari(int length,int breadth){
int pari = 2*(length + breadth);
return(pari);
}
}
public class ObjectClass{
public static void main(String args[]){
int sarea1,sarea2,pari1,pari2;
int rectarea1,rectarea2,rectpari1,rectpari2;
square sq = new square();
rectangle rect = new rectangle();
int a=20;
System.out.println("Side of first square = " + a);
sarea1 = sq.sqarea(a);
pari1 = sq.sqpari(a);
System.out.println("Area of first square = " + sarea1);
System.out.println("Parimeter of first square = " + pari1);
a = 30;
System.out.println("Side of second square = " + a);
sarea2 = sq.sqarea(a);
pari2 = sq.sqpari(a);
System.out.println("Area of second square = " + sarea2);
System.out.println("Parimeter of second square = " + pari2);
int x = 10, y = 20;
System.out.println("Length of first Rectangle = " + x);
System.out.println("Breadth of first Rectangle = " + y);
rectarea1 = rect.rectarea(x,y);
rectpari1 = rect.rectpari(x,y);
System.out.println("Area of first Rectangle = " + rectarea1);
System.out.println("Parimeter of first Rectangle = " + rectpari1);
x = 15;
y = 25;
System.out.println("Length of second Rectangle = " + x);
System.out.println("Breadth of second Rectangle = " + y);
rectarea2 = rect.rectarea(x,y);
rectpari2 = rect.rectpari(x,y);
System.out.println("Area of second Rectangle = " + rectarea2);
System.out.println("Parimeter of first Rectangle = " + rectpari2);
}
}
Output:
Javac ObjectClass.java
java ObjectClass
Side of first square = 20
Area of first square = 400
Parimeter of first square = 80
Side of second square = 30
Area of second square = 900
Parimeter of second square = 120
Length of first Rectangle = 10
Breadth of first Rectangle = 20
Area of first Rectangle = 200
Parimeter of first Rectangle = 60
Length of second Rectangle = 15
Breadth of second Rectangle = 25
Area of second Rectangle = 375
Parimeter of first Rectangle = 80
Result:
Thus the java program has been written to calculate the area and perimet
er of shapes.