Montana Tech of The University of Montana
Computer Science Department

CSCI 135
Fundamentals of Computer Science I
Fall 2020



LAB 7 - Exceptions and Debugging

In this assignment, you will be you will practice writing a class with bullet-proof methods. You will implement a fraction class based on the fraction functions we did in class several weeks ago. You will need to write the code for the Fraction class, based on the API below. You are welcome to re-use code from class to complete the methods, but you will want to make them as error-proof as possible. The functions we wrote in class are not necessarily bullet-proof.

Fractions. In past lectures, we talked about building your own data types by using classes. Fractions are a great example of why you would want to do this. Once you have defined a data type (a class) called Fraction and implemented its methods, you can create variables of that type and do fraction math on them. For this assignment, you are to write the Fraction class as defined by the API below and then make the methods as error-proof as possible. You should use logic to catch those errors that can be caught, and exceptions for errors that cannot be detected easily by code.

Below is a list of the methods that should be in your Fraction class. This list is the API, or Application Programming Interface. The first column shows the return type of the method, the second, the name of the method and parameters passed in - the signature - and the third column is a brief description of what the method should do.

For this assignment, assume that fractions like x/0 are acceptable and ensure that your code deals with them properly. That is, it is legal to create such fractions and allow some methods to work on them, but make sure that no methods raise an exception.

It is VERY important that the methods are implemented exactly as stated in the API. If we run test code that, for example, expects to call your multiply method, and you have named it Multiply, have changed the number or type of parameters, or the return type, the code will fail.

class Fraction
-----------------------------------------------------------------------------------------
         __init__(int n, int d)  # create a fraction with numerator n and denominator d 
string   toString()              # return a printable (string) version of the fraction
boolean  equals(Fraction f)      # is one fraction equal to another?
Fraction reduce()                # reduce the fraction to its lowest terms
Fraction reciprocal()            # return the reciprocal of a fraction
Fraction multiply(Fraction f)    # multiply two fractions
Fraction divide(Fraction f)      # divide the first fraction by the second
Fraction add(Fraction f)         # add two fractions
Fraction subtract(Fraction f)    # subtract the second fraction from the first

You can write a FractionClient.py program to create your Fractions and try different (both good and bad) input on them to see whether your Fraction methods are able to catch all the errors. We, of course, will be running our own client test program to make sure that your Fraction methods work correctly. You do not need to turn in your FractionClient program (but you can if you like - it won't be graded).

Be sure to check for troublesome fraction values in your tests to make sure that your methods work as expected.

Good luck -- and have fun!


What kind of errors should I be checking for? All of them? Try to think about any conditions that would cause the code to fail.
  • Doing division? Maybe check that nothing gets divided by 0.
  • Constructing a fraction? Maybe check that the data types sent in are correct.
  • Doing math with a second fraction? Check that the fraction being sent in is valid.
  • Doing math at all? Check that the results are correct for valid input.
  • And any other errors that you can think of that might occur...



  • Grading
    Grade ItemPoints PossiblePoints Earned
    Header Comment
    3
    Constructor
    3
    toString Method
    3
    equals Method
    3
    reduce Method
    3
    reciprocal Method
    3
    multiply Method
    3
    divide Method
    3
    add Method
    3
    subtract Method
    3
    Total
    30

    Submission. Submit your code, Fraction.py, using Moodle. Be sure your submitted code has the required header with your name and a description of the program.

    Page last updated: August 16, 2021