The focus of this lab/project is on working with classes, in the roles of implementer and client. In the first part, you will complete the class for rational numbers, which we began in class. In the second part, you will use that class to build a simple interactive calculator.
We wrote multiplication in class; you can copy the file
to get what we’ve already done. You need to complete the missing arithmetic operations. Note that some operations might need to raise an exception.
You can make the denominator parameter of the constructor optional by providing a default value. Do so, such that
Add a new function str to Rational() to your module that takes a single string as an argument, parses it, and returns a new instance of Rational with the correct value. If the (entire) string is not a valid rational number, you should raise the exception ValueError.
Note that this is a function instead of a method. Your function should be able to convert any string that would be returned by the __str()__ method.
Optional: Allow the string to include a decimal fraction.
Fix the method for converting a Rational as a string so that exact integers are shown without the slash and denominator. Be sure that the function to convert strings can do the reverse conversion.
Be sure that all of your functions and methods have an appropriate docstring. You should also provide a docstring for the class in the same fashion, triple-quoted, immediately after the class line.
It would probably be a good idea to include a function that does some testing of your class. You may also want to include some assertions, but they should be triggered only by errors within your class code.
Update the comment at the beginning of the file so that it includes your name along with acknowledgement that you started from the code from class.
Collect your thoughts about writing this class in a file rational.txt. Also include there a paragraph or two that describes how you know that your class works correctly.
You will now use the class you have written to make a simple class that provides a rational-number calculator using postfix notation. Copy the files from the directory
for a starting point. The file calculator.py contains a start on the class Calculator, which you need to complete. The file calc.py is the user interface, which reads a single line at a time and calls appropriate Calculator methods. You can think of the methods as corresponding to buttons on a calcultor.
Expressions can be written in several different forms; what we usually use in known as infix notation because the operators are placed in between the operands. It is easier to evaluate expressions written in postfix notation, where an operator comes after its operands. The postfix expression
says to subtract 3 from 2, then multiply the result by 5.
The rule for evaluating postfix expressions is very simple if you have a stack. A stack has two operations: push puts an item on top of the stack, and pop removes and returns the value that is on the top of the stack. The calculator enters a value by pushing it onto the stack, each operator works by popping the two operands from the stack, computing the result, and pushing that result onto the stack.
The good news is that Python’s list type is easy to use as a stack. So your calculator will need to have a list for that purpose. Addendum: There is a pop() method; you’ll have to figure out what to use for push.
Addendum: The methods in your Calculator class should handle most exceptions. You don’t need to catch divide-by-zero. Note that it is an error to try to do an operation (such as plus()) when there are fewer than two values on the stack. If your calculator is called in such a case, you should raise IndexError (an already-defined exception).
Polish your portion of the program in calculator.py. Write your reflections in calculator.txt.
To turn in your files, use the command
Turn in rational.py and rational.txt before noon on Tuesday, March 2.
Turn in calculator.py and calculator.txt (along with the previous files, again) before lab meets on Thursday, March 4.