Download Hello World

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
Hello World
(please read this document until you see the heading “We are now ready to begin
our first program.” At that point you will carry out the directions provided to create
and run your “Hello” program.
Explain the architecture of a personal computer.

A personal computer is built around its central processing unit (CPU). Within the CPU,
a computer has a chip (i.e. integrated circuit) such as a Pentium III. Within this chip there
are millions of transistors that route the electricity and perform mathematical
computations. In addition to the CPU, a computer has primary and secondary storage.
Primary storage is another name for random-access memory (RAM), which we often
simply call "memory". Secondary storage is typically a computer's hard disk (i.e. hard
drive). The storage of bits and bytes is magnetic. The CPU, the memory, and other
peripheral devices are connected by a set of electrical lines called a bus. Physically,
these parts are part of or connect to the computer's motherboard.
List several popular high-level programming languages and explain how they are compiled
or interpreted into machine code.






Besides Java, other high-level programming languages are C, C++, Pascal, FORTRAN,
and COBOL.
Java was created relatively recently in 1991 by James Gosling and Patrick Naughton at
Sun Microsystems as a language to be used in consumer devices such as t.v.'s, alarm
clocks, etc. It was designed to be non-specific to the hardware of the device it was
executed upon. While it never really took off with consumer devices, it did become very
popular due to its ability to create applets. Applets are Java programs that execute within
Web pages. Since Web pages are meant to be accessed on many different kinds of
computers and platforms such as Windows PC's, Macintoshes, Unix computers, etc.,
Java fulfilled this role while other programming languages could not.
At the lowest, most basic level, a computer program executes as machine code which is
a sequence of bits (i.e. 1's and 0's). Different kinds of computer CPU's (e.g. Macintosh,
Windows PC's, Sun SPARC, etc.) use different machine code instructions to perform
mathematical, graphical, and other kinds of common tasks. A computer program in a
high-level computer languages such as C, C++, or Basic needs to be translated into one
set of machine code to execute on a Macintosh computer and a different set of machine
code to execute on a Windows PC. Actually, a special computer language known as
Assembly is considered to be a low-level language. You can program directly in
Assembly though few people do so today with the popularity and ease of programming in
high-level languages such as Visual Basic, C++, and Java.
The programming language Java however can be used in such a way that one version of
a program can be executed on any kind of computer. As long as a Java virtual machine
(JVM) has been installed on a computer, a Java program can execute there. JVM's are
free and available for virtually every type of computer.
A Java virtual machine (JVM) is an idealized CPU that can interface with the particular
computer that it's installed upon. Java source code is compiled by a Java compiler into
bytecode, not machine code. The JVM then interprets the bytecode into machine code
and allows the program to execute. The bytecode is stored in class files and library files.
The class files are files that you create and the library files are obtained from Sun or other
sources.
Java is different from C++ in a number of ways.
o
o
Java source code may look similar to C++ but only because some of the
keywords are the same. On a deeper level, the two languages are quite different
semantically.
The Java language does not support global functions or variables. The skeleton
of every Java program is a class definition. However, the entry point of every
Java program is its main method. You specify the name of the class that you
want to execute and the Java interpreter calls the main method defined within
that class.
Explain how to install and use the Java compiler j2sdk as well as the IDE BlueJay





In order to write and test programs on your computer, you must install a Java compiler.
The recommended compiler is Sun's latest version of its Java 2 SDK SE which can be
downloaded here.
When you follow the Java 2 SDK installation instructions, the Java compiler is installed
into the folder C:\j2sdk1.?.? . The ? marks may vary depending on updates made
from semester to semester.
With the Java compiler installed, you must now install the BlueJay IDE. Download it here.
There are numerous free and inexpensive Java integrated development environments
(IDE's) that can be used to make the task of writing, compiling, debugging, executing and
editing Java programs easier. We will be using the free BlueJay IDE, Technically, it is
required that you install a Java compiler such as Java 2 SDK in order for an editor such
as BlueJay to work. Some people incorrectly think that such an editor is a compiler but
technically it is not. Follow these directions to set up your workspace and to create a first
project. You can only execute the program after you've compiled it and fixed any bugs.
To edit a program that was already compiled you must edit the original .java source file
The fixed version of the program must be re-compiled in order to see the changes when it
is re-executed.
Finally we need to create a file folder for our Java Programs. So that we are consistent.
Create a folder under mydocuments call “my java programs”.
Objective #4: Compile and execute a simple Java program.

Memorize the following "Hello World" Java program.
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("hello world");
}
}


