Question Fraction Calculator problem

Angel

New member
Joined
Dec 15, 2013
Messages
1
Programming Experience
Beginner
Hello would someone please help me with my program, I'm stuck all day with this thing. The program runs, but I think the problem is a logical error. The fraction 1/4 + 2 1/2 should be equal to 2 3/4 but the program's result is 2 3/8. Another thing is the expression 1/8 + 2 1/2 should be equals to 2 5/8. Please help me how to fix the codes. Thank you very much!

C#:
using System;
 
class FractionDemo
{
    static void Main(string[] args)
    {
        Fraction firstfraction = new Fraction();
        Fraction secondfraction = new Fraction();
        firstfraction.Numerator = 1;
        firstfraction.Denominator = 4;
        secondfraction.Numerator = 1;
        secondfraction.Denominator = 8;
        secondfraction.WholeNumber = 2;
        Fraction add = new Fraction();
        add = firstfraction + secondfraction;
 
        Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
        Console.WriteLine(" + {0}/{1} = {2}/{3}", secondfraction.Numerator, secondfraction.Denominator, add.Numerator, add.Denominator);
        Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
        Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator,secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
        Console.Write("\n {0}/{1}", firstfraction.Numerator, secondfraction.Denominator);
        Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator, secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
        Console.ReadLine();
 
    }
 
    public class Fraction
    {
        private int wholenumber;
        private int numerator;
        private int denominator;
 
        public int WholeNumber
        {
            get
            {
                return wholenumber;
            }
            set
            {
                wholenumber = value;
            }
        }
 
        public int Numerator
        {
            get
            {
                return numerator;
            }
            set
            {
                numerator = value;
            }
        }
 
        public int Denominator
        {
            get
            {
                return denominator;
            }
            set
            {
                denominator = value;
                if (denominator > 0)
                {
                    denominator = value;
                }
                else
                {
                    denominator = 1;
                }
            }
        }
 
        public Fraction(int wholenumber, int numerator, int denominator)
            : this(numerator, denominator)
        {
            WholeNumber = wholenumber;
        }
 
        public Fraction(int numerator, int denominator)
        {
            WholeNumber = 0;
            Numerator = numerator;
            Denominator = denominator;
        }
 
        public Fraction()
        {
            WholeNumber = 0;
            Numerator = 0;
            Denominator = 1;
        }
 
        public int gcd()
        {
            int x = Numerator;
            int y = Denominator;
            int m;
 
            if (x > y)
                m = y;
            else
                m = x;
 
            for (int i = m; i >= 1; i--)
            {
                if (x % i == 0 && y % i == 0)
                {
 
                    return i;
 
                }
            }
            return 1;
        }
 
        public void Reduce()
        {
            int gcdNum = gcd();
 
            if (gcdNum != 0)
            {
                Numerator = Numerator / gcdNum;
                Denominator = Denominator / gcdNum;
            }
            if (Denominator < 0)
            {
                Denominator = Denominator * -1;
                Numerator = Numerator * -1;
            }
            convertFraction();
        }
 
        public void convertFraction()
        {
            WholeNumber = Numerator / Denominator;
            Numerator = Numerator % Denominator;
        }
 
        public static Fraction operator +(Fraction firstfraction, Fraction secondfraction)
        {
            int firstNum = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator;
            int secondNum = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator;
 
            Fraction Result = new Fraction();
 
            Result.Numerator = firstNum * secondfraction.Denominator + firstfraction.Denominator * secondNum;
            Result.Denominator = firstfraction.Denominator * secondfraction.Denominator;
            Result.Reduce();
            return Result;
        }
    }
}
 
Have you actually debugged your code or have you just read it? Obviously reading the code is the first step but, if that doesn't yield a cause, the next step is to debug and watch your code in action. Set a breakpoint and then step through your code line by line. At each step, you can check the state of all your variables, etc. As soon as the state of the app differs from your expectations, you know you've found an issue and you can hone in on it right there.
 
Hello would someone please help me with my program, I'm stuck all day with this thing. The program runs, but I think the problem is a logical error. The fraction 1/4 + 2 1/2 should be equal to 2 3/4 but the program's result is 2 3/8. Another thing is the expression 1/8 + 2 1/2 should be equals to 2 5/8. Please help me how to fix the codes. Thank you very much!

C#:
using System;
 
class FractionDemo
{
    static void Main(string[] args)
    {
        Fraction firstfraction = new Fraction();
        Fraction secondfraction = new Fraction();
        firstfraction.Numerator = 1;
        firstfraction.Denominator = 4;
        secondfraction.Numerator = 1;
        secondfraction.Denominator = 8;
        secondfraction.WholeNumber = 2;
        Fraction add = new Fraction();
        add = firstfraction + secondfraction;
 
        Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
        Console.WriteLine(" + {0}/{1} = {2}/{3}", secondfraction.Numerator, secondfraction.Denominator, add.Numerator, add.Denominator);
        Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
        Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator,secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
        Console.Write("\n {0}/{1}", firstfraction.Numerator, secondfraction.Denominator);
        Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator, secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
        Console.ReadLine();
 
    }
 
