1.Use CRC Cards to identify the classes, responsibilities and collaborators in your design for the Fraction calculator.

You may use any physical manifestation of CRC cards that you like, e.g. 3x5 index cards, scraps of paper, spreadsheets, or text documents.

To help you to get started, think about what's involved? Start with a calculator, identify the responsibilities and collaborators, and then work your way through the solution until you feel you have a complete solution. My solution has two classes but your solution may be different. Be sure to identify the class, the responsibilities of that class, and the collaborators.

If you choose to use 3x5 index cards (or just scraps of paper), take photos of your cards and submit those to Canvas. If you use an electronic solution, then upload the document with the classes, responsibilities and collaborators. I prefer index cards because they are easy to move around, stack, and rip up when a better idea comes along.

2.Write a Python program that includes:

class Fraction with the following methods:

  • __init__(self, numerator, denominator)
    • This allows f1 = Fraction(1, 2) to represent the fraction 1/2
    • Raise a ValueError exception if the denominator == 0
  • plus(self, other) # return a new instance of class Fraction with the sum of self and other where other is another Fraction
  • minus(self, other) # return a new instance of class Fraction with the difference of self and other where other is another Fraction
  • times(self, other) # return a new instance of class Fraction with the product of self and other where other is another Fraction
  • divide(self, other) # return a new instance of class Fraction with the quotient of self and other where other is another Fraction
  • equal(self, other) # return True/False if the two fractions are equal - Hint: compare the product of the numerator of self and the denominator of other to the product of the numerator of other and the denominator of self. E.g. given 2/3 and 4/6, the two fractions equal if (2 * 6) == (3 * 4). This will be very helpful for your test cases.
  • __str__(self) # return a string to represent the Fraction

a test_suite() function that demonstrates that your Fraction class works properly. For now, just use print or assertstatements to demonstrate that your code works properly. Next week we'll explore some better techniques for automating test cases.

  • Each test case should include both the computed result and a string with the expected result so the user can easily determine if the calculated value is correct by comparing the computed and expected value. (See below for an example.
  • Your test suite should include at least one example of adding three fractions together in a single operation, e.g. 1/2 + 3/4 + 4/4 == 72/32
  • Your test suite should include examples of all of the operations.

main() function that asks the user for the first numerator and denominator, the operator, and the second numerator and denominator, then prints the result of applying the operator to the two fractions.

Be sure that your program handles error cases where the user enters invalid input for the fractions or operator.

Here's the sample output from my solution:

Welcome to the fraction calculator!
Fraction 1 numerator: 1 <-- user entered 1
Fraction 1 denominator: 2 <-- user entered 1
Operation (+, -, *, /): + <-- user entered +
Fraction 2 numerator: 3 <-- user entered 1
Fraction 1 denominator: 4 <-- user entered 1
1/2 + 3/4 = 10/8 <-- final result - The code converted 1/2 to 4/8 and 3/4 to 6/8 and returns 10/8

Here's an example of the code for one of my test cases:

f12 = Fraction(1, 2)
f44 = Fraction(4, 4)

print(f12, '+', f12, '=', f12.plus(f12), '[4/4]') # print the oo
print(f44, '-', f12, '=', f44.minus(f12), '[4/8]')

Here's the output from the test cases.

1/2 + 1/2 = 4/4 [4/4]
4/4 - 1/2 = 4/8 [4/8]

The print statement in each test case includes the expected result so the reader can easily determine if the code is working properly by comparing the computed result against the expected result.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.