CSCI15a: Chapter 19

CSCI15a: Notes Chapter 19
Applications
new edition this is in Chapter 17


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. 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 on a remote machine from your PC.

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.

main

Applications must have a main method that has the exact syntax as below: public static void main(String[] args) { // code here } The String[] args is used to pass parameters to the program
(in web archive lab 6 we see the parameters "save" and "restore")
(for campus 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, arg[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.java we see an old familiar Applet made into an application. This is really lazy of the text...it is using Java 1.0. Discuss how I know and what it means...
Here was the Applet version using interfaces for controllers.

This application is then enhanced to have a menu bar (done correctly)

Check out this old familiar code

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...and that it is java 1.0 code and thus will cause deprecation warnings. Again, how do I know this?