Question

Write a program that prompts the user to read three positive hex integers and display their...

Write a program that prompts the user to read three positive hex integers and display their sum in both hex and decimal. Your program should prompt the user to read the number again, if the input is incorrect. You must use java.lang.NumberFormatException. (chapter12.AddThreeHexNumbers.java)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//AddThreeHexNumbers.java
import java.util.Scanner;
public class AddThreeHexNumbers {
    public static int hexToDecimal(String hex){
        hex = hex.toLowerCase();
        int result = 0, temp = 1;
        for(int i = hex.length()-1;i>=0;i--){
            char ch = hex.charAt(i);
            if(ch>='0' && ch<='9'){
                result = result + (ch-'0')*temp;
            }
            else if(ch=='a'){
                result = result + temp*10;
            }
            else if(ch=='b'){
                result = result + temp*11;
            }
            else if(ch=='c'){
                result = result + temp*12;
            }
            else if(ch=='d'){
                result = result + temp*13;
            }
            else if(ch=='e'){
                result = result + temp*14;
            }
            else if(ch=='f'){
                result = result + temp*15;
            }
            temp = temp * 16;
        }
        return result;
    }

    public static String decimalToHex(int n){
        String res = "";
        int reminder;
        while(n>0){
            reminder = n%16;
            if(reminder>=0 && reminder<=9){
                res = reminder +res;
            }
            else if(reminder==10){
                res="A"+res;
            }
            else if(reminder==11){
                res="B"+res;
            }
            else if(reminder==12){
                res="C"+res;
            }
            else if(reminder==13){
                res="D"+res;
            }
            else if(reminder==14){
                res="E"+res;
            }
            else{
                res="F"+res;
            }
            n = n /16;
        }
        return res;
    }

    public static boolean validateHex(String s){
        char ch;
        for(int i = 0;i<s.length();i++){
            ch = s.charAt(i);
            if(!((ch>='0' && ch<='9') || (ch>='a' && ch<='f'))){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int sum = 0;
        String hex;

        int i = 0;
        while(i<3){
            System.out.print("Enter hex value: ");
            hex = scan.next().toLowerCase();
            if(validateHex(hex)) {
                sum += hexToDecimal(hex);
                i++;
            }
            else{
                System.out.println("Invalid input");
            }
        }

        System.out.println("Sum in decimal: "+sum);
        System.out.println("Sum in hexa decimal: "+decimalToHex(sum));
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a program that prompts the user to read three positive hex integers and display their...
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
  • in java 3) Sum. Write a program that prompts the user to read two integers and...

    in java 3) Sum. Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.

  • In Java - Write a program that prompts the user to enter two integers and displays...

    In Java - Write a program that prompts the user to enter two integers and displays their sum. If the input is incorrect, prompt the user again. This means that you will have a try-catch inside a loop.

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • Write a program that prompts the user to enter three integers and display the integers in...

    Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. For example if you enter 10 and 9 and 12, in the result we will first see 9, then 10 and then 12 (because 9 < 10 < 12). **using c++

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Write a program that prompts the user to input three different integers from the keyboard, then...

    Write a program that prompts the user to input three different integers from the keyboard, then prints the sum, the average, and the product. The screen dialogue should appear exactly as follows: Enter three different integers: 13 27 14 Sum is 54 Average is 18 Product is 4914 Please code in c program

  • c++ Write a program that prompts the user to enter a length in feet and then...

    c++ Write a program that prompts the user to enter a length in feet and then enter a length in inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Your error message should read A non positive number is entered and allow the user to try again.

  • A java program Write a program that prompts for and reads in a positive    integer...

    A java program Write a program that prompts for and reads in a positive    integer into a variable n. Your program should then sum    the first n ODD integers and display the sum. For    example, if 3 is entered for n, your program should display    the the sum 1 + 3 + 5. If 5 is entered, your program should    display the sum 1 + 3 + 5 + 7 + 9. What is the...

  • C++ Write a program that prompts the user to enter two positive integers: num1 and num2....

    C++ Write a program that prompts the user to enter two positive integers: num1 and num2. - Validate that num1 is less than num2 and that both numbers are positive. If any of these conditions are not met, allow the user to re-enter num1 and num2 until the input is determined valid. - For all integers from num1 through num2, print the word keyboard if the current integer is divisible by 2 and print the word mouse if the current...

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