CISC2000: Computer Science II Lab 6

This lab practices define class, more particular, identifying the member variables, declaring and defining constructors, and other member functions.

Background reading:

Please review textbook for the basics of class, and the idea of Abstract Data Type (Section 10.2 and 10.3 in Savitch).

This project is adapted from the Project 7 in Page 614 of Savitch book.

Requirement

Write a rational number class. Recall a rational number is a ratio-nal number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator.

A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide the following two constructors:

  1. one constructor to make rational objects without any argument. The constructor sets the rational object's numerator to 0, and denominator to 1.
  2. one constructor to make rational objects out of pairs of int values, i.e., a constructor with two int parameters. The constructor uses the two values to initialize the rational object's numerator and denominator respectively.
  3. one constructor to make rational objects out of a single int values, as in 2/1, 17/1, i.e., a constructor with only one int paramter. The constructor sets the rational's numerator to the given parameter, and sets the denominator to 1.
You should also provide the following member functions:
  1. an input function that reads from standard input the value for current object. The input should be in the form of 2/3 or 27/51.
  2. an output function that displays the current object in the terminal. The output should also be in the form of 2/3, i.e., numberator/denominator.
  3. Two getter functions that return the numerator and denominator respectively.
  4. a Sum function that takes two rational objects as parameters. It sets the current rational object to be the sum of the two given rational numbers.

    Note the following formular for adding two rational numbers:

    a/b + c/d = ( a*d + b*c)/(b*d)
    
  5. an isEquals function that compares the current object member variables to the member variables of the input object. Remember that objects of the same class have access to private members of the class, even if it's a parameter. Remember that numerator and denomintor do not have to be the same for the value to be equal. Be careful with type coercion!
The following code segment demonstrate the usage of the above mentioned member functions, and constructors. Your main function should be some code like this.
Rational a(1,2);	//a is 1/2, first constructor is called to initialize a 
a.output ();  // display a in standard output, as 1/2 

Rational b(2); 	//b is 2/1, the second constructor is called to initialize b 
b.output ();  // display b in standard output, as 2/1 

Rational c; 	//the default constructor is called to initialize c to 0/1 
c.output ();  //You can see how the default construtor initializes the member variables... 

c.input(); //read value for c from input. If the user enters 3/4, then 
	   // c's numerator is set to 3, and its denominator is set to 4 
c.output(); //Display c's value 

//Now testing the Sum function
c.Sum (a,b); 	// c will be set to 1/2+2/1 = 5/2, see formular given above 
c.output ();  // it should display 5/2 

// Now test isEqual with the following two variables
Rational d;
Rational e;

// To Do: Get the values using the member function input 

// To Do: Test and Write out the result.

Hints

Please follow the order suggested below when working on this lab, always maintaining a compilable version of the code. Take an incremental approach. Write and test one function at a time!

  1. Define the class rational first, i.e., list the member variables, and declare the member functions in the class, including the constructors. Please refer to notes and textbook for the special syntax for declaring constructors.
  2. Implement the three constructors, test it in main, by declaring rational objects with no parameter, one paramter, and two paramters respectively.
  3. implement output function, and test it in main. This will also help you verify/test the three constructors.
  4. Implement input function, and test it in main.
  5. Implement Sum function, and test in main.
  6. Implement isEqual function, and test in main.
Suggested code template:

You are encouraged to work on this lab from scratch. If you encounter significant difficulties doing so, please email me to ask for a template code, which will help you get started. Please use the following command to copy the template provided.

copy_lab6
or copy and past from here.
You are encouraged to start from scratch, and you will earn extra credits if you successfully do that.