In this exercise you will be using Microsoft Unit Test framework to test a class
ID: 3848843 • Letter: I
Question
In this exercise you will be using Microsoft Unit Test framework to test a class.
This class consist of 6 members.
Fraction
Class
Properties
+ <<C# property>>Top : int
+ <<C# property>>Bottom : int
Methods
+ <<constructor>> Fraction(int top = 0, int bottom = 1)
+$ <<operator>> +( Fraction left, Fraction right): Fraction
+$ <<operator>> -( Fraction left, Fraction right): Fraction
+ ToString() : string
Some common decorators for class members
+ public
# protected
- private
$ class member (static)
There are no fields
All the properties have public getters and private setters.
Top – this property represents the numerator of this object.
Bottom – this property represents the denominator of this object
Fraction(int top = 0, int bottom = 1) – This constructor takes two optional parameters and assigns them to the appropriate properties.
There are two overloaded operators: the addition and the subtraction operators
Look at the Complex Number Lab to for hints on operator overloading
public static Fraction operator +(Fraction left, Fraction right) – This will implement the addition operation.
public static Fraction operator -(Fraction left, Fraction right) – This will implement the subtraction operation. This will throw an exception if the right argument is larger than the left side argument.
public override string ToString() – This is a public method overrides the corresponding method in the object class to return a stringify form of the object. You get to decide how the properties will be display to the user.
Try to keep the output on a single line.
Remember in testing you will compare the expected value to the actual value. You need to write the testing methods for the following:
If you are having problems accessing your Fraction class from your testing project, or running your tests check the following:
· Ensure a reference to the library is added to your testing project
· Add the necessary using statement
· Verify that the Fraction class is public
· Confirm that you are NOT running from an unsecured drive such as the network H drive
//1 – Constructor. You must test the three cases: creating a fraction with two ints, with one int and with no argument
//arrange
//declare and suitably initialise two int: expectedTop and expectedBottom
//act
//create a fraction using the above two arguments
//assert
//compare the expectedTop with the Top property of the above object
Note: To test this constructor exhaustively, you need to check three conditions: constructor with zero, one and two arguments
//compare the expectedBottom with the Bottom property of the above fraction
//2 – ToString.
//arrange
//declare and initialise two ints
//declare the variable expectedString that will depend on your ToString() method.
//act
//declare and create an object using the above two ints
//assert
//compare the expectedString variable to the actual output of the ToString() method
.
You may use the following sample for your addition
//3 – Addition.
You may use the following sample for your subtraction
//4 – Subtraction without exception.
//5 - Subtraction with exception.
You may use the following sample for your subtraction
Fraction
Class
Properties
+ <<C# property>>Top : int
+ <<C# property>>Bottom : int
Methods
+ <<constructor>> Fraction(int top = 0, int bottom = 1)
+$ <<operator>> +( Fraction left, Fraction right): Fraction
+$ <<operator>> -( Fraction left, Fraction right): Fraction
+ ToString() : string
Explanation / Answer
Answer to given Question:
using System;
class Fraction
{
public int top;
public int bottom;
// create a fraction by passing in the top
// and bottom
public Fraction(int top=0, int bottom=1)
{
this.top=top;
this.bottom=bottom;
}
// overloaded operator + takes two fractions
// and returns their sum
public static Fraction operator+(Fraction left, Fraction right)
{
// like fractions (shared bottom) can be added
// by adding their tops
if (left.bottom == right.bottom)
{
return new Fraction(left.top+right.top,
left.bottom);
}
// simplistic solution for unlike fractions
// 1/2 + 3/4 == (1*4) + (3*2) / (2*4) == 10/8
// this method does not reduce.
int firstProduct = left.top * right.bottom;
int secondProduct = right.top * left.bottom;
return new Fraction(
firstProduct + secondProduct,
left.bottom * right.bottom
);
}
// overloaded operator - takes two fractions
// and returns their sum
public static Fraction operator+(Fraction left, Fraction right)
{
// like fractions (shared bottom) can be added
// by adding their tops
if (left.bottom == right.bottom)
{
return new Fraction(left.top-right.top,
left.bottom);
}
if(righ.bottom>left.bottom)
{
throw 1;
}
// simplistic solution for unlike fractions
// 1/2 - 3/4 == (1*4) - (3*2) / (2*4) == 10/8
// this method does not reduce.
int firstProduct = left.top * right.bottom;
int secondProduct = right.top * left.bottom;
return new Fraction(
firstProduct - secondProduct,
left.bottom * right.bottom
);
}
// return a string representation of the fraction
public override string ToString()
{
String s = top.ToString() + "/" +
bottom.ToString();
return s;
}
}
// Driver Class
public class TesterOverrideToString
{
static void Main()
{
Fraction f1 = new Fraction(3,4);
Console.WriteLine("f1: {0}", f1.ToString());
Fraction f2 = new Fraction(2,4);
Console.WriteLine("f2: {0}", f2.ToString());
Fraction f3 = f1 + f2;
Console.WriteLine("f1 + f2 = f3: {0}", f3.ToString());
Fraction f3 = f1 - f2;
Console.WriteLine("f1 - f2 = f3: {0}", f3.ToString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.