    public class Fraction
    {
        private int wholenumber;
        private int numerator;
        private int denominator;
 
        public int WholeNumber
        {
            get
            {
                return wholenumber;
            }
            set
            {
                wholenumber = value;
            }
        }
 
        public int Numerator
        {
            get
            {
                return numerator;
            }
            set
            {
                numerator = value;
            }
        }
 
        public int Denominator
        {
            get
            {
                return denominator;
            }
            set
            {
                denominator = value;
                if (denominator > 0)
                {
                    denominator = value;
                }
                else
                {
                    denominator = 1;
                }
            }
        }
 
        public Fraction(int wholenumber, int numerator, int denominator)
            : this(numerator, denominator)
        {
            WholeNumber = wholenumber;
        }
 
        public Fraction(int numerator, int denominator)
        {
            WholeNumber = 0;
            Numerator = numerator;
            Denominator = denominator;
        }
 
        public Fraction()
        {
            WholeNumber = 0;
            Numerator = 0;
            Denominator = 1;
        }
 
        public int gcd()
        {
            int x = Numerator;
            int y = Denominator;
            int m;
 
            if (x > y)
                m = y;
            else
                m = x;
 
            for (int i = m; i >= 1; i--)
            {
                if (x % i == 0 && y % i == 0)
                {
 
                    return i;
 
                }
            }
            return 1;
        }
 
        public void Reduce()
        {
            int gcdNum = gcd();
 
            if (gcdNum != 0)
            {
                Numerator = Numerator / gcdNum;
                Denominator = Denominator / gcdNum;
            }
            if (Denominator < 0)
            {
                Denominator = Denominator * -1;
                Numerator = Numerator * -1;
            }
            convertFraction();
        }
 
        public void convertFraction()
        {
            WholeNumber = Numerator / Denominator;
            Numerator = Numerator % Denominator;
        }
 
        public static Fraction operator +(Fraction firstfraction, Fraction secondfraction)
        {
            int firstNum = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator;
            int secondNum = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator;
 
            Fraction Result = new Fraction();
 
            Result.Numerator = firstNum * secondfraction.Denominator + firstfraction.Denominator * secondNum;
            Result.Denominator = firstfraction.Denominator * secondfraction.Denominator;
            Result.Reduce();
            return Result;
        }
    }
}

Hello, been lurking here for several weeks, first time poster.

I am in an advanced Visual C# class (using .net 4.0). A recent assignment was to create a class to do math operations with fractions, and a windows forms application to demonstrate it. Now, I think I have the math operations done correctly (in that the math should provide a correct answer) but the program is not behaving as I expected. When the program calls on of the math methods in my fraction class, it either hangs completely, forcing me to terminate the program, or outputs an incorrect result. I've attached my code here :FractionCalculator_-_Michael_Kestner-2014-04-11.zip Before anybody asks, the instructor specified the method headers to be used in the Fraction class, based on what I've seen while searching for answers, they're a bit unusual.

I think the problem is partly that there's no way to change a fraction's properties once it's been created. For example, I'm pretty sure the program is hanging when I do something like
Code:
Fraction thirdFrac = new Fraction();
then later trying to change it like this :
Code:
thirdFrac = firstFrac.Add(secondFrac);
because it can't actually do what I'm telling it to do. Again, the instructor specified read-only properties, otherwise I think this would be pretty easy to fix. I'm just drawing a blank on how to work around this issue. Any advice would be appreciated.
 
Look closely at your lines 11-13. You are initializing a fraction that is line 2 1/8, not the 2 1/2 that you wanted as the second addend.

Look closely at 18. You are not showing the whole parts of the second addend, nor the sum.

Look closely at lines 20 and 22. You are printing out the whole part of the second addend as the denominator.

Be aware that .NET Framework 4.0 is out of support. You'll want to use at least .NET Framework 4.6.2 or higher.

One way to sort of deal with the confusion of lines 20, and 22 is to use the C# string interpolation feature. So instead of:
C#:
Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator,secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
you would write:
C#:
Console.WriteLine($" + {secondfraction.WholeNumber} {secondfraction.Numerator}/{secondfraction.WholeNumber} = {add.WholeNumber} {add.Numerator}/{add.Denominator}");
and quickly see the issue.
And obviously to fix things you would write:
C#:
Console.WriteLine($" + {secondfraction.WholeNumber} {secondfraction.Numerator}/{secondfraction.Denominator} = {add.WholeNumber} {add.Numerator}/{add.Denominator}");

But there is something that you could do to fix the issue at line 18, as well as avoid all the extra typing you've got on all those lines of code trying to pull out the whole part, numerator, and denominator. You can override the ToString() method in your Fraction class. Something like:
C#:
class Fraction
{
:
    public override string ToString()
    {
        return $"{WholeNumber} {Numerator}/{Denominator}";
    }
:
}

And so lines 17-22 could simply be:
C#:
Console.WriteLine($"{firstfraction} + {secondfaction} = {add}");
Console.WriteLine($"{firstfraction} + {secondfaction} = {add}");
Console.WriteLine($"{firstfraction} + {secondfaction} = {add}");
 
Last edited:

Latest posts

Back
Top Bottom