Sample Solution for Take-Home Quiz 4 for CSCI 201

(each question is worth 2 pts)

 

Below is a class definition (taken from Ch 9):

class RandomInt {
   public:
      RandomInt(int low = 0, int high = RAND_MAX);  ç  A
      RandomInt(int low, int high, unsigned int seed);
      int Draw();
      void SetInterval(int low, int high);
      void SetSeed(unsigned int seed);
      int GetLow() const;
      int GetHigh() const;
   private:
      int low;
      int high;
      int seed; <=== added in the solution
};
  1. What are the data members in the class defined above?
  2. Ans: low, high, and seed

  3. What kind of member function is pointed to by A above, and what is it used for?
  4. Ans: A constructor. A constructor is used to initialize the data members of a newly created object.

  5. Circle the statements below that are legal C++ code:
    1. RandomInt myInt;
    2. RandomInt myInt(1,5);
    3. RandomInt myInt(1,5,100);
    4. RandomInt myInt(1);

    Ans: They are all legal

  6. Circle the statements below that are legal C++ code:
  7. RandomInt randInt1(1,5), randInt2(100,500);

    1. int a = randInt1.Draw();
    2. int b = randInt1.GetLow();
    3. randInt1.low=5;
    4. randInt1.SetSeed=5000;

    Ans: The question had errors in formatting that have been corrected above---everybody recieved full credit for this question. The answer should have been (a) and (b). (c) is not legal because the data member low is private and, you can not directly access a private variable outside of a member function. (d) is not illegal because it is an incorrect call to the member function SetSeed(); the correct call looks like: randInt1.SetSeed(5000);

  8. The purpose of the SetInterval() member function is to set the values of the data members of the RandomInt class. Write an implementation of the member function SetInterval(). Be sure to use the proper syntax for writing a function that is a member of a class, and be sure your implementation matches the function prototype given in the class definition above.

    Ans: As discussed in class, you could have taken one of two approaches to writing this function:

    
    void RandomInt::SetInterval(int low, int high)
    {
       RandomInt::low = low;
       RandomInt::high = high;
    }
    

    Alternatively, you could have renamed the parameter so that they did not have the same names as the data members; any names would work:

    
    void RandomInt::SetInterval(int l, int h)
    {
       low = l;
       high = h;
    }
    

    Below is the definition of the Rational class that we saw in lecture.

    class Rational {
       public:
       // default constructor
          Rational();
       // specific constructor
          Rational(int numer, int denom = 1);
       // arithmetic facilitators
          Rational Add(const Rational &r) const;
          Rational Multiply(const Rational &r) const;
       // stream facilitators
          void Insert(ostream &sout) const;
          void Extract(istream &sin);
       // inspectors
          int GetNumerator() const;
          int GetDenominator() const;
       // mutators
          void SetNumerator(int numer);
          void SetDenominator(int denom);
       private:
       // data members
          int NumeratorValue;
          int DenominatorValue;
    };
    
  9. If I write a program that uses the Rational class, and, in that program, I define an object of type rational as follows:
  10. Rational myRat;
    Write a line of code to assign the value 12 to the numerator of myRat in that program.

    Ans: myRat.SetNumerator(12);

  11. If in the same program, I define an object of the Rational class as follows:

  12. Rational myRat(1,2);
    Write code to print the value of myRat to the monitor. (No auxiliary functions have been defined.)

    Ans: myRat.Insert(cout);

  13. If I define two objects of this class as follows:
  14. Rational rat1(1,2), rat2(8,3);
    Write code to multiply these two objects together and store the result of the multiplication in a third Rational object called rat3.

    Ans: Rational rat3 = rat2.Multiply(rat1);

  15. What will be printed by the following program:
  16. 
    #include<iostream.h>
    #include "rational.h"
    void main() {
         Rational rat1(1,2), rat2(8,3);
         rat1.Add(rat2);
         cout << rat1.GetNumerator() << endl << rat2.GetNumerator() << endl;
    }
    

    Ans:
    1
    8

  17. Write a function called MaxNum() that takes two Rational numbers as input (i.e., it has two formal parameters that are Rational numbers) and returns the value of the largest numerator (i.e., the return value should be a integer).
  18. Ans:

    int MaxNum(const Rational &rat1, const Rational &rat2)
    {
       int num1 = rat1.GetNumerator();
       int num2 = rat2.GetNumerator();
       if (num1 > num2)
          return num1;
       else 
          return num2;
    }