Practice examples: abstract data types and implementations

Provide an interface then a full implementation for each of the following ADTs:

  1. ADT for fractions
    Data values to track:
       numerator, denominator: both integer values
    
    Operations:
       read a fraction 
       print a fraction
       given two fractions, f1 and f2, 
          compute and return f1+f2
       given two fractions, f1 and f2, 
          compute and return f1-f2
       given two fractions, f1 and f2, 
          compute and return f1*f2
       given two fractions, f1 and f2, 
          compute and return f1/f2
       given two fractions, f1 and f2, 
          compute and return f1%f2
    

  2. ADT for cash amounts (dollars and cents)
    Data values to track:
       dollars, cents: both integer values
    
    Operations:
       read a cash amount
       print a cash amount
       given two cash amounts, c1 and c2, 
          compute and return c1+c2
       given two cash amounts, c1 and c2, 
          compute and return c1-c2
       given a cash amount, c, and a multiplier, m,
          compute and return the cash amount m*c
    
    Note: the reason we don't simply represent the dollars as a float (e.g. $0.01 for a penny) is that there is no precise binary representation for the value 0.01. To see the impact, examine the following program and it's output:
    
    #include <cstdio>
    
    int main()
    {
       float penny = 0.0f;
       printf("The cents from one to 100 are:\n");
       for (int i = 1; i <= 100; i++) {
           penny += 0.01f;
           printf(" %g", penny);
           if ((i%5) == 0) printf("\n");
       }
    }
    
    The cents from one to 100 are: 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.839999 0.849999 0.859999 0.869999 0.879999 0.889999 0.899999 0.909999 0.919999 0.929999 0.939999 0.949999 0.959999 0.969999 0.979999 0.989999 0.999999