Learning Objectives:
• Writing and running a small Java program on BlueJ IDE
• Primitive data types, String class, basic control structures, and simple parameter passing

Problem: Given a number in hexadecimal number system, convert it into its equivalent decimal representation. In hexadecimal number system, the digits are denoted by the symbols 0, 1 ..., 9, A, …, F. For example, if the hexadecimal number entered is 1EA, the program should output 490 as the decimal value.

Notice that the decimal value of the hexadecimal 1EA can be computed in stages as the digits are revealed one-by-one in left to right order. In other words, there is no need for computing powers of 16 explicitly. Initially, before the digits are revealed, the decimal value is zero. But as the left-most digit is revealed, the decimal value changes to 1. After the second digit E is revealed, the decimal value should become 30 corresponding to hexadecimal 1E. Specifically, when a new digit is revealed, multiply the current decimal value by 16 and add the value of the new digit. Continuing further, when A is revealed, we multiply 30 by 16 and add 10. Since we have reached the end of the string, we stop and print out 490. That is, the decimal value of hexadecimal 1EA is computed as

( ( 0 * 16 + 1 ) * 16 + 14 ) * 16 + 10 = 490

First, create a test case table showing possible input values and equivalent decimal values. Consider the test cases indicated at the end of the assignment. Write a complete, well-structured Java program to accomplish the above computational task. The program should respond appropriately for a sequence of input hexadecimal numbers. For the purpose of this program, you may assume that the user will indeed enter a valid number in the hexadecimal number system. In other words, there is no need to perform any error-check on this input data. Name the project PROG02 and the class This program will use the services of a class named Hex. file will include the class Hex with:

• a public static method parseInt that accepts one String parameter named hex, computes the decimal value and returns the int computed, and
• a private static method value that accepts one char parameter named hexChar, determines and returns its int value

You are not allowed to use any predefined method in the Integer class. A typical run of the program will appear as   shown   in the next page. Be sure that your output matches the sample output, in terms of prompts and messages.

Sample Output to show prompts and messages

This program reads a string, checks if forms a valid hex numeral, and determines its decimal value. The input is assumed to be a valid hexadecimal number; no error checking is performed.

Want to run? (enter y to run): y

Enter a valid hexadecimal number: A
The equivalent decimal value is 10

Want to run? (enter y to run): y

Enter a valid hexadecimal number: 1EA
The equivalent decimal value is 490

Want to run? (enter y to run): y

Enter a valid hexadecimal number: 0
The equivalent decimal value is 0

Want to run? (enter y to run): n

Program has terminated.

Optional (No extra credit but creates a good impression that often helps)

Include and implement in the Hex class

• a public static method isValid that accepts one String parameter named hex, computes and returns true if the parameter is a valid hexadecimal string and false otherwise

In the HexProcessor class, invoke isValid to verify that the user input represents a valid hexadecimal string before invoking parseInt to determine the equivalent decimal value.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

// Run program in a loop
       while (true) {
            // Query run from user
            System.out.print("\nWant to run? (enter y to run): ");
            if (!scanner.nextLine().equals("y"))
            // Query hexadecimal number from user
            System.out.print("\nEnter a valid hexadecimal number: ");...

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats