CMPT 200 Coding Homework

Write a class called Fraction that can store a rational number (reminder: those numbers that can be expressed in the form a/b, where a and b are integers are rational numbers). For example, a variable with a value of ½ would be created using oneHalf = Fraction(1,2) Your class should be able to: • Get the numerator - oneHalf.num() • Get the denominator - oneHalf.denom() • Print the value of the fraction. - print(oneHalf) would print: 1/2 • Be able to: add, subtract, multiply, divide fractions (return a Fraction object with the answer). This should be done by overloading the appropriate operators rather than writing regular methods for these. • Be able to compare fractions to see if they are ==, !=, <=, >=, <, > You should write a comprehensive test program to check the functioning of your class. For full marks, your Fraction class should ensure that your fractions are represented using a canonical (unique) form. In particular, your fractions should be in reduced form. Other questions to consider are 1) how is 0 represented and 2) how is a negative number represented. Handling these questions about fraction representation can be considered part of robust programming. Be sure to follow the specification given above as we will be testing your class using our own test program. You only need to submit your class definition. Greatest common divisor: To represent a fraction in reduced form, you need to calculate the greatest common divisor of the numerator and denominator. This can be done using the Euclidean algorithm. Assume that we want gcd(a,b), where a>=b>0. Then, using integer arithmetic, we can calculate the gcd using the following algorithm: m = a n = b r = m % n while (r != 0): m = n n = r r = m % n return n The value (n) returned by this method is gcd(a,b). For full marks, you need to write your own version of the gcd() function. (gcd() is available as a function in the math module, but we want you to write your own.)

Get Help With a similar task to - CMPT 200 Coding Homework

Login to view and/or buy answers.. or post an answer

Related Questions

Similar orders to CMPT 200 Coding Homework
11
Views
0
Answers
Data Structures and Algorithm
needs to follow rubric...
26
Views
0
Answers
Python Project containing 4 tasks
2 Python files. Everything is in the pdf in detail...
19
Views
0
Answers
Beginning C++
The requirement is attached as "Problem Description." The work I have completed so far but needs to be put in functions is "Function" pdf....
14
Views
0
Answers
Student Class in Record Manage Program C# assignment
Follow all instructions in the file linked. All I ask is you include everything that is being asked and if you have any questions ask them....