Question

In C# programming. Create a fractions class that represents fractions in the form a/b Your class...

In C# programming.

Create a fractions class that represents fractions in the form a/b Your class should implement the following members:

int Numerator

int Denominator

Fraction(int numerator, int denominator) ­ creates a new Fraction

double ToDecimal() ­ returns the fraction as a double

Fraction Add(Fraction f) ­ adds the fraction to the one passed in and simplifies the result

Fraction Multiply(Fraction f) ­ multiplies the fraction by the one passed in and simplifies the result

Fraction Simplify() ­ simplifies the fraction using the GreatestCommonDivisor method from the first Exercise

Create a test program that demonstrates the following:

1/2 = 0.5

1/7 + 1/5 = 12/35

1/4 * 2/3 * 4/5 = 2/15

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*C sharp program that demonstrats  the Fraction class and display the results of the
add and multiply operations of the Fraction to  console window.*/

//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FractionApp
{
class Program
{
static void Main(string[] args)
{
//create a Farction class
Fraction f1 = new Fraction(1, 2);
//print in decimal as 0.5
Console.WriteLine("{0}", f1.ToDecimal());

Fraction f2 = new Fraction(1, 7);
Fraction f3 = new Fraction(1, 5);
  
//call Add to add two fractions
Fraction add = f2.Add(f3);
add = add.simplify();//call simplify method
//print the numerator and denominator
Console.WriteLine("{0} / {1}", add.Numerator ,add.Denominator );

//Create three fraction objects
Fraction f4 = new Fraction(1, 4);
Fraction f5 = new Fraction(2, 3);
Fraction f6 = new Fraction(4, 5);

//call multiply two times
Fraction mul = f4.Multiply(f5);
mul = mul.Multiply(f6);
//call simplify on multiply
mul = mul.simplify();
Console.WriteLine("{0} / {1}", mul.Numerator, mul.Denominator);

Console.ReadLine();
}
}
}
------------------------------------------------------------------------------------------------------------------------


//Fraction.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FractionApp
{
//class Fraction
class Fraction
{
/*default constructor*/
public Fraction()
{
//set 0 and 1 to numerator and denominator
Numerator = 0;
Denominator = 1;
}
/*constructor to set the numerator and denominator*/
public Fraction(int numerator, int denominator)
{
Numerator = numerator;
Denominator = denominator;
}
//Setter and getter methods
public int Numerator { get; set; }
public int Denominator { get; set; }
//Returns the decimal value of fraction
public double ToDecimal()
{
return ((double )Numerator / Denominator);
}
//Method to add two Fractions
public Fraction Add(Fraction f)
{
Fraction temp=new Fraction ();
temp.Numerator = Numerator * f.Denominator + Denominator * f.Numerator;
temp.Denominator = Denominator * f.Denominator;
return temp;
}
//Method to multiplication of two Fractions
public Fraction Multiply(Fraction f)
{
Fraction temp = new Fraction();
temp.Numerator = Numerator * f.Numerator ;
temp.Denominator = Denominator * f.Denominator;
return temp;
}
/*Simplify the fraction*/
public Fraction simplify()
{
int gcd = GreatestCommonDivisor();
Numerator = Numerator / gcd;
Denominator = Denominator / gcd;
return new Fraction(Numerator, Denominator);
}

/*Returns the greatest common devisor*/
int GreatestCommonDivisor()
{
int gcd = 1;
for (int i = 1; i <= Numerator && i <= Denominator ; i++)
{
if (Numerator % i == 0 && Denominator % i == 0)
gcd = i;
}
return gcd;
}
}
}

------------------------------------------------------------------------------------------------------------------------

Sample Output:

Add a comment
Know the answer?
Add Answer to:
In C# programming. Create a fractions class that represents fractions in the form a/b Your class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    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...

  • C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....

    C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...

  • java code Write a class called FractionObject that represents a fraction with an integer numerator and...

    java code Write a class called FractionObject that represents a fraction with an integer numerator and denominator as fields. A Fraction Object object should have the following methods: A constructor with int numerator, int denominator as parameters - Constructs a new fraction object to represent the ratio (numerator/denominator). The denominator cannot be 0, so output a message if O is passed and set the numerator and denominator to 1. A constructor with no parameters - Constructs a new FractionObject to...

  • In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

    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)...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    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....

  • C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

    C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...

  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers variables to represent the private data of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT