Homewrk0

profilebasil101516
w01_f_eclipselab_.docx

Page 8 of 8

3.Tutorial: Get Started with Eclipse in the Computer Lab[footnoteRef:1] [1: This tutorial is modified from Eclipse Tutorial – Getting Started by Dr. John Dean (http://captain.park.edu/jdean/tutorials/eclipseDnldTutorial.doc ) and How To Install Eclipse and Get Started with Java Programming from https://www3.ntu.edu.sg/home/ehchua/programming/howto/EclipseJava_HowTo.html (retrieved on 8/8/2015)]

(Last updated: 8/16/2015)

In this tutorial you will practice using Eclipse to build Java programs in computer labs (such as SC113) on campus. We will practice:

A. Get familiar with the Eclipse IDE

B. Set up the coding style preference (such as two-space indent and soft tab) used in this course

C. Build and run the famous Hello World! Project

D. Create a project by copying from an existing project

E. Terminate a running program

* * *

Eclipse is an Integrated Development Environment (IDE) that allows you to enter, compile, and run programs. The programs can be written in one of many different languages, such of Java, C/C++, and PHP. Computer labs on campus already has Eclipse ready for Java development.

The screenshot below shows the default layout of Eclipse. Take a look now and refer back to this screenshot as to where things are while you work through this tutorial.

1. Menu

2. Toolbar

3. Package Explorer: display all your projects and files. Yours will be empty at this point.

4. Editor

5. Outline view: displays data and method members of the currently selected Java class

6. Console panel

If you accidentally closed or moved one of the panels above, you can always restore this default layout by:

· First choose “Window” menu => “Open Perspective” => “Other…” and select “Java (default)” in the “Open Perspective” dialog. In Eclipse a perspective defines the initial set and layout of views in the Workbench window.

· Then choose “Window” menu => “Reset Perspectives” and click “Yes”

* * *

Tip: when taking screenshots of the running result for your homework report, you may temporarily make the console panel section bigger so it shows the complete running result. Afterwards follow the steps above to restore the layout.

* * *

SAVING FILES:

· Naturally, you are required to save your program files. In the lab, you are required to save your files on a USB flash drive storage device. If you're working at home, you may save your files on your computer's hard disk, but you're still required to have a flash drive so that you can transport your files to and from school. If you're at the school's lab, do not use the hard disks ever!

ECLIPSE TUTORIAL:

If you forget how to do something in Eclipse, it is up to you to carefully reread this Eclipse tutorial and figure out how to do things. To reduce the number of rereads, go through this tutorial slowly and try to remember what you're doing.

This tutorial uses x: to refer to the drive that you're saving your work on. In the lab, x: should be replaced by the USB drive (assuming that you're using a USB flash drive storage device). At home, x: should be replaced by the USB drive or the hard drive, whichever you prefer.

Whenever you are asked to perform an action (left column below) that you've done before, I will refrain from providing the supplemental information (right column below). If you don't remember the details of how to do something, look for it earlier in the tutorial. I expect you to have to look up a lot of previously covered material. This is my attempt to force you to start memorizing how to do things.

Actions

Supplemental Information

Create a workspace.

Selecting Eclipse (above) should cause a "Workspace Launcher” dialog to appear.

In the “Workspace” box, enter x:\219pgms.

Since this is the first time you've specified x:\219pgms in the workspace box, you'll be creating a new workspace – the 219pgms workspace, which is a folder named 219pgms on drive x: to store your work. If the 219pgms folder doesn't exist, Eclipse will create it for you.

Make sure the "Use this as a default and do not ask again" box is unchecked.

Click OK. Eclipse will create the 219pgms workspace and load a “Welcome” screen. It's not required, but feel free to explore the “Welcome” screen's links.

Go to the workbench.

The workbench is the area where you'll do all your work – where you'll enter, debug, and run code. To close the “Welcome” screen and go to the workbench, click on the Workbench icon in the top-right corner of the screen. Note you can get the “Welcome” screen back at any time by choosing “Help” menu => “Welcome”.

If there was no prompt for workspace.

If you ever load Eclipse and the “Workspace Launcher” dialog doesn't appear, that means someone checked the "Use this as a default and do not ask again" box. That's a bad thing (particularly in the lab) because then all users are taken to the same workspace location and that location might be inappropriate. To correct this problem, perform these steps after getting to the Eclipse workbench:

Choose “Window” menu => “Preferences” to bring out the “Preferences” window.

In the container tree at the left of the “Preferences” window, choose “General” => “Startup and Shutdown” => “Workspaces”.

Make sure the "Prompt for workspace on startup" box is checked and click “OK”.

Make sure you’re using the Java perspective.

Eclipse can be used for many different languages. If the previous Eclipse user used it for a language other than Java, switch the Eclipse perspective back to Java.

In the top-right corner of your workspace window, you should see tabs for the perspectives that Eclipse has used in the past. Make sure the Java perspective tab is selected. If you want to select it, but can’t see it as a selection option, choose “Window” menu => “Open Perspective” => “Other…” and select “Java (default)” in the “Open Perspective” dialog.

B. Set up the coding style preference

Coding-style preferences - background information.

You'll now set Eclipse's coding-style preferences so they match the ICS Department's coding-style conventions (the convention followed in this class).

If you're in the lab, you should import the ICS Department's preferences file. If you're at your home computer, you should set your preferences manually (see Eclipse Download tutorial).

How to set your preferences when you're in the lab.

Choose “File” menu => “Import…” to bring out an “Import” window.

In the container tree at the left of the “Import” window, choose “General” container=> “Preferences”, and click “Next” button.

In the “Import Preferences” window, click “Browse…” button to search for and select the cimDeptProfile.epf file in the c:\ folder.

Click “Finish” button.

C. Build and run the famous Hello World! Project

What is a project?

A project is an entity that helps to organize all the files in a particular program. It corresponds to a folder with the same name in your workspace x:\219pgms. For example if you have created a project named tutorial, then you will have a folder x:\219pgms\tutorial created by Eclipse. A project's settings are stored in a .project file which is contained in the project's folder.

How to organize your projects.

Normally you should create one new project for each new Java program (with one or multiple .java class files, but only one of those classes contains a main() method).

I recommend that you use one workspace to store all your CS 219 projects. In this tutorial, you've already created a 219pgms workspace and you will create a tutorial project within that workspace. For your homework assignments, I recommend that you create hw1, hw2, etc. projects within the 219pgms workspace.

Create a project.

Choose “File” menu => “New” => “Java Project”.

In the “New Java Project” window, enter tutorial for “Project name”.

Verify that

· “Use default location” is selected

· In the “JRE” area “Use default JRE” (3rd option) is selected. The 1st option “Use an execution environment JRE” is okay too as long as the version is at least 1.7.

· and in the “Project layout” area, “Use project folder as root for sources and class files” (1st option) is selected.

Click the “Finish” button.

Verify project creation.

Go to “Windows Explorer” and locate the x:\219pgms\tutorial folder. Verify the existence of the .project file within the tutorial folder. If you don't see the tutorial folder, try double clicking on the 219pgms folder.

Find Package Explorer.

At the left of your Eclipse window, you should see a “Package Explorer” pane that contains your workspace's project folders. If the package explorer pane is not shown, open it by choosing “Window” menu => “Show View” => “Package Explorer”.

Create a class and add to your project.

In the “Package Explorer” pane, right click on the tutorial project.

Select “New” => “Class” from the pop-up menu.

In the “New Java Class” window,

· In the “Package” field, delete the content if it is not empty.

· In the “Name” field, enter the name of your class Hello.

· Check option (uncheck if this class is not a driver class)

Click the “Finish button”. That should cause an Hello.java with an empty main() method created and open in the “Editor” window.

Enter this text:

Enter this text in Hello.java such that <your name> is replaced with your actual name.

/****************************************

* Hello.java

* <your name>

*

* This program says "hello" to the world.

****************************************/

public class Hello

{

public static void main(String[] args)

{

System.out.println("Hello world!");

} // end main

} // end class Hello

Save the source file.

Press Ctrl+s or click on the save icon in the toolbar.

A source code file with unsaved changes is displayed with an asterisk preceding its name . Get into the good habit of saving your work periodically.

Compile and run your program.

There is no need to compile the Java source file explicitly. It is because Eclipse performs the so-called incremental compilation, i.e., the Java statement is compiled as and when it is entered.

Any syntax error is detected and signaled immediately in the “Editor” and suspicious code is underlined, as shown below:

Move your mouse over to view the error message(s). After the error(s) are fixed those markers will be cleared.

To run the program, right-click anywhere on the source file Hello.java (or from the “Run” menu) => Choose "Run As" => "Java Application".

The output "Hello, world!" appears on the "Console" panel.

If necessary, fix runtime errors (semantic i.e. algorithmic errors).

D. Create a project by copying from an existing project

Create a second program.

To create a new program, you can of course enter it from scratch as you did for the Hello.java program. As a shortcut, this time we'll copy from an existing program and edit the copy.

Create a project and files by copying from an existing project.

In the “Package Explorer” pane, right click on your old tutorial project and choose “Copy” from the popup menu to copy the project.

Next right click anywhere in the “Package Explorer” pane and choose “Paste” from the popup menu. This will bring forth a “Copy Project” window. Change the project name to counting.

Locate this new counting project in the “Package Explorer” pane, expand it and look for the (default package) container and the Hello.java inside. This new project and the Hello.java can be modified independently of the existing tutorial project.

‘Rename” a Java class

There is no way to directly rename a Java class in Eclipse, but we can fake it by first making a copy, changing the name of the copy, and then deleting the original.

In the “Package Explorer” pane, right click on the Hello.java in your new counting project. Choose “Copy” from the popup menu or just press Ctrl+c. Next right click on the same Hello.java or the (default package) in the counting project and choose “Paste” to create a copy. This will bring forth a “Name Conflict” dialog and enter Countdown (no need to enter .java) as the new name. A Countdown.java file will appear in the project. Double-click on it to open it in the Editor area. Check the file and you will see that the class name is now Countdown. You still need to change manually the old name appeared in comments, such as “xxx.java” in the prolog and “// end class xxx” at the end of your program.

In the “Package Explorer” pane, right click on the Hello.java in your counting project and choose “Delete” to remove it from this project.

Edit the Countdown.java file.

Edit the Countdown.java file so that it contains this:

/****************************************************

* Countdown.java

* <your name>

*

* This program prints a countdown from a user-entered

* starting position.

****************************************************/

import java.util.Scanner;

public class Countdown

{

public static void main(String[] args)

{

int startPos; // starting position for countdown

Scanner stdIn = new Scanner(System.in);

System.out.print("Enter countdown starting position: ");

startPos = stdIn.nextInt();

stdIn.close(); // close stdIn after all input is done.

for (int i=startPos; i>0;)

{

System.out.println(i + "...");

}

System.out.println("Lift off!");

} // end main

} // end class Countdown

Run the program using the run icon.

Compile and run the program as you did for the first program, but this time run your program using the run icon on the toolbar.

Type in 30 at the prompt to enter the countdown starting position in the “Console”. You may notice that the cursor is flashing at the beginning of the prompt message. Don’t worry. Just start typing and the cursor will automatically move to where it should be.

After pressing enter, your “Console” should be filled with multiple lines of 30...

Wait, why is it going nonstop? (Pause and think for a minute before proceeding)

E. Terminate a running program

Terminate the program.

Your program is repeatedly printing the line 30... That's an example of an infinite loop.

Note the red button at the upper-right corner of your “Console”. The red button allows the user to terminate a program that's currently running. (Makes sense, right? Red for stop.) Click it to stop the infinite loop.

Find the error.

An infinite loop is an example of a run-time error.

Run-time errors are often more difficult to debug than compilation errors because you do not have the benefit of a compiler-generated error message.

Can you figure out the error?

Do not continue until you have attempted to find the error.

Fix the error and recompile.

That's right, the for loop heading is missing its third component.

Replace the for loop heading with this line:

for (int i=startPos; i>0; i--)

Save Countdown.java and re-run with the same input 30. Your “Console” should display this:

Enter countdown starting position: 30

30 ...

29 ...

<28 through 2 go here>

1 ...

Lift off!

Note that you'll probably need to scroll up and down to see the entire output.

- END -