0

I am trying to test my abstract class but I am running into problems when I call the methods from the test class. It has been a while since I used Java and I have not used abstract classes before. Any pointers on where I am going wrong would be appreciated. Thanks.

The abstract class

public abstract class RationalNumbers {

    public int numerator, denominator, temp;

    public void setNumerator(int n) {
        numerator = n;
    }

    public void setDenominator(int d) {
        denominator = d;
    }

    public int getNumerator() {
        return numerator;
    }

    public int getDenominator() {
        return denominator;
    }

    public int add() {
        temp = numerator + denominator;
        return temp;
    }

    public int subtract() {
        temp = numerator - denominator;
        return temp;
    }

    public int multiply() {
        temp = numerator * denominator;
        return temp;
    }

    public int divide() {
        temp = numerator / denominator;
        return temp;
    }

   public boolean isEqual() {
        boolean isEqual;
        if (numerator == denominator) {
            isEqual = true;
        } else {
            isEqual = false;
        }
        return isEqual;
    }
}

The test class

public class testClass extends RationalNumbers {

    public static void main(String[] args) {
        setNumerator(5);
        setDenominator(10);
        System.out.println("Equal: " + isEqual());
        System.out.println("Numerator: " + getNumerator());
        // etc...
    }
}
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Meowbits
  • 586
  • 3
  • 9
  • 28
  • 1
    You have to 1) create a concrete child class of your abstract class, 2) create an object of the concrete class that you can call methods on (here's where you're failing in the code above), and most important 3) re-read the intro-to java-tutorials. There's a lot to relearn, but with diligence and effort, you will succeed. – Hovercraft Full Of Eels Jul 07 '12 at 02:14
  • Thanks, I will get it figured out eventually ;) – Meowbits Jul 07 '12 at 02:21
  • You will, I'm sure, but don't try to guess at this stuff as it will surely lead to frustration. Reread the tutorials and walk first, then run, then fly. – Hovercraft Full Of Eels Jul 07 '12 at 02:25

1 Answers1

1

I'm sorry to tell you this, but your attempt at creating an abstraction for a rational number is wrong in every way. It is not correct at all. None of those methods are correct: add, subtract, multiply, divide, isEqual - all are utterly incorrect.

Wouldn't you want to override equals() - and hashCode()? What made you think that isEqual() was a good idea?

Look at this for an example of how to do it properly.

duffymo
  • 305,152
  • 44
  • 369
  • 561