Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

A rational number is any number that can be expressed as the quotient or fraction of two integer values, with the denominator not equal to

A rational number is any number that can be expressed as the quotient or fraction of two integer values, with the denominator not equal to 0. That is, any number that can be written in the form . Everyone learned to work with fractions at some stage in their youth so we just present, without comment, a list of operators together with their definitions.
We will use these definitions when we implement operators for our rational numbers.
An interesting observation about rational numbers is that equal values can have different forms. For example, The normalized value of all of these fractions is because each of the other values can be simplified to this value.
Note that when you add one rational number to another one the result of the addition is a new rational number. The original values used in the addition are not changed or modified. Therefore, operations add, sub, mult, etc all return new Rational numbers.
interface Operations{
public Rational add(Rational q);
public Rational sub(Rational q);
public Rational mult(Rational q);
public Rational multBy(int k);
public Rational div(Rational q);
public Rational divBy(int k);
public boolean eq(Rational q); //returns true if this equals q
}
The class, as presented below, has a single constructor that takes two integer arguments It assumes that the denominator, d, is not zero. It also has two attributes, num and den, that refer to the numerator and denominator of the fraction. The private function gcd, greatest common divisor, is used to ensure that all fractions are stored in normalized form. This function calculates the greatest common divisor using only the absolute values of both the numerator and denominator. Both n and d are divisible by g.
Rewrite the class Rational below implementing the Operations Interface.
class Rational {
private int num,den;
public Rational(int n, int d){//assume d != 0
if(n < 0 && d < 0){
n = -n; d = -d;
} else if(d < 0){
n = -n; d = -d;
}
//ensures that d never negative and n positive e.g. 1/-2 is changed to -1/2
int g = gcd(Math.abs(n), Math.abs(d));
num = n/g;den = d/g;
}
public Rational(int n){//d == 1 ...}
public int num(){return num;}
public int den(){return den;}
public String toString(){...}
private int gcd(int a, int b){
if(b == 0) return a;
else return gcd(b,a%b);
}
}

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_2

Step: 3

blur-text-image_3

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

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students explore these related Databases questions

Question

How do books become world of wonder?

Answered: 3 weeks ago