5
\$\begingroup\$

The exercise is as follows:

Create a class called Fraction that can be used to represent the ratio of two integers. Include appropriate constructors, properties, and methods. If the denominator becomes zero, throw and handle an exception. Create an application class to test the Fraction class.

The program runs. Will someone look over my code for this exercise?

Fraction.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Part2
{
    public class Fraction
    {
        private int numerator;
        private int denominator;

        /// <summary>
        /// Initialize the fraction properties
        /// </summary>
        /// <param name="numerator">Upper number</param>
        /// <param name="denominator">Lower number</param>
        public Fraction(int numerator, int denominator)
        {
            this.Numerator = numerator;
            this.Denominator = denominator;
        }

        /// <summary>
        /// Access tot he numerator property
        /// </summary>
        public int Numerator
        {
            get { return this.numerator; }
            set { this.numerator = value; }
        }

        /// <summary>
        /// Access to the denominator property
        /// </summary>
        public int Denominator
        {
            get { return this.denominator; }
            set
            {
                if (value == 0)
                {
                    throw new Exception("0 denominator");
                }

                this.denominator = value;
            }
        }

        /// <summary>
        /// Return a string representation of a fraction
        /// </summary>
        /// <returns>Displayable attribute</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(this.Numerator + "/" + this.numerator);
            sb.Append(" or ");
            sb.Append(this.Numerator / this.Denominator);

            return sb.ToString();
        }
    }
}

Application.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Part2
{
    public class Application
    {
        /// <summary>
        /// Entry point of the program
        /// </summary>
        public static void Main(string[] args)
        {
            // Create a fraction
            Fraction f1 = new Fraction(1, 2);
            Console.WriteLine(f1);

            try
            {
                // Create another fraction
                Fraction f2 = new Fraction(2, 0);
                Console.WriteLine(f2);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Would he allow unit testing to be considered a class to "test" your class? \$\endgroup\$ Apr 13, 2013 at 19:24

1 Answer 1

8
\$\begingroup\$

I think your class is pretty much useless. If you have a class that represents a fraction, you should be able to do operations with it that you can do with a fraction, like addition, subtraction, multiplication and division (possibly using operator overloading). Another possible operation is fraction simplification.

Also, you shouldn't throw Exception, you should instead a custom type that inherits from Exception (called something like ZeroDenominatorException) and throw that.

And your Main() isn't very realistic. In real code, you wouldn't write code that you know will always throw an exception. So I think that your try should be around both of your new Fraction() statements.

Also, if you're going to have all the operations I have mentioned above, a better application to show that would be a very simple fraction calculator.

Something like:

Enter numerator:
1
Enter denominator:
4
Enter operation:
+
Enter numerator:
3
Enter denominator:
4
Result: 4/4
Simplifies result: 1/1
\$\endgroup\$
5
  • \$\begingroup\$ Thank you. I will change some of the things you mentioned. I will refrain from adding anything that the exercise doesn't explicitly call for like math operations(although it would be a better program). Less to grade = less chance for grade deduction. Thank you very much for your help! \$\endgroup\$
    – user24087
    Apr 13, 2013 at 19:39
  • \$\begingroup\$ you should actually throw an IllegalArgumentException. \$\endgroup\$ Apr 13, 2013 at 19:45
  • 1
    \$\begingroup\$ @JakobWeisblat This is not Java. In .Net you could use ArgumentException or ArgumentOutOfRangeException. \$\endgroup\$
    – svick
    Apr 13, 2013 at 21:17
  • \$\begingroup\$ @svick oops. I should look at tags more carefully. \$\endgroup\$ Apr 13, 2013 at 23:53
  • \$\begingroup\$ Could also use DivideByZeroException \$\endgroup\$
    – juharr
    Apr 15, 2013 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.