ClickMe.html
above).
A slight modification of the above applet:
ClickMe2.
Echo demonstrates how to pass command line
arguments into a Java program. Command line arguments are treated
as elements of an array of strings, conventionally named args.
A slight modification of the Echo program shows how to address
individual characters in a string. Class Reverse prints out
command line argumets in reverse.
Here is a sample output of these programs:
> java Echo hello my dear > hello my dear > > java Reverse hello my dear > raed ym olleh
ContinueWithLabel demonstrates labeled versions
of break and continue statements. These allow us
to break out of (or, respectively, to continue with) an outer loop in a
series of nested loops. The program uses some methods from the
String class:
Palindrome determines whether a string is
a palindrome. This program uses many methods from the String
and StringBuffer classes. The program also demonstrates how
to set up text console based interactive input using
BufferedReader class. (Note that another possibility to
set up interactive input is to use
JOptionPane.showInputDialog() discussed in the
D&D textbook. To be consistent one also needs
to rewrite all program dialogs to use JOptionPane class then.)
Factorial defines factorial() method which
uses caching technique to optimize repeated computations of factorials.
The cache is implemented not as a fixed-size array, but as an instance
of java.util.Vector class, which gives us an array-like data
structure that can grow on demand. Since factorials quickly become very
large, we use class java.math.BigInteger, that is designed to
represent arbitrarily-large integer values and provides methods to perform
arithmetic operations on them. Because factorials are defined only for
non-negative integers, the factorial() throws an exception
if its argument is a negative integer. A simple main()
method provided just as a driver to test the factorial()
method. Class FactCalculator is a stand-alone interactive
GUI program, that allows user to compute factorials repeatedly. It uses
javax.swing.JOptionPane class to communicate with the user.
In order to protect the user (from his/her own mistakes), the program
tries to CATCH all possible EXCEPTIONS that might arise
because of bad input.
ListOfNumbers gives yet another demonstration of
exception handling in Java. In particular the difference between
so-called runtime exceptions and checked exceptions.
The latter should be either caught or explicitly declared by a
method which can throw them, while the former do not require such
explicit handling. Explicit handling of checked exceptions
is enforced by the Java platform.
(Note that this class does not define a stand-alone program, since
it does not have main() method. To test it one needs
to write a separate driver.)
LinkedList class suggested in
D. Flanagan, "Java Examples in a Nutshell". It demonstrates
many specific features of Java OOP (such as interfaces and
nested classes, for example).
HelloWorldSwing directly extends the JFrame and
performs all frame initialization in constructor and methods called from
constructor. This makes main() method very simple and
straightforward.
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.
java -jar colorswitch.jar
Alternatively, if the above does not work, you can first unpack the archive
and then run the application:
jar xvf colorswitch.jar
java ColorSwitch
The source code will be posted here:
ThreadsDemo.java) are packed into a single java
archive file threadsdemo.jar. When a browser runs the applet,
it loads the necessary classes from this jar file.
Timer and TimerTask classes
which are useful when you want to perform a task repeatedly or after
a delay. These classes were added to version 1.3 of Java2 platform.