Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
531 views

Program No: 1 / A. Write A JAVA Program To Demonstrate Constructor Overloading and Method Overloading.

This program demonstrates constructor overloading and method overloading in Java. It defines a box class with multiple constructors that take different parameters to initialize the object. It also contains methods like volume() and area() that are overloaded for different parameter types. The main method creates box objects using the different constructors, calls their methods to calculate volume and area, and prints the results.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
531 views

Program No: 1 / A. Write A JAVA Program To Demonstrate Constructor Overloading and Method Overloading.

This program demonstrates constructor overloading and method overloading in Java. It defines a box class with multiple constructors that take different parameters to initialize the object. It also contains methods like volume() and area() that are overloaded for different parameter types. The main method creates box objects using the different constructors, calls their methods to calculate volume and area, and prints the results.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program No: 1

/*a. Write a JAVA Program to demonstrate Constructor Overloading and


Method Overloading.*/
package pgm1a;
class box
{
double width, height, depth, length, breadth;
box()
{}
box(double w,double h,double d)
{
width = w;
height = h;
depth = d;
}
box(double len)
{
width = height = depth = len;
}
void rect(double x,double y)
{
length = x; breadth = y;
}
void rect(double x)
{
length = breadth = x;
}
double volume()
{
return(width * height * depth);
}
double area()
{
return(length * breadth);
}
}

public class ConstMethodOverload


{
public static void main(String[] args)
{
box mybox1 = new box(10,20,15);
box mybox2 = new box(5);
box rect1 = new box();
box rect2 = new box();
rect1.rect(10,20);
rect2.rect(8);
double areaR, vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 = " +vol);
vol = mybox2.volume();
System.out.println("Volume of mybox2 = " +vol);
areaR = rect1.area();
System.out.println("Area of rect1= " +areaR);
areaR = rect2.area();
System.out.println("Area of rect1 = " +areaR);
}
}

Output:
Volume of mybox1 = 3000.0
Volume of mybox2 = 125.0
Area of rect1= 200.0
Area of rect1 = 64.0

You might also like