11.) a.) Create a Fraction class with fields that hold a whole number, a numerat
ID: 3619663 • Letter: 1
Question
11.)a.) Create a Fraction class with fields that hold a whole number, a numerator, and a
denominator. In addition:
» Create properties for each field. The set accessor for the denominator should not allow a 0 value; the value defaults to 1.
» Add three constructors. One takes three parameters for a whole number, numerator, and denominator. Another accepts two parameters for the numerator and denominator; when this constructor is used, the whole number value is 0. The last constructor is parameter-less; it sets the whole number and numerator to 0 and the denominator to 1. (After construction, Fractions do not have to be reduced to proper form. For example, even though 3/9 could be reduced to 1/3, your constructors do not have to perform this task.)
» Add a Reduce()method that reduces a Fraction if it is in improper form. For example, 2/4 should be reduced to 1/2.
» Add an operator+()method that adds two Fractions. To add two fractions, first eliminate any whole number part of the value. For example, 2 1/4 becomes 9/4 and 1 3/5 becomes 8/5. Find a common denominator and convert the fractions to it. For example, when adding 9/4 and 8/5, you can convert them to 45/20 and 32/20. Then you can add the numerators, giving 77/20. Finally, call the Reduce()method to reduce the result, restoring any whole number value so the fractional part of the number is less than 1. For example, 77/20 becomes 3 17/20.
» Include a function that returns a string that contains a Fraction in the usual display format—the whole number, a space, the numerator, a slash (/), and a denominator. When the whole number is 0, just the Fraction part of the value should be displayed (for example, 1/2 instead of 0 1/2). If the numerator is 0, just the whole number should display (for example, 2 instead of 2 0/3). Write a Main()method that instantiates several Fractions and demonstrate that all the methods work correctly. Save the program as FractionDemo.cs.
b.) Add an operator*()method to the Fraction class created in Exercise 11a so that it correctly multiplies two Fractions. The result should be in proper, reduced format. Demonstrate that the method works correctly. Save the program as FractionDemo2.cs.
c.) Create an array of four Fractions. Prompt the user for values for each. Display every possible combination of addition results and every possible combination of multiplication results for each Fraction pair (that is, each type will have 16 results). Figure 7-48 shows a sample execution. Save the program as FractionDemo3.cs.
Explanation / Answer
Dear... Here is the code.. a) using System; using System.Text; namespace Console1 { public class Fraction { private int wholenumber; private int numerator; private int denominator; public int WholeNumber { get { return wholenumber; } set { wholenumber = value; } } public int Numerator { get { return numerator; } set { numerator = value; } } public int Denominator { get { return denominator; } set { denominator = value; if (denominator > 0) { denominator = value; } else { denominator = 1; } } } //constructor with three parameters public Fraction(int wholenumber, int numerator, int denominator) : this(numerator, denominator) { WholeNumber = wholenumber; } //constructor with two parameters public Fraction(int numerator, int denominator) { WholeNumber = 0; Numerator = numerator; Denominator = denominator; } public Fraction() { WholeNumber = 0; Numerator = 0; Denominator = 1; } //public static showFraction() //{ // total = //} public static Fraction operator+(Fraction firstfraction, Fraction secondfraction) { if (firstfraction.Denominator == secondfraction.Denominator) { int firstProduct = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator; int secondProduct = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator; return (new Fraction(firstProduct + secondProduct, firstfraction.Denominator)); } else { int commondenominator = firstfraction.Denominator * secondfraction.Denominator; int firstProduct1 = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator; int secondProduct2 = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator; int whole=((firstProduct1*secondfraction.Denominator)+ (secondProduct2*firstfraction.Denominator))/ commondenominator; return (new Fraction(whole,firstProduct1 + secondProduct2, commondenominator)); } }//end function }//end classs class Program { static void Main(string[] args) { Fraction firstfraction = new Fraction(); Fraction secondfraction = new Fraction(); Fraction total = firstfraction + secondfraction; Console.WriteLine(total); Console.Write("Enter whole number portion of fraction: "); firstfraction.WholeNumber = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter numerator: "); firstfraction.Numerator = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter denominator: "); firstfraction.Denominator = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter whole number portion of fraction: "); secondfraction.WholeNumber = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter numerator: "); secondfraction.Numerator = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter denominator: "); secondfraction.Denominator = Convert.ToInt32(Console.ReadLine()); Console.Write("{0} {1}/{2}", firstfraction.WholeNumber, firstfraction.Numerator, firstfraction.Denominator); Console.WriteLine(" + {0} {1}/{2}", secondfraction.WholeNumber, secondfraction.Numerator, secondfraction.Denominator); Fraction add=new Fraction(); add=firstfraction+secondfraction; Console.Write("Result of adding two fractions is:{0} {1}/{2}", add.WholeNumber, add.Numerator, add.Denominator); Console.readLine(); }//end main }//end class }//end namespace b) public static Fraction operator*(Fraction frac1, Fraction frac2) { int firstProduct1 = (frac1.WholeNumber * frac1.Numerator) + frac1.Numerator; int secondProduct2 = (frac2.WholeNumber * frac2.Denominator) + frac2.Numerator; int iNumerator=firstProduct1*secondProduct2; int iDenominator = frac1.Denominator * frac2.Denominator; return ( new Fraction(iNumerator,iDenominator)); } Remaing bit do post in another so that we can help you.. Hope this will help you...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.