Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following Java code shows an implementation of a class for the fraction representation for rational numbers. class RationalNum { private int numerator; private int

The following Java code shows an implementation of a class for the fraction representation for rational numbers.

class RationalNum

{

private int numerator;

private int denominator;

// default constructor to initialize the rational number to 0

RationalNum()

{

// Please add your code here

...

}

// initialize the rational number to a/b

RationalNum(final int a, final int b)

{

// Please add your code here

...

}

// copy constructor that is used to initialize the number to ralNum

RationalNum(final RationalNum ralNum)

{

// Please add your code here

...

}

// display the number in the form of numerator/denominator

void display()

{

// Please add your code here

...

}

// addition, returns the sum num1 + num2

static RationalNum add(final RationalNum num1, final RationalNum num2)

{

// Please add your code here

...

}

// subtraction, returns the difference num1 - num2

static RationalNum sub(final RationalNum num1, final RationalNum num2)

{

// Please add your code here

...

}

// multiplication, returns the product num1 * num2

static RationalNum mul(final RationalNum num1, final RationalNum num2)

{

// Please add your code here

...

}

// division, returns the quotient num1/num2

static RationalNum div(final RationalNum num1, final RationalNum num2)

{

// Please add your code here

...

}

/* simplifying the rational number, the numerator and the denominator

in the result should be relatively prime

*/

void simplify()

{

// Please add your code here

...

}

}

Please complete the above class and test it using the following code.

RationalNum a = new RationalNum(2,3);

RationalNum b = new RationalNum(6,4);

RationalNum c = RationalNum.add(a,b);

c.simplify();

a.display();

System.out.print(" + ");

b.display();

System.out.print(" = ");

c.display();

c = RationalNum.sub(a,b);

c.simplify();

a.display();

System.out.print(" - ");

b.display();

System.out.print(" = ");

c.display();

c = RationalNum.mul(a,b);

c.simplify();

a.display();

System.out.print(" * ");

b.display();

System.out.print(" = ");

c.display();

c = RationalNum.div(a,b);

c.simplify();

a.display();

System.out.print(" / ");

b.display();

System.out.print(" = ");

c.display();

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

T Sql Fundamentals

Authors: Itzik Ben Gan

4th Edition

0138102104, 978-0138102104

More Books

Students also viewed these Databases questions