Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program named FractionDemo3 that includes an array of four Fractions. Prompt the user for values for each. Display every possible combination of addition

Write a program named FractionDemo3 that includes 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).

A sample program execution is shown below. The program should accept input and display output in the same format as the sample program.

Enter whole number portion of fraction 1 Enter numerator 2 Enter denominator 3 Enter whole number portion of fraction 2 Enter numerator 3 Enter denominator 4 Enter whole number portion of fraction 3 Enter numerator 4 Enter denominator 5 Enter whole number portion of fraction 4 Enter numerator 5 Enter denominator 6 Addition 1 2/3 + 1 2/3 = 3 1/3 1 2/3 + 2 3/4 = 4 5/12 1 2/3 + 3 4/5 = 5 7/15 1 2/3 + 4 5/6 = 6 1/2 2 3/4 + 1 2/3 = 4 5/12 2 3/4 + 2 3/4 = 5 1/2 2 3/4 + 3 4/5 = 6 11/20 2 3/4 + 4 5/6 = 7 7/12 3 4/5 + 1 2/3 = 5 7/15 3 4/5 + 2 3/4 = 6 11/20 3 4/5 + 3 4/5 = 7 3/5 3 4/5 + 4 5/6 = 8 19/30 4 5/6 + 1 2/3 = 6 1/2 4 5/6 + 2 3/4 = 7 7/12 4 5/6 + 3 4/5 = 8 19/30 4 5/6 + 4 5/6 = 9 2/3 Multiplication 1 2/3 * 1 2/3 = 2 7/9 1 2/3 * 2 3/4 = 4 7/12 1 2/3 * 3 4/5 = 6 1/3 1 2/3 * 4 5/6 = 8 1/18 2 3/4 * 1 2/3 = 4 7/12 2 3/4 * 2 3/4 = 7 9/16 2 3/4 * 3 4/5 = 10 9/20 2 3/4 * 4 5/6 = 13 7/24 3 4/5 * 1 2/3 = 6 1/3 3 4/5 * 2 3/4 = 10 9/20 3 4/5 * 3 4/5 = 14 11/25 3 4/5 * 4 5/6 = 18 11/30 4 5/6 * 1 2/3 = 8 1/18 4 5/6 * 2 3/4 = 13 7/24 4 5/6 * 3 4/5 = 18 11/30 4 5/6 * 4 5/6 = 23 13/36

image text in transcribed

image text in transcribed

using static System.Console;

class FractionDemo3

{

static void Main()

{

Fraction f1 = new Fraction(2, 3);

Fraction f2 = new Fraction(4, 5);

Fraction f3 = new Fraction();

f3 = f1 * f2;

WriteLine("{0} * {1} = {2}",

f1.FracString(), f2.FracString(), f3.FracString());

f1.WholeNum = 1;

f3 = f1 * f2;

WriteLine("{0} * {1} = {2}",

f1.FracString(), f2.FracString(), f3.FracString());

f2.Numerator = 8;

f2.Reduce();

f3 = f1 * f2;

WriteLine("{0} * {1} = {2}",

f1.FracString(), f2.FracString(), f3.FracString());

}

}

class Fraction

