Question: In Java, using the following code as public class Rational extends Object implements Comparable { private final int num; private final int den; public final

 In Java, using the following code as public class Rational extends

In Java, using the following code as

public class Rational extends Object implements Comparable {
private final int num;
private final int den;
public final static Rational ZERO = new Rational();
public final static Rational ONE = new Rational(1);
// ****************************************************************
public int getNumerator() {
return num;
}
public int getDenominator() {
return den;
}
// ****************************************************************
public Rational() {
this(0, 1);
}
public Rational(int num) {
this(num, 1);
}
public Rational(int[] fract) {
this(fract[0], fract[1]);
}
public Rational(int numerator, int denominator) {
if (denominator == 0) {
throw new ArithmeticException("denominator is zero");
}
// reduce fraction
int g = gcf(numerator, denominator);
if (denominator > 0) {
this.num = numerator / g;
this.den = denominator / g;
} else {
this.num = -numerator / g;
this.den = -denominator / g;
}
}
// ****************************************************************
public Rational plus(Rational b) {
Rational a = this;
// special cases (Really Not Needed)
if (a.equals(ZERO))
return b;
if (b.compareTo(Rational.ZERO) == 0)
return a;
int num = a.num * b.den + a.den * b.num;
int den = a.den * b.den;
return new Rational(num, den);
}
public Rational times(Rational b) {
Rational a = this;
int num = a.num * b.num;
int den = a.den * b.den;
return new Rational(num, den);
}
public Rational negate() {
return new Rational(-this.num, this.den);
}
public Rational minus(Rational b) {
Rational a = this;
return a.plus(b.negate());
}
public Rational divides(Rational b) {
Rational a = this;
return a.times(b.reciprocal());
}
public Rational reciprocal() {
return new Rational(this.den, this.num);
}
public Rational abs() {
if (this.num >= 0)
return this;
return this.negate();
}
// ****************************************************************
public double toDouble() {
return (double)this.num / this.den;
}
public String toString() {
if (den == 1)
return num + "";
else
return num + "/" + den;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
Rational other = (Rational) obj;
if (den != other.den)
return false;
if (num != other.num)
return false;
return true;
}
// return { -, 0, + } if a b
// a.compareTo(b)
public int compareTo(Rational b) {
Rational a = this;
int lhs = a.num * b.den;
int rhs = a.den * b.num;
if (lhs
return -1;
if (lhs > rhs)
return 1;
return 0;
}
private static int gcf(int m, int n) {
if (m
m = -m;
if (n
n = -n;
while (n != 0) {
int rem = m % n;
m = n;
n = rem;
}
return m;
}
// Not needed or used, but just cool to see
// @SuppressWarnings("unused")
// private static int lcm(int m, int n) {
// if (m
// m = -m;
// if (n
// n = -n;
// return m * n / gcf(m, n);
// }
}
import java.util.Scanner;
public class ComplexNumberDriver {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double real;
double imag;
while (true) {
System.out.print("Enter Real And Imaginary Components of Complex Number 1: ");
real = sc.nextDouble();
imag = sc.nextDouble();
Complex c1 = new Complex(real, imag);
System.out.print("Enter Real And Imaginary Components of Complex Number 2: ");
real = sc.nextDouble();
imag = sc.nextDouble();
Complex c2 = new Complex(real, imag);
sc.nextLine();
Complex plus = c1.add(c2);
Complex minus = c1.subtract(c2);
Complex times = c1.multiply(c2);
Complex quot = c1.divide(c2);
System.out.println("c1 = " + c1);
System.out.println("c2 = " + c2);
System.out.println("c1 + c2 = " + plus);
System.out.println("c1 - c2 = " + minus);
System.out.println("c1 * c2 = " + times);
System.out.println("c1 / c2 = " + quot);
System.out.println("-c1 = " + c1.negate());
System.out.println("-c2 = " + c2.negate());
System.out.println("Conj c1 = " + c1.conjugate());
System.out.println("Conj c2 = " + c2.conjugate());
System.out.println("|c1| = " + c1.abs());
System.out.println("|c2| = " + c2.abs());
System.out.println("Dist = " + c1.distance(c2));
System.out.println("==? = " + c1.equals(c2));
System.out.println(">? = " + c1.greaterThan(c2));
System.out.println(" = " + c1.lessThan(c2));
System.out.print("Do again? ");
if (!sc.next().toUpperCase().equals("Y"))
break;
sc.nextLine();
}
}
}

Review the program on Ralicaal Numbers. This code ghould serve a8 a guide to witing the Complex da8s. dava provides several types of numbers, but it does not provide suppott for complex numbers. Your tagk is do implement this support. The following table lats the kind of data and the kind of operations you wil need Tha Complex class should hava 4 constructars - One that takes two doubla parameters, a real part and an imaginary part - One that takes one double perameter, the real part (the imeginary part is sasumed to be zero) - One thal lakes no parameters, the real and imaginary parts arre zeru. - (As a bonus implement this constructor): One that takes a String such as "3+5i", or "17.5-21.3i, or "3.1416", or "99i" (allow blanks in the string) And thasa public mathods - puailic Complex addicomplex c] - pubic Complex subtracticomplex c) - pubic Complex muliply(Complex el - pubir Complex divide(Complex c) - pubic double abed - public Complax nagatad - puaili Complex conjugate(i) - pubiric double diatance[Complex c) - pubir beolear equaly(Cumplex c) - pubir boolean greaterThan(Complex c) - pubir hooloan lessThan(Complax c) - pubile String toStringlo Al numbers should be immutable thet is, there should be no way to change their components after the numbers have been crebted Most of your methods simply rebum a new Complex number. Use the Rational Number dass as a guide to writing this Complex Number dass. Make sure you implement the ComparableeComplex interface. If the distance between the numbers diviled by the abs(larger) distance is less than IE-6. Clarification an the equals method: Becausa doublas cannot ba accurataly chacked for aquality, you nsad to do samething alsa to defna an equalty mathoo The class has a distance method. If calculates the distance between two points. x absa is a method that cakulates the magntude of the compler number x. So say you have two complex numbers x and y. They are equal if. Hsppy Coding

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!