Design and implement an application that simulates binary integers using boolean arrays. Repeatedly (but at least once) execute the following steps:
- Declare a boolean array (but don't initialize yet), a long variable (initialize to 0), and a String variable (initialize to "")
- Ask the user what type they want (byte, short, int)
- Assign an appropriate size boolean array to the declared array variable (8 for byte, 16 for short, etc.)
- Randomly initialize each variable in the array to true or false (Hint: The Random class in the java.util package has an instance method that can generate random true and false values.)
-
Process the boolean array as if it were a binary number, which you can accomplish via the following steps:
- Print both the binary representation and the integer value.
- Prompt the user to continue or quit
Code:
// Import any classes from java.util, as needed
public class BinaryWithBooleans {
public static void main(String[] args) {
}
// This may be easier than Math.pow(...) because
// you won't have to do any casting.
private static long power(long base, long exponent){
if (exponent == 0)
return 1;
else if (exponent == 1)
return base;
else{
long median = (exponent % 2 == 0) ? exponent / 2 : (int) Math.ceil(exponent / 2.0);
return power(base, median) * power(base, exponent - median);
}
}
}
Program output example:
What type of integer do you want?
1 for byte
2 for short
3 for int
Make your choice: 1
You chose...byte
01001010 is 74
Continue (true or false): true
What type of integer do you want?
1 for byte
2 for short
3 for int
Make your choice: 2
You chose...short
1101101101000111 is -9401
Continue (true or false): true
What type of integer do you want?
1 for byte
2 for short
3 for int
Make your choice: 3
You chose...int
10011100001101001001000101011011 is -1674276517
Continue (true or false): false
Question: How did the process of editing this program 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?