Question

In: Computer Science

Program that does conversions (i.e. decimal to hexadecimal, binary to decimal) but needs to have a...

Program that does conversions (i.e. decimal to hexadecimal, binary to decimal) but needs to have a drop down menu to give you the option to choose with conversion you want to go from and convert into (JOptionPane). Cannot use the conversion tool that is built-in Java. Need to use equations or some sort. Please include comments on what is happening to understand.

Solutions

Expert Solution

Code:-

import javax.swing.JOptionPane;
import java.lang.*;
import java.util.*;
class hai {
public static void main(String[] a) {
String[] types = { "Decimal","Hexadecimal","Binary" }; //string array to store conversion types
String input1 = (String) JOptionPane.showInputDialog(null, "conversion from","Conversion", JOptionPane.QUESTION_MESSAGE, null,types,types[2]); //dropdown for taking first input
String input2 = (String) JOptionPane.showInputDialog(null, "conversion into","Conversion", JOptionPane.QUESTION_MESSAGE, null,types,types[2]); //drop down for taking second input
System.out.println(input1 +" to "+ input2 +" conversion"); //printing
   Scanner scan=new Scanner(System.in); //scanner to take input from user
   if(input1.equals("Binary") && input2.equals("Decimal")){ //Binary to Decimal Conversion
       System.out.print("Enter Binary Number you want to convert: "); //asking user to enter value
       int value=scan.nextInt(); //taking value from user
       int k=value; //storing value in k for reusing purpose
       int dec=0,i=0; //declaring variables as 0
       while(value!=0){ //loop until value is 0
           int rem=value%10; //take ramainder value and store it in rem
            value=value/10; //take quotient value and decrement it
           dec=dec+(int)( rem * (Math.pow(2,i))); //calculating decimal value
           ++i; //incrementing i
       }
       System.out.print("The decimal number Equivalent to "+ k+" is "+dec); //printing
   }
   else if(input1.equals("Decimal") && input2.equals("Hexadecimal")){ //Decimal to HexaDecimal conversion
       System.out.print("Enter Decimal Number you want to convert: "); //asking user to enter value
       int value=scan.nextInt(); //taking value from user
       char[] hexaarray = new char[100]; //character array to store each digit of hexa decimal value
   int i = 0; //declaring i as 0
       int k=value; //storing value in k for reusing purpose
   while(value!=0){ //loop until value is 0
   int hex = 0;   
   hex = value % 16; //take remainder value when we divide value with 16
   if(hex < 10){ //if hex is less than
   hexaarray[i] = (char)(hex + 48); //appending normal integer
   i++; //incrementing i
       }
   else{ //if hex is greater than1 10
   hexaarray[i] = (char)(hex + 55); //appending appropriate character in the place digit
   i++; //incrementing i
   }
   value = value/16;    //decrement to quotient of value
   }
       System.out.print("The hexadecimal value for " +k +" is "); //printing
   for(int j=i-1; j>=0; j--)
   System.out.print(hexaarray[j]); //printing array elements
       }

   else if(input1.equals("Decimal") && input2.equals("Binary")){ //Decimal to Binary conversion
       System.out.print("Enter Decimal Number you want to convert: "); //asking user to enter value
       int value=scan.nextInt(); //taking input from user
       int k=value; //storing vlaue in k for reusing purpose
       int binary=0; //declaring binary integer with 0
       while(value!=0){ //loop until value is 0
           int rem=value%2; //take remainder
           value=value/2; //take quotient
           binary = (binary*10)+rem; //calculating binary value
       }          
       System.out.print("The Binary value for " +k +" is "+binary); //printing
   }
   else
       System.out.print("Invalid Input"); //if input is not valid
   }
}

Output1:-

Output2:-

Any Doubts please comment


Related Solutions

Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are not allowed to use library functions for conversion. The output should look exactly as follows: DECIMAL      BINARY                     HEXDECIMAL                      BCD 0                      0000 0000                   00                                            0000 0000 0000 1                      0000 0001                   01                                            0000 0000 0001 2                      0000 0010                   02                                            0000 0000 0010 .                       .                                   .                                               . .                       .                                   .                                               . 255                  1111 1111                   FF                                            0010 0101 0101
Convert from hexadecimal to binary to decimal (PLEASE SHOW WORK) 1. B2 - binary: - decimal:...
Convert from hexadecimal to binary to decimal (PLEASE SHOW WORK) 1. B2 - binary: - decimal: 2. 37 - binary: - decimal: 3. 0A -binary: - decimal: 4. 11 - binary: - decimal:
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal....
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal. 1. Request input data type. 2. Request input data. 3. Request output data type. 4. Output the data. Use any algorithm
Convert a list of decimal numbers into their binary and hexadecimal equivalents Add the elements of...
Convert a list of decimal numbers into their binary and hexadecimal equivalents Add the elements of each of these lists to generate a total sum Print the lists, and the total sum of each value C++ contains some built-in functions (such as itoa and std::hex) which make this assignment trivial. You may NOT use these in your programs. You code must perform the conversion through your own algorithm. The input values are: 5 9 24 2 39 83 60 8...
Convert from binary to decimal to hexadecimal (PLEASE SHOW ALL WORK) a. 1010 1011 - decimal:...
Convert from binary to decimal to hexadecimal (PLEASE SHOW ALL WORK) a. 1010 1011 - decimal: - hexadecimal b. 0011 0001 - decimal: - hexadecimal: c. 1110 0111 - decimal: - hexadecimal: d. 1111 1111 - decimal: - hexadecimal:
Any integer can be represented in binary, octal, decimal, and hexadecimal. What are the limits on...
Any integer can be represented in binary, octal, decimal, and hexadecimal. What are the limits on representing integers using unary or only having the 1 digit?
write a python code to Determine the binary and decimal representations of the following hexadecimal numbers....
write a python code to Determine the binary and decimal representations of the following hexadecimal numbers. Store answers in the appropriate variables in the .py file. (a) 0xfca1 (b) 0xc4d8
Convert 110.7510 to binary ______ and hexadecimal ______. Show the answer in both binary and hexadecimal....
Convert 110.7510 to binary ______ and hexadecimal ______. Show the answer in both binary and hexadecimal. There are ____________ kilobytes in a megabyte. Convert -13210 to a 16-bit 2’s complement in binary ____________ and hexadecimal ______________.
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.
With a detailed step-by-step process, convert the following decimal number into binary, Hexadecimal and IEEE 754...
With a detailed step-by-step process, convert the following decimal number into binary, Hexadecimal and IEEE 754 formats : 72.nn ( where nn is 80)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT