Java Student Project         


Sum Integers
Ability Level: Intermediate
Estimated Time: 45 minutes
Objectives: · Learn about the applet life cycle

· Learn how to create a more sophisticated Interactive applet

· Learn how to use the setLocation() method

· Learn how to disable a component

· Learn how to get help

Materials & Resources:
  • Java Software
  • Textbook
Overview: When an applet is started in the web browser or applet viewer you see "initializing... starting..." as the result of the applet being loaded. When an applet is loaded an instance of the applet's class is created, the applet is initialized, and then starts running.

When the user leaves the web page the applet stops itself, and can start itself when the user returns to the page. The same will occur when the user minimizes and then maximizes the window that contains the applet.

Some browsers reload the applet when you return to its page. If you are testing your applet in a web page and you modify the applet, recompile it and then try to reload the web page into the browser by clicking the reload button. The browser may or may not actually reload the applet depending on the browser version. When developing applets, it is best to use the applet viewer. When you are satisfied with the applet, test it in both the Netscape and Microsoft browsers to see how it behaves.

Note when the user quits the browser or applet viewer, the applet has a chance to stop itself before the browser exits and do any cleanup procedures necessary.

Instructions: Create an applet named SumIntegers that allows the user to enter two integers into two separate TextFields. When the user clicks a Button, the sum of the integers is displayed.
Hints: Some important points to keep in mind in doing this project:

I. The Applet life cycle

    A. When an applet is loaded the init( ) method executes. The init( ) method may be provided by the programmer, but if not, it is automatically provided. The process of providing your own init( ) method to replace the one created by Java is called overriding the method. Overriding methods is very common in object-oriented programs. The init( ) method is one of the methods that is automatically provided because the applet extends the Applet class

    B. The start( ) method executes after the init( ) method. It will execute again every time the applet becomes active, usually when the user leaves the web page, then returns. A common use of this method is to start an animation

    C. The stop( ) method is invoked when the user leaves the web page. Often code is placed here to stop a thread or animation at this point

    D. The destroy( ) method is called when the user closes the browser or applet viewer 

II. Using the setLocation() method 

     A. The setLocation( ) method of a GUI object may be used to control its placement in the applet 

     B. The methods specifies and x-coordinate which starts at 0 and increases as you go left to right on the screen, and a y-coordinate that starts at the top of the screen at at 0 and increases as you move down the screen

     C. The coordinates are measured in pixels. The range of pixels is determined by the HEIGHT and WIDTH paarameters you provide in the HTML file that loads the applet.

III. Getting Help in Java

     A. Some sources of help with Java programming can be found at http://java.sun.com where all of the Java documentation may be read, searched or downloaded.

     B. The Sun site, as well as many other Java web sites, provide Frequently Asked Questions (FAQ) sections where developers can submit questions and post answers in a searchable format.

     C. Another web site with a wealth of information on Java is http://www.gamelan.com. The name gamelan implies that the site provides games, but is actually the name of a type of orchestra that exists on the island of Bali, and is pronounced gamma-lawn.

     D. Using the Java language to any extent requires constant use of reference materials due, in part, to the huge number of classes that are provided by the Java APIs. Using these APIs requires that you often have to research the classes for information about their methods and attributes. 

Extra: