Well I need help one of the two questions which are similar to the topic I'm asking. The first question that I solve was "Taller method should return true if the tree on which the method is invoked has a greater height than the tree passes as a paramter".

public static boolean Taller(STree B)
{
    if(this.Height() > B.Height())
            return true;
        else
            return false;
    }

which is correct. The next question is what I'm trying to solve which is "The Positive method should return
true if the fraction passed as a parameter is positive, that is, if the numerator and denominator have the same
sign; otherwise it should return false". Here's my attempt, so any feedback is appreciated.

public class LFraction implements HasPos<IFraction>
{
    public static boolean Positive(LFraction T)
    {
    if((T.Numerator && >= 0) * (T. Denominator && >= 0) > 0 || (T.Numerator && <= 0) * 
    (T.Denominator && <= 0) < 0)
    return true;
    else
    return false;

Recommended Answers

All 7 Replies

Hey can you put up the LFraction class code? Thanks.

Sure no problem sariberri.

public class LFraction
{
        public static class DivideByZero extends Exception {
                private static final long serialVersionUID=0;
        };
    public LFraction(int top, int bottom) throws DivideByZero{
        numerator = top;
        denominator = bottom;
        if (bottom == 0) throw new DivideByZero();

    }

    public static LFraction Multiply(Fraction L, Fraction R) throws DivideByZero
    {

        LFraction Answer = new LFraction( L.Numerator() * R.Numerator(),
        L.Denominator() * R.Denominator() );

        return Answer;

    }

    public static LFraction Divide (Fraction L, Fraction R) throws DivideByZero 
    {
        LFraction Answer = new LFraction( L.Numerator() * R.Denominator(),
            L.Denominator() * R.Numerator() );
        return Answer;
    }

    public int Numerator()
    {
        return numerator;
    }

    public int Denominator()
    {
        return denominator;
    }

    public String ToString()
    {
        return (" " + Numerator() + " / " + Denominator());
    }
    public String toString() 
    {
     return (" " + Numerator() + " / " + Denominator());
    }
    public void Reciprocal() throws DivideByZero

    {
        if (numerator == 0) throw new DivideByZero();
        int flip = numerator;
        numerator = denominator;
        denominator = flip; 
    }
    private int numerator;
    private int denominator;

}

I may be misunderstanding the question but you should just be able to just do:

if(T.numerator() < 0 && T.denominator() < 0){
    return true;//two negatives -> positive
}

else if(T.numerator() < 0 && T.denominator() > 0){
    return false;//one negative, one positive -> negative
}

else if(T.numerator() > 0 && T.denominator() < 0){
    return false;//one negative, one positive -> negative
}

else{
    return true;//both positive
}

Does that help?

Yes that helps. Appreciate the help :). Makes sense. In a few mins, i'll mark this Question as solved.

Yay! :)

commented: Solved my problem thanks +1

I might be a bit picky, but what exactly has this to do with "inheritance/interfaces", as you state in your title? might have overlooked it, but I didn't see the interface, and most of your methods are static, which means they aren't inherited anyway.

also: a tip on code design, which 'll make your future code a lot shorter (and therefor easier to read:)

public static boolean Taller(STree B)
{
    if(this.Height() > B.Height())
            return true;
        else
            return false;
    }

can very easily be replaced by:

public static boolean Taller(STree B){ // I do recommend to keep an eye on naming conventions, though
  return this.height > B.Height(); 
   // for the current instance, you don't need a method to get the
   // members, you may also just say: return height > B.Height(); 
}

Hey stultuske. The interface was

interface HasTaller<T>
{
    public boolean Taller(T);
}
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.