import java.util.Scanner;
public class Binary{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String input = "";
// So long as the String input is NOT 1-60 in length,
// Ask the user to enter a valid binary number up to 60 digits
// Save the next line of user input to the variable "input"
// If the input string is NOT binary, tell the user "Bad input!"
// Otherwise, tell the user "You entered [numeric value of input]"
}
private static boolean isBinary(String input){
// Go over each character of input
// If that single character is not '1' or'0', return false immediately
// If you finish going through the characters, return true
}
private static long binaryValue(String input){
// To start, the exponent is 0 and the next position in
// in the input string is the string length minus one.
// You will also need a long variable to hold your result.
// While the exponent is smaller than the string length,
// Get the character at the next position in the input string
// and store it in a char variable
// Subtract '0' (not 0) from that character and multiply
// the result of that subtraction by (long) Math.pow(2, exponent)
// When the loop is finished, return the long variable holding the result
}
}
You will need to do the following things:
- Get a String of input from the user.
- Depending on whether the input is valid, announce the number's value; otherwise, let the user know it is bad input.
- Write a method to verify whether or not the user's input string is valid.
- Write a method to calculate the value of the user's input string.
Note that because of user input, the output will look different each time you run the program.
Program output examples:
Enter a valid binary number up to 60 digits long:
101010101010101
You entered 21845
Enter a valid binary number up to 60 digits long:
1101010001
You entered 849
Enter a valid binary number up to 60 digits long:
10101010130011
Bad input!
Questions: - How did the process of creating these programs go for you?
- What were your main challenges and how did you overcome them?
- What did you learn that may be of use as you move along in this class?