Question
C# - NO JAVA Create a Fraction class with private fields that hold an int numerator and an int denominator. In addition, create Properties for
C# - NO JAVA
Create a Fraction class with private fields that hold an int numerator and an int denominator. In addition, create Properties for each field with the set mutator for the denominator being coded so as not to allow a 0 value (illegal values should be set to 1).
The Fraction class should have two constructors: the first should be a no-parameter constructor which sets the numerator to 0 and the denominator to 1 while the second should take two parameters, one for the numerator and one for the denominator. Be sure to Reduce the fraction to its simplest form when the fraction is created or altered.
In terms of methods, the Fraction class should have a private method Reduce() which reduces the fraction to its simplest form (e.g., 3/6 should be reduced to 1/2). The reduction is performed by finding the largest value that can divide evenly into both the numerator and the denominator (use a for loop and starting with the smaller of the numerator or denominator).
Another method that is to be included is the overridden public method for ToString() which should return a string that represents the fraction (i.e., 3/4). Please note that ToString() does not print out the fraction but simply returns a string that can be printed when a Fraction object is referenced in a WriteLine statement.
Also, include a public operator*() method that multiples two Fraction objects and returns a Fraction object. Recall to multiply fractions, multiply the numerators and multiply the denominators and then reduce the result. Also, include a public operator+() method that adds two Fraction objects and returns a Fraction object. Recall to add two fractions, you first need a common denominator. While there are several approaches to achieve this, the simplest way is to multiply the numerators of the two fractions by their opposite denominators. Then add the two numerators to create the numerator for the new fraction with its denominator being the product of the operand denominators. Be sure to Reduce() the result before returning the fraction. For example: 3/8 + 1/4 = (3x4)/(8x4) + (1x8)/(4x8) = 12/32 + 8/32 = 20/32 ... which when reduced is 5/8.
The final methods to include are public operator>=() and public operator<=() methods that compare two Fraction objects and return a boolean (true/false) value. Since fractions may have different denominators, the simplest way to compare two fractions is to compute the floating point value for each fraction and then compare. To compute the floating point value, divide the numerator by the denominator. For example, if the fraction
You will also have to write a driver class in C# (i.e. contains a Main() method in another file) that tests the functionality of the Fraction class. That is, you need to show that all the constructors, properties, methods, and overloaded operators work ... you will need at least three Fraction objects. In addition to using the two argument constructor to assign values to fractions, involve the user by asking for a numerator and denominator and then use the set property to assign values to another fraction. Main() should multiply and add fractions objects and assign the value to another fraction. Main() should also compare the fraction objects using both relational operators (>= and <=) and print out an appropriate message. Be sure to print out the values of all of the fractions you created. Use a loop to allow the program to test several different fractions so you can demonstrate that the program works for a range of input.
The basic structure of your Fraction class needs 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, Fraction fract2)
{
// insert code here
}
// Less Than or Equal operator
public static bool operator<=(Fraction fract1, Fraction fract2)
{
// insert code here
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started