Results 1 to 6 of 6
  1. #1
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default help in describe code

    hi, can you tell me what this code do?
    is from binary add
    Java Code:
    static String [] lookupTable = {
            "0+0+0=00",
            "0+0+1=01",
            "0+1+0=01", 
            "0+1+1=10",
            "1+0+0=01",
            "1+0+1=10",
            "1+1+0=10",
            "1+1+1=11",
    };
    
    
    static String lookup(char s1, char s2, char c) {
            String formula = String.format("%c+%c+%c=", s1, s2, c);
            for (String s : lookupTable) {
                if (s.startsWith(formula)) {
                    return s.substring(s.indexOf("=") + 1);
                }
            }
            throw new IllegalArgumentException();
        }
        static String zeroPad(String s, int length) {
            while (s.length() < length) {
                s = "0" + s;
            }
            return s;
        }   
        static String add(String s1, String s2) {
            int length = Math.max(s1.length(), s2.length());
            s1 = zeroPad(s1, length);
            s2 = zeroPad(s2, length);
            String result = "";
            char carry = '0';
            for (int i = length - 1; i >= 0; i--) {
                String columnResult = lookup(s1.charAt(i), s2.charAt(i), carry);
                result = columnResult.charAt(1) + result;
                carry = columnResult.charAt(0);
            }
            
            if (carry == '1') {
                result = carry + result;
            }
            return result;
        }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: help in describe code

    What happened when you ran it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,363
    Blog Entries
    7
    Rep Power
    20

    Default Re: help in describe code

    Why did you ignore what I wrote in this thead: binary addition and Substraction?

    kind regards,

    Jos
    Last edited by JosAH; 10-21-2011 at 06:11 PM. Reason: quoted the wrong person ...
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: help in describe code

    because i am noob , and i cant do it, so i got this one, and i know how to put to subtration too, but why i can put with the options , with sum and sub?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: help in describe code

    Quote Originally Posted by m1ke4fun View Post
    because i am noob , and i cant do it, so i got this one, and i know how to put to subtration too, but why i can put with the options , with sum and sub?
    Please see the link in my signature on asking questions the smart way.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    m1ke4fun is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  2. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Replies: 2
    Last Post: 04-01-2010, 12:55 PM
  5. [SOLVED] Please describe this code
    By ezee in forum New To Java
    Replies: 12
    Last Post: 03-18-2009, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •