Hey people. Ok to be honest I don't even know where to begin on this one. I have pieces of knowledge all over the place but I can't seem to put any of it together. I taught myself how to do binary to decimal conversion on paper using the "convert from binary to decimal wiki how" tutorial.
I don't need you to do my assignment for me but I need to be hinted in right directions on what I should be doing, what the next logical step would be. Preferably in plain english and code.
Here is a correct "example run" of the program:
|
Code:
|
3 Input a binary number: -1
4 Input must be >= 0
5
6 Input a binary number: 1121
7 Input must be a Binary number
8
9 Input a binary number: 0011100
10 11100
11 Base 2 Equals
12 28
13 Base 10 |
Here is what I have so far:
|
Code:
|
import java.util.*; //Scanner class is defined here
public class BinaryToDecimal {
public static void main(String[] args) {
BinaryToDecimal obj = new BinaryToDecimal();
obj.process();
}
private void process(){
Scanner in = new Scanner(System.in); //required to read input
System.out.print("Input a binary number: ");
int number = in.nextInt(); //reads a line of input
while (number < 0){
System.out.println("Input must be >= 0");
System.out.print("\nInput a binary number: ");
number = in.nextInt(); //accepts new keyboard input again
}
}
} |
Other rules:
* You may
not use any of the Math class built in power() or binary digit processing methods. All calculations must be done explicitly.
* We do not need to use arrays.
It's best to use:
* % (modulus operator) to access the current right-most digit
* / (int division operator) to divide down the current value after processing the right-most digit
* Repeat for all digits
This is the best and easiest way to solve this assignment.