Assignment-02a

 

 

 

Foreword

Resources

Code Listings

Foil Sets

Assignments

Solutions

External Resources

SiteMap

Search

Home

Up ] Assignment-01a ] Assignment-01b ] Assignment-01c ] Assignment-01d ] Assignment-02 ] [ Assignment-02a ] Assignment-03 ] Assignment-03a ] Assignment-03b ] Assignment-03c ] Assignment-03d ] Assignment-04 ] Assignment-04a ] Assignment-04b ] Assignment-05 ] Assignment-06 ] Assignment-07 ]

Writing a Java Applet

Unlike applications, Java applets are web-based, that is, they are transferred from server to client via HTTP. To facilitate this, every Java applet requires an HTML wrapper, which depends on a browser or appletviewer for the necessary runtime environment.

Whereas Java applications are run at the command line, applets are executed within a web page. To test your applet, load a web page containing it into your browser. Alternatively, an applet may be tested with a handy tool called appletviewer.

Your first applet is a modification of the program in Fig. 4.9 of the textbook. Before starting this assignment, we recommend you work through some simple exercises related to Java applets.

  1. Copy the file Craps.java to your Web document directory on osprey7.
  2. Compile Craps.java to a .class file with the command
      % javac Craps.java  
  3. Copy the file craps.html to the same directory and test with your browser.
  4. Modify the craps program to allow wagers. Initialize a class variable bankBalance to 1000 dollars and prompt the user to enter a wager. Check that the wager is less than or equal to bankBalance and if not have the user reenter the wager until a valid amount is entered. After a correct wager is entered, let the user play a game of craps. Increase or decrease the bankBalance by wager, as the case may be, and print the new bankBalance. When bankBalance becomes zero, print the message "Sorry, you are busted!".
  5. This is exercise 4.48 on page 227 of the textbook.
  6. Here's a simple user interface that supports the above exercise:

    You may use the source code of this non-functional version of Craps.java as a starting point. Note carefully the code that has been commented out. This was done to make the applet Java 1.0 compliant (and therefore compatible with most browsers).

  7. As always, your code should be neatly written, easy to read, and well documented. Sloppy code will lose points!
  8. Put two links on your homework page, one to an HTML document containing the applet and another to the applet source code.