|
|
A Simple Java Program |
|
||
|
|
Introduction In this lab we will write some simple Java programs,
compile the programs and then execute them.
We will do this using an IDE (Integrated Development Environment). An
IDE is a program that makes writing, debugging and executing Java programs
easier. When we write a program for Java, we generate what is called the
source code. Source code can be
written in a simple text file using a text editor like Notepad. Even within the IDE, source code is a
simple text file. You cannot use a
word processor like MS Word unless you specifically save the document as a
text file (do not get confused with rich text format, it-must be text). All source code files have the ".java" extension. After the program is written it must be
compiled. The compiler will take the
source code and convert it into Java "bytecode". Bytecode is a
type of machine code that is compatible with the Java Virtual Machine. All bytecode
files will have the same name as the source code but will have ".class" as the extension. We
then execute the program using the Java Virtual Machine. A Java Virtual Machine is a program that is
specific to a given machine but allows any Java compiled program to be
executed on that machine with the same result. Hence, one of the distinct advantages of
Java as a programming language is that it is extremely portable unlike other
languages like C++ or Visual Basic which often have to be configured to a
specific machine. |
|
||
|
|
Procedure |
|
||
|
|
|
|
||
|
|
A. Using an
Integrated Development Environment (IDE) 1. Using Windows Explorer, create a
folder on your key drive with the name "JavaProjects"
( File à New à Folder ).
Your key drive should appear as the "F" or "G"
drive and may have the label "TRAVELDRIVE" or " 2. There are a number of Java IDE's that can be used.
The one that we will use in this course is called JCreator. It is available from Xinox
at http://www.JCreator.com/ for free.
You can download a copy of this and the Java SDK and install them on
your computer at home. There are
instructions on how to do this on the class website. We will be using version 3.5 and all screen
shots will refer to this version. To
get started, start JCreator from your computer's
start menu. 3. When JCreator
opens you should see four "frames", two on the left labeled
"File View" and "Package View", and a third at the bottom
labeled "General Output". The fourth frame should be a gray area
which will contain the text editor that allows you to edit your source code
once you start entering code. At the
top are the tool bars with buttons that will allow you to perform various
actions on your source code. 4. The first thing that you will need to
do when starting a new program is to create a new workspace. After the
program has started, select File à New à Blank Workspace. A Dialog Box will appear like
the one shown below. In the top text box type "ChapterOne"
as shown below. In the bottom text box
change the path to the "JavaProjects"
folder on your removable drive using the browse button ("...") . The path should
appear similar to that shown below.
Next click on the "Finish" button. Note that the "File
View" frame should now say "Workspace ChapterOne" |
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|
||
|
|
5. Now we have to add a project to the
workspace. From the File menu select File
à New à Project (or
alternatively Project à New Project from the
Project menu) on the top tool bar. You
should see a dialog box that looks like the following: |
|
||
|
|
|
|
||
|
|
6. Select the "Empty Project"
icon and click on the "Next" button. A new dialog box will come up
that looks similar to the following except the "Name" text box will
be blank. Type "HelloWorld" in the "Name" text box and
click on the "Finish" button |
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|
||
|
|
7. Your work space should appear. If you double click on the "ChapterOne" folder you should
there should be no files in the folder. Now we want to add a file that contains our
source code. All Java applications
must have at least one class so now we want to add a class to the
project. To do this, select Project à New Class from the
Project menu on the top tool bar. The
following dialog box should appear. As
shown, in the "Name" text box type "HelloWorld"
and then click on the "Finish" button. |
|
||
|
|
|
|
||
|
|
8. A code listing should appear in the
work space that looks like the following.
Note the data view on the left hand side of the workspace. This will list the major attributes of your
code, especially the methods (which we will discuss in more detail later). |
|
||
|
|
class HelloWorld
{ } |
|
||
|
|
9. Replace the code in the HelloWorld.java file with the code listing below. It must be exactly as below except that you
can substitute your name, version number and the present date as indicated in
the second line. |
|
||
|
|
/* HelloWorld.java (Your Name) Version (x.x) (date xx/xx/xx) Program prints Hello
World on screen */ class HelloWorld { public static void main (String args[]) { System.out.println("Hello
World"); }//end main }//HelloWorld |
|
||
|
|
10. Once the program is entered, you can
compile from within JCreator. To do this select
"Build" from the main or
top menu bar and then select the command "Compile File". If the
program compiled successfully it will respond with "Process completed" in the comment text area at the bottom of
the screen. The screen, after compiling
the program, should look like the screen below. If you made an error in entering the code
you should get a listing of what line in your code mistakes were made. |
|
||
|
|
|
|
||
|
|
11. To execute the program, again go to the
"Build" menu and select
"Execute File". Enter a letter from the keyboard and
observe the result. Your output should
be similar to the following: |
|
||
|
|
|
|
||
|
|
12. Now modify the program. Change the statement to something else and
recompile and run. Leave off the
semicolon and recompile. What
happens? Leave our a
curly brace. What happens? Leave off a quotation mark. What happens. Insert a backslash and an "n" ("\n")
somewhere in the line. What happens? |
|
||
|
|
Programming Assignment Modify the HelloWorld program to
print your name and something about you.
Turn in a program listing your source code. Source code should use proper formatting
(see handout on formatting source code).
Also turn in your copy of your output.
This can be obtained by running the program and then using AltPrintScreen to copy the window of the output onto the
clipboard. Prior to using the
Alt-Print Screen, change the background of the window to white and the text
to black. This can be done by clicking
on the icon in the upper left hand corner of the output window and going to
the properties item in the menu that appears. In the dialog box that appears
just change background to white and text or foreground to black. Then paste the contents of the clipboard into
WordPad and print out the document. In
addition to the source code and the output, also turn in the answer to the
following questions. |
|
||
|
|
Questions 1. Explain
the difference between a compiled and an interpreted program. 2. List one
advantage and one disadvantage of an IDE. 3. What are
the two ways to comment a program? 4. What is
the difference between System.out.print and System.out.println? 5. In the “ReadHello” program, what does char represent? |
|
||
|
|
References http://www.particle.kth.se/~lindsey/JavaCourse/Book/chapter01.html A history of Java as well as introductory materials http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html A listing of the many coding conventions used in writing
Java source code http://chortle.ccsu.ctstateu.edu/cs151/Notes/chap05/ch05_1.html An interactive introduction to Java programming http://math.hws.edu/javanotes/c2/s1.html A lesson on the basic Java program |
|
||
|
|
|
|
||
|
|
|
|