This in in C#
There are two classes, class Fraction and class FractionDemo
The user needs to be able to input a value for the numerator and the denominator
Create a Fraction class with private fields that hold a positive int numerator and a positive int denominator. In addition, create Properties for each field with the set mutator such that the numerator is greater than or equal to 0 and the denominator is greater than 0 (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 can be performed by finding the largest value that can divide evenly into both the numerator and the denominator (suggestion: use a for loop and starting with the smaller of the numerator or denominator).
Another method that is to be included is an overridden public method for ToString() which returns 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. For example: if f1 is an object of type Fraction containing a numerator of 3 and a denominator of 4, then the statement
Console.WriteLine(“The fraction is {0}, f1)
would output: The fraction is 3/4. Note the use of f1 as oppose to
f1.ToString() as the latter is redundant.
Also include an overloaded multiplication operator: i.e., public operator*(). This method multiples two Fraction objects and returns a Fraction object. Recall to multiply fractions, multiply the numerators and multiply the denominators. Be sure to also Reduce()the result.
In addition to multiplication, you are also required to overload the addition operator, i.e., public operator+(). This method 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 theiroppositedenominators. Thenaddthetwonumeratorstocreatethenumeratorforthe newfractionwithitsdenominatorbeingtheproductoftheoperanddenominators. Besureto Reduce()the result. 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
1
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 was 3/4, then double val = 3 / (double) 4; would assign 0.75 to val (notice the explicit 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 called FractionDemo in another file that contains a Main(). The purpose of this class is to test the functionality of your Fraction class. Main() is to include an array called testFractions that contains five (5) Fraction objects. testFraction[0] is to be created using the two-parameter constructors (use a denominator of 1 and a numerator of 2), while testFraction[1] is to be created using the no-parameter constructor. You are the to prompt the user to enter values for testFraction[1] and assign it values using the properties. Before you move on, be sure to have your program print out the values of testFraction[0] and testFraction[1].
Now you are prepared to demonstrate that your overloaded operators function properly. testFraction[2] is to hold the result from adding testFraction[0] to testFraction[1] while testFraction[3] is to store the result from multiplying testFraction[0] to testFraction[1]. Finally, testFraction[4] is to hold the result of testFraction[2] added to testFraction[3] and then multiplied by testFraction[0]. Your program should now print out the values of testFraction[2], testFraction[3] andtestFraction[4].
Finally, Main() should also compare the fraction objects using to show that both relational operators (>= and <=) work properly.
The basic structure of your Fraction class is required 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
}
}
-------------------- I used Visual Studio 2013, C# Language, Console Application --------------------
-------------------- OUTPUT --------------------


-------------------- FractionDemo.cs (contains main method) --------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp_FractionProgram
{
class FractionDemo
{
static void Main(string[] args)
{
Fraction[] testFraction = new Fraction[5];
testFraction[0] = new Fraction(1, 2);
testFraction[1] = new Fraction();
Console.Write("Enter testFraction[1] Numerator : ");
testFraction[1].Numerator =
Convert.ToInt32(Console.ReadLine());
Console.Write("Enter testFraction[1] Denominator : ");
testFraction[1].Denominator =
Convert.ToInt32(Console.ReadLine());
Console.WriteLine("testFraction[0] is {0}", testFraction[0]);
Console.WriteLine("testFraction[1] is {0}", testFraction[1]);
testFraction[2] = testFraction[0] + testFraction[1];
testFraction[3] = testFraction[0] * testFraction[1];
testFraction[4] = (testFraction[2] + testFraction[3]) *
testFraction[0];
Console.WriteLine("testFraction[2] is {0}", testFraction[2]);
Console.WriteLine("testFraction[3] is {0}", testFraction[3]);
Console.WriteLine("testFraction[4] is {0}",
testFraction[4]);
if (testFraction[0] <= testFraction[1])
Console.WriteLine("testFraction[0] is less than or equal to
testFraction[1]");
else if (testFraction[0] >= testFraction[1])
Console.WriteLine("testFraction[0] is greater than or equal to
testFraction[1]");
Console.ReadLine();
}
}
}
-------------------- Fraction.cs --------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp_FractionProgram
{
class Fraction
{
private int numerator;
private int denominator;
// No Parameter Constructor
public Fraction()
{
this.numerator = 0;
this.denominator = 1;
}
// Two Parameter Constructor
public Fraction(int num, int den)
{
this.numerator = num;
this.denominator = den;
}
// Numerator Property
public int Numerator
{
set
{
if (value >= 0)
{
this.numerator = value;
}
}
get
{
return this.numerator;
}
}
// Denominator Property
public int Denominator
{
set
{
if (value > 0)
this.denominator = value;
else
this.denominator = 1;
}
get
{
return this.denominator;
}
}
//Reduce method
private void Reduce()
{
//Store prime numbers from 1 to 100 into an integer array
int[] primeNumArr = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 };
//Loop through the array
for (int i = 0; i < primeNumArr.Length; i++)
{
//Check numerator and denominator is divisible by any prime
number
//while loop run until that prime number is divisible
while (numerator % primeNumArr[i] == 0 &&
denominator % primeNumArr[i] == 0)
{
numerator = numerator / primeNumArr[i];
denominator = denominator / primeNumArr[i];
}
}
}
// ToString method
public override string ToString()
{
return numerator + "/" + denominator;
}
// Multiply method
public static Fraction operator *(Fraction fract1, Fraction
fract2)
{
Fraction f1 = new Fraction();
f1.numerator = fract1.numerator * fract2.numerator;
f1.denominator = fract1.denominator * fract2.denominator;
f1.Reduce();
return f1;
}
// Add operator
public static Fraction operator +(Fraction fract1, Fraction
fract2)
{
Fraction f1 = new Fraction();
f1.numerator = (fract1.numerator * fract2.denominator) +
(fract1.denominator * fract2.numerator);
f1.denominator = fract1.denominator * fract2.denominator;
f1.Reduce();
return f1;
}
// Greater Than or Equal operator
public static bool operator >=(Fraction fract1, Fraction
fract2)
{
double f1 = fract1.numerator / (double)fract1.denominator;
double f2 = fract2.numerator / (double)fract2.denominator;
if (f1 >= f2)
{
return true;
}
return false;
}
// Less Than or Equal operator
public static bool operator <=(Fraction fract1, Fraction
fract2)
{
double f1 = fract1.numerator / (double)fract1.denominator;
double f2 = fract2.numerator / (double)fract2.denominator;
if (f1 <= f2)
{
return true;
}
return false;
}
}
}
This in in C# There are two classes, class Fraction and class FractionDemo The user needs...
Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...
Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...
I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...
Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...
I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your toString method needs to return the numerator followed by / followed by the denominator - NO spaces. DO NOT make any attempt to reduce the fractions (we shall do that later). Please add comments throughout. import java.util.Scanner; public class H4 { public static class Fraction { } public static void main(String[] args) { Fraction f=...
In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator; public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...
Hello, im looking for help on adding overload operators to my code. It needs to consist of + (addition) for the Fraction class * (multiplication), no need not reduce == (equality operator) = (assignment operator) << and >> so in main you can use << and >> as follows: cin>>frac1; cout<< frac1; where frac1 is a an object of the Fraction class the original code is: #include <iostream> using namespace std; class Fractions { private: int numerator; int denominator;...
Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....
java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...
You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...