Constructor Overloading Method Overloading
Constructor Overloading Method Overloading
METHOD OVERLOADING---------------------------------------------------
Q1.Write a Java program that defines a MathOperations class with method overloading
to
perform arithmetic operations. Implement the following methods:
add(int num1, int num2): Returns the sum of two integers.
add(double num1, double num2): Returns the sum of two doubles.
multiply(int num1, int num2): Returns the product of two integers.
multiply(double num1, double num2): Returns the product of two doubles.
In the main method, create an instance of MathOperations and demonstrate the use of
each method
by performing calculations with different types of arguments (integers and
doubles).
ANS :
public class MyProgram
{
public static void main(String[] args)
{
MyProgram m1 = new MyProgram();
System.out.println("Sum of an Integer : "+m1.add(5,6));
System.out.println("Sum of an Double : "+m1.add(5,6));
System.out.println("Multiplication of an Integer : "+m1.multiply(5,6));
}
public int add(int num1,int num2)
{
return num1+num2;
}
public double add(double num1,double num2)
{
return num1+num2;
}
public double multiply(int num1,int num2)
{
return num1*num2;
}
}
-----------------------------------------------------------------------------------
---------------------
Q2.Create a Java program that defines a Product class representing products in a
store.
Implement method overloading in the Product class to initialize products with
different attributes:
Product(String name): Constructor to initialize a product with a name.
Product(String name, double price): Constructor to initialize a product with a name
and price.
Product(String name, double price, int quantity): Constructor to initialize a
product with a name, price, and quantity.
In the main method, create instances of Product using different constructors and
display the details of each product.
ANS:
class Product{
private String name;
private double price;
private int quantity;
product1.displayDetails();
product2.displayDetails();
product3.displayDetails();
}
}
-----------------------------------------------------------------------------------
---------------------
Q3.Write a Java program that defines a ShapeCalculator class with method
overloading to
calculate the area of geometric shapes. Implement the following methods:
calculateArea(int sideLength): Returns the area of a square (side * side).
calculateArea(int length, int width): Returns the area of a rectangle (length *
width).
calculateArea(double radius): Returns the area of a circle (π * radius^2).
ANS :
public class MyProgram
{
ANS :
lass Employee{
String name;
int employeeId;
String department;
}
public void displayInfo()
{
System.out.println("Employee Name :"+name);
System.out.println("Employee Id : "+employeeId);
}
public void displayInfo(boolean detail)
{
if(detail==true)
{
System.out.println("Employee Name :"+name);
System.out.println("Employee Id : "+employeeId);
System.out.println("Department Name : "+department);
}
}
}
public class MyProgram
{
public static void main(String[] args)
{
Employee e1 = new Employee("Tejas");
Employee e2 = new Employee("Tejas",5);
Employee e3 = new Employee("Tejas",5,"Technical");
e2.displayInfo();
e3.displayInfo(true);
}
}