# Creating and testing a Fraction class

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();
}
}
}

• Would he allow unit testing to be considered a class to "test" your class? – Robert Snyder Apr 13 '13 at 19:24

## 1 Answer

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

• 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! – user24087 Apr 13 '13 at 19:39
• you should actually throw an IllegalArgumentException. – Jakob Weisblat Apr 13 '13 at 19:45
• @JakobWeisblat This is not Java. In .Net you could use ArgumentException or ArgumentOutOfRangeException. – svick Apr 13 '13 at 21:17
• @svick oops. I should look at tags more carefully. – Jakob Weisblat Apr 13 '13 at 23:53
• Could also use DivideByZeroException – juharr Apr 15 '13 at 23:01