Question: ***JAVA**** Intructions given..... Re-implement the Rational class Extend the Number class and implement the Comparable interface to introduce new functionality to your Rational class. The

***JAVA****

Intructions given.....

Re-implement the Rational class

Extend the Number class and implement the Comparable interface to introduce new functionality to your Rational class. The class definition should look like this:

public class Rational extends Number implements Comparable { // code goes here } 

In addition a new private method gcd should be defined to ensure that the number is always represented in the lowest terms.

Here is the Main class that will be used to test your Rational classes.

public class Main { public static void main(String[] args) { Rational a = new Rational(2, 4); Rational b = new Rational(2, 6); 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)); Rational[] arr = {new Rational(7, 1), new Rational(6, 1), new Rational(5, 1), new Rational(4, 1), new Rational(3, 1), new Rational(2, 1), new Rational(1, 1), new Rational(1, 2), new Rational(1, 3), new Rational(1, 4), new Rational(1, 5), new Rational(1, 6), new Rational(1, 7), new Rational(1, 8), new Rational(1, 9), new Rational(0, 1)}; selectSort(arr); for (Rational r : arr) { System.out.println(r); } Number n = new Rational(3, 2); System.out.println(n.doubleValue()); System.out.println(n.floatValue()); System.out.println(n.intValue()); System.out.println(n.longValue()); } public static > void selectSort(T[] array) { T temp; int mini; for (int i = 0; i < array.length - 1; ++i) { mini = i; for (int j = i + 1; j < array.length; ++j) { if (array[j].compareTo(array[mini]) < 0) { mini = j; } } if (i != mini) { temp = array[i]; array[i] = array[mini]; array[mini] = temp; } } } } 

End of Instruction.........................................................................

This is what i have and im getting some error can someone help me out and make it run

public class Main {

public static void main(String[] args) {

Rational a = new Rational(2, 4);

Rational b = new Rational(2, 6);

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));

Rational[] arr = {new Rational(7, 1), new Rational(6, 1),

new Rational(5, 1), new Rational(4, 1),

new Rational(3, 1), new Rational(2, 1),

new Rational(1, 1), new Rational(1, 2),

new Rational(1, 3), new Rational(1, 4),

new Rational(1, 5), new Rational(1, 6),

new Rational(1, 7), new Rational(1, 8),

new Rational(1, 9), new Rational(0, 1)};

selectSort(arr);

for (Rational r : arr) {

System.out.println(r);

}

Number n = new Rational(3, 2);

System.out.println(n.doubleValue());

System.out.println(n.floatValue());

System.out.println(n.intValue());

System.out.println(n.longValue());

}

public static > void selectSort(T[] array) {

T temp;

int mini;

for (int i = 0; i < array.length - 1; ++i) {

mini = i;

for (int j = i + 1; j < array.length; ++j) {

if (array[j].compareTo(array[mini]) < 0) {

mini = j;

}

}

if (i != mini) {

temp = array[i];

array[i] = array[mini];

array[mini] = temp;

}

}

}

}

public class Rational extends Number implements Comparable {

public int compareTo(Rational o){

if (Rational < o.Rational){

return -1;

}

else if (Rational > o.Rational){

return +1;

}

return 0;

}

int Rational;

public Rational(double num, double den) {

numerator = num;

denominator = den;

}

public Rational add(Rational o) {

double numerator1 = numerator * o.denominator;

double numerator2 = o.numerator * denominator;

double denominator1 = denominator * o.denominator;

return new Rational(numerator1 + numerator2, denominator1);

}

public Rational div(Rational o) {

double numerator1 = numerator * o.denominator;

double denominator1 = denominator * o.numerator;

return new Rational(numerator1, denominator1);

}

public Rational mul(Rational o) {

return new Rational(numerator * o.numerator, denominator * o.denominator);

}

public Rational sub(Rational o) {

double numerator1 = numerator * o.denominator;

double numerator2 = o.numerator * denominator;

double denominator1 = denominator * o.denominator;

return new Rational(numerator1 - numerator2, denominator1);

}

private Rational gcd(Rational o){

int gcd = 1;

int k = 1;

while (k <= denominator && k <= o.denominator) {

if (numerator % k == 0 && o.numerator % k == 0){

gcd = k;

k++;

}

}

return new Rational(numerator, gcd);

}

public String toString() {

return "(" + numerator + "/" + denominator + ")";

}

private double numerator;

private double denominator;

abstract class Number {

public double doubleValue() {

return doubleValue();

}

public float floatValue() {

return floatValue();

}

public int intVaue() {

return intValue();

}

public long longValue() {

return longValue();

}

}

}

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!