Skill Programming EXPT 4
Skill Programming EXPT 4
: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.
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.
Algorithm:
Step 1: Start
Step 3: In class complex, Declare n initialize input variables real and imag of type float.
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 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);
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.