What will be the output if the input is 0? Revise line 27 in Listing 5.11 so that the program displays hex number 0 if the input decimal is 0. import java.util.Scanner; public class Dec2Hex {     /** Main method */     public static void main(String[] args){     //Create a Scanner     Scanner input = new Scanner(System.in);          //Prompt the user to enter a decimal integer     System.out.print("Enter a decimal number: ");     int decimal = input.nextInt();          //Convert decimal to hex     String hex = "";          while (decimal != 0){         int hexValue = decimal % 16;                  // Convert a decimal value to a hex digit         char hexDigit = (0 <= hexValue && hexValue <= 9) ?         (char) (hexValue + '0') : (char) (hexValue - 10 + 'A');                  hex = hexDigit + hex;         decimal = decimal / 16;     }          System.out.println("The hex number is " + hex);     } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

What will be the output if the input is 0? Revise line 27 in Listing 5.11 so that the program displays hex number 0 if the input decimal is 0.

import java.util.Scanner;

public class Dec2Hex {

    /** Main method */

    public static void main(String[] args){

    //Create a Scanner

    Scanner input = new Scanner(System.in);

    

    //Prompt the user to enter a decimal integer

    System.out.print("Enter a decimal number: ");

    int decimal = input.nextInt();

    

    //Convert decimal to hex

    String hex = "";

    

    while (decimal != 0){

        int hexValue = decimal % 16;

        

        // Convert a decimal value to a hex digit

        char hexDigit = (0 <= hexValue && hexValue <= 9) ?

        (char) (hexValue + '0') : (char) (hexValue - 10 + 'A');

        

        hex = hexDigit + hex;

        decimal = decimal / 16;

    }

    

    System.out.println("The hex number is " + hex);

    }

}

Listing 5.11 Dec2Hex.java
1 import java.util.Scanner;
3 public class Dec2Hex {
4 /** Main method */
5 public static void main (String[] args) {
6 7/ Create a Scanner
7 Scanner input
= new Scanner (System.in);
%3D
8
9 // Prompt the user to enter a decimal integer
10 System.out.print ("Enter a decimal number: ");
11 int decimal = input.nextInt();
12
13 // Convert decimal to hex
14 String hex = "";
%3|
15
16 while (decimal != 0) {
%3D
17 int hexValue
decimal % 16;
18
19 // Convert a decimal value to a hex digit
(0 <= hexValue && hexValue <= 9) ?
20 char hexDigit
21 (char) (hexValue + '0') : (char) (hexValue
%3D
10 + 'A');
22
hexDigit + hex;
decimal / 16;
23 hex
24 decimal
25 }
26
27 System.out.println ("The hex number is
28 }
29 }
+ hex);
Transcribed Image Text:Listing 5.11 Dec2Hex.java 1 import java.util.Scanner; 3 public class Dec2Hex { 4 /** Main method */ 5 public static void main (String[] args) { 6 7/ Create a Scanner 7 Scanner input = new Scanner (System.in); %3D 8 9 // Prompt the user to enter a decimal integer 10 System.out.print ("Enter a decimal number: "); 11 int decimal = input.nextInt(); 12 13 // Convert decimal to hex 14 String hex = ""; %3| 15 16 while (decimal != 0) { %3D 17 int hexValue decimal % 16; 18 19 // Convert a decimal value to a hex digit (0 <= hexValue && hexValue <= 9) ? 20 char hexDigit 21 (char) (hexValue + '0') : (char) (hexValue %3D 10 + 'A'); 22 hexDigit + hex; decimal / 16; 23 hex 24 decimal 25 } 26 27 System.out.println ("The hex number is 28 } 29 } + hex);
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY