- a) Explain the difference between primitive types and reference types.
b) Describe the two different ways Java uses to pass parameters and state which
way applies to each of the types described above in (a).
- Exercise 5.17 If you don't frequent gambling halls (and thus might not be
intimately familiar with the game of "craps"), you can read a description of
the game in the textbook in section 4.8. There's also source code for an
applet which plays craps (Figure 4.9), but you won't need much of what's in
Figure 4.9, because you don't need any user input. (You can write either an
applet or an application for this exercise.)
If you write an applet, you can save yourself some trouble by using
System.out.println() to print out results, instead of the
drawString() method. If you use System.out.println(),
you'll see the results either in the Messages window (in Visual Cafe) or
in the Java console of the browser (either Netscape or Explorer). Neither
browser displays the Java console by default, so you'll have to ask to see
it. In Netscape use the Communicator menu and choose Java Console. In Explorer,
use either Java Console (Windows) or Java Messages (Mac) under the View menu.
- Exercise 5.20 For this exercise you should write an applet. Your applet
should have text fields for a user to enter a salesperson number (1-4), a
product number (1-5) and total sales for that person. The lower portion of
the applet should then update the table which shows sales totals (broken
down by salesperson and total).
A fairly bare interface (which is fine) might look like below:

As you work in this problem, here are some things to think about:
- You should separate the display of the sales totals from the
updating of the totals. You'll want to use the same array for both,
but updating (storing new values in the array) should be distinct
from showing the current values.
- For updating, you'll need to read the values from the TextFields
and then store them in the appropriate element in the array, sales.
- For display, you want to print out the table showing the array values
and the totals. The paint() method would be a good place to draw the
table.
- After the array is updated, you should display the updated array values.
You'll probably want to include the code for updating in the
actionPerformed() method. At the end of your actionPerformed()
method, you can call the repaint() method, which will then (magically) call the
paint() method. If you followed the previous tip (and included your
display code in the paint() method), then the new array will be
displayed every time the array is updated. Cool, huh?
- OPTIONAL If you prefer, you can solve this problem, rather than the
previous one.
Write an applet which displays a Label object with the text of your choice.
Your applet should change the foreground and background colors of the label
as the user moves the mouse over the label -- every mouse position (over the
label) should have a unique foreground and background color combination.
(If you want to randomly generate the color combinations, that's okay, but every
possible color combination be included in your random generation i.e., if you
used the applet for long enough, you'd see all the possible color combinations.)
Once the user moves the mouse off the label, the colors should stop changing until
the mouse moves back over the label.
Here are a few tips:
- Take a look at the Label class. If you look in the Java docs, you'll see that a Label is a
Component, so some of the properties of Labels will only be described in
the documentation for
Components.
- You'll need to generate new color values, and you'll find what you need
to know in the docs for the Color
class.
- Remember that you'll need to test your applet with Internet Explorer,
since Netscape won't work.