import java.util.Random;
public class Complex
{
//fields (aka instance variables)
private int real;
private int imaginary;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//CONSTRUCTORS
//CONSTRUCTOR 1 - constructs a complex number with real part a and imaginary part b
public Complex(int a, int b)
{
real = a;
imaginary = b;
}
//CONSTRUCTOR 2 - constructs a complex number with only the real part, imaginary part is 0
public Complex(int a)
{
real = a;
imaginary = 0;
}
//CONSTRUCTOR 3 - contructs a random complex number
public Complex()
{
Random link = new Random();
real = link.nextInt(100);
imaginary = link.nextInt(100);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//METHODS
//METHOD 1 - create a string of the complex number so it can print
public String toString()
{
return real + "+" + imaginary + "i";
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//METHOD 2 - add two complex numbers
public Complex plus(Complex other)
{
int left = this.real + other.real;
int right = this.imaginary + other.imaginary;
Complex result = new Complex(left, right);
return result;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//METHOD 3 - subtract two complex numbers
public Complex minus(Complex other)
{
int left = this.real - other.real;
int right = this.imaginary - other.imaginary;
Complex result = new Complex(left, right);
return result;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//METHOD 4 - multiply two cmoplex numbers
public Complex times(Complex other)
{
int left = this.real * other.real + -this.imaginary * other.imaginary;
int right = this.real * other.imaginary + this.imaginary * other.real;
Complex result = new Complex(left, right);
return result;
}
}
Just so you might wanted to know, the warning reads: Warning: The local variable sum1 is never read