Lab assignment
1) WAP to find the area of circle, rectangle and square
using method overloading.
Program:
import [Link].*;
import [Link].*;
class Area
{
public void area(double r)
{
double area =3.14*r*r;
[Link]("Area of circle is "+area);
}
public void area(double l,double b)
{
double area = l*b;
[Link]("Area of rectangle is "+area);
}
public void area(int side)
{
double area = side * side;
[Link]("Area of square is "+area);
}
};
public class Main
{
public static void main(String args[])
{
Area a=new Area();
[Link](4.5);
[Link](6,4);
[Link](7);
}
};
output
2) WAP to find the area of circle, rectangle and square using
constructor overloading?
Program:
import [Link].*;
import [Link].*;
class Area
{
public Area(double r)
{
double area =3.14*r*r;
[Link]("Area of circle is "+area);
}
public Area(double l,double b)
{
double area = l*b;
[Link]("Area of rectangle is "+area);
}
public Area(int side)
{
double area = side * side;
[Link]("Area of square is "+area);
}
};
public class Main
{
public static void main(String args[])
{
Area a1 = new Area(4.5);
Area a2 = new Area(6,4);
Area a3 = new Area(7);
}
};
Output:
[Link] to find the volume of cube and cuboid using method
overloading.
Program:
import [Link].*;
import [Link].*;
class Volume
{
public void Cube (double s)
{
double vol =s*s*s;
[Link]("Volume of cube is "+vol);
}
public void cuboid (double l, double b,double h)
{
double vol =l*b*h;
[Link]("Volume of cuboid is "+vol);
}
};
public class Main
{
public static void main(String args[])
{
Volume v = new Volume();
[Link](5)
[Link](3,6,2)
}
};
[Link] to find the volume of cube and cuboid using
constructor overloading.
Program:
import [Link].*;
import [Link].*;
class Volume
{
public Volume (double s)
{
double vol =s*s*s;
[Link]("Volume of cube is "+vol);
}
public Volume (double l, double b,double h)
{
double vol =l*b*h;
[Link]("Volume of cuboid is "+vol);
}
};
public class Main
{
public static void main(String args[])
{
Volume v1 = new Volume(5);
Volume v2 = new Volume(3,6,2);
}
};
Output
[Link] using this keyword display three variables name roll
agewhere these values would be pass through the metod?
Program
import [Link].*;
import [Link].*;
class Student
{
int roll;
String name;
int age;
public void data(String n ,int r,int a)
{
[Link]=r;
[Link]=n;
[Link]=a;
}
public void display()
{
[Link]("Name = "+name);
[Link]("Roll = "+roll);
[Link]("Age = "+age);
}
};
public class Main
{
public static void main(String args[])
{
String name;
int age, roll;
Scanner sc=new Scanner([Link]);
[Link]("Enter name, roll no and age: ");
name=[Link]();
roll=[Link]();
age=[Link]();
Student s1=new Student();
[Link](name,roll,age);
[Link]();
};
output
6. write a program to overload constructor using default
and parametrizd constructor?
Program:
import [Link].*;
import [Link].*;
class Sum
{
public Sum()
{
[Link]("Default constructor called you didn’t
passed any argument”);
}
public Sum(double m,double n)
{ double s=m+n
[Link]("parameterized constructor called
and sum is ”+s);
}
};
public class Main
{
public static void main(String args[])
{
Sum s1 = new Sum();
Sum s2 = new Sum(6,4);
}
};
Output: