QuestionQuestion

Program #1: Fraction.py

When completed, the Fraction class should have:

1) The attributes num (numerator) and den (denominator).

2) A constructor with default values for num = 0 and den = 1. The constructor should not allow zero in the den.

3) Another constructor that will accept a string argument in the form 'num/den' with no spaces between the numbers and the slash.
• If the string is in the correct format, the constructor should parse the string and store the numerator and denominator.
• If the string is not in the correct format, the constructor should print an error message and assign the default values num = 0 and den = 1 to the Fraction.

4) The arithmetic operator functions for
add +
sub -
mul *
truediv /

5) The relational operator functions for
lt <
le <=
gt >
ge >=
eq ==
ne !=

Note: for the operators that involve "equal to", the function should compare the reduced versions of the fractions (without changing the the Fraction objects).

6) A function called reduce that will reduce the fraction to lowest terms.

7) The str function to make it convenient to print Fraction objects

The format of a Fraction is num/den. There should not be spaces between the numbers and the slash.

Program #2: MixedNumber.py
A mixed number is a number consisting of a whole number and a proper fraction. In object-oriented programming terms "A mixed number is composed of an int and a Fraction." This class uses composition, not inheritance.
When completed, the MixedNumber should have:
1) Two attributes: wn (whole number, type int) and pf (proper fraction, type Fraction).
2) A constructor that accepts two arguments, an integer and a Fraction. The constructor should make sure that the MixedNumber is stored in the proper form. For example, if the arguments are 3 and Fraction(9/4), the MixedNumber should be stored with wn = 5 and pf = Fraction(1/4).

3) A constructor that accepts a string in the form 'wn num/den' with one space between the whole number and the numerator.

• If the string is in the correct format, the constructor should parse the string and store the wn and pf.

• If the string is not in the correct format, the constructor should print an error message and assign the default values wn = 0 and pf = Fraction(0/1).
4) The arithmetic operator functions for
add +
sub -
mul *
truediv /

5) The relational operator functions for
lt <
le <=
gt >
ge >=
eq ==
ne !=

Note: for the operators that involve "equal to", the function should compare the reduced versions of the fractions (without changing the the Fraction objects).

6) A function called reduce that will reduce the proper fraction to lowest terms. (Hint: take advantage of the code you already wrote in the Fraction class.)

7) The str function to make it convenient to print MixedNumber objects.

The format of a MixedNumber is wn num/den. There should be one space between the whole number and the numerator but should not be spaces between the num or den and the slash.

Program #3: FractionTest.py

Write program that will test the functions you wrote for the Fraction class.

Use inline comments to document each test and include meaningful text in all lines of output so that I can see what you were testing.

Program #4: MixedNumberTest.py

Write a program that will test the functions you wrote for the MixedNumber class.

Use inline comments to document each test and include meaningful text in all lines of output so that I can see what you were testing.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import math

# Fraction class
class Fraction:
   
    # Constructor
    def __init__(self, arg1 = 0, arg2 = 1):
       if type(arg1) == int:
            # initialize the instance data
            self.num = arg1
            if (arg2 == 0):
                print('denominator cannot be zero')
                print('we are making it 1 instead')
                arg2 = 1
            self.den = arg2
       else:
            try:
                params = arg1.split("/")
                self.num = int(params[0])
                self.den = int(params[1])
            except:
                print('invalid input format')
                print('assigning fraction 0/1')
                self.num = 0
                self.den = 1
      
    # Add method
    def __add__ (self, fraction2):
       answer = Fraction(0,1)
       answer.num = ( self.num * fraction2.den ) + ( fraction2.num * self.den )
       answer.den = self.den * fraction2.den
       return answer.reduce()

    # Subtract method
    def __sub__ (self, fraction2):
       answer = Fraction(0,1)
       answer.num = ( self.num * fraction2.den ) - ( fraction2.num * self.den )
       answer.den = self.den * fraction2.den
       return answer.reduce()

    # Multiply method
    def __mul__ (self, fraction2):
       answer = Fraction(0,1)
       answer.num = self.num * fraction2.num
       answer.den = self.den * fraction2.den
       return answer.reduce()

    # Divide method
    def __truediv__ (self, fraction2):
       answer = Fraction(0,1)
       answer.num = self.num * fraction2.den
       answer.den = self.den * fraction2.num
       return answer.reduce()
   
    # Less than method
    def __lt__ (self, fraction2):
       return self.num * fraction2.den < self.den * fraction2.num

    # Less than or equal to method
    def __le__ (self, fraction2):
       return self.num * fraction2.den <= self.den * fraction2.num...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

50% discount

Hours
Minutes
Seconds
$60.00 $30.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats