Answered step by step
Verified Expert Solution
Question
1 Approved Answer
python code Fraction Description Declare a class: ons Praction that has 2 properties A den which are integers representing numerator(97) and denominator(99) of a frac
python code
Fraction Description Declare a class: ons Praction that has 2 properties A den which are integers representing numerator(97) and denominator(99) of a frac tion, respectively ID Design a constructor for the Fraction class so that it can be initialized in following ways. Time Men ION Cres Leve Sco Forf - Fraction().f.num is 1,f1.denis 1 (Note their types should be int) . For f2 = Fraction(24), f2.num is 2 f2.den is 4 . For f3 = Fraction(3), f3.num is 3, f3.den is 1. (den is 1 by default) . For f4 = Fraction(f2), f4.num is 2, f4.den is 4. (copy num and den from f4) Overload_str_function so that it can be printed as (Note there are spaces befor e/after) . For i = fraction(3,6).print(f1) should show 1/2 For 12 - Fraction(0,1), print(f2) should show 0/1 . For 13 = Fraction(1,0).print(f3) should show 1/0 For f4 = Fraction().print(14) should show 1/1 You can assume that. The values feed into it. of Fraction are always .0. Thus you don't need to consider negative fractions. Tags Input The input is a string with multiple fields separated by space if the number of fields is 0 => You construct fraction with Fraction() if the number of fields is 1 => You construct fraction with Fraction(field 1) If the number of fields is 2 => You construct fraction with Fraction(field1, field2) if the number of fields is 3 => You construct fraction with Fraction(Fraction(field, > , field2) )) Output The string format of the fraction numer Sample Input 1 Sample Output 1 Sample Input 2 Sample Output 2 Sample Input 31 Sample Output 3 Sample Input 4 Sample Output 4 Language Python Theme Salerad 1 class Fraction: 2 # Finish class definition Dass 3 4 Language: Python3 0 Theme: Solarized Light N 4 1 class Fraction: 2 # Finish class definition 3 pass 4 5 6 if _name __main_": 7 try: 8 fields = [ int(f) for f in input("").split() ] 9 except: 10 fields = [] 11 if len(fields) == 0: 12 f = Fraction 13 elif len(fields) = 1: 14 f = Fraction(fields[0]) 15 elif len(fields) == 2: 16 f = Fraction(fields [0], fields[1]) 17 elif len(fields) == 3: 18 f = Fraction(Fraction(fields [O], fields[1])) 19 print(f) 21 20 Submit Online Judge Footer Powered by Online Judge Version: 20191217-12cbfStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started