Programming Exercises
For Chapter 4
THIS PAGE CONTAINS programming exercises based on
material from Chapter 4 of this on-line
Java textbook. Each exercise has a link to a discussion of one possible solution of that
exercise.
Exercise 4.1:
To "capitalize" a string means to change the first
letter of each word in the string to upper case (if it is not
already upper case). For example, a capitalized version of
"Now is the time to act!" is "Now Is The Time To Act!".
Write a subroutine named printCapitalized that will print
a capitalized version of a string to standard output. The
string to be printed should be a parameter to the subroutine.
Test your subroutine with a main() routine that gets
a line of input from the user and applies the subroutine to it.
Note that a letter is the first letter of a word if it is not
immediately preceded in the string by another letter.
Recall that there is a standard boolean-valued function
Character.isLetter(char) that can be used to test whether
its parameter is a letter. There is another standard char-valued
function, Character.toUpperCase(char), that returns a capitalized
version of the single character passed to it as a parameter. That is,
if the parameter is a letter, it returns the upper-case version.
If the parameter is not a letter, it just returns a copy of the parameter.
See the solution!
Exercise 4.2:
The hexadecimal digits are the ordinary, base-10 digits '0' through '9'
plus the letters 'A' through 'F'. In the hexadecimal system, these digits
represent the values 0 through 15, respectively.
Write a function named hexValue that uses a switch
statement to find the hexadecimal value of a given character. The character
is a parameter to the function, and its hexadecimal value is the return value
of the function. You should count lower case letters 'a' through 'f' as having
the same value as the corresponding upper case letters. If the parameter
is not one of the legal hexadecimal digits, return -1 as the value of the
function.
A hexadecimal integer is a sequence of hexadecimal digits, such as 34A7, FF8,
174204, or FADE. If str is a string containing a hexadecimal integer,
then the corresponding base-10 integer can be computed as follows:
value = 0;
for ( i = 0; i < str.length(); i++ )
value = value*16 + hexValue( str.charAt(i) );
Of course, this is not valid if str contains any characters that are
not hexadecimal digits. Write a program that reads a string from the user.
If all the characters in the string are hexadecimal digits, print out the
corresponding base-10 value. If not, print out an error message.
See the solution!
Exercise 4.3:
Write a function that simulates rolling a pair of dice until the total on
the dice comes up to be a given number. The number that you are rolling for
is a parameter to the function. The number of times you have to roll the
dice is the return value of the function. You can assume that the parameter
is one of the possible totals: 2, 3, ..., 12. Use your function in
a program that computes and prints the
number of rolls it takes to get snake eyes. (Snake eyes
means that the total showing on the dice is 2.)
See the solution!
Exercise 4.4:
This exercise builds on Exercise 4.3. Every time you roll the dice repeatedly,
trying to get a given total, the number of rolls it takes can be different.
The question naturally arises, what's the average number of rolls?
Write a function that performs the experiment of rolling to get a given
total 10000 times. The desired total is a parameter to the subroutine.
The average number of rolls is the return value. Each individual experiment
should be done by calling the function you wrote for exercise 4.3.
Now, write a main program that will call your function once for each of
the possible totals (2, 3, ..., 12). It should make a table of the
results, something like:
Total On Dice Average Number of Rolls
------------- -----------------------
2 35.8382
3 18.0607
. .
. .
See the solution!
Exercise 5:
The sample program RandomMosaicWalk.java
from Section 4.6 shows a "disturbance" that
wanders around a grid of colored squares. When the disturbance visits a
square, the color of that square is changed. The applet at the
bottom of Section 4.7 shows a variation on
this idea. In this applet, all the squares start out with the
default color, black. Every time the disturbance visits a square,
a small amount is added to the red component of the color of that
square. Write a subroutine that will add 25 to the red component of
one of the squares in the mosaic. The row and column numbers of the
square should be passed as parameters to the subroutine. Recall
that you can discover the current red component of the square in
row r and column c with the function call
Mosaic.getRed(r,c). Use your subroutine as a substitute for
the changeToRandomColor() subroutine in the program
RandomMosaicWalk2.java.
(This is the improved version of the program from Section 4.7
that uses named constants for the number of rows, number of columns,
and square size.) Set the number of rows and the number of columns
to 80. Set the square size to 5.
See the solution!
Exercise 6:
For this exercise, you will write a program that has the same behavior
as the following applet. Your program will be based on the non-standard Mosaic
class, which was described in Section 4.6.
(Unfortunately, the applet doesn't look too good on many versions of Java.)
The applet shows a rectangle that grows from the center of the
applet to the edges, getting brighter as it grows. The rectangle is
made up of the little squares of the mosaic. You should first
write a subroutine that draws a rectangle on a Mosaic window.
More specifically, write a subroutine named rectangle such that
the subroutine call statement
rectangle(top,left,height,width,r,g,b);
will call Mosaic.setColor(row,col,r,g,b) for each little square
that lies on the outline of a rectangle. The topmost row of the rectangle
is specified by top. The number of rows in the rectangle is
specified by height (so the bottommost row is top+height-1).
The leftmost column of the rectangle is specifed by left. The
number of columns in the rectangle is specified by width
(so the rightmost column is left+width-1.)
The animation loops through the same sequence of steps over and over.
In one step, a rectangle is drawn in gray (that is, with all three color
components having the same value). There is a pause of 200 milliseconds
so the user can see the rectangle. Then the very same rectangle is
drawn in black, effectively erasing the gray rectangle. Finally, the
variables giving the top row, left column, size, and color level of the
rectangle are adjusted to get ready for the next step. In the applet,
the color level starts at 50 and increases by 10 after each step.
You might want to make a subroutine that does one loop through
all the steps of the animation.
The main() routine simply opens a Mosaic window and then
does the animation loop over and over until the user closes the window.
There is a 1000 millisecond delay between one animation loop and the next.
Use a Mosaic window that has 41 rows and 41 columns. (I advise you
not to used named constants for the numbers of rows
and columns, since the problem is complicated enough already.)
See the solution!