JAVA PLEASE
4. Write a recursive program that converts a Hex base value to its decimal equivalent. This program must validate the ueser’s input. You program must have the main method, inputValidate method and hexToDecimal method. Must implement the last method recursively.
Here is a sample output.
Enter a valid hex number: 10 Your number in decimal is: 16 Do you have another number: yes Enter a valid hex number: 123 Your number in decimal is: 291 Do you have another number: yes Enter a valid hex number: 111 Your number in decimal is: 273 Do you have another number: yes Enter a valid hex number: num Enter a valid hex number: EF Your number in decimal is: 239 Do you have another number: no
import java.util.Scanner;
public class HexNumber {
public static long hexToDecimal(String hexString) {
if(hexString.isEmpty()) {
return 0;
}
int dec = 0;
char c = hexString.charAt(hexString.length() - 1);
if(c <= '9') {
dec = dec*16 + (c - '0');
} else {
dec = dec*16 + (10 + (c - 'A'));
}
return dec + 16 * hexToDecimal(hexString.substring(0, hexString.length() - 1));
}
public static boolean checkValidHexaChar(char c) {
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F');
}
public static boolean inputValidate(String hex) {
for (int i = 0; i < hex.length(); i++) {
// take every char from the input one by one
char c = hex.charAt(i);
// check the validity of each char
if(!checkValidHexaChar(c)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String choice;
do {
String hex;
do {
System.out.println("Enter a valid hex number: ");
hex = in.nextLine().toUpperCase();
} while(!inputValidate(hex));
System.out.println("Your number in decimal is: " + hexToDecimal(hex));
System.out.println("Do you have another number(yes/no): ");
choice = in.nextLine().toLowerCase();
} while (choice.equals("yes"));
in.close();
}
}
please upvote
JAVA PLEASE 4. Write a recursive program that converts a Hex base value to its decimal...
Need this in java please
Write a program that converts between decimal, hex, and binary numbers, as shown in Figure. When you enter a decimal value in the decimalvalue text field and press the Enter key, its corresponding hex and binary numbers are displayed in the other two text fields. Likewise, you can enter values in the other fields and convert them accordingly. (c) The program converts between decimal, hex, and binary numbers.
*In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a decimal number to a different base number system. Print out the results after testing your method on a few different inputs. Task 1 – Recursive Method Create a recursive method that returns a given number converted from base ten to a given other base number system ranging from two to thirty-six. A decimal number, or base ten number, can be expressed in any other...
[Using Python] Write a program to convert a hexadecimal number to its decimal value using the ord builtin function (Reminder: hexadecimal numbers are 0 through 9, A,B,C,D,E,F. hex(A) = 10, hex(F) = 15). example outputs: 1. `Enter a hex number: f` `The decimal value for hex number f is 15` 2. `Enter a hex number: g` `Incorrect hex number` 3. `Enter a hex number: 091c` `The decimal value for hex number 091c is 2332` 4. `Enter a hex number: 091g`...
Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...
[Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal numbers are 0 through 9, A,B,C,D,E,F. hex(A) = 10, hex(F) = 15). example outputs: 1. `Enter a hex number: f` `The decimal value for hex number f is 15` 2. `Enter a hex number: g` `Incorrect hex number` 3. `Enter a hex number: 091c` `The decimal value for hex number 091c is 2332` 4. `Enter a hex number: 091g` `Incorrect hex number` Hints: you...
To write a C program (not C++) that converts numbers between Decimal and IEEE-754 format and vice versa. Inputs: Number in Decimal format (including special case of 0) Number in IEEE-754 format (including special cases) Output: Equivalent number in IEEE-754 format Equivalent number in Decimal Specification: The program converts a number based on choosing from a menu of choices, where each choice calls the appropriate procedure, where the choices are: Decimal to IEEE-754 conversion IEEE-754 to Decimal conversion Quit program...
Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...
C++
For this program you'll write a program that uses a recursive function. This program plays a simple token-taking game. The rules are simple 1. 2. You start the game with exactly 13 tokens. On each turn, you may do one of two things You may ask for exactly 25 more tokens; or IF the number of tokens you have is an even number, you may give back exactly half of the tokens you have a. b. 3. The object...
<Java> 1-Write a program that converts from a string into an integer number. For example: For "123" the program should return 123 For "131", The program should return 131 For "-121" the program should return -121 For "1e3" the program should print an Invalid number For "a1" the program should print an invalid number Do not use Integer.parse function 2. Generate a random number in [0, 100]. Write an efficient guess procedure that takes no more than 7 guesses to...
/**
* This program Performs various number base conversions. It also
verifies if
* a number is valid in its base.
* Author: M. Rahman
* Date: 06 September 2018
*/
public class NumberConversion {
public static String dec2any(String dec, int base) {
/**
* Converts a decimal value to a target base
* inputs:
* dec: the decimal value to be converted
* base: the target base
* output: 256-base as dotted decimal, hex as usual, bases
* over...