{

private int wholeNum;

private int numerator;

private int denominator;

public Fraction(int w, int n, int d)

{

WholeNum = w;

Numerator = n;

Denominator = d;

}

public Fraction(int n, int d) : this(0, n, d)

{

}

public Fraction() : this(0, 0, 1)

{

}

public int WholeNum

{

get

{

return wholeNum;

}

set

{

wholeNum = value;

}

}

public int Numerator

{

get

{

return numerator;

}

set

{

numerator = value;

}

}

public int Denominator

{

get

{

return denominator;

}

set

{

if(value != 0)

denominator = value;

else

denominator = 1;

}

}

public static Fraction operator+(Fraction f1, Fraction f2)

{

int num1 = (f1.WholeNum * f1.Denominator + f1.Numerator) *

f2.Denominator;

int num2 = (f2.WholeNum * f2.Denominator + f2.Numerator) *

f1.Denominator;

int num = num1 + num2;

int denom = f1.Denominator * f2.Denominator;

Fraction newFrac = new Fraction(num, denom);

newFrac.Reduce();

return newFrac;

}

public static Fraction operator*(Fraction f1, Fraction f2)

{

int num1 = (f1.WholeNum * f1.Denominator + f1.Numerator);

int num2 = (f2.WholeNum * f2.Denominator + f2.Numerator);

int num = num1 * num2;

int denom = f1.Denominator * f2.Denominator;

Fraction newFrac = new Fraction(num, denom);

newFrac.Reduce();

return newFrac;

}

public void Reduce()

{

int gcd;

int y;

if(numerator >= denominator)

{

wholeNum += numerator / denominator;

numerator = numerator % denominator;

}

gcd = 1;

for(y = numerator; y > 0; --y)

{

if(numerator % y == 0 && denominator % y == 0)

{

gcd = y;

y = 0;

}

}

numerator /= gcd;

denominator /= gcd;

}

public string FracString()

{

string fracString;

if(WholeNum == 0 && Numerator == 0)

fracString = "0";

else

if(WholeNum == 0)

fracString = Numerator + "/" + Denominator;

else

if(Numerator == 0)

fracString = "" + WholeNum;

else

fracString = WholeNum + " " + Numerator +

"/" + Denominator;

return fracString;

}

}

