Reputation: 123
I’m still new to C#
programming and I am stuck on the following project. I know it must be easy but I’m brain-locked as to where to place the Reduce()
method and how to convert the fraction by eliminating the whole number and calling this to the Reduce()
method. Here is the project:
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.
Here is the code that I have so far. I’m also going to have an issue with multiplying these fractions and later creating an array but for now I’m trying to take this one step at a time. Any and all responses would be greatly appreciated.
public class Fraction
{
public int WholeNumber { get; private set; }
public int Numerator { get; private set; }
public int Denominator { get; private set; }
private void Reduce()
{
if (Numerator < Denominator)
{
WholeNumber = 0;
return;
}
if (Numerator == Denominator)
{
WholeNumber = 1;
Numerator = 0;
return;
}
if (Numerator > Denominator)
{
WholeNumber = (int)Math.Floor((decimal)Numerator / Denominator);
Numerator = Numerator - WholeNumber * Denominator;
}
}
//constructor with three parameters
public Fraction(int wholenumber, int numerator, int denominator)
{
WholeNumber = wholenumber;
Numerator = WholeNumber * Denominator + Numerator;
Reduce();
}
//constructor with two parameters
public Fraction(int numerator, int denominator)
{
WholeNumber = 0;
Numerator = numerator;
Denominator = denominator;
Reduce();
}
//parameter less with set values
public Fraction()
{
WholeNumber = 0;
Numerator = 0;
Denominator = 1;
}
public static Fraction operator +(Fraction firstfraction, Fraction secondfraction)
{
if (firstfraction.Denominator == secondfraction.Denominator)
{
int firstProduct = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator;
int secondProduct = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator;
return (new Fraction(firstProduct + secondProduct, firstfraction.Denominator));
}
else
{
int commondenominator = firstfraction.Denominator * secondfraction.Denominator;
int firstProduct = ((firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator) * secondfraction.Denominator;
int secondProduct = ((secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator) * firstfraction.Denominator;
return (new Fraction(firstProduct + secondProduct, commondenominator));
}
}
public Fraction Reduce(int WholeNumber, int Numerator, int Denominator)
{
int reduceWhole = (int)WholeNumber;
while (Numerator > Denominator)
{
Numerator -= Denominator;
reduceWhole += 1;
}
while (Denominator % Numerator == 0)
{
int factor = (int)Denominator / Numerator;
Numerator = 1;
Denominator = factor;
}
return new Fraction(reduceWhole, Numerator, Denominator);
}
}//end class
class Program
{
static void Main(string[] args)
{
Fraction firstfraction = new Fraction();
Fraction secondfraction = new Fraction();
Fraction total = firstfraction + secondfraction;
Console.WriteLine(total);
Console.Write("Enter whole number portion of fraction: ");
firstfraction.WholeNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter numerator: ");
firstfraction.Numerator = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter denominator: ");
firstfraction.Denominator = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter whole number portion of fraction: ");
secondfraction.WholeNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter numerator: ");
secondfraction.Numerator = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter denominator: ");
secondfraction.Denominator = Convert.ToInt32(Console.ReadLine());
Console.Write("{0} {1}/{2}", firstfraction.WholeNumber, firstfraction.Numerator,
firstfraction.Denominator);
Console.WriteLine(" + {0} {1}/{2}", secondfraction.WholeNumber,
secondfraction.Numerator, secondfraction.Denominator);
Fraction add = new Fraction();
add = firstfraction + secondfraction;
Console.Write("Result of adding two fractions is:{0} {1}/{2}", add.WholeNumber,
add.Numerator, add.Denominator);
Console.ReadLine();
}//end main
}//end class
Now I am getting 3 error messages that say the property or indexer
'FractionProgram.Fraction.Denominator'
cannot be used in this context because the set accessor is inaccessible.
Plus do I have the correct calculations for firstProduct and secondProduct?
Upvotes: 2
Views: 3561
Reputation: 2782
You are very close, so you deserve upvoting. The Reduce()
method should be part of your fraction class and called in the constructors. You should make your properties WholeNumber
, Numerator
, and Denominator
readonly to ensure consistency in your fraction:
public int WholeNumber { get; private set; }
public int Numerator { get; private set; }
public int Denominator { get; private set; }
The method will calculate the WholeNumber
based on the Numerator
and Denominator
as if the whole number is always included in the fraction. It should take care of the situations where the numerator is greater then the denominator, when they are equal, etc. Here is the Reduce()
method:
private void Reduce()
{
if (Numerator < Denominator)
{
WholeNumber = 0;
return;
}
if (Numerator == Denominator)
{
WholeNumber = 1;
Numerator = 0;
return;
}
if (Numerator > Denominator)
{
WholeNumber = (int)Math.Floor((decimal)Numerator / Denominator);
Numerator = Numerator - WholeNumber * Denominator;
}
}
The constructors become:
//constructor with three parameters
public Fraction(int wholenumber, int numerator, int denominator)
{
WholeNumber = wholenumber;
Numerator = WholeNumber * denominator + numerator;
Denominator = denominator;
Reduce();
}
//constructor with two parameters
public Fraction(int numerator, int denominator)
{
WholeNumber = 0;
Numerator = numerator;
Denominator = denominator;
Reduce();
}
Then the operator +()
becomes:
public static Fraction operator+(Fraction firstfraction, Fraction secondfraction)
{
if (firstfraction.Denominator == secondfraction.Denominator)
{
int firstProduct = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator;
int secondProduct = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator;
return (new Fraction(firstProduct + secondProduct, firstfraction.Denominator));
}
else
{
int commondenominator = firstfraction.Denominator * secondfraction.Denominator;
int firstProduct = ((firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator) * secondfraction.Denominator;
int secondProduct = ((secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator) * firstfraction.Denominator;
return (new Fraction(firstProduct + secondProduct, commondenominator));
}
}
Upvotes: 0
Reputation: 14688
I'm not going to do the problem for you, but I'll give you a few hints that will help you solve the problem by yourself and learn.
First, for the placement of the Reduce method, there are a few ways you can go about it:
myFraction.Reduce()
)string
or your operators) (myFraction = myFraction.Reduce()
)myFraction = Fraction.Reduce(myFraction)
From reading the assignment, it looks like you're supposed to use the first one. Do keep in mind that there usually is more than one way to implement a feature like this for your future projects, though.
As for the actual contents of the method, here are a few hints to lead you in the direction of the answer.
The rest of it you should be able to figure out mathematically, but if you still need help just leave a comment.
Upvotes: 3