Question

In: Computer Science

3. Write a java method that accepts a binary number and converts it to decimal then...

3. Write a java method that accepts a binary number and converts it to decimal then display
the result. For Example:
(110)2 = (6)10
(2
2 *1)+ (21 *1) + (20*0) = 6
Additional task: write a method that accepts a decimal and converts it to binary.

i need to solve it as soon as
and i will upvote you directly

Solutions

Expert Solution

Binary to Decimal Method:

    // function to convert binary to decimal
    static int binaryToDecimal(String bin)
    {
        String num = bin;
        int dec = 0, base = 1, len = num.length();
        for (int i = len - 1; i >= 0; i--) {
            if (num.charAt(i) == '1')
                dec += base;
            base = base * 2;
        }
        return dec;
    }

Decimal to binary Method:

    // function to convert decimal to binary
        static int decimalToBinary(int dec)  
        { 
                int bin = 0, cnt = 0;  
                while (dec != 0) 
                {  
                        int rem = dec % 2;  
                        double c = Math.pow(10, cnt);  
                        bin += rem * c;  
                        dec /= 2; 
                        cnt++;  
                }  
                return bin;  
        }

Testing above methods:

Code:

public class Main
{
    // function to convert binary to decimal
    static int binaryToDecimal(String bin)
    {
        String num = bin;
        int dec = 0, base = 1, len = num.length();
        for (int i = len - 1; i >= 0; i--) {
            if (num.charAt(i) == '1')
                dec += base;
            base = base * 2;
        }
        return dec;
    }
    // function to convert decimal to binary
        static int decimalToBinary(int dec)  
        { 
                int bin = 0, cnt = 0;  
                while (dec != 0) 
                {  
                        int rem = dec % 2;  
                        double c = Math.pow(10, cnt);  
                        bin += rem * c;  
                        dec /= 2; 
                        cnt++;  
                }  
                return bin;  
        }
        public static void main(String[] args) {
                System.out.println("Decimal Eqivalent of 110 = " + binaryToDecimal("110"));
                System.out.println("Binary Eqivalent of 6 = " + decimalToBinary(6));
        }
}

Output:

i hope it helps..

If you have any doubts please comment and please don't dislike.

PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME


Related Solutions

1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
Write a program that accepts a number of minutes and converts it both to hours and...
Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. (I currently have what is below) import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numOfMinutes = sc.nextInt(); double hour = numOfMinutes/60.00; double days = (hour/24); System.out.println(numOfMinutes + " minutes is " + hour + " hours or " + days + " days.");...
Write a program that accepts a number of minutes and converts it to days and hours....
Write a program that accepts a number of minutes and converts it to days and hours. For example, 6000 minutes represents 4 days and 4 hours. Be sure to provide proper exception handling for non-numeric values and for negative values. Save the file as  MinuteConversionWithExceptionHandling.java
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This function takes a single parameter, a non-negative integer, and returns a string corresponding to the binary representation of the given value. Your function should be named decimalToBinaryRecursive Your function should take a single argument An integer to be converted to binary Your function should not print anything Your function should use recursion instead of a loop. Note that if you try to use a...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); //...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); // converts a hexadecimal number (represented as a STRING) to decimal string dectobin(int); // converts a decimal number to binary (represted as a STRING) string dectohex(int); // converts a decimal number to hexadecimal (represted as a STRING) //the addbin and addhex functions work on UNsigned numbers string addbin(string, string); // adds two binary numbers together (represented as STRINGS) string addhex(string, string); // adds two hexadecimal...
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name...
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name the function b = deciTObina (d), where the input argument d is the number to be converted and the output argument b is a 30-element-long vector with 1s and 0s that represents the number in binary form. The first 15 elements of b store the digits to the left of the decimal point, and the last 15 elements of b store the digits to...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT