Question: intro to java ( graphics and interface chapter) Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator
intro to java ( graphics and interface chapter)
Consider a class Fraction of fractions. 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, and convert a fraction to a string. Your class should handle denominators that are zero. Fractions should always occur in lowest terms, and the class should be responsible for this requirement. For example, if the user tries to create a fraction such as 4/8, the class should set the fraction to 1/2. Likewise, the results of all arithmetic operations should be in lowest terms. Note that a fraction can be improper-that is, have a numerator that is larger than its denominator. Such a fraction, however, should be in lowest terms. Design the class Fraction. Begin by writing a CRC card for this class. Then write a Java interface that declares each public method To reduce a fraction such as 4/8 to lowest terms, you need to divide both the numerator and the denominator by their greatest common denominator. The greatest common denominator of 4 and 8 is 4, so when you divide the numerator and denominator of 4/8 by 4, you get the fraction 1/2. The following recursive algorithm finds the greatest common denominator of two positive integers: Algorithm gcd(integerOne, integerTwo) if (integerOne % integer,Two-:0) result-integerTwo else results gcd(integerTwo, integerOne % integerTwo) return result It will be easier to determine the correct sign of a fraction if you force the fraction's denominator to be positive. However, your implementation must handle negative denominators that the client might provide Write a program that adequately demonstrates your class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
