As part of my Computing and Information Systems degree I have made some programs to help me out with my studies, which I will now be sharing with you a bit at a time.
For the first post, I have done a quick program in Java to convert a decimal number into binary. In effect this touches three of the first year subjects of Mathematics, Computing and Java programming from the University of London curriculum.
Here is the full source code:
/**
* @(#)dec2bin.java
*
* @author Edward Vela
* @version 1.00 2007/12/15
*/
import java.io.*;
public class dec2bin {
public static int getDecimal(){ System.out.print("Input a positive integer ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int decNum = 0;
try{ decNum = Integer.parseInt(in.readLine());
}
catch (Exception e) {
}
return decNum;
}
public static String dec2Bin (int dec){ int remainder = dec % 2;
int quotient = dec/2;
if (dec > 0){ return ("" + dec2Bin(quotient) + remainder);
} else { return ("" );
}
}
public static void main(String[] args) { int decNum;
String binNum;
decNum = getDecimal();
binNum = dec2Bin(decNum);
System.out.println(decNum + " converted to binary = " + binNum);
}
}
We start with the getDecimal() function. This function just asks the user to type in a number from the keyboard and returns it as an integer. No fancy error handling for our first program, but Java beginners can note that to get input from the keyboard we have to first add the
import java.io.*; line at the top of our code. Then we can read from the keyboard BufferedReader object using the
in.readLine() method. This is then passed to the
Integer.parseInt() method which handily converts for us the String we got from the keyboard into an integer. There is a form of error trapping though, as every input that is not an integer is trapped and the program just acts as the user just typed in a 0.
Our second function (dec2Bin) is short and sweet, but does a lot of work as it calculates the binary number recursively. Remember that in mathematics, we obtain the binary number by keeping on dividing the original number by 2 and taking note of the remainder, which can be either 0 or 1. Then when we can divide no more, we read the remainders from the bottom up to end with the binary number. This is what we are doing in this function. If you think in a recursive way, all we are doing in maths is to take the first number and divide it by 2. Then we divide the answer by 2, then the second answer by 2 and so on. So a function that calls itself will keep on dividing the number by 2 until the answer is zero.
Finally, with the support of the previous functions, the main method becomes very easy to tie everything up together. We just declare the variables
decNum and
binNum, call the
getDecimal() function to get the user's input and pass this value along to the
dec2Bin() function which will convert the number into binary which we then display nicely to the user.
As an exercise for you, there are some improvements that can be done on this program. Note that there is no output if the user enters 0 or a wrong input. The easiest improvement is to display the binary number 0 when a 0 input is entered. The second improvement is to re-ask the user for the number if it is not a positive integer.