I’ve been working on this problem for a while and could use someguidance because I’m not entirely sure what I’m doing. Thank youfor any help you can give me.
This in in C#
There are two classes, class Fraction and classFractionDemo
The user needs to be able to input a value for thenumerator and the denominator
— The question is below —
Create a Fraction class with private fields that hold a positiveint numerator and a positive int denominator. In addition, createProperties for each field with the set mutator such that thenumerator is greater than or equal to 0 and the denominator isgreater than 0 (illegal values should be set to 1).
The Fraction class should have two constructors: the firstshould be a no-parameter constructor which sets the numerator to 0and the denominator to 1 while the second should take twoparameters, one for the numerator and one for the denominator. Besure to Reduce the fraction to its simplest form when the fractionis created or altered.
In terms of methods, the Fraction class should have a privatemethod Reduce() which reduces the fraction to its simplest form(e.g., 3/6 should be reduced to 1/2). The reduction can beperformed by finding the largest value that can divide evenly intoboth the numerator and the denominator (suggestion: use a for loopand starting with the smaller of the numerator or denominator).
Another method that is to be included is an overridden publicmethod for ToString() which returns a string that represents thefraction (i.e., “3/4”). Please note that ToString() does not printout the fraction but simply returns a string that can be printedwhen a Fraction object is referenced in a WriteLine statement. Forexample: if f1 is an object of type Fraction containing a numeratorof 3 and a denominator of 4, then the statementConsole.WriteLine(“The fraction is {0}, f1) would output: Thefraction is 3/4. Note the use of f1 as oppose to f1.ToString() asthe latter is redundant.
Also include an overloaded multiplication operator: i.e., publicoperator*(). This method multiples two Fraction objects and returnsa Fraction object. Recall to multiply fractions, multiply thenumerators and multiply the denominators. Be sure to alsoReduce()the result.
In addition to multiplication, you are also required to overloadthe addition operator, i.e., public operator+(). This method addstwo Fraction objects and returns a Fraction object. Recall to addtwo fractions, you first need a common denominator. While there areseveral approaches to achieve this, the simplest way is to multiplythe numerators of the two fractions by theiroppositedenominators.Thenaddthetwonumeratorstocreatethenumeratorforthenewfractionwithitsdenominatorbeingtheproductoftheoperanddenominators.Besureto Reduce()the result. For example: 3/8 + 1/4 = (3×4)/(8×4) +(1×8)/(4×8) = 12/32 + 8/32 = 20/32 … which when reduced is5/8.
The final methods to include are public operator>=() andpublic operator<=() methods that compare two Fraction objectsand return a boolean (true/false) value. Since fractions may havedifferent denominators, the simplest way to compare two fractionsis to compute the floating point value for each fraction and thencompare. To compute the floating point value, divide the numeratorby the denominator. For example, if the fraction was 3/4, thendouble val = 3 / (double) 4; would assign 0.75 to val (notice theexplicit casting to avoid the perils of integer division: val = 3 /4; would assign 0 to val ).
You are also required to write a static class calledFractionDemo in another file that contains a Main(). The purpose ofthis class is to test the functionality of your Fraction class.Main() is to include an array called testFractions that containsfive (5) Fraction objects. testFraction[0] is to be created usingthe two-parameter constructors (use a denominator of 1 and anumerator of 2), while testFraction[1] is to be created using theno-parameter constructor. You are the to prompt the user to entervalues for testFraction[1] and assign it values using theproperties. Before you move on, be sure to have your program printout the values of testFraction[0] and testFraction[1].
Now you are prepared to demonstrate that your overloadedoperators function properly. testFraction[2] is to hold the resultfrom adding testFraction[0] to testFraction[1] whiletestFraction[3] is to store the result from multiplyingtestFraction[0] to testFraction[1]. Finally, testFraction[4] is tohold the result of testFraction[2] added to testFraction[3] andthen multiplied by testFraction[0]. Your program should now printout the values of testFraction[2], testFraction[3]andtestFraction[4].
Finally, Main() should also compare the fraction objects usingto show that both relational operators (>= and <=) workproperly.
— The basic structure of your Fraction class isrequired to look like the following: —
public class Fraction
{
private int numerator; private int denominator; // No Parameter Constructor public Fraction() { // insert code here } // Two Parameter Constructor public Fraction(int num, int den) { // insert code here } // Numerator Property public int Numerator { // insert code here } // Denominator Property public int Denominator { // insert code here } //Reduce method private void Reduce() { // insert code here } // ToString method public override string ToString() { // insert code here } // Multiply method public static Fraction operator*(Fraction fract1, Fraction fract2) { // insert code here } // Add operator public static Fraction operator+(Fraction fract1, Fraction fract2) { // insert code here }
// Greater Than or Equal operator
public static bool operator>=(Fraction fract1, Fractionfract2)
{
// insert code here }
// Less Than or Equal operator
public static bool operator<=(Fraction fract1, Fractionfract2)
{
// insert code here }
}
Expert Answer
Answer to I’ve been working on this problem for a while and could use some guidance because I’m not entirely sure what I’m doing. …