Download public class HelloWorld { public static void main(String args

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 1
Elementary Applications
By tradition, the first program in many programming language is
named HelloWorld. It is designed to show the user the elements of
syntax, compiling, and displaying a very simple program. In
addition there are various issues about word processors and
directories that need to be considered.
The program we shall write will output the string
Hello World!!!!!
To write the program first create a directory called HelloWorld.
The HelloWorld directory I have created is a subdirectory of my
Pic20 directory, which in turn is a subdirectory on my Z drive.
Its full address is
Z:\Pic20\HelloWorld
Then using a simple editor, such as Notepad on a Windows system,
type in the program below and save it in a file named
HelloWorld.java.
Put the file in the HelloWorld directory you just created. Thus
the address of the file is
Z:\Pic20\HelloWorld\HelloWorld.java
Do not use Word or any other fancy word processor. Such
processors have various commands imbedded in their files which
confuse the compiler.
The program is:
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("
Hello World!!!!!");
}
}
The most important point of syntax at this time is that the name
of the class HelloWorld, in the first line, be identical to the
name used in the .java file HelloWorld.java.
1
There is only one instruction in this program
System.out.println("
Hello World!!!!!");
Note that it ends with a semi-colon. All instructions in java end
with a semi-colon.
As for the remainder, just type in what is here. Explanations for
the code will come later.
To compile the program, assuming that the HelloWorld.java file is
in the directory is in the HelloWorld directory, open the DOS
command window change your directory to Pic20\HelloWorld, and
then execute the command javac HelloWorld.java. See below:
If you have not made any errors you will get the message above.
If you have made an error, the compiler will print out a message,
giving you a clue as to where the error might be.
To run the program execute java HelloWorld. The string
"
Hello World!!!!!", will appear:
If you look at your HelloWold directory at this point you will
see two files; one is the text file HelloWorld.java. The other,
HelloWorld.class, was produced at compile time when you executed
the java HelloWorld command.
2
Some Generalities
Several generalities are in order at this point. Java programs
are, at bottom, classes. The program itself is typed in as a
NAME.java text file, using a simple word processor, such as
Notepad. The program is compiled using the command
javac NAME.java. It is run with the command java NAME. The
compiler is case sensitive.
Example 2
In this program the user will input a positive integer n and the
program will return n!. That is if n = 3, 3! = 6; if n = 7, 7! =
720. The input and output will look like this:
When the program is first run the screen on the left appears,
prompting the user to enter an integer. The user types in 7, and
presses the OK button. The computer then responds.
The skeleton of our program is:
import javax.swing.JOptionPane;
public class Factorial
{
public static void main(String args[])
{
}// end main(String args[])
}// end class Factorial
3
The first line, import javax.swing.JoptionPane, says the program
will use the "built-in" class JoptionPane. The class is one of
many that are part of the java swing system; it is used to
produce the input and output screens above.
Next, the first line, public class Factorial , says the program
is called Factorial. Curly brackets
{
}
are used in java to mark the beginning and end of blocks of code.
The double pair of slanted lines, // , is used to mark a comment.
It is a good programming practice to introduce the brackets that
begin and end a block of code and to comment where the block ends
before writing any code. When our programs become more complex
one can spend a great deal of time debugging, especially if the
blocks are not marked.
Our class Factorial is going to have one main routine
public static void main(String args[]).
This line is common to all java applications.
Although there is no code defining what the class Factorial does
it can be compiled:
Compiling empty code afer it has been outlined is a good practice
because the compiler will indicate most simple syntax errors
(misspellings, unbalanced brackets, etc) at compile time.
The complete program follows:
4
import javax.swing.JOptionPane;
public class Factorial
{
public static void main(String args[])
{
String w;
int n;
w = JOptionPane.showInputDialog("Enter a positive integer");
n = Integer.parseInt(w);
int product = 1;
for (int i = 1; i <= n; i++) product = product*i;
JOptionPane.showMessageDialog
(
null,
"When n = "+ n + ", "+ n+"! = " + product,
"Factorials",
JOptionPane.PLAIN_MESSAGE
);
System.exit(0);
}// end main(String args[])
}// end class Factorial
The first two lines of main(String args[]) block
String w;
int n;
illustrate two of java's primitive types, the String and the
integer, which is denoted by int. Two variables w, a string, and
n, an integer are declared.
5
The next line
JOptionPane.showInputDialog("Enter a positive integer");
utilizes the built-in class JoptionPane . A "class" is a piece of
software that is dedicated to one or more tasks. In this line it
uses a method of the class, showInputDialog(), to produce the
screen:
A method is sometimes described in other languages as a function
or a procedure; java uses the word method.
The method showInputDialog() is part of the JOptionPane class and
the link is shown in the syntax "className.methodName()". The
argument of the method "Enter a positive integer" is a message to
be shown on the screen.
The complete instruction
w = JOptionPane.showInputDialog("Enter a positive integer");
has two purposes. First, it produces the screen shown above.
Second, after the user types in an integer and clicks on the OK
button, it captures the number typed in. It is captured as the
string w.
The string w has to be converted to an integer, n. This done by
the next line:
n = Integer.parseInt(w);
Integer is a java class that contains a method parseInt(w). The
method takes the string w and coverts it to an integer. We will
discuss classes and their methods in great detail later.
The line
int product = 1;
accomplishes two ends. First, it declares that product is a
variable of type integer. Second, it intializes the value of the
product to be 1. This needs to be done so that the for loop works
correcrtly:
for (int i = 1; i <= n; i++) product = product*i;
6
Note that if a for loop has only one instruction in its defining
block then curly brackets , { and }, are not needed.
The
lines
JOptionPane.showMessageDialog
(
null,
"When n = "+ n + ", "+ n+"! = " + product,
"Factorials",
JOptionPane.PLAIN_MESSAGE
);
can be read as follows. The class
JoptionPane
will produce the output pane. This class has a method
showMessageDialog( , , , )
for producing the output screen:
This method has 4 variables. These variables are null, a string
that will appear in the output, the title for the output Pane,
and a command to erase the green question mark that appears on
the input message.
The string in
"When n = " + n + ",
" + n +"! = " + product
shows another feature of java: Strings, included between double
quote marks, can be combined with integers by means of + signs.
The grand total will be interpreted and displayed as a string.
The last command
System.exit(0);
closes the program when the user clicks the OK button in the
output panel.
7