CS 205 - Programming for the Sciences
Spring 2008 - In-class Exercise for 03/25/08


Today's exercise is to define and implement a Rational number class. For this exercise, you are given a C# project with a GUI already created. You are to write the C# code to provide the Rational class and handers for the GUI buttons that use the Rational class methods.


Use a Web browser to go to the course webpage http://csserver.evansville.edu/~hwang/s08-courses/cs205.html. Under today's date, save the file RationalDemoInClass.zip. Extract the solution folder. Double-click into the folder, then double-click on RationalDemo.sln (the Visual Studio solution file). This will launch Visual Studio with the solution loaded.


The GUI design for the program has been completed. To see it, right-click on Form1.cs in the Solution explorer window and select View Design. It should look like the following:




Currently, there is no handler code for any of the buttons.


This GUI is intended to be used as an interface to test various methods and operations on Rational number objects More on this during class.


Some GUI notes:




Notes on Rational Numbers

A rational number is a number that can be expressed as an integer or the quotient of an integer divided by a nonzero integer. Expressed as a/b, a is called the numerator and b is called the denominator. Results of the binary arithmetic operations on rational numbers represented by a/b and c/d are as follows:


Operator

Result

a/b + c/d

(ad + bc) / bd

a/bc/d

(ad - bc) / bd

a/b * c/d

ac / bd

a/b / c/d

ad / bc, where c not equal to 0


In addition, a/b = c/d when ad = bc. From this relationship, the equality and relational operations can be defined. E.g., a/b < c/d when ad < bc. This is because ad and bc are the numerators of the left and right operands, respectively, when both are written with the common denominator bd.


The beginning of a Rational class for this exercise is in the file Rational.cs, which contains the private attributes, property definitions to get the numerator and denominator of a Rational object, and the empty and explicit-value constructors to create a Rational object. As discussed in last class, one reason for constructors is to ensure that all created Rational objects are valid. In particular, the denominator of a Rational number cannot be 0. The explicit-value constructor checks for this and uses standard C# way of informing the system there has been an error by throwing an Exception object.


Note that the explicit-value constructor calls a private method Reduce that further calls a private method GCD that computes the greatest common divisor of two positive integers. The algorithm that GCD uses was discovered by the ancient mathematician Euclid: For two positive integers m and n,

The GCD method implements this algorithm directly and is a little different than the methods we have seen before because it is recursive. That is, as part of computing its result, it calls itself. This does not cause a problem because eventually, the first case (also called the base case) is reached ending the series of calls.


C# notes

The C# code used for today's exercise is described in the following chapters of the textbook:



Assignment

(10 points) Working in pairs, complete this program by writing the Rational methods and handlers for each of the buttons. We will work on this in class in the following order:



03/23/08 2 of 3