CSCI 136
Fundamentals of Computer Science II
Spring 2018

Montana Tech of The University of Montana
Computer Science & Software Engineering



ASSIGNMENT #12 - Fraction Math - C++ Classes and Operator Overloading

In this assignment, you will be you will be practicing using C++ classes by building a fraction class and defining the functions (using overloaded operators) that you might want to use on fractions.

Fractions. Last semester, in CSCI 135, we looked at how Java classes could be used to define a Fraction class. The slides for that class can be found here, if you need a refresher. For this assignment, you are to recreate that Fraction class in C++, and take advantage of the capability to overload operators that you couldn't do in Java. Name this program Fraction.cpp.

Your program should implement the following API (yes, API means Application Programming Interface, and it's not just a Java thing):

class Fraction
-----------------------------------------------------------------------------------------
         Fraction(int x, int y)  // create a fraction with numerator x and denominator y at maze coordinates x, y 
         Fraction()              // create a fraction with numerator 1 and denominator 1
Fraction operator=(Fraction f)   // assign the value of one fraction to another
bool     operator==(Fraction f)  // check to see if two fractions are equal
void     print()                 // print the fraction to the screen in the form "n/d"
Fraction reduce()                // reduce the fraction to its lowest terms
Fraction reciprocal()            // return the reciprocal of a fraction
Fraction operator+(Fraction f)   // add two fractions
Fraction operator-(Fraction f)   // subtract the second fraction from the first
Fraction operator*(Fraction f)   // multiply two fractions
Fraction operator/(Fraction f)   // divide the first fraction by the second

You should also implement a main method in your program that creates fractions and tests the various functions. We, of course, will be doing the same to test that your Fraction class works correctly. Be sure to look through the grading criteria below to make sure you've completed everthing.

Good luck -- and have fun!


Grading
Grade ItemPoints PossiblePoints Earned
Program Compiles
1
Program Runs
1
javadoc comments on all classes and methods
2
Two constructor methods
2
Assignment operation
3
Test for equality
3
Print to screen
2
Reduce to lowest terms
3
Reciprocal
1
Math operators (all 4)
12
Total
30

Submission. Submit your code, Fraction.cpp, using Moodle. Be sure your submitted code has the required header with your name and a description of the program. Remember! All your comments must be in javadoc format!

Page last updated: December 27, 2018