Problem statement: Rational fractions are of the form a / b, in which a and b ar
ID: 3771885 • Letter: P
Question
Problem statement:
Rational fractions are of the form a / b, in which a and b are integers and b 0
If a / b and c / d are fractions all the arithmetic operations would be defined as follows:
a / b + c / d = (ad + bc) / bd
a / b - c / d = (ad - bc) / bd
a / b * c / d = ac / bd
(a / b ) / (c/d ) = ad / bc , in which c / d 0
CODE:
Design a class Fraction – that performs that performs the arithmetic and relational operations on fractions.
Provide the following three constructors:
- default (no- argument) constructor
- argument constructor
- copy constructor
Provide a class destructor
Overload the arithmetic( +, -, *. / ) and relational operators ( >, >=, <, <=, !=, ==)
Overload the stream insertion and extracting operators for input and output operations
Provide appropriate header and implementation files – Fraction.h and Fraction.cpp
Write a driver program(client.cpp) that uses the class Fraction and performs operations on fractions.
For example let x, y, z are objects of class Fraction.
If the user input is 2/3, then cin >> x should store 2 / 3 in x.
The statement cout << x + y; should output the value of x + y in fraction form.
The statement z = x + y; should store the sum of x and y in z in fraction form.
Your answer need not be in the lowest terms.
Extra Credit : If your answer is in their reduced form
Please use visual studio compiler C++. Also provide a sample screen shot of the result
Explanation / Answer
Answer :
public class Fraction
{
int upper;
int lower;
public Fraction()
{
upper = 0;
lower=1;
}
public Fraction(Fraction params)
{
upper = params.upper;
lower = params.lower;
}
public Fraction(int upper, int lower)
{
this.upper = upper;
this.lower= lower;
}
public Fraction(int upper)
{
upper = 0;
}
public String toString()
{
String value = this.getClass().getName()
+ "[ "
+ " upper = "
+ this.upper
+ ", "
+ " lower = "
+ this.lower
+ " ]";
return value;
}
public int getNumerator()
{
return this.upper;
}
public int getDenominator()
{
return this.lower;
}
public Fraction add(Fraction params)
{
if(this.lower == 0 || params.lower == 0)
throw new ArithmeticException("addition is not possible");
Fraction res = new Fraction();
res.upper = this.upper* params.lower
+ this.lower * params.upper;
res.lower = this.lower * params.upper;
return res;
}
public Fraction substract(Fraction params)
{
if(this.lower == 0 || params.lower == 0)
throw new ArithmeticException("subtraction is not possible");
Fraction res = new Fraction();
res.upper = this.upper * params.lower
- this.lower * params.upper;
res.lower = this.lower * params.lower;
return res;
}
public Fraction multiply(Fraction params)
{
if(this.lower == 0 || params.lower == 0)
throw new ArithmeticException("multiplication is not possible");
Fraction res = new Fraction();
res.upper = this.upper * params.upper;
res.lower = this.lower * params.lower;
return res;
}
public Fraction divide(Fraction params)
{
if(this.lower == 0 || params.lower == 0)
throw new ArithmeticException("division is not possible");
Fraction res = new Fraction();
res.upper = this.upper * params.lower ;
res.lower = this.lower * params.upper;
return res;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.