patterncsharpMinor
Creating and testing a Fraction class
Viewed 0 times
creatingtestingfractionandclass
Problem
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
Application.cs
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Te
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;
///
/// Initialize the fraction properties
///
/// Upper number
/// Lower number
public Fraction(int numerator, int denominator)
{
this.Numerator = numerator;
this.Denominator = denominator;
}
///
/// Access tot he numerator property
///
public int Numerator
{
get { return this.numerator; }
set { this.numerator = value; }
}
///
/// Access to the denominator property
///
public int Denominator
{
get { return this.denominator; }
set
{
if (value == 0)
{
throw new Exception("0 denominator");
}
this.denominator = value;
}
}
///
/// Return a string representation of a fraction
///
/// Displayable attribute
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.Te
Solution
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
And your
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:
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
Context
StackExchange Code Review Q#25054, answer score: 8
Revisions (0)
No revisions yet.