Java type casting

Started by
1 comment, last by Alberth 1 year ago

I'm attempting to develop a calculator app that works the way it should, as opposed to the typical ones that ask for the first and second numbers, followed by the operator.

static int initVal = 1;
static int finalVal = 1;
..
..
..

            work.screen.setText(work.screen.getText() );
            String keyPressed = Integer.toString(initVal);
            initVal = Integer.parseInt(keyPressed);

I developed mine to accept user input as it is typed and then process it. My issue is that if I want to execute a division such that either operator is a float/double, how do I set the initial type of the variables and how do I update the type to accept new variables once input differs from the initial type?

If I key in numbers that include a decimal place or my answer has a decimal place, the interface does not type cast correctly. I tried changing everything to Double instead of integer, however when I key in numbers without a decimal place, they are transformed to fractions. confused.

A solution will be greatly appreciated.

Advertisement

No idea how your calculator works, but the simplest is to store values as they are, and decide/convert when you run a computation.

So use an Integer to store the result if the user types an integer number, and a Double to store the result if they type a real number. Both kinds of values fit in an Object, eg

Object left = Integer.parseInt("123");
Object right = Double.parseDouble("123.456");  // Didn't check it exists but likely it does.

When you must do a division, you can check the type of “left” and “right” and act accordingly. For example:

if (left instanceof Integer left_int) {
    if (right instanceof Integer right_int) {
    	// Compute left_int / right_int
    } else if (right instanceof Double right_real) {
    	// Compute left_int / right_real
    } else {
        // Throw error as 'right' has an unexpected type.
    }
} else if (left instanceof Double left_real) {
    if (right instanceof Integer right_int) {
    	// Compute left_real / right_int
    } else if (right instanceof Double right_real) {
    	// Compute left_real / right_real
    } else {
        // Throw error as 'right' has an unexpected type.
    }
} else {
    // Throw error as 'left' has an unexpected type.
}

While this is simple, Object can be used to fit almost anything, so it's not very type safe in this way.

As an alternative you can make a Value base class, and derive a RealValue and IntValue from it to store your values and query the stored data inside.

With these classes you could do something like

Value left = new IntValue(123);
Value right = new RealValue(123.345);
Value result;
if (left.isInt() && right.isInt()) {
	result = new IntValue(left.getValueAsInt() / right.getValueAsInt());
} else { // At least one is a real value, perform a real / real division.
	result = new RealValue(left.getValueAsReal() / right.getValueAsReal());
}

This topic is closed to new replies.

Advertisement