The following is Java sample code to read an integer from the keyboard and conve
ID: 3590469 • Letter: T
Question
The following is Java sample code to read an integer from the keyboard and convert it to binary string. Finish this program by adding code to do the followings (1) Convert the integer from keyboard to hexadecimal string and show on screen; (2) Convert the integer from keyboard to octal string and show on screen; package javaapplication1; import java.util.Scanner; I/ Import Scanner class (contains methods @author yao public class JavaApplication1 public static void main(Stringl) args) int i Scanner in new Scanner(System..n); // Construct a Scanner object System.out.print("Enter an integer:"; IPrompt message i in.nextlnt(): // Read in next number and store in i System.out.printin(i+toBinary(i)): pre: none post: returns a String with base10Num in base 2 public static String toBinary(int base10NumX boolean isNeg base10Num 1) results (base!ONum % 2) + result; base10Num /2 assert base10Num =:01 | base10Num- 1 : "value is notExplanation / Answer
Below is your code: -
import java.util.Scanner;
public class JavaApplication1 {
public static void main(String[] args){
int i;
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer: ");
i = in.nextInt();
System.out.println(i+" to binary: "+toBinary(i));
System.out.println(i+" to hex: "+toHex(i));
System.out.println(i+" to octal: "+toOctal(i));
in.close();
}
/*
* pre: none
* post: returns a String with base10Num in base 16
*/
public static String toHex(int base10Num){
// For storing remainder
int rem;
// For storing result
String str="";
// Digits in hexadecimal number system
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(base10Num>0)
{
rem=base10Num%16;
str=hex[rem]+str;
base10Num=base10Num/16;
}
return str;
}
/*
* pre: none
* post: returns a String with base10Num in base 8
*/
public static String toOctal(int base10Num){
int counter=0;
int result = 0;
while(base10Num !=0) {
int temp = (int) ((base10Num%8) * Math.pow(10, counter));
counter++;
result += temp;
base10Num /= 8;
}
return result+"";
}
/*
* pre: none
* post: returns a String with base10Num in base 2
*/
public static String toBinary(int base10Num){
boolean isNeg = base10Num < 0;
base10Num = Math.abs(base10Num);
String result = "";
while(base10Num > 1){
result = (base10Num % 2) + result;
base10Num /= 2;
}
assert base10Num == 0 || base10Num == 1 : "value is not <= 1: " + base10Num;
result = base10Num + result;
assert all0sAnd1s(result);
if( isNeg )
result = "-" + result;
return result;
}
/*
* pre: cal != null
* post: return true if val consists only of characters 1 and 0, false otherwise
*/
public static boolean all0sAnd1s(String val){
assert val != null : "Failed precondition all0sAnd1s. parameter cannot be null";
boolean all = true;
int i = 0;
char c;
while(all && i < val.length()){
c = val.charAt(i);
all = c == '0' || c == '1';
i++;
}
return all;
}
}
Sample Output
Enter an integer: 34
34 to binary: 100010
34 to hex: 22
34 to octal: 42
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.