CS 1054: Laboratory Exercise 1
Purpose
This exercise is to introduce you to using the Eclipse Integrated Development
Environment (IDE) and to help you become familiar with some of the basic
concepts in Java. To Receive Credit: Record the results you get after each
exercise and have the TA check it by the end of lab. Also, show your working
program to the TA. Activities:
-
Launch the Eclipse IDE. Create a new project
byselectingFile->New->Project. You should get a pop-up window that looks
like this:
Click on the Next button to continue. This should lead to another pop-up window
that looks like this:
Type in Lab1 in the textfield for the 'Project Name' and click the Next button.
This should lead to another window that looks like this:
Click the Finish button to see this Window:
Click on the radio button next to "Change workspace compliance settings to 5.0"
and press the Yes button. At this point, you are ready to create a new Java
class. Select File->New->Class. This should launch a window that looks
like this:
Enter Hello in the textfield next to 'Name:' and press Finish. This should now
allow you to start entering your source code. Delete all the code that is
provided in the editor window by default and follow the next step below.
-
The Java language syntax tells us what the Java programs should look like. In
particular, in a Java application we need to write our program so that it has
the form:
import java.io.*;
public class Hello {
public static void main(String[] arg) {
System.out.println("Hello World!!!");
}
}
Enter the above code in the editor window and then save it by pressing Ctrl-S.
To run the following code, select Run from the main menu, then "Run As ->
Java Application". This should run your program and display the output of your
program in the lower area of your IDE with the tab labelled as "Console".
-
In Eclipse, if there is a syntax error on any line, a small red cross appears
before that line. Hovering the mouse over that cross displays a tooptip with
the particular error. Try removing each of the following from your program and
see what kind of error message you get. After each time replace the word before
removing the next. The words to remove are:
-
import
-
class
-
public
-
static
-
void
-
main
Record whether you get an error message, and what the message is. The words we
are removing are examples of Java keywords, which are words that have a
specific meaning. Java is also case sensitive. Record what happens if you
change System to system.
-
The syntax of the language does not say how the program should be laid out.
Edit your program so that the end braces ‘}’ are on the same line as the
println statement. Run your program and record whether or not there is any
difference. Return the program to its original state before going on. The rules
you should follow for formatting programs are:
-
Use only one statement per line
-
Use tab key to indent
-
Follow textbook style
-
Comments are used to communicate with people who may read your programs. In our
case, that is whoever is grading your programs, but on the job it may be other
programmers that have to modify your programs. There are two kinds of comments
in Java. A single-line comment that tells the compiler to ignore everything
from the comment to the end of the line; and a bracket comment that tells the
compiler to ignore everything in between. Add comments to your program that
tell us who wrote it. For example:
import java.io.*;
/* Author: Mir Farooq Ali
Description: Program to practice with basic Java syntax */
public class Hello
{
public static void main(String[] arg) {
//This is the typical first program message
System.out.println(“Hello World!!!”);
} // end main
} // end class Hello
Notes on use of comments
-
Always put a comment before the line that starts with class to state who wrote
the class, and to describe it.
-
Comments should not explain how Java works, just what your program does
-
Do not comment things that are obvious from code
-
Do not place comments in the middle of a statement
© Mir Farooq Ali 2005.