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
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
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)
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as...
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as 0x##(ex 0x0A, 0xFF) Binary Hexadecimal Decimal 0001-1011 0x 0000-1000 0000-0100 0000-1001 0001-1111 1001-1001 0111-1010 1100-0010 1110-0101 1000-1010 0011-0100 0001-1001 0100-0011 1111-1111 1110-0111 0001-0010 0100-1000 0100-1110 1001-0001 0110-1100 Name: Convert the following hexadecimal values to binary and decimal Write binary numbers as 0000-0000 Hexadecimal Binary Decimal 0xf1 0xac 0x56 0x6c 0x32 0x30 0x05 0x28 0xf0 0x07 0x42 0xb9 0x6d 0x2f 0x71 0x0e 0x2d 0xfb 0xba...
Explain the following: a bit, octet, hexadecimal, and decimal value
Explain the following: a bit, octet, hexadecimal, and decimal value
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT