ITEC 120
L01a: First Java Program

Objectives

In this lab you will write, compile, and execute your first Java program. To do this you must first establish a development environment. You will also learn about your home directory and you will learn some basic commands to navigate the file system from a command line.

To successfully complete this lab you will:

Set Up Your Environment

to write, compile, and edit code. If you haven't done so already, you will need to install Java and perhaps a text editor. Instructions here.

Create a Workspace

Every time you start a new assignment (e.g., lab, homework, etc) you should create a new directory (or folder) to store your files. You will create many files during the semester. Having separate directories for each assignment will keep your files organized and easier to work with.

You are probably familiar with graphical user interfaces (GUI) like Windows. In this class you will learn to work from a command line. In Windows, click on the Start button, type cmd in the search text box, and hit return. This will launch a command prompt window. On OS X (Mac), open the Terminal application to launch a command prompt window.

We will start with three simple DOS/OS X commands:

DOS

OS X

Description

dir

ls

list the contents of a directory (folder)

cd

cd

change directory to a different directory

mkdir

mkdir

make a new directory

Let's start by listing the files in your current directory. Enter dir at the command prompt and hit return. The dir command displays all of the files and directories in your current location.

   dir

Make a new directory named itec120 to store all of your files for CS1. Note: itec120 is all one word – there are no spaces. It is not wise to use spaces in the names of files and directories. First, switch to your H: drive or your Z: drive by typing

   h:

followed by return. If this gives you an error that the system can not find the specified drive, type

   z:

followed by return. Then type the command below and hit return.

   mkdir itec120

Use the dir command again to list the contents of your current directory and you should see a directory named itec120.

Type the command below to change into the itec120 directory.

   cd itec120

Use the dir command to list the contents of your current directory. The directory should be empty because you just created it. If you are using the OS X Terminal, use the ls command instead of dir.

   dir

Use the mkdir command to create a directory named lab01a. Once again, there are no spaces in the name of the directory. The lab01a directory will be your workspace for this lab.

   mkdir lab01a

Use the cd command to change into the lab1 directory.

   cd lab01a

Now that you have created a workspace for this lab, you are ready to start writing code.

Writing Code

Programmers write code in an editor. An editor is similar to Microsoft Word. Note: do not use Microsoft Word to write code. Word is not designed for writing code.

There are many editors to choose from. We recommend jEdit because jEdit runs on OS X and on Windows. If you are using Windows you may also use Notepad or Notepad++. If you are using OS X you may use TextEdit, but be sure to change the format to plain text.

jEdit is free to use. You may download jEdit here.

Here is a short video that shows how to use jEdit:

Your First Java Program

Type the program shown below into jEdit. Substitute your name as the author and today's date for the version. Save the file in your lab01a directory with the name Hello.java.  This is especially helpful with jEdit because the editor will highlight the syntax when you save a file with the .java extension.  It is wise to save your files often.

 /**
  * Hello.java
  *
  *
  * @author   Jeff Pittges
  * @version  12-AUG-2014
  */
  public class Hello
  {
      public static void main (String[] args)
      {
          System.out.println("\nHello World!\n");
      }
  }

Compile and Execute Your Code

People typically write code in a high level language like Java that is easy for people to read and write. Computers understand machine code. A compiler translates source code, written in a high level language, into machine code. The compiler also checks the syntax of the code and identifies syntax errors.

We will use the command line to compile and execute our code. Return to your command prompt window (cmd on Windows, Terminal on OS X). Enter the command below to list the contents of your current directory.

   > dir

When you list the contents of your directory you should see a file named Hello.java. javac is the command that runs the java compiler.  When you compile a file, you must use the include the .java file extention. To compile your program, type:

   > javac Hello.java

If there are errors in the program the compiler will provide error messages to help you find and fix the errors. If there are no errors you are ready to execute your program. When you run your program, you execute a class, and you do not use a file extension. To run your program, type:

   > java Hello

If you entered the program correctly and the complier did not display any errors, the program will print Hello World! on the screen.

Song

Create a new file named Song.java. It would be wise to open your Hello.java file and save the file as Song.java. This ensures that you are starting with a working program and it will save you some time. Be sure to change the filename in the header comments and change the name of the class to Song.

Use three print statements and three println statements (see below) to print a few lyrics to a favorite song. How is the print statement different from the println statement?

     System.out.print();
     System.out.println();

Challenge Problem 

PP 1.9

Write a program that prints the diamond shape shown to the right. Do not print any unneeded characters (i.e., do not make any character string longer than it needs to be).

You will find other fun programming problems (PP) at the back of each chapter.

 

*
***
*****
*******
*********
*******
*****
***
*

Save Your Work

When you work locally your files are only available on the computer you are using. Each student has a home directory on the University network. This directory is accessible from any computer with Internet access. When you switched to your H:/Z: drive you changed into your home directory. Therefore, the files in your lab01a directory are accessible from any computer with Internet access. However, if you are working off campus you may need to mount your home directory. We will discuss remote access in another lab.

The contents of your home directory are backed up each night (i.e., copied to a second location).  This protects your files.  If you delete, corrupt, or otherwise lose a file in your home directory, it is possible to recover a previous version of the file.  For this reason you should keep a copy of your program files in your home directory to reduce the chances of losing your work. 

If you are not working in your home directory, then you should save all of the files in your workspace to your home directory before you logout.

Submit Your Assignment

Submit Hello.java and Song.java to the L01a dropbox on D2L. The challenge problem Diamond.java is optional. You are encouraged to try all of the challenge problems.