Project04
Home ] Up ]

 

CSIS 250 Spring 2000 Assignment #4

Date Assigned:            March 1, 2000

Date Due:                     March 8, 2000 at 10:00 AM

Create a class called CRational for performing arithmetic with fractions.  Write a main driver program and several test functions (one for each function member and operator) to test your class.

Use integer variables to represent the private data of the class-the numerator and denominator.  Provide a constructor function that enables an object of this class to be initialized when it is declared.  The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form (i.e., the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator).  Provide the following functions/operators (public, private, or friend):

  1. Default constructor

  2. A constructor with given values for numerator and denominator

  3. A constructor to create a random fraction

  4. gcd function (private) to calculate the greatest common divisor of two integers

  5. Addition operator (+) for two CRational numbers.  The result should be stored in reduced form.

  6. Subtraction operator (-) for two CRational numbers.  The result should be stored in reduced form

  7.  Multiplication operator (*) for two CRational numbers.  The result should be stored in reduced form

  8. Division operator (/) for two CRational numbers.  The result should be stored in reduced form

  9. Insertion operator (<<) for printing CRational numbers in the form a/b where a is the numerator and b is the denominator

  10. Function displayValue for printing CRational numbers in floating-point format

 

Self-Evaluation Sheet

Component

Max Score

Your Score

Default constructor

1

 

A constructor with given values for numerator and denominator

1

 

A constructor to create random fraction

1

 

Operator +

1

 

Operator -

1

 

Operator *

1

 

Operator /

1

 

Operator <<

1

 

Function displayValue

1

 

gcd function

1

 

Test functions (10) for the above

10

 

Test runs (10) from the test functions

10

 

Total

30

 

 

Submit the following:

  1. Properly commented printed source code
  2. Properly labeled printed output from test runs
  3. Self-evaluation sheet