CS209 Spring 2024 Michael Eckmann Assignment 1 Assigned: February 10, 2024 Due: February 18, 2024 ================== Purpose: To get familiar with the following topics Writing a class based on a specification and overriding operators and testing the class and writing a program that uses all the class functionality ================== You are NOT allowed to search the internet or use any AI to help you figure out how to do this assignment. You are not allowed to search for or get python code to solve any parts of this assignment. ================== Create 2 python files for this assignment. One named fraction.py that will include a class named Fraction and one named fracmath.py Add comments at the top of each of those files like so: ############################################################# # File name: fraction.py (or fracmath.py) # Contains class(es): Fraction # (exclude the line aboe for fracmath.py since it does not contain any class definitions) # Written by: # Date: # For CS209 Spring 2024 Assignment 1 # at Skidmore College ############################################################# Add any other comments that you feel necessary to describe any particularly hard to understand code. For example, whatever code you write to reduce the fraction into lowest terms might benefit from a comment nearby explaining your thought process of how to do that. In the fraction.py, create a class named Fraction. The instance variables must be private and represent the numerator and denominator of the fraction. The constructor of the Fraction class should take in 2 integers, one that represents the numerator and one that represents the denominator. You are required to store fractions in lowest terms. e.g. if I try to create a fraction with a numerator 18 and a denominator 24, you should figure out that this is the fraction 3/4 and store it in that fashion (with 3 as numerator and 4 as denominator). The instance variables are required to be made private (start their names with __ ). Note well: There should be NO printing nor random number generation inside the Fraction class in the fraction.py file. Random number generation and printing only go in the fracmath.py file. fracmath.py will have code that will generate 4 random integers (2 numerators and 2 denominators). The numerators should be in range 1 .. 5 and the denominators should be in the range 2 .. 15. Your code should create 2 objects of type Fraction with that data. Multiply and divide these fractions (not their floating point representations) and add and subtract these fractions (not their floating point representations). **** Note well: use the gcd (greatest common divisor) function in the math module import math math.gcd(__,__) and use that to get your numerator and denominator in lowest terms (by dividing each by that greatest common divisor) in the constructor of Fraction. Fractions are allowed to be positive or negative. The denominator of a Fraction is never allowed to be 0. If a 0 is passed in as the demoninator in the Fraction constructor, DO NOT assign 0 to the denominator, instead assign 1 or some other valid value to the denominator. 0 is allowed to be a numerator. In Fraction, you should have methods to multiply, divide, add and subtract that do those operations to two Fraction objects. Use these method names to override the operators +, -, *, / __add__ __sub__ __mul__ __truediv__ The four methods above should each return a new Fraction object. Add the following comparison operators to overload >, <, ==, >= and <= respectively: __gt__ __lt__ __eq__ The three methods above should each return a boolean (True or False). All seven of those methods should have self and another fraction object in the parameter list. e.g. # note: self is the Fraction object before the + # rhs is the Fraction object to the right of the + # when this method is called def __add__(self, rhs): ====== Example output of your program in fracmath.py: First random numerator: 4 First random denominator: 7 That is the fraction 4 / 7 Second random numerator: 4 Second random denominator: 6 That is the fraction 2 / 3 (4 / 7) * (2 / 3) = (8 / 21) (4 / 7) / (2 / 3) = (6 / 7) (4 / 7) + (2 / 3) = (26 / 21) (4 / 7) - (2 / 3) = (-2 / 21) (4 / 7) is less than (2 / 3) Note: that last line can be 1 of 3 possible statements which are ____ is less than ____ ____ is greater than ____ ____ is equal to ____ It just so happens that 4/7 is less than 2/3. Notice also that when 4 and 6 were generated for the second fraction the constructor reduced it to lowest terms 2 and 3. =========================== Testing your Fraction class: Create a file named: fractesting.py Put code in it that tests your Fraction class. To test out your Fraction class well, generate Fractions other than the ones that can happen in the randomly generated ones that I ask you to do in fracmath.py Some ideas: Test it out with negative numerators and denominators and also test a variety of fractions that should get reduced to lowest terms and verify them. Also test out passing 0 to the denominator of the Fraction constructor to make sure you do not allow 0 as a denominator. =========================== What to submit: fraction.py fracmath.py fractesting.py