// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class NumberFun {
    public static void main(String[] args) {
        Number n1 = new RationalNumber(1, 2);
        Number n2 = new RationalNumber(1, 3);
        Number n3 = new IntegerNumber(3);
        System.out.println(n1.times(n2));
        System.out.println(n1.times(n3));
    }
}

abstract class Number {
    public abstract Number times(Number other);
    public abstract double asDouble();
    public abstract String toString();
    public boolean hasSpecialRational() { return false; };
    public boolean hasSpecialInteger() { return false; };
}

class FloatNumber extends Number {
    private double value;
    public FloatNumber(double value) {
        this.value = value;
    }
    public Number times(Number other) {
        return new FloatNumber(value * other.asDouble());
    }
    public double asDouble() {
        return value;
    }
    public String toString() {
        return "[FloatNumber: " + value + "]";
    }
}

class IntegerNumber extends Number {
    private int value;
    public IntegerNumber(int value) {
        this.value = value;
    }
    public Number times(IntegerNumber other) {
        return new IntegerNumber(value * other.value);
    }
    public Number times(Number other) {
        //if (other instanceof IntegerNumber) {
        if(other.hasSpecialInteger()) {
            return times((IntegerNumber)other);
        } else {
            return new FloatNumber(this.asDouble() * other.asDouble());
        }
    }
    public double asDouble() {
        return value;
    }
    public String toString() {
        return "[IntegerNumber: " + value + "]";
    }
    public boolean hasSpecialInteger() {
        return true;
    }
}

class RationalNumber extends Number {
    private int num;
    private int den;
    public RationalNumber(int num, int den) {
        this.num = num;
        this.den = den;
    }
    public Number times(IntegerNumber other) {
        return new RationalNumber(num*(int)other.asDouble(), den);
    }
    public Number times(RationalNumber other) {
        return new RationalNumber(num*other.num, den*other.den);
    }
    public Number times(Number other) {
        //if (other instanceof RationalNumber) {
        if(other.hasSpecialRational()) {
            return times((RationalNumber)other);
        //} else if (other instanceof IntegerNumber) {
        } else if(other.hasSpecialInteger()) {
                return times((IntegerNumber)other);
        } else {
            return new FloatNumber(this.asDouble() * other.asDouble());
        }
    }
    public double asDouble() {
        return num/(double)den;
    }
    public String toString() {
        return "[RationalNumber: " + num + "/" + den + "]";
    }
    public boolean hasSpecialRational() {
        return true;
    }
    public boolean hasSpecialInteger() {
        return true;
    }
}
