JAIRAM ARTS & SCIENCE COLLEGE, SALEM – 08.
CLASS : II BCA SUBJECT : PROGRAMMING IN JAVA
SUBJECT CODE : 22UCAP04 NO. OF PROGRAMS : 10
Ex.No:1 FIND THE AREA OF SQUARE, RECTANGLE AND CIRCLE USING METHOD
OVERLOADING
AIM:
To write a java program to find area of the square, rectangle and circle using method overloading.
ALGORITHM:
STEP 1: Start the process
STEP 2: Create the class ‘Funoverload’ with necessary member functions of same name with arguments to
calculate area of square, rectangle and circle.
STEP 3: Declare the variables side, length, width and radius
STEP 4: Create the object for the class Funoverload as ob
STEP 5: Get the input from the user using DataInputStream class and read from the keyboard.
STEP 6: Call the member functions using object by passing different parameters and calculate the area for
i. ob.area(side);
Square= side*side (Area of square)
ii. ob.area(length,width);
Rectangle= length* width (Area of rectangle)
iii. ob.area(radius);
Circle= 3.14*radius*radius (Area of circle)
STEP 7: Display the corresponding results in the prompt window
STEP 8: Stop the process
FLOWCHART:
START
Declare side, length, width, radius
area (int side)
Create Object (ob) for the Class
int result= side * side
Prompt Input from User
Print result
Print “Enter The Side For Square”
A
, A
area (int l, int w)
Read side
int result= l * w
Print “Enter Length and Width For Rectangle”
Print result
Read length, width
Print “Enter Radius For Circle”
Read radius area (double r)
ob.area(side) int result= 3.14*r*r
ob.area(length,width)
Print result
ob.area(radius)
STOP
SOURCE CODE:(FunOverload.java)
import java.io.*;
class FunOverload
{
void area(int side)
{
int result =side*side;
System.out.println("Area of square : "+result);
}
void area(int l,int w)
{
int result = l*w;
System.out.println("Area of rectangle : "+result);
}
void area(double r)
{
, double result = 3.14*r*r;
System.out.println("Area of circle : "+result);
}
public static void main(String args[])throws IOException
{
int side,length,width;
double radius;
FunOverload ob = new FunOverload();
DataInputStream in=new DataInputStream(System.in);
System.out.println("\tThe area of a square, rectangle and circle using method
overloading");
System.out.println("\t\t********************************************");
System.out.println("Enter the side of the square");
side=Integer.parseInt(in.readLine());
System.out.println("Enter length and width of a rectangle");
length=Integer.parseInt(in.readLine());
width=Integer.parseInt(in.readLine());
System.out.println("Enter the radius of a circle");
radius=Double.parseDouble(in.readLine());
ob.area(side);
ob.area(length,width);
ob.area(radius);
}
}
SAMPLE INPUT AND OUTPUT:
The area of a square, rectangle and circle using method overloading
********** ********************************************
Enter the side of the square
5
Enter length and width of a rectangle
58
56
Enter the radius of a circle
8
Area of square : 25
Area of rectangle : 3248
Area of circle : 200.96
RESULT:
Thus the above java program to find the area of square, rectangle and circle using method
overloading has been successfully executed.
CLASS : II BCA SUBJECT : PROGRAMMING IN JAVA
SUBJECT CODE : 22UCAP04 NO. OF PROGRAMS : 10
Ex.No:1 FIND THE AREA OF SQUARE, RECTANGLE AND CIRCLE USING METHOD
OVERLOADING
AIM:
To write a java program to find area of the square, rectangle and circle using method overloading.
ALGORITHM:
STEP 1: Start the process
STEP 2: Create the class ‘Funoverload’ with necessary member functions of same name with arguments to
calculate area of square, rectangle and circle.
STEP 3: Declare the variables side, length, width and radius
STEP 4: Create the object for the class Funoverload as ob
STEP 5: Get the input from the user using DataInputStream class and read from the keyboard.
STEP 6: Call the member functions using object by passing different parameters and calculate the area for
i. ob.area(side);
Square= side*side (Area of square)
ii. ob.area(length,width);
Rectangle= length* width (Area of rectangle)
iii. ob.area(radius);
Circle= 3.14*radius*radius (Area of circle)
STEP 7: Display the corresponding results in the prompt window
STEP 8: Stop the process
FLOWCHART:
START
Declare side, length, width, radius
area (int side)
Create Object (ob) for the Class
int result= side * side
Prompt Input from User
Print result
Print “Enter The Side For Square”
A
, A
area (int l, int w)
Read side
int result= l * w
Print “Enter Length and Width For Rectangle”
Print result
Read length, width
Print “Enter Radius For Circle”
Read radius area (double r)
ob.area(side) int result= 3.14*r*r
ob.area(length,width)
Print result
ob.area(radius)
STOP
SOURCE CODE:(FunOverload.java)
import java.io.*;
class FunOverload
{
void area(int side)
{
int result =side*side;
System.out.println("Area of square : "+result);
}
void area(int l,int w)
{
int result = l*w;
System.out.println("Area of rectangle : "+result);
}
void area(double r)
{
, double result = 3.14*r*r;
System.out.println("Area of circle : "+result);
}
public static void main(String args[])throws IOException
{
int side,length,width;
double radius;
FunOverload ob = new FunOverload();
DataInputStream in=new DataInputStream(System.in);
System.out.println("\tThe area of a square, rectangle and circle using method
overloading");
System.out.println("\t\t********************************************");
System.out.println("Enter the side of the square");
side=Integer.parseInt(in.readLine());
System.out.println("Enter length and width of a rectangle");
length=Integer.parseInt(in.readLine());
width=Integer.parseInt(in.readLine());
System.out.println("Enter the radius of a circle");
radius=Double.parseDouble(in.readLine());
ob.area(side);
ob.area(length,width);
ob.area(radius);
}
}
SAMPLE INPUT AND OUTPUT:
The area of a square, rectangle and circle using method overloading
********** ********************************************
Enter the side of the square
5
Enter length and width of a rectangle
58
56
Enter the radius of a circle
8
Area of square : 25
Area of rectangle : 3248
Area of circle : 200.96
RESULT:
Thus the above java program to find the area of square, rectangle and circle using method
overloading has been successfully executed.