The name of the class is HelloWorld (note: the name convention for
classes. They start with a capitol letter and secondary words
are also capitalized)and can be chosen by the programer. Except for the String
Literal "Hello World", the rest of this program consists of Java keywords and
operators. A string (or string literal) is a set of characters that are enclosed in quotation
marks.
A Java source code file must be named to match the name of the class that it contains.
The code above must be saved in a file named HelloWorld.java (using a 4-letter
extension even if you're going to compile the program on a Windows computer.)




The System class is part of the API (application programming interface) provided with
the Java environment. It gives you system-independent access to system-dependent
functionality. In other words, it allows you to take advantage of graphic methods with a
Java program even if you aren't sure of the platform that your Java program is going to
be executed on.
System.out is the full name of the out object in the System class. Notice that there is
no instantiated System object. Rather, out is referred to directly from the class name.
This is because out is a class variable rather than an instance variable. When you
execute a program, one class variable is created per class, no matter how many
instances exist of that class. So even if there is another System.out statement in the
main function, the same out variable would be used.
You can also associate methods with a class which are called class methods which
should not be confused with instance methods.
An escape sequence can be used inside of a string to print special characters that would
otherwise be impossible to print due to the special role of certain characters. For
example, because double quotes are used to delimit strings, how would you print a
double quote if you actually wanted to? The statement
System.out.println("He said, "Come over here!"");
causes compiler errors since the double quote before the capital C is interpreted as the
end of the string. In order to print the desired quotation above, you would need to use
escape sequences to "escape" the inside double quotes. The statement
System.out.println("He said, \"Come over here!\"");
would work correctly because it uses the escape sequence \" in two places to indicate
to the compiler that the inside double quotes should be displayed as double quotes. In
Java (and many other computer languages), the backslash is used as the escape
operator. By preceding certain characters with a backslash, you are indicating to the
compiler to do something special. In the case of the escape sequence \", you are telling
the compiler to display a double quote rather than to interpret the double quote as the
beginning or end of a string literal.
Another escape sequence is \n which is used to create a new line in console output.
System.out.print("hello world\n");
is more or less equivalent to
System.out.println("hello world");
since the \n causes a new line after the phrase "hello world" prints just as the println
method causes a new line to print. Note that the print method does NOT cause a new
line to follow the phrase "hello world". The statement
System.out.println("Sunday\nMonday\nTuesday\nWednesday
\nThursday\nFriday\nSaturday");
would cause the days of the week to display on separate lines of output.
The escape sequence \\ causes a single backslash to be displayed. The statement
System.out.println("some\\none");

would cause the string literal
some\none to display since the escape sequence
\\ causes a backslash to appear. Notice that the two consecutive characters \n are not
treated as an escape sequence in this example.
There are three methods of creating comments in Java.
o You can begin a comment with // such as
o
// Your Name and program #
You can enclose a comment within the matching symbols /* .... */ such as
/*
Mr. Langner
Your instructor
o
/*
You can enclose a javadoc comment within the symbols /** .... */ as in
/**
The class Tank can be used to manipulate
a virtual army tank.
/*
Notice that there are two asterisks in the opening comment tag. Javadoc
comments are used to create HTML help pages for the code that you create. By
enclosing javadoc comments along with your source code in the appropriate
places, a utility can be used that automatically creates HTML Web pages that
explain your code and serve as online help.
We are now ready to begin our first program.
A more visual instruction sheet for creating/compiling/saving/submitting a file is available under at:
http://www.dare.k12.nc.us/langner/apcs/firstprogram.doc
1. Open BlueJay
2. Click on “Project” on the menu bar and select “New Project”
3. Change to the folder under my documents called “my java programs” (you created this
earlier)
4. In the file name box enter HelloWorld and click on the Create Button.
5. Click on the new Class Button and type in the name of the class HelloWorld in the Class
Name box.
6. Click on the OK button
7. Double click on the stripped box (the stripes indicate that this is an uncompliled program).
8. You are now in the BlueJay Editor for your first program (Class). Erase everything you
see. (Later we will fix this so that just the shell of what we need is the default.)
9. At the beginning of each program we write we will include the following documentation
(comments) [more will be required at a later date.] In programming comments you will
include the following items.
 Your Name, Class, Program#/Name, and Date
 You will also need sections for the following:
 A description of the program.
These items will be completed after or as you write your program.


A list of any difficulties you had with the program (this would be compile errors,
logic, syntax or any other error that you were unable to resolve on your own.)
Explain how you correct the problems and who helped you.
What did you learn from this program (what new concept was practiced and/or
what problems did you solve on your own.
A Sample of the required documentation might look like this for this program.
/*
Mr. Langner
Manteo High School
Helllo World
August 26, 2005
Description: This is my first program. It displays “Hello World” on the monitor
Difficulties: I forgot to put a semi colon at the end of the println statement.. John helped me with
the error message “semi-colon possibly missing on line above”.
What I learned: I learned how to use BlueJay to write a Java program. I also learn the difference
between print and println. I also learned how to include documentation into my programs. When
my program wouldn’t run I saw that I had forgotten to include a closing comment notation.
*/
Your program would be entered here.
Enter the program you memorized above here. If you didn’t memorize it go back and do so now.
This framework will be used for every program for the next many weeks.
After you have entered the program click on the compile button.
If there is an error it will be reported in the box near the bottom of the screen. A little more help
will be given if you click on the ? (question mark). Check all you typing paying special attention to
capitalization and punctuation. Spacing will not be a problem unless it is between quotation
marks ( “ “). After checking thoroughly for problems you may ask for help from your
classmates/instructor by posing your problem on the discussion board for the particular problem
you are having difficulty with. It may also be helpful to upload your program. (the program you
need to upload will be the: mydocuments/my java programs/HelloWorld/HelloWorld Java File
When your program has successfully compiled you will need to run and test the program for
accuracy.
1. Close the BlueJay editor.
2. Right Click on the Program (notice no stripes)
3. Select: “Void main (String[] args)” and click OK
Your output if displayed in the Terminal Window. If it’s correct you are done. If incorrect you
must make changes in the editor. (you can reopen it from the project window by double clicking
on it). Recompile and run the program until it is correct
When you have successfully completed your program copy and paste the output in a comment
below your program code.
From the terminal window:
1. Highlight your output
2. Click Options/Copy
3. Return to the BlueJay Editor and past your output after the last line of your code as a
comment
/*
hello world
*/
Before you submit you will probably want to compile and run the program one last time to make
sure that you have not affected your code.
Make sure your program is fully documented. Email your Java File to your instructor for
evaluation. (We will follow this same method for turning in programs the rest of the class unless
otherwise instructed.)
Good Luck. All programs should be submitted on the day they are due. Occasionally a 2 day
grace period may be used. Abusing this will terminate that priviledge. Programs after this time
will be evaluated but the grade will be a zero for that assignment. Only participation credit (50%)
can be earned for programs that do not run correctly. It is your responsibility to put in the upload
comment box a note telling me that a program does not work. Failure to do this will result in a
zero and a harsh scolding. Use this comment box to communicate any thing about the program
with me. Not all programs will be grade but will be participation programs and self evaluated. If
you have done something special that you want me to be sure and see please let me know in the
comment area.
You have been creating your first program, compiling and running it. You may have noticed that
spaces and new lines in the program are not critical. (However, you can't put spaces in the
middle of a word, and spaces inside the quote marks do matter.) For example, the following
version of the program will compile correctly and will do exactly the same thing when it is run:
public class HelloWorld{ public static void
main(String[] args)
{System.out.println("hello world"); }}
The compiler does not "see" the two dimensional lay-out of the program. It regards the program
as a stream of characters, one following the other.
However, humans are sensitive to the lay-out of text, and it is important to be neat and consistent
when you create a source file. Although the second version of the program runs correctly, it is
much harder for a person to understand.
Below is a sample of how your submitted program might appear:
/*
Mr. Langner
Manteo High School
Helllo World
August 26, 2005
Description: This is my first program. It displays “Hello World” on the monitor
Difficulties: I forgot to put a semi colon at the end of the println statement.. John helped me with
the error message “semi-colon possible missing on line above”.
What I learned: I learned how to use BlueJay to write a Java program. I also learn the difference
between print and println. I also learned how to include documentation into my programs and
show the output my program generated. When my program wouldn’t run I saw that I had
forgotten to include a closing comment notation.
*/
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("hello world");
}
}
/*output:
hello world
*/