Checksum program in java

On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
code in Java:
import java.io.*;
public class Checksum {
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter the Hex Dump:");
        String ip = br.readLine();
        String word1 = Integer.toBinaryString(Integer.parseInt(ip.substring(0,4),16));
        word1 = setLength(word1,16);
        for(int i = 4; i < ip.length(); i=i+4)
        {
            String word2 = Integer.toBinaryString(Integer.parseInt(ip.substring(i,i+4),16));
            word2 = setLength(word2,16);
            word1 = add(word1,word2);
        }
        System.out.println("The Checksum is: " + not(word1));
    }  
    static String add(String word1,String word2)
    {
        String result = "";
        char carry = '0';
        for(int i = (word1.length() - 1); i >= 0; i--)
        {
            if(word1.charAt(i) == '0' && word2.charAt(i) == '0' && carry == '0')
            {
                result = "0" + result;
                carry = '0';
            }
            else if(word1.charAt(i) == '0' && word2.charAt(i) == '0' && carry == '1')
            {
                result = "1" + result;
                carry = '0';
            }
            else if(word1.charAt(i) == '0' && word2.charAt(i) == '1' && carry == '0')
            {
                result = "1" + result;
                carry = '0';
            }
            else if(word1.charAt(i) == '0' && word2.charAt(i) == '1' && carry == '1')
            {
                result = "0" + result;
                carry = '1';
            }
            else if(word1.charAt(i) == '1' && word2.charAt(i) == '0' && carry == '0')
            {
                result = "1" + result;
                carry = '0';
            }
            else if(word1.charAt(i) == '1' && word2.charAt(i) == '0' && carry == '1')
            {
                result = "0" + result;
                carry = '1';
            }
            else if(word1.charAt(i) == '1' && word2.charAt(i) == '1' && carry == '0')
            {
                result = "0" + result;
                carry = '1';
            }
            else if(word1.charAt(i) == '1' && word2.charAt(i) == '1' && carry == '1')
            {
                result = "1" + result;
                carry = '1';
            }
        }
        if(carry == '1')
        {
            result = add(result,setLength(String.valueOf(carry),16));
        }
        return result;
    }
    
    static String setLength(String word,int n)
    {
        while(word.length() < n)
            word = "0" + word;
        return word;
    }
    
    static String not(String word)
    {
        String result = "";
        for(int i =  0; i < word.length(); i++)
        {
            if(word.charAt(i) == '0')
                result += "1";
            else
                result += "0";
        }
        return result;
    }
    
}
output:-
Please enter the Hex Dump:
45120731297186301946
The Checksum is: 1110101011010100
--------------------------------

0 comments: