0% found this document useful (0 votes)
12 views

Skill Programming EXPT 4

The document describes a Java program to demonstrate method and constructor overloading using complex numbers. It defines a Complex class with overloaded constructors to initialize complex numbers with one or two float parameters. Methods like add() and sub() perform addition and subtraction, while overloaded show() methods display the complex numbers and operation results. The main method creates Complex objects, calls the methods to perform operations, and displays the outputs, illustrating how polymorphism is achieved through constructor and method overloading in the program.

Uploaded by

mayanksanjay70
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Skill Programming EXPT 4

The document describes a Java program to demonstrate method and constructor overloading using complex numbers. It defines a Complex class with overloaded constructors to initialize complex numbers with one or two float parameters. Methods like add() and sub() perform addition and subtraction, while overloaded show() methods display the complex numbers and operation results. The main method creates Complex objects, calls the methods to perform operations, and displays the outputs, illustrating how polymorphism is achieved through constructor and method overloading in the program.

Uploaded by

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

Experiment No.

:4
Aim: To Write Java Program on method and constructor overloading

Input Specification: real and imaginary part of complex nos. A ,B of type float.
Output Specification:. Addition and subtraction of Complex nos A,B

Objectives:-Student/we will learn the concept of compile time polymorphism using method
and constructor overloading.

.
Theory:

Polymorphism:
The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form. there
are two types of polymorphism ,Compile time and Run time.

Compile time Polymorphism can be achieved by using , Method Overloading and Constructor
Overloading.

a)Method Overloading

If a class has multiple methods having same name but different in parameters, it is known
as Method Overloading.

If we have to perform only one operation, having same name of the methods increases the
readability of the program.

Suppose you have to perform addition of the given numbers but there can be any number of
arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for
three parameters then it may be difficult for you as well as other programmers to understand the
behavior of the method because its name differs.

Advantage of method overloading


Method overloading increases the readability of the program.
Different ways to overload the method
There are two ways to overload the method in java
1. By changing number of arguments
2. By changing the data type
b)Constructor Overloading

In Java, we can overload constructors like methods. The constructor overloading can be defined
as the concept of having more than one constructor with different parameters so that
every constructor can perform a different task.

logic of the program.


In this program , complex class is created which consists of data members d real and
imag of type float. complex constructors are defined to initialize complex nos. here
constructor overloading is achieved in such a way that one constructor is define with single
parameter and another constructor is defined with two parameters. Add() method is defined to
do the addition of complex nos. A,B. Sub()method is defined to do the subtraction of complex
nos.A and B. method overloading is achieved such that one show() function is defined to display
the complex nos. A and B. Another show() function is defined to display addition and
subtraction of complex nos. with string.

Algorithm:

Step 1: Start

Step 2: Create a class complex and define it.

Step 3: In class complex, Declare n initialize input variables real and imag of type float.

Step 4: Define two constructors to initialize complex nos.

Step 5: Define Add() and Sub() method to do addition and subtraction of complex nos A,B

Step 6: Define two show() methods to display complex nos. and to show addition and
subtraction of two Complex nos.

Step 7: In main method class,create 3 objects of complex class. Call show() method t
display complex nos. call Add() and Sub() method to do the addition and subtraction. Again call
show() method to display addition and subtraction of complex nos with string.

Step 8: stop.
Program(Code):

class complex
{
private float real,imag;

complex (float x,float y)


{
real=x;
imag=y;
}
complex (float m) // overloading constructor
{
real=m;
imag=m;
}
void show()
{
System.out.println("Real Part = "+real);
System.out.println("Imag Part = "+imag);
System.out.println("complex No= "+real+" +i("+imag+")");
}
void show( String S) // overloading method
{
System.out.println(S);
System.out.println("Real Part = "+real);
System.out.println("Imag Part = "+imag);
System.out.println("complex No= "+real+" +i("+imag+")");
}

void add(complex A,complex B)


{
real = A.real + B.real;
imag = A.imag + B.imag;
}

complex sub(complex B)
{
complex T=new complex(0.0f,0.0f);
T.real = real - B.real;
T.imag = imag - B.imag;
return T;
}

}
class compm
{
public static void main(String args[])
{
complex c1= new complex(2.5f,5.0f);
complex c2= new complex(1.5f);
complex c4= new complex(0.0f,0.0f);

System.out.println("complex number c1 is ....");


c1.show();
System.out.println("complex number c2 is ....");
c2.show();

System.out.println(" addition of complex number c1 and c2 is ....");


c4.add(c1,c2);
c4.show("Addition");

c4=c1.sub(c2);
System.out.println("Resultant complex number is c4 is..........");
c4.show("Substraction");
}
}
Output:
D:\Codes>javac compm.java

D:\Codes>java compm
complex number c1 is ....
Real Part = 2.5
Imag Part = 5.0
complex No= 2.5 +i(5.0)
complex number c2 is ....
Real Part = 1.5
Imag Part = 1.5
complex No= 1.5 +i(1.5)
addition of complex number c1 and c2 is ....
Addition
Real Part = 4.0
Imag Part = 6.5
complex No= 4.0 +i(6.5)
Resultant complex number is c4 is..........
Substraction
Real Part = 1.0
Imag Part = 3.5
complex No= 1.0 +i(3.5)

Outcome: with this we/student will able to use constructor overloading and method overloading.

You might also like