Question

What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of

Programming Exercise 2 (12 points): Write a program that prompts the user to enter a character c that represents a hexadecima

write programs with detailed instructions on how to execute.

code is java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class HexadecimalToDecimal {

   public static void main(String[] args) {
       String ch;
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter a character: ");
       ch=sc.next();
       try{
           System.out.println("The hex value of "+ch+" is : "+Integer.valueOf(ch, 16));
       }
       catch(Exception e){
           System.out.println("The character "+ch+" is invalid:"+ch+" is not a hexadecimal digit");
       }
       sc.close();
   }

}

Enter a character: The hex value of 4 is : 4

Enter a character: The hex value of B is : 11

Enter a character: The character I is invalid:I is not a hexadecimal digit

import java.util.Scanner;

public class DecimalToBinary {

   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter decimal number: ");
       int decimal=sc.nextInt();
       System.out.println("The binary value of "+decimal+" is : "+convertToBinary(decimal));
   }
   public static String convertToBinary(int decimal){
       String binary="";
       StringBuilder sb=new StringBuilder();
       int temp=decimal;
       int mod=0;
       while(temp>0){//will divide 66 by 2 till the last dividend is less than 0
           mod=temp%2;//will get the quotient and add to string
           temp=temp/2;//will get the new dividend by dividing with 2
           binary=binary+mod;//adds the binary bits to string binary it will be 0100001
       }
       sb.append(binary);//adding to a string builder
       binary=sb.reverse().toString();//reverses the string builder to get exact output(1000010)
       return binary;
      
   }

}

Enter decimal number: The binary value of 66 is : 1000010

Add a comment
Know the answer?
Add Answer to:
write programs with detailed instructions on how to execute. code is java What you need to...
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
  • write codes and give detailed explanation of how to execute. What you need to turn in:...

    write codes and give detailed explanation of how to execute. What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of your programs. All programming files (source code) must be put in a zipped folder named "labl-name," where "name" is your last name. Submit the zipped folder on the assignment page in Canvas; submit the report separately (not inside the zipped folder) as a Microsoft Word or PDF...

  • JAVA 2. (8 points) Write a code segment which prompts the user to enter the price...

    JAVA 2. (8 points) Write a code segment which prompts the user to enter the price of a tour and receives input from the user. A valid tour price is between $52.95 and $259.95, inclusive. Your program should continue to repeat until a valid tour price has been entered. If the user enters an invalid price, display a message describing why the input is invalid. You may assume that the user only enters a real number. The user will not...

  • 3 Programming Question (45 points) 3.1 Instructions You need to write the code by yourself. Your...

    3 Programming Question (45 points) 3.1 Instructions You need to write the code by yourself. Your implementation must use C/C++ and your code must run on the Linux machine general.asu.edu. Please refer to the programming guide (available under the Assignments folder on Blackboard) for using the general.asu.edu server, as well as compiling and running C/C++ code under Linux For this question, you need to provide a Makefile that compiles your program to an executable named a3 that runs on the...

  • In Java, write a recursive method that converts an integer to hexadecimal. The function should print...

    In Java, write a recursive method that converts an integer to hexadecimal. The function should print out the hexadecimal character instead of returning the character. Write a test program that prompts the user to enter an integer and displays its hexadecimal equivalent.

  • You will write Java programs in Eclipse to solve these word problems Read these instructions on...

    You will write Java programs in Eclipse to solve these word problems Read these instructions on how to name and submit the Java files. Please make sure your program compiles before you submit it. Make sure you name your Java file and class the same (otherwise it will not compile). For example, the class names are case-sensitive 1. will be in a file called CompareNames.java; remember, the Do not create a package. You can tell if your file is in...

  • write a c++ code be sure the user only enter A or a for packageA, B...

    write a c++ code be sure the user only enter A or a for packageA, B or b for packageB, C or c for packageC. If the user inputs some other value display an error message saying " invalid out". When you ask the user to input the number of minute and the user enters a negative value you should display the same error message "invalid input" in both cases, after you display the error message the program should stop...

  • print fruits Python 3 1 fruit = in 8. Print Fruits 3 # Enter you The...

    print fruits Python 3 1 fruit = in 8. Print Fruits 3 # Enter you The user is asked to input a single character representing a kind of fish as indicated in the table below. It may be upper or lower case. You should print the message indicated for the entry in the table. For example, if the user enters "b" you should print the message The fruit is a banana If the user enters "p you should print the...

  • Using a Python environment, complete two small Python programs - Write a function which repeatedly reads...

    Using a Python environment, complete two small Python programs - Write a function which repeatedly reads numbers until the user enters “done” Once “done” is entered, print out the sum, average, maximum, and minimum of the values entered If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number. 2. Write another function so that it draws a sideways tree pointing right; for example,...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Please make this Python code execute properly. User needs to create a password with at least...

    Please make this Python code execute properly. User needs to create a password with at least one digit, one uppercase, one lowercase, one symbol (see code), and the password has to be atleast 8 characters long. try1me is the example I used, but I couldn't get the program to execute properly. PLEASE INDENT PROPERLY. def policy(password): digit = 0 upper = 0 lower = 0 symbol = 0 length = 0 for i in range(0, len(password)): if password[i].isdigit(): # checks...

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