Sample Code from "Java How to Program, Fifth Edition" from Deitel and Deitel.


The following code gives to an example of a simple Java program.

Notice that the whole thing is a class, within it is a main function

Be sure to name your file the same as the class name

Welcome1.java

Compile with "javac Wecome1.java"

Run with "java Welcome1"


Another simple printing program with "print"

Welcome2.java

Compile and run as above except use "Welcome2"


Using "\n"

Welcome3.java

Graphical User Interface (GUI) example

Notice that we are making use of a predefined java class "JOptionPane", therefore, we "import javax.swing.JOptionPane"

Welcome4.java

Graphical User Interface (GUI) getting user input (showInputDialog) and printing out a message (showMessageDialog).

Addition.java

Comparisons in Java

Comparison.java
APPLETS

Basic "Welcome to Java Applet". Notice:

WelcomeApplet.java

Also need an html file:

WelcomeApplet.html (after clicking of this link "View" the "Source")

Compile with "javac WelcomeApplet.java"

Run by opening .html file in browser or

"appletviewer WelcomeApplet.html


Drawing two strings at different locations.

x and y coordinates are (0,0) in the upper left-hand corner

WelcomeApplet2.java WelcomeApplet2.html

Drawing lines

WelcomeLines.java WelcomeLines.html

Drawing rectangles and getting input from user. Notice overriding "init" function. This does the initialization of two numbers.

"paint" draws the results.

AdditionApplet.java AdditionApplet.html