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;
}
}
In java Write a program that asks the user to choose a selection (Enter 1 to...
Java Letter Counter: 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. Must use a do while loop and a counter!!!!
In Java, write a program using a loop that asks the user to enter a series of decimal numbers. The user must enter -88 to end the input of the decimal numbers. After the user enters all numbers, the program should display the sum of all numbers entered.
***** 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 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 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) 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 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 user, determines if they are even or odd numbers and prints their sum, product, difference, and quotient (Division).
Write a program in JAVA that asks the user to enter an integer. Store the integers in an array. Allow for up to 10 integers. When the user enters -1, the program should end. Print the number of integers entered as well as the number of times each integer was entered.
Use printf(...) method to Write a java program that asks the user to enter three different names and then sorts and display names in ascending order. For example, if the names "Maria", "Kelley", and "Angie" were entered, the program will display: Names in sorted order are: FIRST: Angie SECOND: Kelley THIRD: Maria