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
  • ***** JAVA ONLY ***** Write a program that asks the user to enter the name of...

    ***** JAVA ONLY ***** Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad to create a simple file that can be used to test the program. ***** JAVA ONLY *****

  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • write a program in java that asks the user for a sentence, and then returns that...

    write a program in java that asks the user for a sentence, and then returns that string with the words backwards. For example: Enter a sentence! Mary had a little lamb. Your sentence backwards: lamb. little a had Mary Write a method called reverse() that accepts a sentence as a string, and then returns that string with the words backwards. Write a main() method that gets the string from the user and then displays the string backwards. demonstrate program by...

  • (JAVA NetBeans) Write a Java program, which asks the user to enter a string, then (a)...

    (JAVA NetBeans) Write a Java program, which asks the user to enter a string, then (a) reverse the string, (b) change the case of the string (CaT cAt) (c) print the chars at even positions (Object Ojc)

  • java program QUESTION 2: 1. Write a do-while loop that asks the user to select a...

    java program QUESTION 2: 1. Write a do-while loop that asks the user to select a task from the following menu to continue: 1. Option 1 2. Option 2 3. Option 3 4. Option 4 5. Exit Read the selection from the keyboard then write the switch statement: For option 1, display the message "Do the option 1" For option 2, display the message "Do the option 2" For option 3, display the message "Do the option 3" For option...

  • Write a java program that asks the user to enter 2 integers, obtains them from the...

    Write a java program that asks the user to enter 2 integers, obtains them from the user, determines if they are even or odd numbers and prints their sum, product, difference, and quotient (Division).

  • Write a Java program that: • Asks the user to enter the number of integers that...

    Write a Java program that: • Asks the user to enter the number of integers that he need to enter; • Asked him to enter integer by integer; • Print The number of Odd numbers entered and the number of Even numbers entered. Important notes: 1. You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code. It must be editable. 2. Take a screen shot for your...

  • Write a Java program to meet the following requirements: 1. Prompt the user to enter three...

    Write a Java program to meet the following requirements: 1. Prompt the user to enter three strings by using nextLine(). Space can be part of the string). ( Note: your program requires using loop: for-loop, while-loop or do-while-loop to get the input String ) 2. Write a method with an input variable (string type).The method should return the number of lowercase letters of the input variable. 3. Get the number of the lowercase letters for each user input string by...

  • Write a C program that asks the user to think of an integer number from 1...

    Write a C program that asks the user to think of an integer number from 1 to 20. Have the computer guess what the number is by using a binary tree to determine the next guess. You can create the binary tree “by hand” by typing in a set of malloc commands and explicitly linking the nodes. (make the tree completely, then traverse the tree) Use a menu to provide the user with a selection to answer the results of...

  • Write a program that asks the user to enter a string and then asks the user...

    Write a program that asks the user to enter a string and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. python programming

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
ADVERTISEMENT