EXAMPLES

PIC 20A, Spring 2002


  1. The very first example, traditional "Hello World!" program:
  2. A first Applet, example is cited from The Java Tutorial: a spot appears when you click the mouse within the applet's bounds: The source files for the applet: Certainly, to run the applet, you also need to create and HTML file with appropriate APPLET tag in it (see the source of the ClickMe.html above).
  3. First steps in defining and using objects:
  4. First steps in defining and using objects continued:
  5. First non-trivial example: a minimalistic class that represents a bank account. It comes along with a driver program.
  6. Example of a LinkedList which can handle any Linkable objects. It comes along with a driver program and with a sample implementation of the Linkable interface. This is a simplified version of an excellent example of a LinkedList created by D. Flanagan (see "Java Examples in a Nutshell").
  7. This example demonstrates implementation of inheritance and polymorphism in Java Platform. It is based on example 5 above. A set of specific bank accounts derived from BankAccount.java. This is a slightly modified version of an excellent example created by Prof. Cay Horstmann.
  8. An example of exception handling mechanism in action. This is a modified version of a set of examples of computation of factorials created by D. Flanagan (see "Java Examples in a Nutshell").
  9. An example on use of various text/number formatters. It also features a static nested class. This example is created by D. Flanagan (see "Java Examples in a Nutshell").
  10. An example of the simpliest applet, traditional HelloApplet:
  11. Three examples demonstrating Life Cycle of an Applet, along with responce to mouse events and other simple features. To run all three applets, please see Simple.html.
  12. Two sample applets from Official Java2 SDK Demos by Sun Microsystems. These might help you to get started with homework #6.
  13. This applet allows user to plot f(x) = x * sin( 1/xa ) for any decimal value of exponent a , where -10 < a < 10 . User can specify the desired exponent and the horizontal range of the graph: -b < x < b . The applet automatically adjusts the vertical range -c < y < c of the graph in accordance with the chosen horizontal range of the graph, so that maximum and minimum of the current function fit within the plotting area. In addition to the graph of the function, the applet plots coordinate axes in a different color. User can repeat plotting with different parameters any number of times. Finally, this applet can be also run as a stand-alone application, without any browser support.
  14. This program is our first example of Java Threads API. The example is cited from D. Flanagan "Java Examples in a Nutshell".
  15. A multithreaded applet that continuously runs two threads of randomly choosen priorities. One thread sleeps for 1000 milliseconds before printing the current time, while the other sleeps for 1500 milliseconds before printing the current time. After printing the time, each thread tries to go to sleep again. This cycle is repeat until the applet stops. Each thread is given a name. The name and current priority level of each thread are printed along with its time. In addition to different names, the two threads use different colors for printing (red and blue). All class files of the applet (i.e. files generated by compilation of ThreadedAppletDemo.java) are packed into a single java archive file threadedappletdemo.jar. When a browser runs the applet, it loads the necessary classes from this jar file.


Last modified on by fedandr@math.ucla.edu.