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.
And there's just no way to fix that with the old system.
What if you did b = a?
Name:
American2012-07-04 11:29
*claps*
Name:
Anonymous2012-07-04 11:35
>>4
Agreed, but the following things are also shit:
-virtual memory allocation
-context switching
-serial processing
-electricity
-time
-concepts built from noise caused by the suboptimality other concepts (call this unavoidable. I say that it is possible to avoid by avoiding concepts that cause noise.)
-time-dependent concepts
-the 3 laws of thought
In general, the way the computers we nowadays use process information is similar to the brain of a primitive animal, except that would be an insult to the primitive animals since all the useful stuff is picked out and only the noise is left.
It goes like this:
-hurr I build a system to measure something
-durr I programmed a bug in the system
-derp the bug is funny so let's call it a feature
after a while the system is so full of concepts built on concepts that it does not even measure what originally was meant to be measured, but the noise between the concepts. That does not stop us from pretending the program is working just fine! Why not? Because we are so full of ourselves we can't see the all the "interesting phenomena" are what we originally programmed and then forgot, and not phenomena of "nature" or "life" itself! It's like building a house out of shit! And you must also build your house from shit because everyone else does it too! Our reasoning is, after all, shaped by our experiences!
And thanks to this we will never have quantum computers. Look at what happened with the QWERTY keyboard and you will understand why you will never get the x86 instructions out of your stream of consciousness.
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.
for (int i=0; i < 10; i++) {
System.out.println("fizz buzz");
}
class For {
private Declaration decl;
private TerminatingCondition cond;
private IterationStatement iter;
public For(decl, cond, count) {
this.decl = decl;
this.cond = cond;
this.count = count;
}
public void execute() {
decl.execute();
while (cond.evaluate()) {
iter.execute();
}
}
}
interface Declaration {
public abstract void execute();
}
interface TerminatingCondition {
public abstract boolean evaluate();
}
interface IterationStatement {
public abstract void execute();
}
/*
* Wrapper array to permit access to the counter variable from anonymous inner class
* methods Declaration.execute() and Condition.evaluate().
*/
final int[] i = new int[1];
For f0r = new For(
new Declaration() {
public void execute() {
i[0] = 0;
}
},
new Condition() {
public boolean evaluate() {
return i[0] < 10;
}
},
new IterationStatement() {
public void execute() {
System.out.println("fizz buzz");
}
});
f0r.execute();
It took me over an hour to realize it is a joke, and I still am not 100% sure if it is a joke. Does that make me retarded or enlightened?
Needs more eval, though.
Name:
Anonymous2012-07-04 13:47
>>10
you don't increase i anywhere, you are using an array instead of ArrayList i would suggest:
class For {
private IterationStatement iter;
private IterationObject var;
public For(var, iter) {
this.var = var;
this.iter = iter;
}
public void execute() {
var.setup();
while (var.evaluateCondition()) {
iter.execute();
var.iterate();
}
}
}
interface IterationObject{
public abstract void setup();
public abstract boolean evaluate();
public abstract void iterate();
}
interface IterationStatement {
public abstract void execute();
}
Class MyFor implements IterationClass{
private Integer i;
MyFor(){
setup();
}
public void setup(){
i = 0;
}
public evaluate(){
i < 10;
}
public iterate(){
i = i + 1;
}
}
For f0r = new For(
new MyFor(),
new IterationStatement() {
public void execute() {
System.out.println("fizz buzz");
}
});
f0r.execute();
I System.out.println((new InternetAbbreviation(new InternetWord(Util.Internet.LOL.FirstWord), new InternetWord(Util.Internet.LOL.SecondWord), new InternetWord(Util.Internet.LOL.ThirdWord))).toString())'d
Name:
Anonymous2012-07-04 14:59
>>17
Oh, another idiot who needs syntax highlighting because he is dependent on an IDE, learn to program scum
Name:
Anonymous2012-07-04 16:54
Library to reimplement Java artihmetics reimplement rei
I have updated the OperatorNode class. It now supports exponent operators.
Here's the code:
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;
public static final int POW = 5;
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;
case POW: return Math.pow(left, right);
}
return 0;
}
}
new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new new
After many days of coding, I finally managed to make useful contribution to your library.
It's a class that gives constant value of PI to you.
Here's the code:
class PiNode extends ConstantNode {
public PiNode() {
value = 3.14;
}
}
Name:
Anonymous2012-07-11 13:57
>>35
OP here,
you need to document your code before I can accept it to my library. Please post here when you have done so.
Name:
Anonymous2012-07-11 14:05
>>35
I have improve your code: class PiNode extends ConstantNode {
public PiNode() {
value = 3.14;
}
}
class PiNodeFactory {
public PiNode node;
public PiNodeFactory() {
node = new PiNode();
}
}
See how I used a factory class to create the object automatically rather than using its constructor. This is more robust and scalable, perfect for enterprise modules.
Name:
Anonymous2012-07-11 14:33
>>37
OP here
You forgot to document your factory code.
>>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:
Anonymous2012-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