Download //This is a comment. public class MyProgram { public static void main

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

Library (computing) wikipedia , lookup

Java performance wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
//This is a comment.
public class MyProgram
{
public static void main (String[ ] args)
{
System out println(“This
System.out.println(
This is my first program.
is my first program ”);
);
}
}
This is the first program that you will write in IS 147.
This is what the program prints when it is run at the linux prompt
is run at the linux prompt. 1
//Some notes about the program.
public class MyProgram
{
}
Use a comment to give information about your program such the date
program, such the date when you wrote it.
Think of this line as hi k f hi li
being used to give your program a name, such as MyProgram.
y g
Your program is written inside
written inside these braces.
2
//Notes: Version 2.
public class MyProgram
{
public static void main (String[ ] args)
public
static void main (String[ ] args)
{
System.out.println(“This is my first program.”);
}
}
Program actions take place here. In this example, the action is to p
print This is my first program. at y
p g
the command line prompt when you run the program, like this: java MyProgram
3
//This is a comment.
//This
is a comment
public class MyProgram
{
public static void main (String[ ] args)
{
System.out.println(“This
System.out.println(
This is my first program.
is my first program.”);
);
}
}
Think of this line as the beginning of program statements that perform actions that you want your f
ti
th t
t
program to perform. At this point, you will just have to memorize this line without understanding it.
g
The word main is the name of a method, which is the beginning of a sequence of programming
a sequence of programming statements. A method is always followed by parentheses. Sometimes the parentheses are empty, and other times they contain information. In this case, you can see (String[ ] args) within the parentheses
the parentheses.
Memorize this line. You will see it and use it over and over again. You will learn what the components mean later.
4
Sometimes it may be required to //This is a comment.
write a computer program that public class MyProgram
receives a list of items when you start
receives a list of items when you start {
the program. The term (String[] args)
represents that list. For example, you public static void main (String[ ] args)
public static void main (String[ ] args)
may want a computer program to {
print a list of cities. You might start the program this way:
jjava MyProgram Baltimore Atlanta
y g
You can think of String[] as a column System.out.println(“This is my first program.”); of words in a spreadsheet and args
}
would represent the actual words in the cells Baltimore and Atlanta
the cells. Baltimore
and Atlanta are are
put in the spreadsheet cells automatically when you start the }
program.
You must always use public static void main (String[] args) in a Java program even when you
in a Java program even when you don’t plan to receive a list of items.
Memorize it.
5
//This is a comment.
public class MyProgram
{
public static void main (String[ ] args)
{
System out println(“This
System.out.println(
This is my first program.
is my first program ”);
);
}
}
System.out.println(
System
out println(“This
This is my first program.
is my first program ”);
);
This is an action line of code in your program. The sentence within the quotation marks is printed when you run the program. You will learn later that you run the program with the following command at the Unix prompt: java MyProgram
This is my first program. will be displayed at the prompt.
Whatever you include within the quotation marks will be displayed. For example, you Whatever
you include within the quotation marks will be displayed For example you
could write the program as follows:
System.out.println(“Hello World”);
Most Java courses begin by printing Hello World, but in this course, you will be spared f
from having that initiation.
h i
h i ii i
6
This window appears after you logon with TeraTerm.
Type pwd, and then press the Enter key.
7
What is printed shows your location in your account. The home directory is not accessible on the Internet.
Typing cd www is a shortcut way to move to your Internet directory The
move to your Internet directory. The term cd means change directory.
8
The www directory is accessible on the Internet. If needed, your instructor may examine your dot java files. However, only you can write, revise, compile, and run your programs by accessing your account with TeraTerm
with TeraTerm.
The mkdir command makes a y
command subdirectory. This mkdir
makes a subdirectory named IS147. 9
The cd command changes the directory. Here, the command moves to the IS147 subdirectory moves to the IS147
subdirectory
that was created.
10
The Java program will be written in this subdirectory.
11
We will use the pico text editor to write Java programs to run on linux. This shows the command to create a
This shows the command to create a file named MyProgram.java.
Here is the pico editor ready for input to be y
p
typed.
12
The Java program has been typed. Note that This is my very first program in IS147.
This is my very first program in IS147. has been written to be printed.
On the keyboard, CTRL‐X will exit pico. Enter Y (meaning Yes) to save the program
the program.
13
At this point, press the Enter key.
The program has been saved, and you’re
you
re back to the command back to the command
prompt.
14
The ls command lists the files in your directory.
Listed is the file that you just created, MyProgram.java.
15
The javac command compiles MyProgram.java into a bytecode file, MyProgram.class, that you can run at the command prompt.
It may take a few seconds to It
may take a few seconds to
compile your program.
16
If your program has no errors in it, no messages will appear.
17
Now your directory has two files: MyProgram.class and MyProgram.java.
Run the program by typing
h
b
java MyProgram.
18
Here is the output produced by the program
program.
19
Use the below link at UMBC to download and install TeraTerm. It will work on Vista platforms To use TeraTerm to connect to UMBC enter
work on Vista platforms. To use TeraTerm to connect to UMBC, enter gl.umbc.edu
https://wiki.umbc.edu/display/faq/Software+Downloads
20
The below link presents the "Hello World" program presented
in the Java Tutorial at Oracle. You do not need Step 1. Java
has already installed on the UMBC machines.
docs.oracle.com/javase/tutorial/getStarted/cupojava/unix.html
21
There are many different ways to format a program. The below link shows the Hello World program presented at Oracle.
docs.oracle.com/javase/tutorial/getstarted/application/examples/HelloWorldApp.java
22