Computer Science Department |
CSCI 135 |
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!
Grade Item | Points Possible | Points 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 |
Page last updated: August 16, 2021