Question

In: Computer Science

Write a JAVA program called DBConvertor that has two methods. The first one converts ANY user...

  • Write a JAVA program called DBConvertor that has two methods. The first one converts ANY user input binary into its equivalent decimal number. The second method converts ANY user input decimal number into its equivalent binary.​
  • binaryToDecimal, decimalToBinary are the names for those methods.​
  • binaryToDecimal receives a binary number as a string and returns a decimal number as an integer. And vice versa for the decimalToBinary method.​

Solutions

Expert Solution

Implement the program as follows:

  1. import Scanner class
  2. create a class DBConverter
  3. define the method binaryToDecimal()
  4. initialize decimal to 0
  5. initialize n=0, and i = length of binary -1
  6. repeat until index i>= 0, iterate over each character from last to first
  7. calculate the int value of each character
  8. calculate its positional value and add to decimal value
  9. increment n and decrement i
  10. return decimal value
  11. define the method decimalToBinary()
  12. initialize binary to ""
  13. repeat until decimal>0
  14. Prepend the remainder when decimal is divided by 2 to binary
  15. divide decimal by 2
  16. return binary
  17. define main() method
  18. initialize Scanner
  19. ask the user to enter a binary value and read input
  20. convert binary to decimal using binaryToDecimal()
  21. ask the user to enter a decimal and read input
  22. convert decimal to binary using decimalToBinary()

Program: DBConverter.java

import java.util.Scanner;                                                                                           /* import Scanner class */
public class DBConverter{                                                                                               /* create a class DBConverter */
        public static int binaryToDecimal(String binary){                                       /* define the method binaryToDecimal() */
                int decimal = 0;                                                                                                /* initialize decimal to 0 */
                int n = 0, i = binary.length()-1;                                                               /* initialize n=0, and i = length of binary -1 */
                while(i>=0){                                                                                                         /* repeat until index i>= 0, iterate over each character from last to first */
                        int temp = Integer.parseInt(""+binary.charAt(i));                       /* calculate the int value of each character */
                        decimal += temp*Math.pow(2, n);                                                         /* calculate its positional value and add to decimal value */
                        n++;                                                                                                            /* increment n and decrement i */
                        i--;
                }  
                return decimal;                                                                                                 /* return decimal value */
        }
        public static String decimalToBinary(int decimal){                                      /* define the method decimalToBinary() */
                String binary = "";                                                                                     /* initialize binary to "" */
                while(decimal>0){                                                                                            /* repeat until decimal>0 */
                        binary = decimal%2 + binary;                                                            /* Prepend the remainder when decimal is divided by 2 to binary */
                        decimal = decimal/2;                                                                            /* divide decimal by 2 */
                }
                return binary;                                                                                                  /* return binary */
        }
        public static void main(String[] args){                                                         /* define main() method */
                Scanner scnr = new Scanner(System.in);                                                  /* initialize Scanner */
                System.out.print("Enter a binary number : ");                                   /* ask the user to enter a binary value and read input */
                String binary = scnr.nextLine();
                System.out.println("Decimal is " + binaryToDecimal(binary));    /* convert binary to decimal using binaryToDecimal() */
                System.out.print("Enter a decimal number : ");                                  /* ask the user to enter a decimal and read input */
                int decimal = scnr.nextInt();
                System.out.println("Binary is " + decimalToBinary(decimal));    /* convert decimal to binary using decimalToBinary() */
        }
}

Screenshot:

Output:

Please don't forget to give a Thumbs Up.


Related Solutions

Write a JAVA program called Convertor that has two methods. The first one converts an input...
Write a JAVA program called Convertor that has two methods. The first one converts an input binary into its equivalent decimal number. The second method converts a decimal number into its equivalent binary.​ binary2Decimal, decimal2Binary are the names for those methods.​ binary2Decimal receives a binary number as a string and returns a decimal number as an integer. And vice versa for the decimal2Binary method.​
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a java program that lets the user to enter any two integers and weave them...
Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of...
Java Programing Write a program called reverseProg.   This program will do the following Asks the user...
Java Programing Write a program called reverseProg.   This program will do the following Asks the user to input a string, it reads the string and does the followings Prints the string in reverse order. Displays the characters that are in position 7th, 8th and 9th of this string Displays the length of the string Displays the string in all UPPER CASE Sample output example: Enter a string: Wilmington University String Entered is: Wilmington University Wilmington University spelled backward is: ytsirevinU...
Write a java program to let the user enter the path to any directory on their...
Write a java program to let the user enter the path to any directory on their computers. Then list all the files in that directory.
Write a program in Java that first asks the user to type in today's price of...
Write a program in Java that first asks the user to type in today's price of one dollar in Japanese yen, then reads U.S. dollar values and converts each to yen. Use 0 as a sentinel to denote the end of dollar input. THEN the program reads a sequence of yen amounts and converts them to dollars. The second sequence is terminated by another zero value.
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to enter two strings. First, the program prompts: Please enter a string. The program should read in the string, and prompts: Please enter another string. The program reads in two strings and it prints a menu to the user. The program asks for user to enter an option and performs one of the following: Here are the options on the menu: Option a: checks if...
Write a Java program called Decision that includes a while loop to prompt the user to...
Write a Java program called Decision that includes a while loop to prompt the user to enter 5 marks using the JOptionPane statement and a System. Out statement to output a message inside the loop highlighting a pass mark >= 50 and <= 100. Any other mark will output a messaging stating it’s a fail.
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user...
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user inputs) as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair. For example, if an array called list stores the values {7, 2, 8, 9, 4, 15, 7, 1, 9, 10}, then the call of shrink(list) should return a new array containing {9, 17, 19, 8, 19}. The first pair from...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT