📄 numericconverter.java
字号:
/**
* A class to conver the integer number to the binary
* and octal.
* @author Ruixiao Wu
* @version 1.0 24/05/2006
*/
public class NumericConverter {
public int number;
public NumericConverter(int number){//constructor
this.number = number;
}//End constructor
/**
* A method to conver integer to binary.
* The detail method is devide the number by 2, get and store the residue in an array.
* Reverse the array and print.
* Thus we can get the result.
* @param number
*/
public void convertIntegerToBinary(int number){//I didn't use the ArrayList, so this method is very complicated.
int counter = 0;//This parameter is used to count the capacity of a array
int testLength = number;
int testNumber = number;
while(testLength != 1){
testLength = testLength /2;
counter++;//Firstly, test the need of capacity of the store array.
}
int[] container = new int[counter+1];
int[] container1 = new int[counter+1];
int i = 0;
while(testNumber != 1){
container[i] = testNumber%2;//Secondly, store the residue in an array .
testNumber = testNumber / 2;
++i;
}
container[container.length-1]=testNumber;//Thirdly, get the last bit of the residu and finish!
for(int j=0;j<container.length;++j){
int length = container.length;
container1[j]= container[length-1-j];
System.out.print(container1[j]);//Print the array!
}
}//End method.
/**
* A method to conver integer to binary.
* The detail method is devide the number by 8, get and store the residue in an array.
* Reverse the array and print.
* Thus we can get the result.
* @param number
*/
public void convertIntegerToOctal(int number){//I didn't use the ArrayList, so this method is very complicated.
int counter = 0;//This parameter is used to count the capacity of a array
int testLength = number;
int testNumber = number;
while(testLength > 7 ){
testLength = testLength /8;
counter++;//Firstly, test the need of capacity of the store array.
}
int[] container = new int[counter+1];
int[] container1 = new int[counter+1];
int i = 0;
while(testNumber > 7){
container[i] = testNumber%8;//Secondly, store the residue in an array.
testNumber = testNumber / 8;
++i;
}
container[container.length-1]=testNumber;//Thirdly, get the last bit of the residu and finish!
for(int j=0;j<container.length;++j){
int length = container.length;
container1[j]= container[length-1-j];
System.out.print(container1[j]);//Print the array!
}
}//End method.
/**
* The main method to run the program.
* The method contain the try and catch method.
*/
public static void main(String[] args){
int number;
try { number = Integer.parseInt(args[0]); }//try whether the input is an integer.
catch (Exception wrongInput) {//catch the exception and print the error.
System.out.println("Error: Cannot convert value into binary and octal formats."+
"\nThe input parameter is not an integer number.");
return;//return in order to break the program.
}
NumericConverter NC = new NumericConverter(number);
System.out.print("The integer number "+ number +" is:");
NC.convertIntegerToBinary(number);
System.out.print(" in binary / ");
NC.convertIntegerToOctal(number);
System.out.print(" in octal.");
}//End main.
}//End class.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -