in C# i need to Add an operator*() method to the Fraction class created in Programming Exercise 11a so that it correctly multiplies two Fractions. The result should be in proper, reduced format. Demonstrate that the method works correctly in a program named FractionDemo2. my errors are in the pictures my code is public class Fraction { private int numerator; private int denominator; public Fraction(int num, int denom) { numerator = num; denominator = denom; } // Define the * operator for Fraction objects public static Fraction operator *(Fraction a, Fraction b) { int num = a.numerator * b.numerator; int denom = a.denominator * b.denominator; return new Fraction(num, denom).Reduce(); } // Reduce the fraction to its lowest terms public Fraction Reduce() { int gcd = GCD(numerator, denominator); numerator /= gcd; denominator /= gcd; return this; } // Calculate the greatest common divisor private int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } } class FractionDemo2 { static void Main() { // Create two Fractions Fraction a = new Fraction(2, 3); Fraction b = new Fraction(3, 4); // Multiply the two Fractions Fraction result = a * b; // Display the result Console.WriteLine("{0} * {1} = {2}", a, b, result); } }
in C# i need to Add an operator*() method to the Fraction class created in Programming Exercise 11a so that it correctly multiplies two Fractions. The result should be in proper, reduced format. Demonstrate that the method works correctly in a program named FractionDemo2. my errors are in the pictures my code is public class Fraction { private int numerator; private int denominator; public Fraction(int num, int denom) { numerator = num; denominator = denom; } // Define the * operator for Fraction objects public static Fraction operator *(Fraction a, Fraction b) { int num = a.numerator * b.numerator; int denom = a.denominator * b.denominator; return new Fraction(num, denom).Reduce(); } // Reduce the fraction to its lowest terms public Fraction Reduce() { int gcd = GCD(numerator, denominator); numerator /= gcd; denominator /= gcd; return this; } // Calculate the greatest common divisor private int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } } class FractionDemo2 { static void Main() { // Create two Fractions Fraction a = new Fraction(2, 3); Fraction b = new Fraction(3, 4); // Multiply the two Fractions Fraction result = a * b; // Display the result Console.WriteLine("{0} * {1} = {2}", a, b, result); } }
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
in C# i need to
Add an operator*() method to the Fraction class created in Programming Exercise 11a so that it correctly multiplies two Fractions. The result should be in proper, reduced format. Demonstrate that the method works correctly in a program named FractionDemo2.
my errors are in the pictures
my code is
public class Fraction
{
private int numerator;
private int denominator;
public Fraction(int num, int denom)
{
numerator = num;
denominator = denom;
}
// Define the * operator for Fraction objects
public static Fraction operator *(Fraction a, Fraction b)
{
int num = a.numerator * b.numerator;
int denom = a.denominator * b.denominator;
return new Fraction(num, denom).Reduce();
}
// Reduce the fraction to its lowest terms
public Fraction Reduce()
{
int gcd = GCD(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
return this;
}
// Calculate the greatest common divisor
private int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
}
class FractionDemo2
{
static void Main()
{
// Create two Fractions
Fraction a = new Fraction(2, 3);
Fraction b = new Fraction(3, 4);
// Multiply the two Fractions
Fraction result = a * b;
// Display the result
Console.WriteLine("{0} * {1} = {2}", a, b, result);
}
}
Expert Solution
Step 1
The correct code is given below with output screenshot
Step by step
Solved in 4 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education