java 1. Create a class called Complex for performing arithmetic with complex num
ID: 3702945 • Letter: J
Question
java
1. Create a class called Complex for performing arithmetic with complex numbers. Complex
numbers have the form
where ?? is ??1
???????????????? + ?????????????????????????? ? ??
Write a program to test your class. Use double variable to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations:
a) Add two Complex numbers: The real parts are added together and the imaginary parts are added together.
b) Subtract two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
c) Print Complex numbers in the form (??, ??) where ?? the real part and ?? is the imaginary part.
Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form 1. rea!Part + imaginaryParti where i is V-1 Write a program to test your class. Use double variable to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations: a) Add two Complex numbers: The real parts are added together and the imaginary parts b) Subtract two Complex numbers: The real part of the right operand is subtracted from c) Print Complex numbers in the form (a, b) where a the real part and b is the are added together. the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. maginary part.Explanation / Answer
Complex.java
public class Complex {
// Declaring variables
private double x, y;
//Parameterized constructor
public Complex(double x, double y) {
super();
this.x = x;
this.y = y;
}
//One argumented constructor
public Complex(double x) {
super();
this.x = x;
this.y = 0;
}
//Zero argumented constructor
public Complex() {
this.x = 0;
this.y = 0;
}
//Getters
public double getX() {
return x;
}
public double getY() {
return y;
}
/* This method will add the two complex numbers
* return a Complex number class object
*/
public Complex add(Complex c) {
Complex comp = new Complex();
comp.x = this.x + c.x;
comp.y = this.y + c.y;
return comp;
}
/* This method will subtract the two complex numbers
* return a Complex number class object
*/
public Complex sub(Complex c) {
Complex comp = new Complex();
comp.x = this.x - c.x;
comp.y = this.y - c.y;
return comp;
}
/*
* toString() method is used to display the contents of an object inside it
*/
@Override
public String toString() {
return "("+x + ", " + y + ")";
}
}
_______________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int real1,imag1,real2,imag2;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter the real part of the first complex number :");
real1=sc.nextInt();
System.out.print("Enter the imaginary part of the first complex number :");
imag1=sc.nextInt();
System.out.print("Enter the real part of the second complex number :");
real2=sc.nextInt();
System.out.print("Enter the imaginary part of the second complex number :");
imag2=sc.nextInt();
//Creating an Instance of Complex Number
Complex c1=new Complex(real1, imag1);
//Creating an Instance of Complex Number
Complex c2=new Complex(real2, imag2);
//calling the add() method on the Complex Class
Complex addRes=c1.add(c2);
//calling the sub() method on the Complex Class
Complex subRes=c1.sub(c2);
//Displaying the Output
System.out.println("First Complex Number :"+c1);
System.out.println("Second Complex Number :"+c2);
System.out.println("Addition of complex numbers is :"+addRes);
System.out.println("subtraction of complex numbers is :"+subRes);
}
}
___________________
Output:
Enter the real part of the first complex number :3
Enter the imaginary part of the first complex number :6
Enter the real part of the second complex number :2
Enter the imaginary part of the second complex number :7
First Complex Number :(3.0, 6.0)
Second Complex Number :(2.0, 7.0)
Addition of complex numbers is :(5.0, 13.0)
subtraction of complex numbers is :(1.0, -1.0)
__________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.