Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class in C++, Java, and Python that represents complex numbers and supp

ID: 3913799 • Letter: D

Question

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 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.

here's a sample code.

Explanation / Answer

for python

class rational:
    def __init__(self, num=1, den=0):
        self.num = num
        self.den = den

    def __add__(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 __div__(self, other):
        if isinstance(other, int):
            return rational(self.num/float(other), self.den /float(other))
        elif isinstance(other, rational):
            divisor= other.num ** 2 + other.den **2
            return rational((self.num*other.num - (self.den * other.den * -1.0))/divisor , ((self.num * other.den * -1.0)+(self.den * other.num))/divisor)
        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 * other)
        elif isinstance(other, rational):
            return rational(self.num*other.num - (self.den * other.den) , (self.num * other.den)+(self.den * other.num))
        else:
            raise TypeError

    def __radd__(self, other):
        return self + other

    def __rdiv__(self, other):
        return rational(other) / rational(self.num,-1*self.den)

    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)
        elif isinstance(other, rational):
            return rational(self.num - other.num , 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))

for 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(this.num + o.num, this.den + o.den);
    }

    public Rational add(int n) {

        return new Rational(this.num + n , den);
    }

    public Rational div(Rational o) {
       float divisor;
       divisor= o.num * o.num + o.den * o.den;
        return new Rational((this.num *o.num - (this.den *o.den * -1))/divisor , (this.num * o. den * -1 + this.den * o.num)/divisor );
    }

    public Rational div(int n) {
        return new Rational(this.num/n, this.den/n);
    }

    public Rational mul(Rational o) {

        return new Rational(num * o.num, den * o.den);
    }

    public Rational mul(int n) {

        return new Rational((this.num *o.num - (this.den *o.den )) , (this.num * o. den + this.den * o.num));
    }

    public Rational sub(Rational o) {

        return new Rational(this.num - o.num , this.den * o.den);
    }

    public Rational sub(int n) {

        return new Rational(this.num - n , den);
    }

    public String toString() {

        return "(" + num + " / " + den + ")";
    }

    private int den;
    private int num;
}

I am not familiar with c++ so

i have done python and java

hope it helped!!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote