Write a void returning function (ie a method in Java) that will display the bina
ID: 659027 • Letter: W
Question
Write a void returning function (ie a method in Java) that will display the binary equivalent value of a number entered in decimal. Implement two methods for additional base conversions of a value entered as a decimal (base 10) value. One method will convert the number to octal (base 8) and the other will convert the value to hexademical (base 16). Conversion to octal should present few problems (assuming you were able to successfully develop a solution for binary conversion), but the hex conversion could be a challenge. Afterall, how do you represent a remainder returned in two digits as a single digit hex equivalent?
Explanation / Answer
import java.util.Scanner;
public class Decimalto_Hex_oct {
Scanner scan;
int num;
void getVal() {
scan = new Scanner(System.in);
System.out.println(" Enter the number :");
num = Integer.parseInt(scan.nextLine());
}
void oct() { //Decimal to Octal conversio
String octal = Integer.toOctalString(num);
System.out.println("Octal Value is : " + octal);
}
void hex() { // Decimal to HexaDecimal conversion
String hexa = Integer.toHexString(num);
System.out.println("HexaDecimal Value is : " + hexa);
}
}
class MainClass {
public static void main(String args[]) {
Decimal_Hexa obj = new Decimal_Hexa();
obj.getVal();
obj.oct();
obj.hex();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.