In: Computer Science
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.
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