Video Tutorial and Free source code: Addition and Subtraction of Complex numbers in Java. It also explains about Do Nothing Constructor. Complex numbers are of the form x+iy. Where x is the real part and iy is the imaginary part. In this program we are using parameterized constructor to copy the real and imaginary values … Continue reading “Addition/Subtraction of Complex Number: Java”
Video Tutorial and Free source code: Addition and Subtraction of Complex numbers in Java. It also explains about Do Nothing Constructor.
Complex numbers are of the form x+iy. Where x is the real part and iy is the imaginary part.
In this program we are using parameterized constructor to copy the real and imaginary values of the objects created, into the local variables of the class calc.(float real, img;)
calc(float r, float i)
{
real = r;
img = i;
}
calc(float r, float i)
{
real = r;
img = i;
}
Passing values to parameterized constructor:
calc c1 = new calc(12.5F, 2.5F);
calc c2 = new calc(09.5F, 0.5F);
calc c1 = new calc(12.5F, 2.5F);
calc c2 = new calc(09.5F, 0.5F);
But since we are also creating a third object c3, which doesn’t pass any parameter values, we need to write a default constructor even though it doesn’t contain any definition in it. Such a constructor is called Do Nothing Constructor.
calc c3 = new calc(); //Not Passing any value,
//hence it starts searching for default constructor.
calc c3 = new calc(); //Not Passing any value, //hence it starts searching for default constructor.
This is Do Nothing Constructor:
calc() { }
calc() { }
Now the actual addition of complex number: The return type is calc, as it has to return value to be stored in object c3(which is of type calc). Here, we add the real part of c1 to the real part of c2. Imaginary part of c1 to imaginary part of c2, and return the value and display the result.
calc add(calc c2)
{
calc res = new calc();
res.real = real + c2.real;
res.img = img + c2.img;
return(res);
}
calc add(calc c2)
{
calc res = new calc();
res.real = real + c2.real;
res.img = img + c2.img;
return(res);
}
Since c1 object called the add method, we need not again write c1.real and c1.img Instead we can directly write real and img.
c3 = c1.add(c2);
c3 = c1.add(c2);
Similarly, subtraction of complex numbers: Here, we subtract the real part of c1 to the real part of c2. Imaginary part of c1 to imaginary part of c2, and return the value and display the result.
calc sub(calc c2)
{
calc res = new calc();
res.real = real - c2.real;
res.img = img - c2.img;
return(res);
}
calc sub(calc c2)
{
calc res = new calc();
res.real = real - c2.real;
res.img = img - c2.img;
return(res);
}
Call to Subtraction method:
c3 = c1.sub(c2);
c3 = c1.sub(c2);
Video Tutorial: Addition and Subtraction of Complex Number: Java
In Java, function overloading is called Method Overloading.
Definition: In java method overloading means creating more than one method with same name but with different signature. i.e., If name of the method remains common but the number and type of parameters are different, then it is called method overloading in Java.
Overloaded Methods: Here, these methods have same return type and name, but the signature(i.e., the parameter it takes are different).
void sum(int a, int b)
{
System.out.println("Int: Sum of "+a+" and "+b+" is "+(a+b));
}
void sum(float a, float b)
{
System.out.println("Float: Sum of "+a+" and "+b+" is "+(a+b));
}
void sum(String a, String b)
{
System.out.println(a+" "+b);
}
One takes int value, other takes float value and the 3rd takes two string values as parameters.
These parameter type decides which method to call.
Video Tutorial for the working of Method/Funtion overloading in Java
import java.lang.*;
class work
{
void sum(int a, int b)
{
System.out.println("Int: Sum of "+a+" and "+b+" is "+(a+b));
}
void sum(float a, float b)
{
System.out.println("Float: Sum of "+a+" and "+b+" is "+(a+b));
}
void sum(String a, String b)
{
System.out.println(a+" "+b);
}
}
class overLoad
{
public static void main(String args[])
{
work w1 = new work();
w1.sum(10,20);
w1.sum(10.5F,1.5F);
w1.sum("I Love","You!");
}
}
output: Int: Sum of 10 and 20 is 30 Float: Sum of 10.5 and 1.5 is 12.0 I Love You!
Wife: Look at that Drunker. Husband: Who is he ? Wife: 10 years back he proposed me and I rejected him. .. .. Husband: oh my God, he is still celebrating!