Question

In java Write a program that asks the user to choose a selection (Enter 1 to...

In java Write a program that asks the user to choose a selection (Enter 1 to convert from binary to decimal, 2 to convert from decimal to binary, 3 to convert binary to hexadecimal, 4 to convert hexadecimal to binary, 5 to convert decimal to hexadecimal, 6 to convert hexadecimal to decimal. Pls do it by creating classes and objects with get and set method.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code To Copy:

import java.util.Scanner;
import java.io.*;

public class HelloWorld{

public static void main(String []args){
System.out.println("Enter 1 to convert from binary to decimal");
System.out.println("Enter 2 to convert from decimal to binary");
System.out.println("Enter 3 to convert from binary to hexadecimal");
System.out.println("Enter 4 to convert from hexadecimal to binary");
System.out.println("Enter 5 to convert from decimal to hexadecimal");
System.out.println("Enter 6 to convert from hexadecimal to decimal");
  
Scanner myObj = new Scanner(System.in);
int choice = myObj.nextInt();
  
Conversions obj = new Conversions();
  
if(choice==1){
System.out.println("Enter the Binary number to be converted : ");
int n = myObj.nextInt();

System.out.print("\nEquivalent Decimal value is : ");
obj.binaryToDecimal(n);
}
else if(choice==2){
System.out.println("Enter the Decimal number to be converted : ");
int n = myObj.nextInt();

System.out.print("\nEquivalent Binary value is : ");
obj.decToBinary(n);
}
else if(choice==3){
System.out.println("Enter the Binary number to be converted : ");
int n = myObj.nextInt();
int dec = obj.binaryToDecimal(n);

  System.out.print("\nEquivalent Hexadecimal value is : ");
obj.decToHexa(dec);
}
else if(choice==4){
System.out.println("Enter the Hexadecimal number to be converted : ");
String s = myObj.next();
char hexdec[] = new char[100] ;
hexdec = s.toCharArray() ;
System.out.print("\nEquivalent Binary value is : ");
try{
obj.HexToBin(hexdec);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.print("");
}
}
else if(choice==5){
System.out.println("Enter the decimal number to be converted : ");
int n = myObj.nextInt();

System.out.print("\nEquivalent Hexadecimal value is : ");
obj.decToHexa(n);
}
else if(choice==6){
System.out.println("Enter the Hexadecimal number to be converted : ");
String n = myObj.next();

System.out.print("\nEquivalent Decimal value is : ");
obj.hexaToDec(n);
}
else{
System.out.println("Invalid Choice ...");
}
  
  
}
}

class Conversions{
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
int base = 1;
  
int temp = num;
while (temp > 0) {
int last_digit = temp % 10;
temp = temp / 10;
  
dec_value += last_digit * base;
  
base = base * 2;
}
  
return dec_value;
}
  
void decToBinary(int n)
{
int[] binaryNum = new int[1000];
int i = 0;
while (n > 0)
{
binaryNum[i] = n % 2;
n = n / 2;
i++;
}

for (int j = i - 1; j >= 0; j--)
System.out.print(binaryNum[j]);
}
  
void HexToBin(char hexdec[])
{
int i = 0;
  
while (hexdec[i] != '\u0000') {
  
switch (hexdec[i]) {
case '0':
System.out.print("0000");
break;
case '1':
System.out.print("0001");
break;
case '2':
System.out.print("0010");
break;
case '3':
System.out.print("0011");
break;
case '4':
System.out.print("0100");
break;
case '5':
System.out.print("0101");
break;
case '6':
System.out.print("0110");
break;
case '7':
System.out.print("0111");
break;
case '8':
System.out.print("1000");
break;
case '9':
System.out.print("1001");
break;
case 'A':
case 'a':
System.out.print("1010");
break;
case 'B':
case 'b':
System.out.print("1011");
break;
case 'C':
case 'c':
System.out.print("1100");
break;
case 'D':
case 'd':
System.out.print("1101");
break;
case 'E':
case 'e':
System.out.print("1110");
break;
case 'F':
case 'f':
System.out.print("1111");
break;
default:
System.out.print("\nInvalid hexadecimal digit " + hexdec[i]);
}
i++;
}
}
  
void decToHexa(int n)
{
char[] hexaDeciNum = new char[100];

int i = 0;
while(n!=0)
{
int temp = 0;
temp = n % 16;

if(temp < 10)
{
hexaDeciNum[i] = (char)(temp + 48);
i++;
}
else
{
hexaDeciNum[i] = (char)(temp + 55);
i++;
}

n = n/16;
}

// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--)
System.out.print(hexaDeciNum[j]);
}
  
int hexaToDec(String hexVal)
{
int len = hexVal.length();
int base = 1;
int dec_val = 0;
  
for (int i=len-1; i>=0; i--)
{
if (hexVal.charAt(i) >= '0' && hexVal.charAt(i) <= '9')
{
dec_val += (hexVal.charAt(i) - 48)*base;
base = base * 16;
}

else if (hexVal.charAt(i) >= 'A' && hexVal.charAt(i) <= 'F')
{
dec_val += (hexVal.charAt(i) - 55)*base;
base = base*16;
}
}
return dec_val;
}
}

Add a comment
Know the answer?
Add Answer to:
In java Write a program that asks the user to choose a selection (Enter 1 to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT