Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to convert a 4 digit hexadecimal number into decimal. The user s

ID: 3887291 • Letter: W

Question

Write a program to convert a 4 digit hexadecimal number into decimal. The user should be instructed to enter a 4-digit hexadecimal number. The corresponding decimal number (0 to 65,535) should be output. Note that the place values for a 4-digit hexadecimal number from left to right are 4096, 256, then 16, then 1. You must input the Hexadecimal number as a String, then use the charAt(N) method to compare each individual character to 'A', 'B', 'C', 'D', 'E', 'F' and '0' through '9', and add the value of the digit times the corresponding place value to the total. For example, 1A3016 = 1*4096+10*256+3*16+0*1 = 670410

First, prompt for and input a String of four characters.

If the input is not four characters, print out an appropriate message and exit.

Use if statements, each meaning something like "If the second character is equal to 'A', the value of the second digit is 256 * 10". If any character is not '0'-'9' or 'A'-'F', print out an appropriate message and exit.

In order to compare each character of the input String to a char, use ==, and be sure to use single quotes rather than double quotes.

Explanation / Answer

HexaDecimalToDecimal.java

import java.util.Scanner;

public class HexaDecimalToDecimal {

public static void main(String[] args) {

// Declaring variables
String hexa;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

// Getting the input entered by the user
System.out.print("Enter String of 4 characters :");
hexa = sc.next();

// If the string has less than 4 characters then display error message
if (hexa.length() < 4) {
System.out
.println("** Invalid Input.String must be 4 characters **");

System.exit(0);
}

// Calling the method by passing the user entered number
int decimal = hexaDecimalToDecimal(hexa);

// Displaying the output
System.out.println("The Decimal for Hexadecimal " + hexa + " is :" + decimal);
}

// This method will convert the Hexadecimal to Decimal
private static int hexaDecimalToDecimal(String hexa) {
String dig = "0123456789ABCDEF";
String upperHexa = hexa.toUpperCase();
int tot = 0, index = 0;

// This for loop will calculate the Hexadecimal to decimal number
for (int i = 0; i < hexa.length(); i++) {

for (int j = 0; j < dig.length(); j++) {
if (hexa.charAt(i) == dig.charAt(j)) {
index = j;
break;
}
}
tot = tot * 16 + (index);
}

return tot;
}
}

______________________

Output#1:

Enter String of 4 characters :1A30
The Decimal for Hexadecimal 1A30 is :6704

______________________

Output#2:

Enter String of 4 characters :1A3
** Invalid Input.String must be 4 characters **


_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote