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: Anonymous 2012-07-11 23:00

This is why God created operator overloading.

Name: Anonymous 2012-07-11 23:13

>>41
fuck off, terry

Name: Anonymous 2012-07-12 2:14

>>40
lamb duh

Name: Anonymous 2012-07-12 2:17

DRINK THE BLOOD OF THE LAMBDA

Name: Anonymous 2012-07-12 10:25

>>36
Ok, I documented the code
[code]
class // declare class
PiNode // named PiNode
extends // that extends
ConstantNode // class named ConstantNode
{ // begin block
public // declare public identifier
PiNode // named  PiNode
( // begin argument list
) // end argument list
{ // begin block
value = // set value of "value"
3.14 // to 3.14
; // end statement
} // end block
} // end block
[code]

Name: Anonymous 2012-07-12 10:26

Sorry mister, forgot to close code tags

class // declare class
 PiNode // named PiNode
 extends // that extends
 ConstantNode // class named ConstantNode
 { // begin block
 public // declare public identifier
 PiNode // named  PiNode
 ( // begin argument list
 ) // end argument list
 { // begin block
 value = // set value of "value"
 3.14 // to 3.14
 ; // end statement
 } // end block
 } // end block

Name: Anonymous 2012-07-16 13:38

Name: Anonymous 2012-07-17 16:52

Our thinking is influenced by the majority of our experience.
This is the deepest thing I have heard, other than "even in heaven, Satan is present".

What I am trying to say, that we might actually need to code shittily, to have our code evolve naturally.

Name: Anonymous 2012-07-17 21:40

>>21
that was lame as fuck

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