Complex number class:: Question Changed to just the Java portion Design a class
ID: 3858238 • Letter: C
Question
Complex number class:: Question Changed to just the Java portion
Design a class in and Java that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division.
(the following is for the c++ and python version, not sure if its needed for this)
op: Complex × Complex Complex
op: Complex × double Complex
op: double × Complex Complex
Where op is one of +, -, *, or /. In addition, you will need to overload the stream insertion operator << to print objects of this type.
A constructor must be defined as well as overloading the assignment operator to allow for implicit conversion from doubles to Complex. Any other methods you deem appropriate should also be included. The more complete your class the better.
The Java version will not have as many methods because Java does not allow for operator overloading or friend functions. Again, the more complete your Java class the better. Override the toString() method.
Complex.java file that is the Java implementation, and a Main.java file that instantiates and tests all methods of the Complex class.
Explanation / Answer
Below is the implementation in C++
complex.cc
#include "complex.h"
ostream &operator<<(ostream &out, const Complex &c)
{
if (c.real == 0 && c.virl == 0) {
out << 0;
} else if (c.real == 0) {
out << c.virl << "j";
} else if (c.virl == 0) {
out << c.real;
} else if (c.virl > 0) {
if (c.virl == 1) {
out << c.real << "+j";
} else {
out << c.real << "+" << c.virl << "j";
}
} else {
if (c.virl == -1) {
cout << c.real << "-j";
} else {
out << c.real << c.virl << "j";
}
}
return out;
}
Complex::Complex(double real, double virl) :
real(real), virl(virl)
{
}
Complex::Complex(double real) :
real(real), virl(0)
{
}
Complex::~Complex()
{
}
// operator overloading
Complex Complex::operator+(const Complex &that) const
{
return Complex(this->real + that.real, this->virl + that.virl);
}
Complex Complex::operator-(const Complex &that) const
{
return Complex(this->real - that.real, this->virl - that.virl);
}
Complex Complex::operator*(const Complex &that) const
{
double real = this->real * that.real - this->virl * that.virl;
double virl = this->real * that.virl + this->virl * that.real;
return Complex(real, virl);
}
Complex Complex::operator/(const Complex &that) const
{
Complex result = *this * Complex(that.real, -that.virl);
double abs = that.real * that.real + that.virl * that.virl;
result.real /= abs;
result.virl /= abs;
return result;
}
Complex operator+(const double x, const Complex &that)
{
return that + x;
}
Complex operator-(const double x, const Complex &that)
{
return Complex(x) - that;
}
Complex operator*(const double x, const Complex &that)
{
return that * x;
}
Complex operator/(const double x, const Complex &that)
{
return Complex(x) / that;
}
double Complex::getReal() const
{
return real;
}
double Complex::getVirl() const
{
return virl;
}
complex.h
#ifndef _COMPLEX_H
#define _COMPLEX_H
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double virl;
public:
Complex(double real, double virl);
// this constructor will converts a double to Complex implictly
Complex(double real);
~Complex();
// operator overloading
Complex operator+(const Complex &that) const;
Complex operator-(const Complex &that) const;
Complex operator*(const Complex &that) const;
Complex operator/(const Complex &that) const;
friend Complex operator+(const double x, const Complex &that);
friend Complex operator-(const double x, const Complex &that);
friend Complex operator*(const double x, const Complex &that);
friend Complex operator/(const double x, const Complex &that);
friend ostream &operator<<(ostream &out, const Complex &c);
double getReal() const;
double getVirl() const;
};
#endif
main.cc
#include "complex.h"
int main() {
// test the functions of Complex class
Complex a(1, 2);
Complex b(3, 4);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
// operators
// two complex
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "a / b = " << a / b << endl;
// complex with double
cout << "a + 1 = " << a + 1 << endl;
cout << "a - 1 = " << a - 1 << endl;
cout << "a * 2 = " << a * 2 << endl;
cout << "a / 2 = " << a / 2 << endl;
// double with complex
cout << "-3 + b = " << -3 + b << endl;
cout << "3 - b = " << 3 - b << endl;
cout << "3 * b = " << 3 * b << endl;
cout << "3 / b = " << 3 / b << endl;
return 0;
}
Sample Run: -
a = 1+2j
b = 3+4j
a + b = 4+6j
a - b = -2-2j
a * b = -5+10j
a / b = 0.44+0.08j
a + 1 = 2+2j
a - 1 = 2j
a * 2 = 2+4j
a / 2 = 0.5+j
-3 + b = 4j
3 - b = -4j
3 * b = 9+12j
3 / b = 0.36-0.48j
Here is Java implementation
Complex.java
public class Complex {
private final double real;
private final double virl;
public Complex(double real, double virl) {
this.real = real;
this.virl = virl;
}
public double getReal() {
return real;
}
public double getVirl() {
return virl;
}
public Complex add(Complex that) {
return new Complex(this.real + that.real, this.virl + that.virl);
}
public Complex subtract(Complex that) {
return new Complex(this.real - that.real, this.virl - that.virl);
}
public Complex multiply(Complex that) {
double real = this.real * that.real - this.virl * that.virl;
double virl = this.real * that.virl + this.virl * that.real;
return new Complex(real, virl);
}
public Complex divide(Complex that) {
Complex result = this.multiply(new Complex(that.real, -that.virl));
double abs = that.real * that.real + that.virl * that.virl;
return new Complex(result.real / abs, result.virl / abs);
}
public Complex add(double that) {
return this.add(new Complex(that, 0));
}
public Complex subtract(double that) {
return this.subtract(new Complex(that, 0));
}
public Complex multiply(double that) {
return this.multiply(new Complex(that, 0));
}
public Complex divide(double that) {
return this.divide(new Complex(that, 0));
}
@Override
public String toString() {
Complex c = this;
if (c.real == 0 && c.virl == 0) {
return "0";
} else if (c.real == 0) {
return "" + c.virl + "j";
} else if (c.virl == 0) {
return "" + c.real;
} else if (c.virl > 0) {
if (c.virl == 1) {
return "" + c.real + "+j";
} else {
return "" + c.real + "+" + c.virl + "j";
}
} else {
if (c.virl == -1) {
return "" + c.real + "-j";
} else {
return "" + c.real + c.virl + "j";
}
}
}
}
Main.java
public class Main {
public static void main(String[] args) {
Complex a = new Complex(1, 2);
Complex b = new Complex(3, 4);
System.out.printf("a = %s ", a);
System.out.printf("b = %s ", a);
// operators
System.out.printf("a + b = %s ", a.add(b));
System.out.printf("a - b = %s ", a.subtract(b));
System.out.printf("a * b = %s ", a.multiply(b));
System.out.printf("a / b = %s ", a.divide(b));
System.out.printf("a + 1 = %s ", a.add(1));
System.out.printf("a - 1 = %s ", a.subtract(1));
System.out.printf("a * 2 = %s ", a.multiply(2));
System.out.printf("a / 2 = %s ", a.divide(2));
}
}
Sample Run: -
a = 1.0+2.0j
b = 1.0+2.0j
a + b = 4.0+6.0j
a - b = -2.0-2.0j
a * b = -5.0+10.0j
a / b = 0.44+0.08j
a + 1 = 2.0+2.0j
a - 1 = 2.0j
a * 2 = 2.0+4.0j
a / 2 = 0.5+j
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.