0

At the moment I have a class Fraction that allows me to create Fractions in three different ways

  • With one Integer, in this case the given Integer will be the numerator and the denominator will be set to 1
  • With 2 Integers, the numerator and the denominator
  • The Last method would be to parse a String that has to match the REGEX -?\d+/[1-9]\d*

The generated Fractions will be reduced as far as possible with the gcd.

public class Fraction extends Number {

        public static final String REGEX = "-?\d+/[1-9]\d*";


        public static int gcd(int a, int b) {
          return b == 0 ? a : gcd(b, a % b);

        }


        public static Fraction parseFraction(String s) {
           if (!s.matches(REGEX)) {
              throw new RuntimeException("Parsing error");
           }
           String[] splitted = s.split("/");
           return new Fraction(Integer.parseInt(splitted[0]),
                   Integer.parseInt(splitted[1]));
        }

        private int numerator;

        private int denominator;


        public Fraction(int numerator) {
           this(numerator, 1);
        }


        public Fraction(int numerator, int denominator) {
           if (denominator == 0) {
              throw new RuntimeException("denominator == 0 is not  possible");
        }


       int gcd = Fraction.gcd(numerator, denominator);

       if (denominator / gcd < 0) {
         gcd *= -1;
       }

       this.numerator = numerator / gcd;

       this.denominator = denominator / gcd;
       }
     }

What I want to implement now is that Instances of Fraction with same numerator and denominator have the same Reference e.g.

 *Fraction.parseFraction("1/2").equals(Fraction.parseFraction("2/4"))*

should return true.

I looked into some chapters about Generics and Bounds, but im unsure if that is even the right direction of what I need. Should the Fraction class be bounded to String and Integer?

5
  • 3
    Generally you don’t need ==. You should aim for fractions to be .equal to each other instead. Commented Jun 5, 2018 at 20:57
  • Did you mean REGEX -?\d+/[1-9]\d*, with \ instead of //?
    – Andreas
    Commented Jun 5, 2018 at 20:58
  • 1
    Should the Fraction class be bounded to String and Integer? No. Generics are a compile time type safety system and will not help you with what is a creational problem. You need to ensure that you return a single reference; part one is almost certainly making the constructors private (or package private), and using a creational pattern. You'll need to populate (and maintain) a pool of references. Commented Jun 5, 2018 at 20:59
  • Yeah, -?\d+/[1-9]\d* is what i meant to do
    – Trikalium
    Commented Jun 5, 2018 at 20:59
  • @Trikalium Then edit the question and fix the regex.
    – Andreas
    Commented Jun 5, 2018 at 21:01

1 Answer 1

0

What I want to implement now is that Instances of Fraction with same numerator and denominator have the same Reference e.g.

I think you mean with the same value here?

You're going to run into issues calculating that value with ints (and also calculating with floats or doubles) -- figuring out why those things are difficult, and how you can mitigate them, will teach you a lot.

Read up on what String.intern() does (from e.g., this is just the first Google result: https://dzone.com/articles/string-interning-what-why-and), and do the same thing, but for your Fraction class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.