1. Evaluate the following Java statements to explain either the value each variable is initialised to or why the statement is invalid:
a) int a = 10 - 3 * 6 + 7 / 2;
b) int b = 2 * 3 * 4 / 5 * 6;
c) int c = 3 / (6 / 9);
d) int d = 4/5/6;
e) int e = 12345678 * 78910112 * 4567895 * 10;
f) double f = 2 + 3;
g) float g = 2 + 3;
h) String h = "Hello" + "World";
i) String i = "Hello" - "o";
j) String j = 2 + "3";
k) char k = 'a' + 3;
l) double l = 2.345 + 6 * 4.55 / 3.5 - 5.4;
m) int[] m = new int[1];
n) int[][]n = new int[20][];
o) String[] o = new String[5][5];
p) boolean[] p = new int[5];
q) long[][][] q = new long[4][][4];
r) int r = 3 + 5.1 * 10;
After the problem class, put these declarations in to a program and try to compile/run them.
2. Prove that this program terminates if x is initialised to a positive integer before the loop:
while (x !=1)
{
if (even(x)) // even(x) returns true if the value of x is even
{
x = x / 2;
}
else
{
x = 3 * x + 1;
}
}
This question is from a well respected book called Programming Pearls by Jon Bentley. Locate and read a copy.
3. Develop an iterative algorithm (i.e., using loops) in Java to determine the square root of a floating point number.
4. Develop an algorithm in Java to convert a value of type int to type String. For example given the int 1234, the String "1234" would be created.
5. Develop an algorithm in Java to convert a String of digits into a value of type int. For example, "4567" would be converted to the int 4567. Remember that the method charAt(int n) returns the character at position n within a String.
6. Develop algorithms in Java to a) convert a String representing a binary number into a decimal int. For example, "1101" would convert to the int 13. b) convert an int to a binary number represented as a String. For example, 13 would convert to "1101".
7. Develop an set of methods that can 'verbalize' any number between 0 and. For example, given 123 the result would be 'one hundred and twenty three'.
8. Develop algorithms to generate all the prime numbers that can a) be represented by a Java int. What is the fastest way this can be done? b) be represented by a String of digits (e.g., "123" representing the value 123) of up to 256 characters in length (very big numbers).
You should be finding and reading the key books in the areas of Science, Computer Science, Mathematics, Design and beyond. Here are a few ideas:
Books by Douglas Hofstadter including "Godel, Escher, Bach: An Eternal Golden Braid" and "Metamagical Themas". These are very well regarded (and serious) books that are well worth the time reading.
"A Pattern Language: Towns, Buildings, Construction" by Christopher Alexander. An introduction to design patterns. Very relevant to the design and implementation of programs.
"A Place of My Own: The Education of an Amateur Builder" by Michael Pollan