Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Battle of the Titans!
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Sat Dec 16, 2006 7:36 pm   Post subject: Battle of the Titans!

Java vs. C#. The beloved children of two 800 pound gorillas go head to head. Read the code, then fight it out over which is more appealing. Smile

C#

code:
using System;
using System.Collections;

public class Test
{
        public static void Main()
        {        
                Range r = new ExclusiveRange(1, 5);
          
                foreach (int i in r) {
                        Console.WriteLine(i);
                }
          
                int sum = r.Reduce(Add, 0);
                Console.WriteLine("Sum: {0}", sum);
        }
   
        static int Add(int a, int b)
        {
                return a + b;
        }
}

class Range : IEnumerable
{
        class Enumerator : IEnumerator
        {
                private Range range;
                private int current;
          
                public Enumerator(Range range)
                {
                        this.range = range;
                        this.current = range.Start - 1;
                }
          
                public Range TargetRange {
                        get { return range; }
                }
          
                public object Current {
                        get { return current; }
                }
          
                public bool MoveNext()
                {
                        if (current >= range.EffectiveEnd) {
                                return false;
                        }
                        else {
                                current++;
                                return true;
                        }
                }
          
                public void Reset()
                {
                        current = range.Start - 1;
                }
        }
   
        public delegate T ReductionArg<T>(T init, int current);

        private int start, end;
   
        public Range(int start, int end)
        {
                this.start = start;
                this.end = end;
        }
   
        public IEnumerator GetEnumerator()
        {
                return new Enumerator(this);
        }
   
        public virtual int Start {
                get { return start; }
        }
   
        public virtual int End {
                get { return end; } 
        }
   
        public virtual int EffectiveEnd {
                get { return end; }
        }
   
        public virtual T Reduce<T>(ReductionArg<T> f, T init)
        {
                foreach (int i in this) {
                        init = f(init, i);
                }
                               
                return init;
        }
}

class ExclusiveRange : Range
{
        public ExclusiveRange(int start, int end) : base(start, end)
        {
        }
   
        public override int EffectiveEnd {
                get { return End - 1; }
        }
}


Java

code:
public class RangeTest {
        public static void main(String[] args) {
                Range r = new ExclusiveRange(1, 5);
                               
                for (int i : r) {
                        System.out.println(i);
                }
                               
                int sum = r.reduce(add, 0);
                System.out.printf("Sum: %d\n", sum);
        }
               
        static Range.ReductionArg<Integer> add = new Range.ReductionArg<Integer>() {
                public Integer perform(Integer init, int current) {
                        return init + current;
                }
        };
}

class Range implements Iterable<Integer> {
        static class Iterator implements java.util.Iterator<Integer> {
                private Range range;
                private int current;
               
                public Iterator(Range range) {
                        this.range = range;
                        this.current = range.getStart() - 1;
                }
                               
                public boolean hasNext() {
                        return current < range.getEffectiveEnd();
                }
                               
                public Integer next() {
                        return ++current;
                }
                               
                public void remove() {
                }
        }
               
        public static interface ReductionArg<T> {
                public T perform(T init, int current);
        }

        private int start, end;
   
        public Range(int start, int end) {
                this.start = start;
                this.end = end;
        }
               
        public java.util.Iterator<Integer> iterator() {
                return new Iterator(this);
        }
               
        public int getStart() {
                return start;
        }
               
        public int getEnd() {
                return end;
        }
               
        public int getEffectiveEnd() {
                return end;
        }
               
        public <T> T reduce(ReductionArg<T> f, T init) {
                for (int i : this) {
                        init = f.perform(init, i);
                }
                               
                return init;
        }
}

class ExclusiveRange extends Range {
        public ExclusiveRange(int start, int end) {
                super(start, end);
        }
               
        public int getEffectiveEnd() {
                return getEnd() - 1;
        }
}
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Sat Dec 16, 2006 7:42 pm   Post subject: (No subject)

I would have to argue that the C# code wins here. To me it just seems more appealing. Especially when it comes to something like this:

Java:

static Range.ReductionArg<Integer> add = new Range.ReductionArg<Integer>() {
                public Integer perform(Integer init, int current) {
                        return init + current;
                }
        };


compared to:

C#:

static int Add(int a, int b)
        {
                return a + b;
        }
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: