Search code examples
javaobjectprogram-entry-pointrational-number

Main Method for My Rational Code


So I have finished the code to my rational tests involving addition, subraction etc for rational numbers. I'm stuck trying to make the main class that actually outputs the code.

Here is my code for the Rational class :

package rational;

public class Rational {

    private static int gcd(int u, int v) {
        if (u == 0) {
            return v;
        } else if (v == 0) {
            return u;
        } else if (u == 1 || v == 1) {
            return 1;
        }
        int temp;
        while (v != 0) {
            temp = u % v;
            u = v;
            v = temp;
        }
        return u;
    }

    private static Rational reduce(Rational num) {
        int gcd = gcd(num.getNumerator(), num.getDenominator());
        return new Rational(num.getNumerator() / gcd, num.getDenominator() / gcd);
    }

    private static Rational neg(Rational num) {
        return new Rational(-(num.getNumerator()), num.getDenominator());
    }

    public static Rational add(Rational a, Rational b) {
        return reduce(new Rational(a.getNumerator() * b.getDenominator() + a.getDenominator() * b.getNumerator(), a.getDenominator() * b.getDenominator()));
    }

    public static Rational subtract(Rational a, Rational b) {
        return add(a, neg(b));
    }

    public static Rational multiply(Rational a, Rational b) {
        return reduce(new Rational(a.getNumerator() * b.getNumerator(), a.getDenominator() * b.getDenominator()));
    }

    public static Rational divide(Rational a, Rational b) {
        return multiply(a, new Rational(b.getDenominator(), b.getNumerator()));
    }

    public static String getString(Rational num) {
        return String.format("%d/%d", num.getNumerator(), num.getDenominator());
    }

    public static String getFloatString(Rational num) {
        return getFloatString(num, 6);
    }

    public static String getFloatString(Rational num, int fixedTo) {
        return String.format("%." + fixedTo + "f", num.getNumerator() / ((float) num.getDenominator()));
    }

    private final int numerator;
    private final int denominator;

    public Rational() {
        this(0, 1); // Note that division by zero is not allowed
    }

    public Rational(int numerator, int denominator) {
        if (denominator == 0) {
            throw new ArithmeticException("Division by zero");
        }
        this.numerator = numerator;
        this.denominator = denominator;
    }

    public int getNumerator() {
        return numerator;
    }

    public int getDenominator() {
        return denominator;
    }

}

Solution

  • First of all you if you want to make a main method in you Rational class, and call your methods:

    You can create a main method like this :

    public static void main(String[] args) {
        //Create a Rational with an empty constructor
        Rational r1 = new Rational();
        int numerator = 1, denominator = 2; 
        //Create a Rational with an the second constructor that take two int
        Rational r2 = new Rational(numerator, denominator);
        //then you can use your methods like this.
        r2.gcd(numerator, numerator);
    }
    

    Like you can call your method which take a Rational object with the same object r2 for example :

    r1.reduce(r2);
    

    or

    r2.reduce(r2);
    

    Note

    If you want to create a main method in another class then make sure to change your method to puclic

    Hope this can help you.