|
|
|
|
Exercises
Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from www.BruceEckel.com.
- There are two expressions in the section labeled “precedence”
early in this chapter. Put these expressions into a program and demonstrate that
they produce different results.
- Put the methods ternary( ) and alternative( ) into
a working program.
- From the sections labeled “if-else” and “return,”
modify the two test( ) methods so that testval is tested to
see if it is within the range between (and including) the arguments begin
and end.
- Write a program that prints values from 1 to 100.
- Modify Exercise 4 so that the program exits by using the break
keyword at value 47. Try using return instead.
- Write a method that takes two String arguments and uses all the
boolean comparisons to compare the two Strings and print the
results. For the == and !=, also perform the equals( )
test. In main( ), call your method with some different String
objects.
- Write a program that generates 25 random int values. For each value,
use an if-else statement to classify it as greater than, less
than, or equal to a second randomly-generated value.
- Modify Exercise 7 so that your code is surrounded by an
“infinite” while loop. It will then run until you interrupt
it from the keyboard (typically by pressing Control-C).
- Write a program that uses two nested for loops and the modulus
operator (%) to detect and print prime numbers (integral numbers that are
not evenly divisible by any other numbers except for themselves and 1).
Create a switch statement that prints a message for each
case, and put the switch inside a for loop that tries each
case. Put a break after each case and test it, then remove
the breaks and see what happens.
href="TIJ305_013.htm">[17] John Kirkham writes, “I started computing in 1962 using FORTRAN II on an IBM 1620. At that time, and throughout the 1960s and into the 1970s, FORTRAN was an all uppercase language. This probably started because many of the early input devices were old teletype units that used 5 bit Baudot code, which had no lowercase capability. The ‘E’ in the exponential notation was also always upper case and was never confused with the natural logarithm base ‘e’, which is always lowercase. The ‘E’ simply stood for exponential, which was for the base of the number system used—usually 10. At the time octal was also widely used by programmers. Although I never saw it used, if I had seen an octal number in exponential notation I would have considered it to be base 8. The first time I remember seeing an exponential using a lowercase ‘e’ was in the late 1970s and I also found it confusing. The problem arose as lowercase crept into FORTRAN, not at its beginning. We actually had functions to use if you really wanted to use the natural logarithm base, but they were all uppercase.”
|
|
|