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

Pages: 1-

Adding one plus one in idiomatic java.

Name: Anonymous 2010-04-10 4:50


abstract class Number{
   private Integer m_number;
   Number(Integer m_number){
      this.m_number=m_number;
   }
   public Integer getInteger(){
      return m_number;
   }
   Number add(Number a,Number b){
     return new Number(new Integer(a.getInteger().intValue()+
                                   b.getInteger().intValue()));
   }
   String toString(){
       return getInteger().toString();
   }
}
class One extends Number{
  static Number cached_one=new Number(Integer.parseInt(BigInteger.ONE.toString())); //cached for efficiency  
  static Number createNumberOne(){
     return cached_one;
  }
}
class Main{
   public static void main(String[] args){
     Number result=One.createNumberOne().add(One.createNumberOne());
     System.out.println(result);  //result.toString() is invoked here
   }
}

Name: Anonymous 2010-04-10 4:58

Needs more interfaces.

Name: Anonymous 2010-04-10 5:31

This is no different from when people make phallus jokes when playing pool/billiards.

Name: official /prog/ fixer of code 2010-04-10 7:06

Hello. I fixed your code.
[code]
/*
 *  Enterprise quality implementation of one plus one
 *  Copyright (C) 2010  /prog/ <http://dis.4chan.org/prog/>;
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License v2
 *  (version 2) as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, refer to the following URL:
 *  http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */
package prog;

import java.math.BigInteger;

class Number {
    private Integer m_number;

    Number(Integer m_number) {
        this.m_number = m_number;
    }

    Number() {
        this.m_number = 0;
    }

    Integer getInteger() {
        return m_number;
    }

    Number add(Number a) {
        return new Number(new Integer(a.getInteger().intValue()
                                      + getInteger().intValue()));
    }

    @Override
    public String toString() {
        return getInteger().toString();
    }
}

class One extends Number {
    static Number cached_one = new Number(Integer.parseInt(BigInteger.ONE.toString())); //cached for efficiency

    static Number createNumberOne() {
        return cached_one;
    }
}

public class Main {
    public static void main(String[] args) {
        Number result = One.createNumberOne().add(One.createNumberOne());
        System.out.println(result);  //result.toString() is invoked here
    }
}

Name: Anonymous 2010-04-10 7:10

>>4
You should have licensed it under the Goatse Public License.

Name: Anonymous 2010-04-10 12:03

I've never really liked the Java primitive wrapper classes.  BigDecimal is pretty much the only worthwhile one and, even then, there already are the NumberFormat and DecimalFormat classes.

Name: Anonymous 2010-04-10 12:34

Just use .NET, where ints are Objects.

Name: Anonymous 2010-04-10 12:36

Just use LISP, where my other car is a cdr.

Name: Anonymous 2010-04-10 14:02

Just use Perl, where anything can be valid code

Name: Anonymous 2010-04-10 15:10

Just use ActionScript, where Just kidding! Hahaha!

Name: Anonymous 2010-04-10 15:13

>>9
Are there any languages with absolutely no invalid syntax?
I assume it's possible, it would just be highly context-sensitive.

Name: Anonymous 2010-04-10 15:28

>>11
I imagine you could get away with it by having a language that makes everything that isn't bound to a symbol a regex or a string or the identity function or something.

Name: Anonymous 2010-04-10 15:44

>>11
APL

Name: Anonymous 2010-04-10 15:45

>>11
Someone mentioned Jot recently: http://esolangs.org/wiki/Jot

I guess it's normally ASCII source coded so it wouldn't really qualify but there are no invalid sequences of syntactically meaningful characters. You could also treat other characters as comments (as is common in Brainfuck) or just use binary source coding to achieve the desired effect.

Name: >>12 2010-04-10 15:50

>>11
While my initial thought was to rebind the #%top macro in PLT, I realise that languages like Brainfuck would meet your criteria as they ignore all characters all the characters that don't have a predefined meaning.

Name: >>15 2010-04-10 15:51

I should've refreshed as >>14 covered that W-|

Name: Anonymous 2010-04-10 16:05

>>11
I've thought of one. Every character is a comment, and every program does nothing. I'll write a compiler for it soon.

Name: Anonymous 2010-04-10 16:14

>>17
How about an interpreted language where every program is a quine? The interpreter can be called cat. Actually I'm pretty sure this is on the Esolang wiki.

Name: Anonymous 2010-04-10 16:42

>>10
Go cops
Get the dogs
Let's go fuck with old folks
Sure is nice being white
Just kidding, black guys!

Name: Anonymous 2010-04-10 16:48

>>16
I was going to properly suggest Brainfuck, but unmatched brackets could be considered syntactically invalid.

The thing about Brainfuck is that it really doesn't have a concept of syntax1 (apart from which characters are valid--if you want to think of that as syntax) so to be supremely pedantic about it, you're right--unmatched brackets are semantically invalid. (This hinges on the way the ops are described.)

I like what I did to Jot better for this because there is also no semantically invalid program, so even when you invent a syntax to describe what's going on the new grammar doesn't have any problems either.

_________________________________________________
1: See commentary on Jot about inventing syntax.

Name: Anonymous 2010-04-10 17:08

>>4
Thanks, originally Number was abstract because I also wanted to implement the Two class and then do some if/switch statement logic in add to find the result.  Also ideally Number would implement the "Addable" interface.  I got lazy though :(.

Name: Anonymous 2010-04-10 17:39

>>21
If you are going to do "idiomatic" Java, you should use polymorphism instead of if/switch

Name: Anonymous 2010-04-10 18:15

>>22
addition does not lend itself easily to polymorphism

Name: Anonymous 2010-04-10 22:17

>>23
everything lends itself to OOP

NO EXCEPTIONS

Name: Anonymous 2010-04-10 22:36

NO EXCEPTIONS
But this is Java.  Everything throws Exceptions.

Name: Anonymous 2010-04-10 22:52

>>25
Man you should check out Python.

Name: barbour mens classic duffle 2011-12-01 22:40

There are many brands of <a href="http://www.barbourjackets-uk.org/"><strong>barbour fusilier</strong></a> in the market today. Each of the brand promises to bring out something new to the customers.

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