0% found this document useful (0 votes)
90 views10 pages

Lab Assignment

The document contains 6 programming assignments involving method and constructor overloading in Java. The assignments include: 1) Finding the area of a circle, rectangle, and square using method overloading. 2) Finding the area of a circle, rectangle, and square using constructor overloading. 3) Finding the volume of a cube and cuboid using method overloading. 4) Finding the volume of a cube and cuboid using constructor overloading. 5) Displaying student name, roll, and age values passed through a method using the this keyword. 6) Overloading a constructor using default and parameterized constructors.

Uploaded by

Chandia Panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views10 pages

Lab Assignment

The document contains 6 programming assignments involving method and constructor overloading in Java. The assignments include: 1) Finding the area of a circle, rectangle, and square using method overloading. 2) Finding the area of a circle, rectangle, and square using constructor overloading. 3) Finding the volume of a cube and cuboid using method overloading. 4) Finding the volume of a cube and cuboid using constructor overloading. 5) Displaying student name, roll, and age values passed through a method using the this keyword. 6) Overloading a constructor using default and parameterized constructors.

Uploaded by

Chandia Panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab assignment

1) WAP to find the area of circle, rectangle and square


using method overloading.

Program:
import java.util.*;
import java.io.*;
class Area
{
public void area(double r)
{
double area =3.14*r*r;
System.out.println("Area of circle is "+area);
}
public void area(double l,double b)
{
double area = l*b;
System.out.println("Area of rectangle is "+area);
}
public void area(int side)
{
double area = side * side;
System.out.println("Area of square is "+area);
}
};
public class Main
{
public static void main(String args[])
{
Area a=new Area();
a.area(4.5);
a.area(6,4);
a.area(7);
}
};

output
2) WAP to find the area of circle, rectangle and square using
constructor overloading?
Program:

import java.util.*;
import java.io.*;
class Area
{
public Area(double r)
{
double area =3.14*r*r;
System.out.println("Area of circle is "+area);
}
public Area(double l,double b)
{
double area = l*b;
System.out.println("Area of rectangle is "+area);
}
public Area(int side)
{
double area = side * side;
System.out.println("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:

3.WAP to find the volume of cube and cuboid using method


overloading.
Program:
import java.util.*;
import java.io.*;
class Volume
{
public void Cube (double s)
{
double vol =s*s*s;
System.out.println("Volume of cube is "+vol);
}
public void cuboid (double l, double b,double h)
{
double vol =l*b*h;
System.out.println("Volume of cuboid is "+vol);
}
};
public class Main
{
public static void main(String args[])
{
Volume v = new Volume();
v.cube(5)
v.cuboid(3,6,2)
}
};

4.WAP to find the volume of cube and cuboid using


constructor overloading.

Program:
import java.util.*;
import java.io.*;
class Volume
{
public Volume (double s)
{
double vol =s*s*s;
System.out.println("Volume of cube is "+vol);
}
public Volume (double l, double b,double h)
{
double vol =l*b*h;
System.out.println("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

5.By using this keyword display three variables name roll


agewhere these values would be pass through the metod?
Program

import java.util.*;
import java.io.*;
class Student
{
int roll;
String name;
int age;
public void data(String n ,int r,int a)
{
this.roll=r;
this.name=n;
this.age=a;
}
public void display()
{
System.out.println("Name = "+name);
System.out.println("Roll = "+roll);
System.out.println("Age = "+age);
}
};

public class Main


{
public static void main(String args[])
{
String name;
int age, roll;
Scanner sc=new Scanner(System.in);
System.out.println("Enter name, roll no and age: ");
name=sc.nextLine();
roll=sc.nextInt();
age=sc.nextInt();
Student s1=new Student();
s1.data(name,roll,age);
s1.display();
};

output
6. write a program to overload constructor using default
and parametrizd constructor?
Program:

import java.util.*;
import java.io.*;
class Sum
{
public Sum()
{
System.out.println("Default constructor called you didn’t
passed any argument”);
}
public Sum(double m,double n)
{ double s=m+n
System.out.println("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:

You might also like