JAVA PROGRAM
Program to find sum of two complex numbers using class and objects |Java Program|
Introduction
When it comes to complex numbers, their representation and manipulation can be efficiently managed using object-oriented programming. In Java, classes and objects provide a structured way to encapsulate data and related methods. This blog post demonstrates how to sum two complex numbers using Java classes and objects. We'll walk through the process step-by-step, and by the end, you'll have a clear understanding of how to implement and use such a program.
What Are Complex Numbers?
A complex number is a number that can be expressed in the form a + bi, where:
- a is the real part.
- b is the imaginary part, and i is the imaginary unit with the property that i^2 = -1.
Implementing Complex Number Addition in Java
Let's break down the program that adds two complex numbers. The program comprises two main components:
- The Complex1 class: This class will encapsulate the properties and behaviors of a complex number.
- The Comp class: This class will contain the main method to execute the program.
Code
import java.util.*;
import java.io.*;
// Class to represent a complex number
class Complex1 {
int real, image;
// Constructor to initialize complex number
Complex1(int r, int i) {
real = r;
image = i;
}
// Default constructor
Complex1() {}
// Method to add two complex numbers
Complex1 addComplex(Complex1 c1, Complex1 c2) {
Complex1 sum = new Complex1();
sum.real = c1.real + c2.real;
sum.image = c1.image + c2.image;
return sum;
}
}
// Main class to execute the program
public class Comp {
public static void main(String args[]) throws IOException {
int r1, r2, m1, m2;
DataInputStream j = new DataInputStream(System.in);
// Taking input for the first complex number
System.out.println("Enter the real and imaginary part of first complex number:");
r1 = Integer.parseInt(j.readLine());
m1 = Integer.parseInt(j.readLine());
// Taking input for the second complex number
System.out.println("Enter the real and imaginary part of second complex number:");
r2 = Integer.parseInt(j.readLine());
m2 = Integer.parseInt(j.readLine());
// Creating complex number objects
Complex1 c1 = new Complex1(r1, m1);
Complex1 c2 = new Complex1(r2, m2);
Complex1 c3 = new Complex1();
// Adding the two complex numbers
c3 = c3.addComplex(c1, c2);
// Displaying the sum
System.out.println("Sum = " + c3.real + " + " + c3.image + "i");
}
}
Output
Enter the real and imaginary part of first complex number:
5
3
Enter the real and imaginary part of second complex number:
4
2
Sum = 9 + 5i
Explanation
1. Attributes:
int real: Holds the real part of the complex number.
int image: Holds the imaginary part of the complex number.
2. Constructors:
Complex1(int r, int i): Initializes the complex number with given real and imaginary parts.
Complex1(): A default constructor.
3. Method:
Complex1 addComplex(Complex1 c1, Complex1 c2): Takes two Complex1 objects as parameters, adds their real and imaginary parts, and returns a new Complex1 object representing the sum.
Conclusion
By encapsulating the properties and behaviors of complex numbers within a class, the code becomes modular, reusable, and easier to maintain. The object-oriented approach helps in logically organizing the code, making complex number operations straightforward and intuitive. This example demonstrates just one of the many ways object-oriented programming can be utilized to solve mathematical problems efficiently in Java.
Post a Comment
0 Comments