Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

mplement a class called Fraction. The class's purpose is to store a representation of a fraction. Fraction's numerator and denominator values stored in class fields

mplement a class called Fraction. The class's purpose is to store a representation of a

fraction. Fraction's numerator and denominator

values stored in class fields as integers.

If the fraction is negative, the negative sign is recorded as part of numerator.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
Fraction Class Implement a class called Fraction . The class's purpose is to store a representation of a fraction . Fraction's numerator and denominator values stored in class fields as integers . If the fraction is negative , the negative sign is recorded as part of numerator . The fraction is always stored in its simplest form . The class should have the following methods " Constructor I that accepts two integer numbers , numerator and denominator . Denominator must not be a 0 . If a O passed as a denominator , all IllegalArgument. Except ion must be thrown by the constructor . Constructor must call simplify !'; method to simplify fraction after* setting the fields .` Constructor 2 accepts three integer numbers to represent mixed fraction : first* number is the whole part the other two are numerator and denominator . Internally* the fraction is still stored as numerator and denominator only . ` Denominator must not be a O . If a O passed as a denominator , all II legal Argument E X Ception must be thrown by the constructor . Example :` Fraction I1 = now Fraction ( 25 , 3, 51: / /' mixed Fraction 25 and 3/ 5) O Constructor must call simplify , method to simplify fraction after setting* the fields .` N N In change this LICENSE header , Choose LICENSE Headers in Project PROPERTIES . In change this TEMPLATE FILE , CHOOSE TOOLS | TEMPLATES* * and open the template in the Editor . Package god !` INFOIt Java . Util . Scanner ?` 10 1 1 12\\ * Cauthor INTECH 13\\ 1 4\\ public class GOD ! 15 16\\ 17\\ * @param args the command Line arguments* IE 19\\ public static void main ( String [ ] args ) I 20 Ifadding two Fractions 1 /' 42 + 1/30 =\\ 21\\ int num one = 1 ; $ numerator OF the First Fraction 1 1 /' 421 22\\ int denom one = 12 : " " denominator OF the First Fraction 1 1 / 421 int num two = 1 ; $ { numerator OF the second Fraction 1 1 /' 301\\ int denom two = 30 ; " " denominator OF the second Fraction ( 1 /' 301 int num sum ;*$ " numerator Of the sum to be calculated 25\\ int denom sum ;*$" denominator Of the sum to be calculated If Finding the greatest common divider OF the denominators OF the two Fractions If in the case Of our Example the GOD OF 42 and 30 is { 30 int common = denom one = denom_ two ? god ( denom_ one , denom_ two ) : god ! denom_ two , denom_ one ) ? int mult one = denom two* common ;* * Finding the multiple For the First Fraction , it is 5 int mult two = denom one* common ;* " Finding the multiple For the second Fraction , it is 34| If the two Fractions are being added now : 1 /' 42 + 1/'30 = 12/' 210 35\\ num sum = num_ ONE* mult_ ONE + num_ two*mult TWO !` 36\\ denom sum = denom one* mult ONE ;* 37 System . out . printf ( " Edied + 3died = =diedin" , num_ one , denom ONE* 30 num two , denom two , 39 num sum , denom sum )* 40 If Simplifying the Fraction 12/ 210 . 41 If Finding GOD OF theE numerator and denominator 42 common = god ( denom_ sum , num_ sum ) ;\\ denom sum = denom sumi' common ;* 4 4\\ num sum = num sum ,' common ;\\ 45\\ System . Out . printf ! " After Simplification ; &died + Ed'sd = =diedin" , num_ one , denom_ one , num two , denom two . num sum , denom gumj {Nonargument constructor. The constructor must set numerator to C! and denominator to 1. {Top},r constructor . Sample CUP}? constructor call: o Fraction a = new Fraction {1, 3]: 1"! regular If constructor call Fraction d = new Fraction {a}: I! copy If constructor call Accessor and mutntor methods for numerator and denominator elds. G D 1|When mutator method used to change the denominator of the Fraction object, and a t] is passes as an argument, the method must throw an I 1 legalArgmnentEJ-tception. Mutators must call simplif] method to simplify fraction after change. Addition method Fract i on acid [Fraction obj ] . When one Fraction objects are added together, the result is 3 Fraction object that is the sum of two fractions. Do not forget that in order to add two fractions together you rst need to convert them to have common denominator. In order to simplify d1e coding process for you, the sample code that adds fractions is provided. See it in GCDJI'FI. [would recommend creating a private method that nds and returns greatest common divider {GDC}. Please see sample code that calis add[) method below: Fraction a = new Fraction {1, 3}: Fraction 1:: = new Fraction (1, 12]: Fraction c = mastitis]: H c = a+b c simplif) method must he called to simplifyr fraction after changing the elds. to DEnom sum = denom sum ,' common " num sum = num sum ,' common ;* SYSTEM . Out . Print E !' " HELET Simplification ; }died + 3died = =diedin" , num one , denom ONE . num two , denom THO . num sum , denom gum )* * This method implements Euclid's algorithm OF calculation OF THE GREATEST Common divider KELLY OF THE INLEVELS . The algorithm has reCurSIVE nature : GOD OF & and y with { } Y IS THE SAME AS GOD OF Y and 1{ } } ! |{ = KY + KEY ! 5 5\\ CFaram* First integer* 59\\ CFaram Y second integer* * Breturn greatest common divider* public static int gedlint &, int } } int FILE " LINE = V` V = KEY " while IVS` i ' LETUIN*&^ 75Subtraction method . Fraction subtract ( Fraction obj] . When two Fraction objects are subtracted from each other , the result is a Fraction object that is the difference of two fractions . Logic here is the same as in fraction addition . Please lise greatest common divider . ` simplify method must be called to simplify fraction after changing the fields . Private method simplify simplifies the fraction as much as possible . This can be done by finding greatest common divider for numerator and denominator . A sample of this operation can be found in GCD jara . L'se simplify method after you do any arithmetic operations on fraction . Fraction should always be stored in it's simplest form . Example : 32 / 68 = 8/17 Multiplication . Fraction multiply ( Fraction obj ; Multiplies two fractions then simplifies the result .simplify method must be called to simplify fraction after changing the fields . " Division . Fraction divide ( Fraction Obj ; Divides two fractions then simplifies the result ." O simplify method must be called to simplify fraction after changing the* fields . String tostring ( ) method that converts the fraction into a string . For example* 3/ }}*` " If the fraction is more than I ( has a whole part ) , like 25/ 24 , in the output it must be converted into a mixed Fraction : " I' and 1/ 24'` Less. boolean less ( Fraction obj] . Method returns true if the calling* fraction object is smaller than the parameter object . Do not forget to convert both fractions so they have a common denominator before you compare them . Fraction E = NEW Fraction 1 1 , 31 : Fraction b = NEW Fraction ( 1 , 121 :` boolean LES = &. LESSIbj; If" res is holding False because 1/ 3 is NOT less than 1 / 12 More boolean more ( Fraction obj; . Similar to Less method Equals boolean Equals ( Fraction Obj) . Again , here you need to get the fractions to the common denominator in order to compare them . Name your project Fraction . java Test all the methods you wrote in main . Make sure to have the code that catches I I legal Argument Exception and handles it in main! ) . Place the mainly in separate file named Fraction Demo java

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

Define marketing and discuss its core concepts.

Answered: 1 week ago

Question

The central nervous system?

Answered: 1 week ago

Question

How many neurones does the human brain contain?

Answered: 1 week ago

Question

Brain part of logical and dicision making......?

Answered: 1 week ago

Question

The supporting and nurturing cells found in brains are........?

Answered: 1 week ago