As usual, create a directory to hold today's activities:
$ mkdir ~/cs170/labs/lab4 $ cd ~/cs170/labs/lab4
Lab was a
on Wednesday, but hopefully you saw the basics of how to take something we had already written, and convert it into a class structure. This is not how we typically go about writing our classes. Instead, we would probably design and write our classes from the beginning, and just use them in our code. Today, you are going to write a class from scratch, using something that is hopefully fairly familiar to you.Today is the day that you create your first legitimate data type, and this time it is from scratch! One of the major complaints about the floating point data type is that it cannot represent every rational number. So, let's make a data type that can do that.
Create a class called Fraction
in a file called
fractions.py. Your class should be able to represent
arbitrary fractions. You should be able to add, subtract, multiply,
and divide fractions. You should also be able to print fractions,
so you can see the result of your work. Your code should only work
for positive fractions.
Think about what attributes you need to represent a fraction. This should be decently straight forward, but if you need a refresher on fractions take a look at this web page.
>>> fraction_1 = Fraction(1, 2) >>> fraction_2 = Fraction(3, 4) >>> result = fraction_1.add(fraction_2) >>> print(fraction_1) 1/2 >>> print(fraction_2) 3/4 >>> print(result) 5/4 >>> result = fraction_2.multiply(fraction_1) >>> print(result) 3/8
Note: Your methods for this class should return a new fraction. It should not modify any fraction in place.
You are definitely going to need two attributes, one for the
numerator
, and another for the
denominator
.
Your operators should behave just like the operators do for other data types: They should not modify either of the current fractions. Instead, they should return a new fraction, which is the result of the operation.
__str__
method,
so you can just use the print
function to print out
your fractions.
Multiplication is easy, just multiply the numerators together, and the denominators together.
Your add function is going to have to compute a common denominator. The easiest way to accomplish this is by multiplying the two denominators together.
This means you need to multiply the numerator of the current fraction by the other fractions denominator (and vice versa) to make sure the value of each fraction does not change. Then you just add the new numerators together.
If you have written your methods correctly, you should be able
to override the built-in operators by simply changing the name
of the methods to __add__
and __mul__
:
>>> result = fraction_1 + fraction_2 >>> print(result) 5/4 >>> result = fraction_1 * fraction_2 >>> print(result) 3/8
The way the fraction class is currently set up, you can easily get a
fraction 2/4
. This probably makes the math majors in
the
room , but is technically a valid fraction. It just is
not in its most reduced form.
Let's define the reduced form of a fraction as follows:
Write a function that converts the current fraction to its most reduced form. You can then fix all of your operators with one call to this new method.
When you have finished, create a tar file of your lab4
directory. To create a tar file, execute the following commands:
cd ~/cs170/labs tar czvf lab4.tgz lab4/
To submit your activity, go to inquire.roanoke.edu. You should
see an available assignment called Lab Assignment 4
.
Make sure you include a header listing the authors of the file.