-4

I was assigned to make a rational class by java but I really don't understand what is required as below:

  1. Rational Implement a rational number class: Rational Augment your class with methods for:

    1. Initialization (Constructor): parameters are numerator and denominator as integers. You must have 3 constructors as follows:

      • No Parameters: 0 / 1
      • One Parameter (x): x / 1
      • Two Parameters (x, y): x / y
    2. float getValue(): returns the value of the number

    3. [bonus] Rational add(Rational r): adds to another rational number

All your numbers should be saved in Reduced Form

Augment your code with a driver class (that contains "main" method) that constructs two Rational numbers, get the average of the two numbers and prints it on the screen.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
ZeroPh33r
  • 1
  • 1
  • 1

1 Answers1

2

This code implements some of your requirement, but the [bonus] task, and the usage of the reduced form is missing, it is up to you to finish it.

class Rational {

    private int nominator;
    private int denominator;

    public Rational() {
        this(0, 1);
    }

    public Rational(int nominator) {
        this(nominator, 1);
    }

    public Rational(int nominator, int denominator) {
        this.nominator = nominator;
        this.denominator = denominator;
    }


    public float getValue() {
        return nominator / (float) denominator;
    }

}
meskobalazs
  • 15,741
  • 2
  • 40
  • 63