In: Computer Science
Implement a class named Complex that represents immutable complex numbers.
Code Given:
import java.util.Arrays;
import java.util.List;
/**
* A class that represents immutable complex numbers.
*
* @author EECS2030 Fall 2019
*
*/
public final class Complex {
/**
* Initializes this complex number to <code>0 +
0i</code>.
*
*/
public Complex() {
}
/**
* Initializes this complex number so that it has the
same real and
* imaginary parts as another complex number.
*/
public Complex(Complex other) {
}
/**
* Initializes this complex number so that it has the
given real
* and imaginary components.
*/
public Complex(double re, double im) {
}
/**
* A static factory method that returns a new complex
number whose real part
* is equal to re and whose imaginary part is equal to
0.0
*/
public static Complex real(double re) {
return null;
}
/**
* A static factory method that returns a new complex
number whose real part
* is equal to 0.0 and whose imaginary part is equal to
im
*/
public static Complex imag(double im) {
return null;
}
/**
* Get the real part of the complex number.
*/
public double re() {
return 0.0;
}
/**
* Get the imaginary part of the complex number.
*/
public double im() {
return 0.0;
}
/**
* Add this complex number and another complex number
to obtain a new
* complex number. Neither this complex number nor c is
changed by
* this method.
*/
public Complex add(Complex c) {
return null;
}
/**
* Multiply this complex number with another complex
number to obtain a new
* complex number. Neither this complex number nor c is
changed by
* this method.
*/
public Complex multiply(Complex c) {
return null;
}
/**
* Compute the magnitude of this complex number.
*/
public double mag() {
return 0.0;
}
/**
* Return a hash code for this complex number.
*/
@Override
public int hashCode() {
return 0;
}
/**
* Compares this complex number with the specified
object. The result is
* <code>true</code> if and only if the
argument is a <code>Complex</code>
* number with the same real and imaginary parts as
this complex number.
*/
@Override
public boolean equals(Object obj) {
return true;
}
/**
* Returns a string representation of this complex
number.
*/
@Override
public String toString() {
return "";
}
/**
* Returns a complex number holding the value
represented by the given
* string.
*/
public static Complex valueOf(String s) {
Complex result = null;
String t = s.trim();
List<String> parts =
Arrays.asList(t.split("\\s+"));
// split splits the string s by
looking for spaces in s.
// If s is a string that might be
interpreted as a complex number
// then parts will be a list having
3 elements. The first
// element will be a real number,
the second element will be
// a plus or minus sign, and the
third element will be a real
// number followed immediately by
an i.
//
// To complete the implementation
of this method you need
// to do the following:
//
// -check if parts has 3
elements
// -check if the second element of
parts is "+" or "-"
// -check if the third element of
parts ends with an "i"
// -if any of the 3 checks are
false then s isn't a complex number
// and you should throw an
exception
// -if all of the 3 checks are true
then s might a complex number
// -try to convert the first
element of parts to a double value
// (use Double.valueOf); this might
fail in which case s isn't
// a complex number
// -remove the 'i' from the third
element of parts and try
// to convert the resulting string
to a double value
// (use Double.valueOf); this might fail in which case
s isn't
// a complex number
// -you now have real and imaginary
parts of the complex number
// but you still have to account
for the "+" or "-" which
// is stored as the second element
of parts
// -once you account for the sign,
you can return the correct
// complex number
return result;
}
}
Done all the method except ValueOf, PLEASE COMMENT if there is any concern.
import java.util.Arrays;
import java.util.List;
/**
* A class that represents immutable complex numbers.
*
* @author EECS2030 Fall 2019
*
*/
public final class Complex {
private double real;
private double imaginary;
/**
* Initializes this complex number to <code>0 + 0i</code>.
*
*/
public Complex() {
real = 0;
imaginary = 0;
}
/**
* Initializes this complex number so that it has the same real and
* imaginary parts as another complex number.
*/
public Complex(Complex other) {
real = other.real;
imaginary = other.imaginary;
}
/**
* Initializes this complex number so that it has the given real
* and imaginary components.
*/
public Complex(double re, double im) {
real = re;
imaginary = im;
}
/**
* A static factory method that returns a new complex number whose real part
* is equal to re and whose imaginary part is equal to 0.0
*/
public static Complex real(double re) {
return new Complex(re,0);
}
/**
* A static factory method that returns a new complex number whose real part
* is equal to 0.0 and whose imaginary part is equal to im
*/
public static Complex imag(double im) {
return new Complex(0,im);
}
/**
* Get the real part of the complex number.
*/
public double re() {
return real;
}
/**
* Get the imaginary part of the complex number.
*/
public double im() {
return imaginary;
}
/**
* Add this complex number and another complex number to obtain a new
* complex number. Neither this complex number nor c is changed by
* this method.
*/
public Complex add(Complex c) {
double r = this.real + c.real;
double im = this.imaginary + c.imaginary;
return new Complex(r,im);
}
/**
* Multiply this complex number with another complex number to obtain a new
* complex number. Neither this complex number nor c is changed by
* this method.
*/
public Complex multiply(Complex c) {
double r = (this.real * c.real - this.imaginary * c.imaginary);
double im = (this.real * c.imaginary + this.imaginary * c.real) ;
return new Complex(r,im);
}
/**
* Compute the magnitude of this complex number.
*/
public double mag() {
return Math.sqrt(real*real + imaginary*imaginary);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(imaginary);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(real);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Complex other = (Complex) obj;
if (Double.doubleToLongBits(imaginary) != Double.doubleToLongBits(other.imaginary))
return false;
if (Double.doubleToLongBits(real) != Double.doubleToLongBits(other.real))
return false;
return true;
}
/**
* Returns a string representation of this complex number.
*/
@Override
public String toString() {
if (imaginary == 0) return real + "";
if (real == 0) return imaginary + "i";
if (imaginary < 0) return real + " - " + (-imaginary) + "i";
return real + " + " + imaginary + "i";
}
/**
* Returns a complex number holding the value represented by the given
* string.
*/
public static Complex valueOf(String s) {
Complex result = null;
String t = s.trim();
List<String> parts = Arrays.asList(t.split("\\s+"));
// split splits the string s by looking for spaces in s.
// If s is a string that might be interpreted as a complex number
// then parts will be a list having 3 elements. The first
// element will be a real number, the second element will be
// a plus or minus sign, and the third element will be a real
// number followed immediately by an i.
//
// To complete the implementation of this method you need
// to do the following:
//
// -check if parts has 3 elements
// -check if the second element of parts is "+" or "-"
// -check if the third element of parts ends with an "i"
// -if any of the 3 checks are false then s isn't a complex number
// and you should throw an exception
// -if all of the 3 checks are true then s might a complex number
// -try to convert the first element of parts to a double value
// (use Double.valueOf); this might fail in which case s isn't
// a complex number
// -remove the 'i' from the third element of parts and try
// to convert the resulting string to a double value
// (use Double.valueOf); this might fail in which case s isn't
// a complex number
// -you now have real and imaginary parts of the complex number
// but you still have to account for the "+" or "-" which
// is stored as the second element of parts
// -once you account for the sign, you can return the correct
// complex number
return result;
}
}
==============================
SEE CODE
Done all the method except ValueOf, PLEASE COMMENT if there is any concern.