Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Library to reimplement Java artihmetics

Name: Anonymous 2012-07-04 10:13

Hello /prog/

I have found out that Java is not the easiest language to understand. Lets look at following example about simple arithemtic.

2*1 + 4/(6+2)

Now, there are multiple problems here to understand for non-Java-experts. For instance, what are *, /, + characters supposed to mean? What are thos weird ( and ) characters? Is the order of numbers from left to right or right to left.

We can fix this. The object system of Java is powerful enough to handle this. When we replace this weird and cryptic internal arithmetic engine with our own OOP implementation, we can reduce the learning curve of Java language. After all, now programmer only needs to learn OOP.

Here's an implementation of classes that are easy to extend by inheritance.


public interface Node {
    public abstract double calculate();
}
public class OperatorNode implements Node {
    public static final int ADD = 1;
    public static final int SUB = 2;
    public static final int MUL = 3;
    public static final int DIV = 4;
    int calculationOperator;
    Node leftNode, rightNode;

    public OperatorNode(int oper, Node left, Node right) {
        calculationOperator = oper;
        leftNode = left;
        rightNode = right;
    }
    public double calculate() {
        double left = leftNode.calculate();
        double right = rightNode.calculate();
        switch (calculationOperator) {
            case ADD: return left + right;
            case SUB: return left - right;
            case MUL: return left * right;
            case DIV: return left / right;
        }
        return 0;
    }
}
public class ConstantNode implements Node {
    double value;
    public ConstantNode(double constant) {
        value = constant;
    }
    public double calculate() {
        return value;
    }
}


Now, how do we use this, you might ask. Look at the following example:


Node calculation = new OperatorNode(
        OperatorNode.ADD,
        new OperatorNode(
            OperatorNode.MUL,
            new ConstantNode(2),
            new ConstantNode(1)),
        new OperatorNode(
            OperatorNode.DIV,
            new ConstantNode(4),
            new OperatorNode(
                OperatorNode.ADD,
                new ConstantNode(6),
                new ConstantNode(2))
            )
        )
calculation.calculate();


This effectively evaluates the 2*1 + 4/(6+2) gibberlish we inspected earlier.

Our OOP solution also removes a lot of code duplication. Consider following example:


a = 2*1 + 4/(6+2);
b = 2*1 + 4/(6+2);


Terrible, right? And there's just no way to fix that with the old system. Now, lets rewrite that with clean OOP way.

Node calculation = new OperatorNode(
        OperatorNode.ADD,
        new OperatorNode(
            OperatorNode.MUL,
            new ConstantNode(2),
            new ConstantNode(1)),
        new OperatorNode(
            OperatorNode.DIV,
            new ConstantNode(4),
            new OperatorNode(
                OperatorNode.ADD,
                new ConstantNode(6),
                new ConstantNode(2))
            )
        )
a = calculation.calcultate();
b = calculation.calcultate()
calculation.calculate();


This effectively fixes our problem, where we had to change two arithmetic sentences when we wanted both variables result to change. Now we need to only change one place when changes to code is required.

This arithmetic library is now officially open sourced, with PPL (/prog/ public licence). You can do whatever you want with it. I hope some of you could contribute some extensions to it.

Also, if anyone has other ideas, how to improve Java language, by reimplementing language features with OOP, I would be very eager to see them.

Name: Rei 2012-07-04 17:50

>>21
TUMBLING DOWN
TUMBLING DOWN
TUMBLING DOWN

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List