Applications are java programs that run from the command line (sometimes called stand-alone programs). All operating systems provide the facility to run such programs.
Applications are run using the java command - notice, with no extension
On Unix machines you are "at the command line" once you log in (typically the $ prompt). On Windows machines you need to get into DOS. See this page for DOS assistance. Particularly note difference in slashes and command differences (ls and dir, cat/more and type)
Typically, you cannot start an application with a GUI on a remote machine from your PC because of the display.
Our lab 6 does not use a GUI, so you should not have problems with the display. However, if you ever do want to run your applications with GUIs off of Unix on your local machine,
here is a link which will allow you to do so.
When
For applications, you can provide arguments to the program from the command line:
java MyProg arg1 arg2 arg3
(note: no extension of .class or .java) arg1 arg2 arg3 will be the values of the parameters you want to send. One often sees
a first line of code like Hmmm, consider if (args.length > 0) ... discuss the difference and array out of bounds Other ideas: Integer.valueOf(args[0]).intValue();
Since you are not inheriting from Applets now,
applications use more conventional backgrounds...
Frame allows the user to put Menus on them, both via MenuBar and
PopUpMenu
In PlayBalloon.txt we see
an old familiar Applet made into an application. Notice Frames also have a paint(Graphics g) method inherited from Container. Download PlayBalloon.java to run
This application is then enhanced to have a menu bar (see it)
Check out this old familiar code: as an application? HOW?
When we wanted to print things on Applets, we used the Graphics area and the command: For applications the output will typically go to the "default" output.
See the java.lang.System class and its Class Variables. (static)
Particularly for our purposes You will see the output on your console (the same place that you started your application with the java command.)
It is a good programming practice to not rely on an
individual Operating System for Windowing options. Always
provide your user with an exit menu option.
WordProcessor.java shows
code for menus - note that it does not process any words. menued WordProcessor
Our lab 6 does have user input. Why is this significant? Remember Commands|Open Terminal in WinSCP.
main
Applications must have a main method that
has the exact syntax as below:
(for lab 6 we will use it to pass how many salespeople)
java MyProgram
is called, Java looks in the MyProgram file for its main
method.
Applications do not instantiate from any superclass
or browser, so the programmer must explicitly instantiate the
class ... usually in the main(String[] args) method
For example: java Main 5
Note that the main method in the class called receives these arguments.
Hence you would need to access the args array to get these values.
Specifically,
args[0] (in the main method) would be whatever is typed above for arg1 in the MyProg call. In the second example, args[0] will equal the String 5.
if (args[0] != null)
This allows the program to do one thing if there are no parameters, and something else if there are parameters.
Also, note the parameters of main are String[] args .
To make an String into an int, consider using Integer.parseInt(String s) (Similar methods in other wrapper classes)
The GUI and Frames
Remember the Balloon Applet class? Here we will convert it to
an application.
Discuss clean OO code and Main.java.
Specifically, the class Frame.
Here was the Applet version using interfaces for controllers.
Output
g.drawString(String str, int x, int y)
What do you suppose this would be? You can use System.out.println() commands in an Applet too. Where would you see the output?
See for more on applications
Closing: System.exit(0)