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

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: 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
    }
}

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