Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need assistance writing the following java code. Consider a class Fraction of fractions. The Fraction class should extend java.lang.Number class and implement java.lang.Comparable interface.

I need assistance writing the following java code.

Consider a class Fraction of fractions. The Fraction class should extend java.lang.Number class and implement java.lang.Comparable interface.

Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction.

The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two fractions are equal (override Object class method), and convert a fraction to a string (override toString() method from Object class).

Things to note: The class should be able to handle denominators that are zero, denominators and/ or numerators that are negative (especially during arithmetic operations) (It will be easier to determine the correct sign of a fraction if you force the fraction's denominator to be positive.), the fraction should always occur in lowest terms (if the user tries to create a fraction such as 4/8, the class should set the fraction to 1/2), the results of all arithmetic operations should be in lowest terms, a fraction can be improper (should still be in lowest terms).

At this stage, design, but do not implement the class Fraction. Begin by listing all the needed operations. Then write a Java interface, named FractionInterface, that declares the following public methods: setFraction, getNumerator, getDenominator, add, subtract, multiply, divide, and getReciprocal.

Write a Java class Fraction that implements the FractionInterface you designed. Begin with reasonable constructors. Design and implement useful private methods.

There should also be a method that makes sure that the input values given by the user are compatible if not, it will throw an exception.

Other methods that should be implemented: set Numerator, set Denominator, gcd (determines the greatest common denominator of the numerator and denominator), lowestTerm (puts the fraction in its lowest terms ie 4/8 becomes , this method should be called in almost all other methods to ensure that the returned fraction is in its lowest terms), getSign (obtains the fraction's sign), setSign (sets the numerator's sign to the fraction's sign, and sets the denominator's sign to positive).

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

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions

Question

What is Aufbau's rule explain with example?

Answered: 1 week ago

Question

Developing and delivering learning that is integrated with the job.

Answered: 1 week ago

Question

Use of assessments to determine trainees learning styles.

Answered: 1 week ago

Question

7. Discuss the advantages of embedded learning.

Answered: 1 week ago