Question
Complex number class Design a class in C++, Java, and Python that represents complex numbers and supports important operations such as addition, subtraction, multiplication and
Complex number class Design a class in C++, Java, and Python that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. For the C++ and Python versions you will need to implement the following functions for each operation: 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. The Python version you should also include functions for converting from complexes to strings. The required files for this project are: a complex.h file that contains the declaration of the complex class, a complex.cc file that contains the implementations of method and functions declared in the complex class, a main.cc that instantiates complex numbers and tests all methods and functions, a Complex.java file that is the Java implementation, and a Main.java file that instantiates and tests all methods of the Complex class. The python files required are a complex.py file. In addition you must write a 2.5 (625+ words) page paper comparing and contrasting the C++, Java, and Python ways of implementing such a class (hint: operator overloading). Comment on the pros and cons of all languages and finally make a determination as to which language is superior. Submit the file as complex.txt
here's a sample code.
/* * * Java version * */ /* Main.java */ public class Main { public static void main(String[] args) { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); int i = 5; System.out.println(a + " + " + b + " = " + a.add(b)); System.out.println(a + " - " + b + " = " + a.sub(b)); System.out.println(a + " * " + b + " = " + a.mul(b)); System.out.println(a + " / " + b + " = " + a.div(b)); System.out.println(a + " + " + i + " = " + a.add(i)); System.out.println(a + " - " + i + " = " + a.sub(i)); System.out.println(a + " * " + i + " = " + a.mul(i)); System.out.println(a + " / " + i + " = " + a.div(i)); } } /* Rational.java */ public class Rational { public Rational() { this(0); } public Rational(int num) { this(num, 1); } public Rational(int num, int den) { this.num = num; this.den = den; } public Rational add(Rational o) { return new Rational(num * o.den + o.num * den, den * o.den); } public Rational add(int n) { return new Rational(num + n * den, den); } public Rational div(Rational o) { return new Rational(num * o.den, den * o.num); } public Rational div(int n) { return new Rational(num, den * n); } public Rational mul(Rational o) { return new Rational(num * o.num, den * o.den); } public Rational mul(int n) { return new Rational(num * n, den); } public Rational sub(Rational o) { return new Rational(num * o.den - o.num * den, den * o.den); } public Rational sub(int n) { return new Rational(num - n * den, den); } public String toString() { return "(" + num + " / " + den + ")"; } private int den; private int num; } /* * * C++ version * */ /* rational.h */ #ifndef RATIONAL_H #define RATIONAL_H #includeusing std::ostream; struct rational { rational(int = 0, int = 1); rational operator+(const rational &) const; rational operator-(const rational &) const; rational operator*(const rational &) const; rational operator/(const rational &) const; rational operator+(int) const; rational operator-(int) const; rational operator*(int) const; rational operator/(int) const; friend rational operator+(int, const rational &); friend rational operator-(int, const rational &); friend rational operator*(int, const rational &); friend rational operator/(int, const rational &); friend ostream &operator<<(ostream &, const rational &); private: int den; int num; }; #endif /* RATIONAL_H */ /* rational.cc */ #include #include "rational.h" rational::rational(int num, int den) : num(num), den(den) {} rational rational::operator+(const rational &o) const { return rational(num * o.den + o.num * den, den * o.den); } rational rational::operator+(int n) const { return rational(num + n * den, den); } rational rational::operator-(const rational &o) const { return rational(num * o.den - o.num * den, den * o.den); } rational rational::operator-(int n) const { return rational(num - n * den, den); } rational rational::operator*(const rational &o) const { return rational(num * o.num, den * o.den); } rational rational::operator*(int n) const { return rational(num * n, den); } rational rational::operator/(const rational &o) const { return rational(num * o.den, den * o.num); } rational rational::operator/(int n) const { return rational(num, den * n); } rational operator+(int n, const rational &o) { return o + n; } rational operator-(int n, const rational &o) { return rational(n) - o; } rational operator*(int n, const rational &o) { return o * n; } rational operator/(int n, const rational &o) { return rational(n) / o; } ostream &operator<<(ostream &out, const rational &o) { out << '(' << o.num << " / " << o.den << ')'; return out; } /* main.cc */ #include #include "rational.h" using std::cout; using std::endl; int main(void) { rational a(1, 2); rational b(1, 3); int i = 5; cout << a << " + " << b << " = " << a + b << endl; cout << a << " - " << b << " = " << a - b << endl; cout << a << " * " << b << " = " << a * b << endl; cout << a << " / " << b << " = " << a / b << endl; cout << a << " + " << i << " = " << a + i << endl; cout << a << " - " << i << " = " << a - i << endl; cout << a << " * " << i << " = " << a * i << endl; cout << a << " / " << i << " = " << a / i << endl; cout << i << " + " << a << " = " << i + a << endl; cout << i << " - " << a << " = " << i - a << endl; cout << i << " * " << a << " = " << i * a << endl; cout << i << " / " << a << " = " << i / a << endl; return 0; } # # # Python version # # class rational: def __init__(self, num=0, den=1): self.num = num self.den = den def __add__(self, other): if isinstance(other, int): return rational(self.num + other * self.den, self.den) elif isinstance(other, rational): return rational(self.num * other.den + other.num * self.den, self.den * other.den) else: raise TypeError def __truediv__(self, other): if isinstance(other, int): return rational(self.num, self.den * other) elif isinstance(other, rational): return rational(self.num * other.den, self.den * other.num) else: raise TypeError def __float__(self): return float(self.num) / self.den def __int__(self): return self.num / self.den def __mul__(self, other): if isinstance(other, int): return rational(self.num * other, self.den) elif isinstance(other, rational): return rational(self.num * other.num, self.den * other.den) else: raise TypeError def __radd__(self, other): return self + other def __rtruediv__(self, other): return rational(other) / self def __rmul__(self, other): return self * other def __rsub__(self, other): return rational(other) - self def __str__(self): return '(' + str(self.num) + ' / ' + str(self.den) + ')' def __sub__(self, other): if isinstance(other, int): return rational(self.num - other * self.den, self.den) elif isinstance(other, rational): return rational(self.num * other.den - other.num * self.den, self.den * other.den) else: raise TypeError if __name__ == '__main__': a = rational(1, 2) b = rational(1, 3) i = 5 print('%s + %s = %s' % (a, b, a + b)) print('%s - %s = %s' % (a, b, a - b)) print('%s * %s = %s' % (a, b, a * b)) print('%s / %s = %s' % (a, b, a / b)) print('%s + %i = %s' % (a, i, a + i)) print('%s - %i = %s' % (a, i, a - i)) print('%s * %i = %s' % (a, i, a * i)) print('%s / %i = %s' % (a, i, a / i)) print('%i + %s = %s' % (i, a, i + a)) print('%i - %s = %s' % (i, a, i - a)) print('%i * %s = %s' % (i, a, i * a)) print('%i / %s = %s' % (i, a, i / a))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started