Implement the following class.the class has two instance variables num for the fraction numerator and den for the fraction denominator.

public class Fraction implements Comparable<Fraction>
public Fraction(int num, int den)
// initialize instance variables
public int den() // returns the denominator
public int num() // returns the numerator
public Fraction mediant(Fraction that, int n)
// returns the medianfraction of this and that fraction
public boolean equals(Object that)
// overrides the equals method
public String toString() // returns the fraction as a string “num/den”

The mediant of two fractions a/b and c/d , given an integer n is the fraction(a+c)/(b+d)only if b+d ≤n

i understand that if the denominator is ≤ 2 i can find the median by just adding the number.
so i want to understand what an integer n does.does it divide the fractions?should it always be 2?

Recommended Answers

All 6 Replies

That's a good question. All the definitions of mediant that I found in a quick google had no use or meaning for such a parameter.
Are you sure that method signature is right? Is there any supporting documentation?

commented: the method signature is really correct +0

Here is talking about mediant of 2 fractions. However, I don't understand your statement "The mediant of two fractions a/b and c/d , given an integer n is the fraction(a+c)/(b+d)only if b+d" because it looks like it is cut off. What is your question regarding the mentioned portion?

commented: oh sorry only if b+d <= n +0

OK, that's strange. I understand that a/b < (a+c)/(b+d) < c/d, and that can be rewritten as a/b < n < c/d.

Edited: Ah got it. It means that b+d can't be greater than the value of the result integer n or the result would be a fraction instead of an integer (less than 1).

In that case please explain it to me, because I'm still confused. n isn't the result of anything, it's an arbitrary value passed in as a parameter. And what's wrong with the result being a fraction? The method signature requires that the result IS a fraction.

ok now i understand what n does its just a condition to be met.Looking at the API above,method signature:

public Fraction mediant(Fraction that,int n)
{
//please correct me if am wrong with my math.i want to add farction such that the answer i //get is the mediant,e.g 1/0+1/1=1/2 and half will be the mediant that is 1/0,1/2,1/1
if(that.den()&&this.den<=n){
return n;
}
Denominator = this.getDenominator()+that.getDenominator();
num = this.getNumerator()*that.getDenominator()*that.getNumerator()*this.getDenominator;
return Fraction (num,Denominator);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.