In: Computer Science
Your first lab is to create a simple class that emulates some mathematical functions in relation to complex numbers. A complex number is a number of the form a + bi, where a is a real number and bi is an imaginary number. Create a class Complex having two private data members real and imag of type double. The class has two constructors, one default (no parameters) and one whose parameters initialize the instance variables. It also need the following member functions, all of which are public:
Hints: For complex numbers:
Addition: add the real and imaginary components separately:
(7 + 4i) + (23 – 12i) = 30 – 8i
Subtraction: subtract the real and imaginary components separately:
(7 + 4i) - (23 – 12i) = -16 + 16i
Multiplication: More complicated (will be discussed in lab).
You MUST write a driver program to test all of the above functions. I also want a UML diagram for the Complex class.
The lab must be turned in on Blackboard. I need to see your Complex class, driver program (call it ComplexRunner.java), and UML diagram. MAKE SURE TO COMMENT YOUR CODE CORRECTLY!
/*Source code*/ :
class ComplexRunner {
/* driver program */
public static void main(String[] args) {
Complex a = new Complex(5.0, 6.0);
Complex b = new Complex(-3.0, 4.0);
System.out.println("a = " + a.print());
System.out.println("b = " + b.print());
System.out.println("Real part of a = " + a.getReal());
System.out.println("Imaginary part of a = " +
a.getImaginary());
System.out.println("Real part of b = " + b.getReal());
System.out.println("Imaginary part of b = " +
b.getImaginary());
Complex c = new Complex();
c.setReal(4.0);
c.setImaginary(-8.0);
System.out.println("c = " + c.print());
/* add two complex numbers */
a.addComplex(b);
System.out.println("a + b = " + a.print());
/* subtract two complex numbers */
b.subtractComplex(a);
System.out.println("a - b = " + b.print());
/* multiply two complex numbers */
a.multiplyComplex(b);
System.out.println("a * b = " + a.print());
}
}
public class Complex {
/* complex number's real and imaginary part */
private double real;
private double imaginary;
/* default constructor */
public Complex() {
this.real = 0;
this.imaginary = 0;
}
/* create a new object with the given real and imaginary parts
*/
public Complex(double real_part, double imaginary_part) {
this.real = real_part;
this.imaginary = imaginary_part;
}
/* getter for real part */
public double getReal() {
return real;
}
/* setter for real part */
public void setReal(double real) {
this.real = real;
}
/* getter for imaginary part */
public double getImaginary() {
return imaginary;
}
/* setter for imaginary part */
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
/* return a string representation of the invoking Complex object
*/
public String print() {
if (imaginary == 0) return real + "";
if (real == 0) return imaginary + "i";
if (imaginary < 0) {
return real + " - " + (-imaginary) + "i";
} else {
return real + " + " + imaginary + "i";
}
}
/* add two complex numbers */
public void addComplex(Complex b) {
this.real += b.real;
this.imaginary += b.imaginary;
}
/* subtract two complex numbers */
public void subtractComplex(Complex b) {
this.real += b.real;
this.imaginary += b.imaginary;
}
/* multiply two complex numbers */
public void multiplyComplex(Complex b) {
this.real = this.real * b.real - this.imaginary *
b.imaginary;
this.imaginary = this.real * b.imaginary + this.imaginary *
b.real;
}
}
/*ComplexRunner.java*/
Screenshot :
Complex.java
Screenshot of the output:
---------------Could you please leave a THUMBS Up for my work---------