Test Case Incomplete Program displays addition and multiplication of four fractions case 1 Input Output 2/3 * 4/5 = 3/15 1 2/3 * 4/5 = 1 1/3 1 2/3 * 1 3/5 = 2 2/3 Results Mia 2 1 / 1 2/3 4 5/6 = 1/2 121.327.51 12/3.7 5/12 = 9 1/12 5/6 1 2/3 = 6 1/2 4 5/6 4 5/6 = 9 2/3 5/6 3 2/3 = 3 1/2 4 5/6 7 5/12 = 12 1/4 32/3 - 12/3 = 5 1/3 4 5/6 = 3 1/2 32/3 32/ 3 7 1/3 2/3 7 5/12 - 11 /12 75/12 12/ 3 1/12 75/12.456 = 12 1/4 75/12 3 2/3 11 1/12 5/22 75/12 245/Pultiplicatim 12/ 5/6 = 3 1/18 12/ 12 21/ 2/3 3 2/3 = 6 1/9 1 277 5/12 = 12 13/36 5/6 1 2/3 = 3 1/10 5/6 4 5/6 = 23 13/35 5/6 3 2/3 = 17 13/18 5/6 7 5/12 = 35 61/72 32/3 12/3= 6 1/9 3 2/3 4 5/6 = 17 13/18 3 2/3 3 2/3 = 13 4/9 3 2/3 - 7 5/12 = 27 7/36 75/12 12/3= 12 13/36 75/12 4 5/6 = 35 61/72 75/12 32/3 = 27 7/36 75/12 7 5/12 - 55 1/144 Test Case Incomplete Program displays addition and multiplication of four fractions case 2 Input Output 2/3 * 4/5 = 3/15 1 2/3 * 4/5 = 1 1/3 1 2/3 + 1 3/5 = 2 2/3 Results Addition 9/ 33/5 = 15 3/305/ 6 5/6 = 32/3 5/6 1 20/34 = 8 /1825 / 6 5 /4 = 3 1/12 3/5 5/6 = 15 13/30 3/ 3 3/5 = 17 1/ 3/5 1 28/34 = 18 16/ 3 /5 5/4 = 17/20 1 20/34 5/6 = /182 1 20/34 33/5 = 18 16/35 1 20/34.1 28/34 33/17 1 28/34 5/4 = 2 57/ 5/ 4 65/6 = 3 1/12 5/4 3/5 = 17/22 514 1 28/34 = 2 57 / 5/4.5/4 = 2 1/2 ltiplication / 665/65 45 25/365/6 3/5 = 58 23/30 65/ 6 1 28/34 = 10 29/345/ 65/4= 3 1/24 $ 3/5 6 5/6 = 58 23/30 3/5 3/5 73 24/25 3/5 -1 28/34 = 13 56/as 3/5 5/4 10 3/4 1 28/34 3/6 = 10 29/34 28/34 8 3/5 = 13 56/35 1 20/34 1 20/34 = 2 151/209 1 28/34 5/4 = 1 67/6B 5/ 4 6 5/6 = 3 13/24 5/4 8 3/5 = 10 3/4 5/4 - 1 28/34 = 1 67/68 5/4 5/4 = 1 9/16 Test Case Incomplete Program displays addition and multiplication of four fractions case 1 Input Output 2/3 * 4/5 = 3/15 1 2/3 * 4/5 = 1 1/3 1 2/3 * 1 3/5 = 2 2/3 Results Mia 2 1 / 1 2/3 4 5/6 = 1/2 121.327.51 12/3.7 5/12 = 9 1/12 5/6 1 2/3 = 6 1/2 4 5/6 4 5/6 = 9 2/3 5/6 3 2/3 = 3 1/2 4 5/6 7 5/12 = 12 1/4 32/3 - 12/3 = 5 1/3 4 5/6 = 3 1/2 32/3 32/ 3 7 1/3 2/3 7 5/12 - 11 /12 75/12 12/ 3 1/12 75/12.456 = 12 1/4 75/12 3 2/3 11 1/12 5/22 75/12 245/Pultiplicatim 12/ 5/6 = 3 1/18 12/ 12 21/ 2/3 3 2/3 = 6 1/9 1 277 5/12 = 12 13/36 5/6 1 2/3 = 3 1/10 5/6 4 5/6 = 23 13/35 5/6 3 2/3 = 17 13/18 5/6 7 5/12 = 35 61/72 32/3 12/3= 6 1/9 3 2/3 4 5/6 = 17 13/18 3 2/3 3 2/3 = 13 4/9 3 2/3 - 7 5/12 = 27 7/36 75/12 12/3= 12 13/36 75/12 4 5/6 = 35 61/72 75/12 32/3 = 27 7/36 75/12 7 5/12 - 55 1/144 Test Case Incomplete Program displays addition and multiplication of four fractions case 2 Input Output 2/3 * 4/5 = 3/15 1 2/3 * 4/5 = 1 1/3 1 2/3 + 1 3/5 = 2 2/3 Results Addition 9/ 33/5 = 15 3/305/ 6 5/6 = 32/3 5/6 1 20/34 = 8 /1825 / 6 5 /4 = 3 1/12 3/5 5/6 = 15 13/30 3/ 3 3/5 = 17 1/ 3/5 1 28/34 = 18 16/ 3 /5 5/4 = 17/20 1 20/34 5/6 = /182 1 20/34 33/5 = 18 16/35 1 20/34.1 28/34 33/17 1 28/34 5/4 = 2 57/ 5/ 4 65/6 = 3 1/12 5/4 3/5 = 17/22 514 1 28/34 = 2 57 / 5/4.5/4 = 2 1/2 ltiplication / 665/65 45 25/365/6 3/5 = 58 23/30 65/ 6 1 28/34 = 10 29/345/ 65/4= 3 1/24 $ 3/5 6 5/6 = 58 23/30 3/5 3/5 73 24/25 3/5 -1 28/34 = 13 56/as 3/5 5/4 10 3/4 1 28/34 3/6 = 10 29/34 28/34 8 3/5 = 13 56/35 1 20/34 1 20/34 = 2 151/209 1 28/34 5/4 = 1 67/6B 5/ 4 6 5/6 = 3 13/24 5/4 8 3/5 = 10 3/4 5/4 - 1 28/34 = 1 67/68 5/4 5/4 = 1 9/16

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions

Question

Explain ways to deal with anger constructively.

Answered: 1 week ago