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

Write an abstract superclass called Number with a single abstract void method sq

ID: 3708832 • Letter: W

Question

Write an abstract superclass called Number with a single abstract void method square. Write a subclass called RationalNumber that has two integer fields, _numerator_ and _denominator_. Write a subclass called ComplexNumber that has two integer fields, _real_ and _complex_. Implement the square() method for both subclasses which will print the number squared with a 5 decimal precision. Write a class called NumberClient and use the following main method to test for correctness.

Make sure you decimal numbers are "pretty."

```java

public static void main(String[] args) {

RationalNumber rn = new RationalNumber(3, 9);

System.out.println("Should see 0.11111");

rn.square();

System.out.println("Should see 0.40496");

rn = new RationalNumber(7, 11);

rn.square();

System.out.println("Should see 9.00000");

rn = new RationalNumber(9, 3);

rn.square();

System.out.println("Should see 7 + 24i");

ComplexNumber cn = new ComplexNumber(4, 3);

cn.square();

System.out.println("Should see 0 + 8i");

cn = new ComplexNumber(2, 2);

cn.square();

System.out.println("Should see 65 + 72i");

cn = new ComplexNumber(9, 4);

cn.square();

}

```

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


Number.java
---------

public abstract class Number {
public abstract void square();
}


RationalNumber.java
-------------------

public class RationalNumber extends Number {
private int _numerator_;
private int _denominator_;

public RationalNumber(int num, int denom)
{
this._numerator_ = num;
this._denominator_ = denom;
}

@Override
public void square() {
double val = ((double)_numerator_) / _denominator_;
val = val * val;
System.out.printf("%.5f ", val);
}


}


ComplexNumber.java
------------------

public class ComplexNumber extends Number {
private int _real_;
private int _complex_;

public ComplexNumber(int real, int complex)
{
_real_ = real;
_complex_ = complex;
}
@Override
public void square() {

// (a + ib)^2 = a^2 + (ib)^2 + 2 * a * ib
// = a^2 - b2 + 2abi [using i^2 = -1]
int r = _real_ * _real_ - _complex_ * _complex_;
int c = 2 * _real_ * _complex_;
System.out.println(r + " + " + c + "i");
}

}


NumberClient.java
------------------


public class NumberClient {
public static void main(String[] args) {

RationalNumber rn = new RationalNumber(3, 9);

System.out.println("Should see 0.11111");

rn.square();

System.out.println("Should see 0.40496");

rn = new RationalNumber(7, 11);

rn.square();

System.out.println("Should see 9.00000");

rn = new RationalNumber(9, 3);

rn.square();

System.out.println("Should see 7 + 24i");

ComplexNumber cn = new ComplexNumber(4, 3);

cn.square();

System.out.println("Should see 0 + 8i");

cn = new ComplexNumber(2, 2);

cn.square();

System.out.println("Should see 65 + 72i");

cn = new ComplexNumber(9, 4);

cn.square();

}
}


output
-----
Should see 0.11111
0.11111
Should see 0.40496
0.40496
Should see 9.00000
9.00000
Should see 7 + 24i
7 + 24i
Should see 0 + 8i
0 + 8i
Should see 65 + 72i
65 + 72i

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