Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1: The Fraction Class (Fraction.java and Fraction_Test.java) Description In mathematics, a simple fraction consists of an integer numerator displayed above a line (or before

image text in transcribed
image text in transcribed
Problem 1: The Fraction Class (Fraction.java and Fraction_Test.java) Description In mathematics, a simple fraction consists of an integer numerator displayed above a line (or before a slash), and a non-zero integer 13 denominator, displayed below for after) that line. For example, and 17 are all fractions. 5 The following UML indicates the definition of a Fraction class, in which: Fraction - numerator int - denominator: int +Fraction() +Fraction num:int, demoint) + set(num:int, democint): void + add(other:Fraction): Fraction + subtract(other:Fraction): Fraction + multiple other:Fraction):Fraction + divideother:Fraction): Fraction +toString()String + toDouble():double indicates the data field or method to be private + indicates the data field or method to be public numerator and denominator are the two instance data fields of the class. The non-argument constructor will call the argument constructor, and set both the numerator and denominator of a fraction object to be 1. The argument constructor will set the numerator and denominator of a fraction object to be specific values. The set() method will set the numerator and denominator of a faction to be specific values. The add() method will add up two fractions, and the result is also a fraction, eg 1/3+1/2 = 5/6 The substract() method will subtract the other fraction from this fraction, and the result is also a fraction.eg 1/2-1/3=1/6 The multiply() method will multiple the this fraction by the other fraction, and the result is also a fraction, eg 1/2* 1/3=1/6 The divide() method will divide the this fraction by the other fraction, and the result is also a fraction, ep 1/2/1/3 = 3/2 The toString() method will convert the numerator and denominator to their respective string format, and concatenate them by a slash eg will be represented as "1/2". Note: We often implement the toString( method in each class. The benefit of having such a method is that, when we pass a fraction object to the System.out.println() method, the toString() method will be automatically called to print this fraction. . For example: Fraction f1 = new Fraction(3, 4); System.out.println(f1); // this will print 3/4 onto screen. The toDouble() method will convert the fraction to its corresponding floating-point number, e.g, 1/2 is 0.5 Please also make sure that the numerator and denominator of a valid fraction has no common divisor that is greater than 1. For example, 4/6 is not a valid fraction number, it should be 2/3 instead. To do this, you need to first find out the greatest common divisor of the numerator and denominator, and divide both the numerator and denominator by it. For example, the greatest common divisor of 4 and 6 is 2, and therefore, we need to divide both 4 and 6 by 2, resulting in the valid fraction be 2/3. You can particularly introduce a method named getGCD within the class definition to do this. Once you have done defining the Fraction class in Fraction.java source file, please write the client program in another java file Fraction_test.java, and test the methods of the fraction class. Outputs: $java Fraction_test Please enter the numerator and denominator of the first Fraction: 34 The first fraction is: 3/4 Its double value is: 8.75 Please enter the numerator and denominator of the second Fraction: 56 The second fraction is: 5/6 Its double value is: 8.8333 3/4 + 5/6 = 19/12 3/4 - 5/6 = -1/12 3/4 * 5/6 = 5/8 3/4/5/6 = 9/10

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

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

Problem 12.06 Part A Answered: 1 week ago

Answered: 1 week ago