Sale!

# ECE-C301 Homework Assignment: Week 2

\$30.00

ECE-C301 Advanced Programming for Engineers
Homework Assignment: Week 2
Implement a class called Fraction that defines an immutable rational number. Users of your class should
be able to work with fractions using the traditional operators—specifically your class should implement the
following “magic methods”:
• init – Construct a rational number with a given numerator and denominator
• add – Add two Fraction instances
• sub – Subtract two Fraction instances
• eq – Check if two Fraction instances are equal
• lt – Check if one Fraction instance is less than the other
• ne – Check if two Fraction instances are not equal

Category:

## Description

5/5 - (2 votes)

ECE-C301 Advanced Programming for Engineers
Homework Assignment: Week 2
Implement a class called Fraction that defines an immutable rational number. Users of your class should
be able to work with fractions using the traditional operators—specifically your class should implement the
following “magic methods”:
• init – Construct a rational number with a given numerator and denominator
• add – Add two Fraction instances
• sub – Subtract two Fraction instances
• eq – Check if two Fraction instances are equal
• lt – Check if one Fraction instance is less than the other
• ne – Check if two Fraction instances are not equal
• le – Check if one Fraction instance is less than or equal to the other
• gt – Check if one Fraction instance is greater than the other
• ge – Check if one Fraction instance is greater than or equal to the other
• float – Gets a floating point representation of a Fraction instance, called by float()
• repr – Gets a string representation of the Fraction instance, called by str()
– Save your Fraction class implementation in a file called fraction.py.
Write a small test program that imports fraction.py as follows:
1 from fraction import Fraction
and use your Fraction class to clearly demonstrate that all of your magic methods work properly.
– Save your test program in a file called test.py
– Save the output of your test program in a file called output.txt
1