Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

---I need help in JAVA --- I need to modify the following code to work with complex numbers op: Complex Complex Complex op: Complex double

---I need help in JAVA --- I need to modify the following code to work with complex numbers 

op: Complex Complex Complex

op: Complex double Complex

op: double Complex Complex

the output should be like :

(1 + 2i) + (1 + 3i) = (2 + 5i) (1 + 2i) - (1 + 3i) = (0 + -1i) (1 + 2i) * (1 + 3i) = (-5 + 5i) (1 + 2i) / (1 + 3i) = (0.7 + -0.1i) (1 + 2i) + 5 = (6 + 2i) (1 + 2i) - 5 = (-4 + 2i) (1 + 2i) * 5 = (5 + 10i) (1 + 2i) / 5 = (0.2 + 0.4i) 5 + (1 + 2i) = (6 + 2i) 5 - (1 + 2i) = (4 + -2i) 5 * (1 + 2i) = (5 + 10i) 5 / (1 + 2i) = (1.0 + -2.0i)

*** need imaginary number i ***

/* * * 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; }

output screenshots for def upvote thanks!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions