Question
Complex number class // Need Help in Java Design a class in Java that represents complex numbers and supports important operations such as addition, subtraction,
Complex number class
// Need Help in Java
Design a class in Java 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:
// Need Help in Java
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.
Here is some example 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; }
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