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 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 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)
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...
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.
(1) Covert a Hexadecimal to decimal (there is a video for you to watch too). Then...
(1) Covert a Hexadecimal to decimal (there is a video for you to watch too). Then write a program that can convert a binary to decimal (only for integer case). a) Directly use Java built-in method to do this. In this case, only couple lines of code. Hint: Study Integer class in Java. b) Write your own code to convert a binary to decimal from scratch. The input is a binary string. The program output is its corresponding decimal value....
Covert a Hexadecimal to decimal (there is a video for you to watch too). Then write...
Covert a Hexadecimal to decimal (there is a video for you to watch too). Then write a program that can convert a binary to decimal (only for integer case). There are two ways to do so. Easy way: Directly use Java built-in method to do this. In this case, only couple lines of code. Hint: Study Integer class in Java. Hard way: Write your own code to convert a binary to decimal from scratch. The input is a binary string....
Covert a Hexadecimal to decimal (there is a video for you to watch too). Then write...
Covert a Hexadecimal to decimal (there is a video for you to watch too). Then write a program that can convert a binary to decimal (only for integer case). There are two ways to do so. Easy way: Directly use Java built-in method to do this. In this case, only couple lines of code. Hint: Study Integer class in Java. Hard way: Write your own code to convert a binary to decimal from scratch. The input is a binary string....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT