Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Adapt your Rational class : public class Rational { private int num; private int denom; public Rational() { num = 0; denom = 1; }

Adapt your Rational class :

public class Rational { private int num; private int denom; public Rational() { num = 0; denom = 1; } public Rational(int num, int denom) { this.num = num; this.denom = denom; } int getNum() { return num; } int getDenom() { return denom; } public Rational add(Rational rhs) { return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom); } public Rational subtract(Rational rhs) { return new Rational(num * rhs.denom - rhs.num * denom, denom * rhs.denom); } public Rational multiply(Rational rhs) { return new Rational(num * rhs.num, denom * rhs.denom); } public Rational divide(Rational rhs) { return new Rational(num * rhs.denom, rhs.num * denom); } public String toString() { return num + "/" + denom; } public static void main(String[] args) { Rational r1 = new Rational(1, 2); // 1/2 Rational r2 = new Rational(3, 4); // 3/4 Rational result = new Rational(); result = r1.add(r2); System.out.println(result); // etc } }

to implement the Comparable interface :

public interface Comparable

int compareTo(T rhs);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students also viewed these Databases questions

Question

How many molecules are present in 0.336 mol of acetylene (C2H2)?

Answered: 1 week ago