Question: The Float and Double classes (and the respective primitive types) offer a good representation of real values suitable for many numeric problems. However, the finite

The Float and Double classes (and the respective primitive types) offer a good representation of real values suitable for many numeric problems. However, the finite precision of those types can sometimes lead to surprising results (try to square 0.1 as float or double :( )1 . In some cases, such problems can be remedied by using a Fraction class. Implement a class Fraction that models fractions, where numerator and denominator are represented as int (or Integer) and offers arithmetic operations. A Fraction object can be created with a constructor that takes a numerator and a denominator as arguments. The Fraction class offers the public methods defined below. Note, the results of arithmetic operations should be simplified. e.g., multiplying two fractions 4/3 and 3/5 should return 4/5 (not 12/15 )^2 .

The Float and Double classes (and the respective primitive types) offer a

good representation of real values suitable for many numeric problems. However, the

Choose appropriate types for the methods arguments and return values.

Add instance variables to represent numerator and denominator.

Implement the methods.

Implement a tester class that exercises the methods. Do not only test behavior that is well defined. e.g., what does happen, if you create a fraction 1/0?

k x Fraction is a class modeling fractions where numerator * and denominator are represented as integral number public class Fraction kx * Constructs a new Fraction object. * Oparam init initial value public Fraction(/* SOMETYPEnum, / SOMETYPE / denom) { /* YOUR CODE ) k* * Computes " this+that" and returns the result in a new object * Oparam that the left-hand side operand * Oreturn a new object representing this+that public /* SOMETYPE */ add(/* SOMETYPE that) { /x YOUR CODE*/ \ k* * Computes " this-that" and returns the result in a new object * Oparam that the left-hand side operand * Oreturn a new object representing this-that public /* SOMETYPE */ sub(/* SOM ETYPE */ that) { /* YOUR CODE */ } k* * Computes "this*that" and returns the result in a new object * Oparam that the left-hand side operand A recent article on issues with floating point arithmetic is available here: https ://cacm.acm.org/magazines/ 2017/8/219594-small-data-computing/fulltext https://en.wikipedia.org/wiki/Greatest_common_divisor

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!