## CS 1401 Introduction to Computer Science Fall 2013, Labs 9 and 10

Objective. The purpose of this lab is to practice objects.

Background. Sometimes, it is convenient to use fractions. In principle, we can represent each fraction as a double, but for fractions like 1/3, this representation is approximate. Let us use objects to represent fractions exactly.

Assignment for Lab 9. Define a class of fractions. Each instance of this class should have two integer-valued fields: numerator and denominator. Define a constructor, appropriate set-methods (modifiers) and get-methods, a method returning a type-double value of a fraction, and a method that prints the fraction as p/q. Use the main method to test your class.

Assignment for Lab 10. Add, to your Fraction class, the following arithmetic operations:

• adding a given fraction to the current fraction;
• subtracting a given fraction from the current fraction;
• multiplying the current fraction by a given fraction;
• dividing the current fraction by a given fraction;
• checking whether a given fraction is equal to the current fraction;
• checking whether the current fraction is smaller than the given fraction.

For extra credit. Add, to your Fraction class, a method that simplifies the current fraction by dividing both numerator and denominator by their greatest common divisor. For example, 12/18 should be simplified into 2/3.

To compute the greatest common divisor g of the values p and q, you can use one of the algorithms described in the textbook or the algorithm presented in the MW 12-1:20 version of Test 2:

```int i = p;
int q = q;
while(i > 0 && j > 0){
if (i >= j)
{i = i%j;}
else
{j = j%i;}
}
if(i > 0)
{g = i;}
else
{g = j;}
```
When it is due. Lab 9 is due at the beginning of the first lab section on the week of November 4, i.e.:
• on Monday November 4 for those who attend Monday-Wednesday labs, and
• on Tuesday November 5 for those who attend Tuesday-Thursday labs.
Lab 10 is due at the beginning of the first lab section on the week of November 11, i.e.:
• on Monday November 11 for those who attend Monday-Wednesday labs, and
• on Tuesday November 12 for those who attend Tuesday-Thursday labs.