Đăng Khoa Huỳnh
Đăng Khoa Huỳnh

Reputation: 130

How to cast a Fraction object to Double object?

When I'm studying Android, I can cast an object type to other type, like this:

TextView txtHello = (TextView) view;

where view is defined as View, a reason, I think, TextView is a subclass of View. Now, I have a Fraction class, here is its content:

public class Fraction {

    private int numerator;
    private int denominator;

    public Fraction() {
        numerator = 0;
        denominator = 1;
    }

    public Fraction(int value) {
        numerator = value;
        denominator = 1;
    }

    public Fraction(int numerator, int denominator) throws ArithmeticException {
        if (denominator == 0) {
            throw new ArithmeticException("Denominator can not be 0!");
        }
        if (denominator < 0) {
             denominator *= -1;
             numerator *= -1;
        }
        this.numerator = numerator;
        this.denominator = denominator;
        this.simplify();
    }

    public double toDouble() {
        return (double) numerator / (double) denominator;
    }

    @Override
    public String toString() {
        return String.format("%d/%d", numerator, denominator);
    }

    public int getNumerator() {
        return numerator;
    }

    public int getDenominator() {
        return denominator;
    }

    private void simplify() {
        int x = getGreatestCommonDivisor(numerator, denominator);
        numerator /= x;
        denominator /= x;
    }

    private int getGreatestCommonDivisor(int x, int y) {
        x = x < 0 ? -x : x;
        y = y < 0 ? -y : y;
        if (x == 0 || y == 0) {
            return x + y;
        }
        while (x != y) {
            if (x > y) {
                x = x - y;
            } else {
                y = y - x;
            }
        }
        return x;
    }
}

And when I create a new Fraction:

Fraction frac = new Fraction(2, 5);

Both two line are errors:

Double d1 = (Double) frac;
double d2 = (double) frac;

I know that I can convert a Fraction to double by call toDouble method. But I want to know how to do such as what I know in Android. I try to make Fraction extends Double but Double is a final class. If I create a new class as Double, how can I do any line code above and d1 and d2 is really value of frac?

Upvotes: 1

Views: 1146

Answers (2)

Andy Turner
Andy Turner

Reputation: 140514

You can't cast as they are unrelated types.

With reference types (like Fraction and Double), casting doesn't actually build a new instance of the casted-to type - (Double) frac simply says to the compiler "trust me, I know that this Fraction is really a Double, let me use it as one".

However, the compiler knows that Double isn't in the inheritance hierarchy of Fraction, so it knows that it is impossible for a reference to a Fraction to really be a Double, so it forbids the cast.

But you have a method to allow conversion of Fraction to double:

double d2 = frac.toDouble();

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533820

If I create a new class as Double, how can I do any line code above and d1 and d2 is really value of frac?

In Java you can't do this without hacking the core libraries, something you shouldn't do.

What you can do is extend Number which is the parent of Double.

Number num = new Fraction(2, 3); // if Fraction extends Number
Number d = 1.33;

Upvotes: 1

Related Questions