CSCI111: Applications

CSCI 111
Applications


Applications: Free-standing Programs

Applets are java programs that run on web pages or browsers.

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

java MyProgram

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.

If you try remotely starting an application with a GUI from your PC (or a machine where you have not set the DISPLAY), you will get an error that looks like: This makes sense - you are trying to open a GUI on a machine where you are not really at the console on which the machine wants to open it. So, work on applications should be done and tested on the local machine and then, when complete, downloaded to the web server for submission. Note that if the application does not have a GUI, then there is no problem.

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.
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: public static void main(String[] args) { // code here } The formal parameter of   (String[] args)    is used to pass parameters to the program
(for lab 6 we will use it to pass how many salespeople)

When
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 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.
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.

One often sees a first line of code like
if (args[0] != null)
This allows the program to do one thing if there are no parameters, and something else if there are parameters.

Hmmm, consider if (args.length > 0) ... discuss the difference and array out of bounds
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)

Other ideas: Integer.valueOf(args[0]).intValue();

The GUI and Frames

Remember the Balloon Applet class? Here we will convert it to an application. Discuss clean OO code and Main.java.

Since you are not inheriting from Applets now, applications use more conventional backgrounds...
Specifically, the class Frame.

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
Here was the Applet version using interfaces for controllers.

This application is then enhanced to have a menu bar (see it)

Check out this old familiar code: as an application? HOW?

Output

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.
What do you suppose this would be?

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.)

See for more on applications

Closing: System.exit(0)

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