Number Converter Write a MARS program that can perform conve
Number Converter: Write a MARS program that can perform conversions between binary, decimal, and hexadecimal representations for positive integers (under 32 bits). Your program should accept a string that follows the following format -- invalid inputs or formats should be rejected. Format: , where the input and output types can be one of \"b\", \"d\", \"h\". Hex inputs will start with 0x and use lower case letters. You should then print the converted answer and prompt the user again (unless the user enters a string starting with e). Here is an example run of this program:
What would you like to convert?
d b 33
100001
What would you like to convert?
b h 100010
0x22
What would you like to convert?
h d 0x23
35
What would you like to convert?
d b 4b
Sorry, invalid format.
What would you like to convert?
f g 43
Sorry, invalid format.
What would you like to convert?
e
Solution
MarsConverter.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MarsConverter {
private BufferedReader br;
private String[] input;
public MarsConverter() {
// TODO Auto-generated constructor stub
}
public void convert(){
br = new BufferedReader(new InputStreamReader(System.in));
while(true){
try{
System.out.println(\"What would you like to convert?\");
input = br.readLine().split(\" \");
if(input[0].charAt(0) == \'e\'){
break;
}
else{
if(input[0].equals(\"b\")){
if(input[1].equals(\"d\")){
converBinaryToDecimal(input[2]);
}
else if(input[1].equals(\"h\")){
convertBinaryToHexa(input[2]);
}
else{
System.out.println(\"Sorry, invalid format. \");
}
}
else if(input[0].equals(\"d\")){
if(input[1].equals(\"b\")){
converDecimalToBinay(input[2]);
}
else if(input[1].equals(\"h\")){
convertDecimalToHexa(input[2]);
}
else{
System.out.println(\"Sorry, invalid format. \");
}
}
else if(input[0].equals(\"h\")){
if(input[1].equals(\"b\")){
convertHexaToBinary(input[2]);
}
else if(input[1].equals(\"d\")){
convertHexaToDecimal(input[2]);
}
else{
System.out.println(\"Sorry, invalid format. \");
}
}
else{
System.out.println(\"Sorry, invalid format. \");
}
}
}
catch(Exception e){
System.out.println(\"Sorry, invalid format. \");
}
}
}
private void convertBinaryToHexa(String string) {
System.out.println(\"0x\"+Long.toHexString(Long.parseLong(string,2)));
}
private void convertDecimalToHexa(String string) {
String str = Integer.toHexString(Integer.parseInt(string));
System.out.println(\"0x\"+str);
}
private void convertHexaToDecimal(String string) {
if(string.charAt(0)!=\'0\' && string.charAt(1)!=\'x\'){
System.out.println(\"Sorry, invalid format. \");return;
}
string = string.substring(2);
String digits = \"0123456789ABCDEF\";
string = string.toUpperCase();
int val = 0;
for (int i = 0; i < string.length(); i++)
{
char ch = string.charAt(i);
int d = digits.indexOf(ch);
val = 16*val + d;
}
System.out.println(val);
}
private void convertHexaToBinary(String string) {
if(string.charAt(0)!=\'x\' && string.charAt(1)!=\'x\'){
System.out.println(\"Sorry, invalid format. \");return;
}
string = string.substring(2);
int number = Integer.parseInt(string, 16);
System.out.println(Integer.toBinaryString(number));
}
private void converDecimalToBinay(String string) {
System.out.println(Integer.toBinaryString(Integer.parseInt(string)));
}
private void converBinaryToDecimal(String string) {
int binaryNumber = Integer.parseInt(string);
int tempBinary=binaryNumber;
int decimal = 0;
int power = 0;
while(true){
if(tempBinary == 0){
break;
}
else {
int tmp = tempBinary%10;
decimal += tmp*Math.pow(2, power);
tempBinary = tempBinary/10;
power++;
}
}
System.out.println(decimal);
}
public static void main(String[] args) throws IOException {
MarsConverter mc = new MarsConverter();
mc.convert();
}
}
Output:
What would you like to convert?
d b 33
100001
What would you like to convert?
b h 100010
0x22
What would you like to convert?
h d 0x23
35
What would you like to convert?
d b 4b
Sorry, invalid format.
What would you like to convert?
f g 43
Sorry, invalid format.
What would you like to convert?
e



