0% found this document useful (0 votes)
17 views4 pages

Constructor Overloading Method Overloading

Uploaded by

tejasmuradi1
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
17 views4 pages

Constructor Overloading Method Overloading

Uploaded by

tejasmuradi1
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 4

---------------------------------------------------------CONSTRUCTOR 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;

public Product(String name)


{
this.name=name;
}
public Product(String name,double price)
{
this.name=name;
this.price=price;
}
public Product(String name,double price,int quantity)
{
this.name=name;
this.price=price;
this.quantity=quantity;
}
public void displayDetails()
{
System.out.println("Name of the Product"+name);
System.out.println("Price of the Product"+price);
System.out.println("Quantity of the Product"+quantity);
System.out.println();
}
}
public class MyProgram{
public static void main(String[] args)
{
Product product1 = new Product("Laptop");
Product product2 = new Product("Phone",629.9);
Product product3 = new Product("Computer",700,5);

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
{

public int calculateArea(int sideLength)


{
return sideLength * sideLength;
}
public int calculateArea(int sideLength,int width)
{
return sideLength*width;
}
public double calculateArea(double radius)
{
return (22.0/7)*radius*radius;
}

public static void main(String[] args)


{
MyProgram m = new MyProgram();
System.out.println("area of Square : "+m.calculateArea(5));
System.out.println("area of Rectangle : "+m.calculateArea(5,4));
System.out.println("area of Circle : "+m.calculateArea(3.0));
}
}
-----------------------------------------------------------------------------------
---------------------
Q4.Create a Java program that defines a Employee class representing employees in a
company.
Implement constructor overloading in the Employee class to initialize employees
with different attributes:
Employee(String name): Constructor to initialize an employee with a name.
Employee(String name, int employeeId): Constructor to initialize an employee with a
name and employee ID.
Employee(String name, int employeeId, String department): Constructor to initialize
an employee with a name, employee ID, and department.
Implement method overloading in the Employee class to display employee information:
displayInfo(): Method to display basic information (name and ID) of the employee.
displayInfo(boolean detailed): Method to display detailed information (name, ID,
and department) if detailed parameter is true.
In the main method, create instances of Employee using different constructors and
invoke
the displayInfo method with different arguments to display employee information.

ANS :
lass Employee{
String name;
int employeeId;
String department;

public Employee(String name)


{
this.name=name;
}
public Employee(String name,int employeeId)
{
this.name=name;
this.employeeId=employeeId;
}
public Employee(String name,int employeeId,String department)
{
this.name=name;
this.employeeId=employeeId;
this.department=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);

}
}

You might also like