Java Reference
In-Depth Information
The remaining exercises may be thought of as optional. Some students (and their pro-
fessors) may choose to go directly to Chapter 2. Exercises 1.3 through 1.9 require studying
the compiler in its entirety, if only cursorily, and then making slight modifications to it.
Notice that, in these exercises, many of the operators have different levels of precedence,
just as * has a different level of precedence in j-- than does + . These levels of precedence are
captured in the Java grammar (in Appendix C); for example, the parser uses one method
to parse expressions involving * and / , and another to parse expressions involving + and - .
Exercise 1.3. To start, follow the process outlined in Section 1.5 to implement the Java
remainder operator % .
Exercise 1.4. Implement the Java shift operators, << , >> , and >>> .
Exercise 1.5. Implement the Java bitwise inclusive or operator, | .
Exercise 1.6. Implement the Java bitwise exclusive or operator, ^ .
Exercise 1.7. Implement the Java bitwise and operator, & .
Exercise 1.8. Implement the Java unary bitwise complement operator ~ , and the Java
unary + operator. What code is generated for the latter?
Exercise 1.9. Write tests for all of the exercises (1.3 through 1.8) done above. Put these
tests where they belong in the code tree and modify the JUnit framework in the code tree
for making sure they are invoked.
Exercise 1.10 through 1.16 are exercises in j-- programming. j-- is a subset of Java and
is described in Appendix B.
Exercise 1.10. Write a j-- program Fibonacci.java that accepts a number n as input
and outputs the nth Fibonacci number.
Exercise 1.11. Write a j-- program GCD.java that accepts two numbers a and b as input,
and outputs the Greatest Common Divisor (GCD) of a and b. Hint: Use the Euclidean
algorithm 14
Exercise 1.12. Write a j-- program Primes.java that accepts a number n as input, and
outputs the all the prime numbers that are less than or equal to n. Hint: Use the Sieve of
Eratosthenes algorithm 15 For example,
>javaPrimes11
should output
>235711
Exercise 1.13. Write a j-- program Date.java that accepts a date in \yyyy-mm-dd"
format as input, and outputs the date in \Month Day, Year" format. For example 16 ,
>javaDate1879-03-14
should output
>March14,1879
14 See http://en.wikipedia.org/wiki/Euclidean_algorithm.
15 See http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes.
16 March 14, 1879, is Albert Einstein's birthday.
 
Search WWH ::




Custom Search