CPSC 124 (Fall 1998): Lab 6
Writing Simple Classes


WELCOME TO THE SIXTH LAB for Computer Science 124. During the first part of this lab, you will write two simple classes. The first class represents a "pair of dice" that can be used in a dice game like craps. In the second exercise, you will build on one of the examples from the text by defining a subclass of an existing class. In the second part of the lab, we take a break from Java programming so that you can work on your Web pages.

Before you begin, copy the folder /home/cs124/lab6 into your home directory.

The exercises at the end of the lab are due in class on the Monday following the lab.


Outline of the Lab


Dice as Objects

You have now worked on several programs that use a simulated pair of dice. Each of these programs use commands like

        die1 = (int)(Math.random()*6 + 1);
        die2 = (int)(Math.random()*6 + 1);
        roll = die1 + die2;

This doesn't look like it has much to do with dice! A pair of dice is an object, and it should be possible to use an object in a program to represent a pair of dice. For your first exercise of the lab, you will write a PairOfDice class.

The files RollForSeven.java and crapstat2.java are modified versions of programs you have seen before. You will find these programs in your lab6 directory. The first program rolls dice until the roll comes up seven and reports the number of rolls. The second simulates 100000 games of craps and reports the number of wins. Each of these programs uses the class PairOfDice to represent a pair of dice. However, this class does not exist yet. If you try to compile the programs, you will get an error message telling you that class PairOfDice cannot be found.

Your assignment is to write a PairOfDice class that will work with the RollForSeven and crapstat2 programs. You should have private instance variables to represent the numbers showing on the two dice. These numbers change when the dice are rolled. You will need a method called roll() that rolls the dice and returns the total. You will also need methods called getDie1() and getDie2() that return the numbers showing on each of the dice. When an object is created, you should make sure that the instance variables are in a legal state. If you don't assign values to them, they will have the value zero, which is not possible with real dice. (You can either initialize the instance to some legal values like 3 and 4, or roll the dice when an object is first constructed.) These initial values are not used in either of the sample program, but when you are simulating real objects, it's a good idea to simulate them as closely as possible.

Your class should be written in a file named PairOfDice.java. After you have written and compiled it, you should be able to compile and run RollForSeven.java and crapstat2.java.


A Subclass of Shapes

The ShapeDraw applet is used in Section 4.3 of the text to illustrate the ideas of inheritance and polymorphism. In this applet, the user can place shapes of various types on a canvas and drag them around. The original applet works with Rectangles, Ovals, and RoundRects. The textbook says,

"In this applet the only time when the actual class of a shape is used is when that shape is added to the screen. Once the shape has been created, it is manipulated entirely as an abstract shape. The routine that implements dragging, for example, works only with variables of type Shape. As the Shape is being dragged, the dragging routine calls the Shape's draw method over and over. The Shape is responsible for drawing itself correctly. If I wanted to add a new type of shape to the program, I would define a new subclass of Shape, add another button to the applet, and program the button to add the correct type of shape to the screen. No other changes in the programming would be necessary."

Your second exercise of the lab is to modify the original ShapeDraw program in just this way. The new program will support ``StripedRects.'' I define a striped rect to be a rectangle with a nested set of rectangles drawn inside it. You can try out my modified applet on a separate page. The stripes on a StripedRect are black, except in the case that the rectangle itself is black. In that case, the stripes are red.

Your lab6 folder contains the file ShapeDraw2.java, which is a copy of the original ShapeDraw.java program, except that the name of the main class has been changed to ShapeDraw2. You can compile this program and use "appletviewer ShapeDraw2.html" to execute it. You should edit this file to make the following changes:

  1. At the bottom of the file, define a new class, StripedRectShape. Your class only needs to define a draw() method. In fact, it can be a copy of the RectShape class, except that drawing the shape is a little more complicated.
  2. Near the top of the file, in the init() method of the ShapeDraw2 class, add a stripedRectButton. You should be able to figure out enough to imitate what is done there for rects, ovals, and roundRects. In addition to creating the stripedRectButton object, you have to "add" it to the "bottom panel", just as is done with the other buttons. This is what makes the button actually appear on the screeen.
  3. Find the actionPerformed() method in the ShapeCanvas class. This is the method that is called when the user clicks on a button. The command string in this method is the label on the button that was clicked. The original method creates a rect, oval, or roundRect, when the user clicks on the corresponding button. You have to add a few lines to this method to handle the stripedRect button.

When your applet is finished, publish it in your www directory. (There are seven different class files that need to be copied. Or you could just say "cp *.class ~/www" to copy all the class files from your lab6 directory to your www directory. The * is a "wildcard" character, and *.class stands for all files that end in .class.


Working on Your Web Page

So far in this course, you haven't learned much about the Web page markup language HTML. As the final exercise of this lab, you should take the next step in learning HTML and using it to improve your home page.

Section 5.3 of the text contains a lot more detail about HTML (although it still covers only the basics of the language). You should read that section of the text, and I won't repeat that material here. Your new, improved home page should include at least one image, a list made using <UL> or <OL>, some colored text, headlines of various sizes, and text of various colors and styles. Include links to a few Web sites, such as the textbook for this course, the Hobart and William Smith Colleges home page, and www.yahoo.com. Include links to pages with the applets you have made for this class. Use at least one animated graphic, like the animated orb at the beginning of this paragraph. This graphic, and many others, are available from the Icon Bazaar at http://www.iconbazaar.com. You can use up to about 20 graphics from Icon Bazaar without paying any fee.

Your page should also have a background image. This is one nice feature that is not mentioned in Section 5.3. If you have any image file, you can use it as a background for you page. The image doesn't have to be very big. It will be repeated as often as necessary to fill the Web browser window. To use an image file as a background on a page, include the backgournd modifier in the <BODY> tag to specify the name of the image file. For example, if the name of the image file is "myBkg.gif", you would use:

           <BODY background="myBkg.gif">

Of course, you might also want to use this tag to set text colors for your page, as described in Section 5.3. By the way, even if you use a background image, it's a good idea to specify a bgcolor in the <BODY> tag, because some people might have turned off loading of images. Also, the bgcolor will be used on the page while the background image is being loaded.

You'll find both tasteful and extravagant graphics and backgrounds at Icon Bazaar. You can also find graphics on many Web pages, but you should be sure that you know the owner's copyright policy before using someone else's work. You can make your own graphics with painting and drawing programs, by scanning photographs, or by using a digital camera.

Note: To get a graphic off a Web page in Netscape, just click on the graphic with the right mouse button. (On a Macintosh, click the mouse button and hold it down for a few seconds.) A pop-up menu appears that allows you to save the image to a file. After you have the file, you still have to transfer it to your www directory on math.hws.edu. To do this, you can mount your Linux account as a networked drive on the Windows desktop. If you don't know how to do this, ask for help from me or from a TA.


Exercise 1. (7 points) Turn in a printout of your PairOfDice.java file. Make sure that you follow good programming style conventions, including formatting and comments. Your class must work with both sample programs, RollForSeven.java and crapstat2.java.

Exercise 2. (7 points) Turn in a printout of your ShapeDraw2.java file, modified to support the "StripedRect" class. Also, post your applet on the Web so I can try it.

Exercise 3. (7 points) Write an essay discussing the advantages of object-oriented programming, as illustrated in this lab. Be sure to discuss both exercise 1 and exercise 2. I would expect your answer to be at least close to a full page in length.

Exercise 4. (9 points) Work on your Web page! Read the requirements for your new, improved page above.


[ Lab Index | Online Notes ]