Help With C# Fractions Program!
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Help With C# Fractions Program!

1. Junior Member
Join Date
Jul 2010
Posts
6

## Help With C# Fractions Program!

All,

I have the code below for the following problem:
Create a Fraction class with fields that hold a whole number, a numerator, and a denominator. In addition:
&#187; Create properties for each field. The set accessor for the denominator should not allow a 0 value; the value defaults to 1.
&#187; Add three constructors. One takes three parameters for a whole number, numerator, and denominator. Another accepts two parameters for the numerator and denominator; when this constructor is used, the whole number value is 0. The last constructor is parameter-less; it sets the whole number and numerator to 0 and the denominator to 1. (After construction, Fractions do not have to be reduced to proper form. For example, even though 3/9 could be reduced to 1/3, your constructors do not have to perform this task.)
&#187; Add a Reduce()method that reduces a Fraction if it is in improper form. For example, 2/4 should be reduced to 1/2.
&#187; Add an operator+()method that adds two Fractions. To add two fractions, first eliminate any whole number part of the value. For example, 2 1/4 becomes 9/4 and 1 3/5 becomes 8/5. Find a common denominator and convert the fractions to it. For example, when adding 9/4 and 8/5, you can convert them to 45/20 and 32/20. Then you can add the numerators, giving 77/20. Finally, call the Reduce()method to reduce the result, restoring any whole number value so the fractional part of the number is less than 1. For example, 77/20 becomes 3 17/20.
&#187; Include a function that returns a string that contains a Fraction in the usual display format—the whole number, a space, the numerator, a slash (/), and a denominator. When the whole number is 0, just the Fraction part of the value should be displayed (for example, 1/2 instead of 0 1/2). If the numerator is 0, just the whole number should display (for example, 2 instead of 2 0/3). Write a Main()method that instantiates several Fractions and demonstrate that all the methods work correctly. Save the program as FractionDemo.cs.
Code:
```using System;
public class FractionDemo
{
public static void Main()
{
Fraction fractionComplete = new Fraction(10, 1, 4);
Fraction fractionNumDen = new Fraction(2, 10);
Fraction fractionNumDen2 = new Fraction(3, 10);
Fraction fractionNone = new Fraction();
Fraction fractionCombined;

fractionCombined = fractionNumDen + fractionNumDen2;

Display(fractionComplete);
Display(fractionNumDen);
Display(fractionNone);
Display(fractionCombined);

}

public static void Display(Fraction incNumbers)
{
string fractionString;

if (incNumbers.Whole >= 1 && incNumbers.Numerator == 0)
{
fractionString = ("The fraction is " + incNumbers.Whole + ".");
Console.WriteLine(fractionString);
}
else if (incNumbers.Whole == 0 && incNumbers.Numerator >= 1)
{
fractionString = ("The fraction is " + incNumbers.Numerator + "/" + incNumbers.Denominator + ".");
Console.WriteLine(fractionString);
}
else if (incNumbers.Whole >= 1)
{
fractionString = ("The fraction is " + incNumbers.Whole + " " + incNumbers.Numerator + "/" + incNumbers.Denominator + ".");
Console.WriteLine(fractionString);
}
}
}

public class Fraction
{
private int whole;
private int numerator;
private int denominator;

public int Whole
{
get { return whole; }
set { whole = value; }
}

public int Numerator
{
get { return numerator; }
set { numerator = value; }
}

public int Denominator
{
get { return denominator; }
set
{
if (value >= 1)
denominator = value;
else
denominator = 1;
}
}

public Fraction (int incWhole, int incNumerator, int incDenominator)
{
Whole = incWhole;
Numerator = incNumerator;
Denominator = incDenominator;
}

public Fraction (int incNumerator, int incDenominator)
{
Whole = 0;
Reduce(incNumerator, incDenominator);
Numerator = incNumerator;
Denominator = incDenominator;
}

public Fraction()
{
Whole = 0;
Numerator = 0;
Denominator = 1;
}

public static Fraction operator+ (Fraction a, Fraction b)
{
int r1 = a.Numerator * b.Denominator + b.Numerator * a.Denominator;
int r2 = a.Denominator * b.Denominator;
return new Fraction(r1, r2);

}

public static int Reduce(int incNum, int incDen)
{
if (incNum == 0)
{
return incDen;
}

while (incDen != 0)
{
if (incNum > incDen)
{
incNum -= incDen;
}
else
{
incDen -= incNum;
}
}

return incNum;

}
}```
I'm getting a number of errors, but I can't figure out exactly what's causing it. I think it may be the Reduce or operator+ method, but I'm not sure how to re-write it. I would greatly appreciate your prompt response. Thanks!
Last edited by yourexodus; July 29th, 2010 at 06:18 PM. Reason: Ok, I got it to run without errors, but the fractions are still not being reduced. Any ideas??

2. Elite Member
Join Date
Jun 2008
Posts
2,477

3. ## Re: Help With C# Fractions Program!

A few improvements...

Refactor your code and remove the Whole property. Reason - this can be easily calculated when required and reduces code. Makes the Maths for adding/subtracting fractions easier too.

Remove the Display function and instead override the ToString() member of the Fraction class. Reason - Reduces code and the method is already known (and, in this case, most likely expected) by users of your class. They don't have to learn the Display() method.

In the ToString() function write the code necessary to display the fraction (including the whole number). Reason - Method is standard and is implied by .NET in many cases.

Don't include any English words in the output of the ToString() (like you have done in the current Display function). Reason - Forcing Users of your class to do things the way YOU like it and not give them the flexibilty to use your class as they see fit.

Then a User can use your class like this:

Code:
```Fraction myFraction= new Fraction(12, 5);
Console.WriteLine("The value of the Fraction is {0}.", myFraction);   // myFraction.ToString() is called here automatically by the .NET Framework

// output:  The value of the Fraction is 2 2/5.```
Last edited by rliq; July 29th, 2010 at 06:37 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•