Skip to content

Instantly share code, notes, and snippets.

@Solijons
Last active January 26, 2023 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Solijons/76fe76181a83269e385bedc9a8f179d5 to your computer and use it in GitHub Desktop.
Save Solijons/76fe76181a83269e385bedc9a8f179d5 to your computer and use it in GitHub Desktop.
Challenge
A complex number is a number that can be expressed in the form of a + bi, where a and b are real numbers, and i is solution of the equation x2 = -1. Because no real number satisfies this equation, i is called an imaginary number. For the complex number a + bi, a is called the real part, and b is called the imaginary part. To add or subtract two complex numbers, just add or subtract the corresponding real and imaginary parts. For instance, the sum of 5 +3i and 4+2i is 9 + 5i. For another, the sum of 3 + i and -1 + 2i is 2 + 3i.
Write a class with name ComplexNumber. The class needs two fields with name real and imaginary of type double. It represents the Complex Number.
The class needs to have one constructor. The constructor has parameters real and imaginary of type double and it needs to initialize the fields.
Write the following methods
Methods named getReal without any parameters, it needs to return the value of real field.
Method named getImaginary without any parameters, it needs to return the value of imaginary field.
Method add with two parameters real and imaginary of type double, it needs to add parameters to fields. In other words, it needs to add a complex number add operation as described above.
Method named add with one parameter of type ComplexNumber class. It needs to add the ComplexNumber parameter to the corresponding instance variables.
Method named subtract with two parameters real and imaginary of type double, it needs to subtract parameters from fields, in other words, it needs to do a complex number subtract operation as described above.
Method named subtract with one parameter other of type ComplexNumber class. It needs to subtract the other parameter from this complex number.
Note: All methods should be defined as public NOT public static.
Note: In total, you have to write 6 methods.
@harshasnsgithub
Copy link

public class ComplexNumber {
private double real;
private double imaginary;

public ComplexNumber(double real,double imaginary){
    this.real=real;
    this.imaginary=imaginary;
}

public double getReal(){
    return real;
}
public double getImaginary(){
    return imaginary;
}
public void add(double real,double imaginary){
    this.real+=real;
    this.imaginary+=imaginary;
}
public void add(ComplexNumber c){
    this.real+=+c.real;
    this.imaginary+=c.imaginary;
}
public void subtract(double real,double imaginary){
    this.real-=real;
    this.imaginary-=imaginary;
}
public void subtract(ComplexNumber c){
    this.real-=+c.real;
    this.imaginary-=c.imaginary;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment