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 yo.
Page 8 of 83.Tutorial Get Started with Eclipse in the Compute.docx
1. 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/Ecli
pseJava_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.
2. 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!
3. 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.
4. 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.
5. 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:219pgmstutorial created by Eclipse. A project's settings are
6. 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:219pgmstutorial
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.
7. 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)
{
8. 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).
9. 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
10. 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
11. 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?
12. 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 -
CS 219 - HOMEWORK 0
(Due: Mon, 8/24/2015. Total: 20 pts)
HOMEWORK SUBMISSION POLICIES:
· Put all of your work, even project source code and screenshots
of project output, into a single homework document, and submit
that document to this assignment on Canvas by midnight of the
day that the homework is due. Put your name at the top of that
document.
· Make sure that the problems in your homework document are
in the same order as the problems in the homework assignment.
13. · In your homework document, make sure that you use
monospace font for your project source code and project output.
The monospace font ensures that text that's supposed to be
aligned stays aligned when it's printed. If your printed source
code is not aligned properly, you will lose style points. To
achieve monospace font, highlight the relevant text, right click,
and select Font. In the Font window, search for Courier New,
select it, and click OK to apply your font selection.
MANDATORY UNGRADED WORK:
· Complete my Get Started with Eclipse in the Computer Lab
tutorial (listed on Canvas ( Modules ( Week1 overview).
· Review Java Coding Conventions Guidelines document (listed
on http://www.park.edu/ics under Software Resources: Tutorials
and Guidelines). You must follow the guidelines found in this
document.
Submit answers for all of the following questions.
EXERCISES (2 pts, ½ pts each):
The first two exercise questions refer to material in the Get
Started with Eclipse in the Computer Lab tutorial mentioned
above.
1. In Eclipse, what is the name of the file that stores a project's
settings? (The project-settings file is different from the file that
stores coding-style preferences.)
2. What button should you click to terminate the currently
running program?
3. Why is it important to use monospace font for program
source code in your homework document?
4. Answer this question AFTER you’ve completed the project
part of this assignment:
14. What’s the hardest part of this project for you? Please explain.
PROJECT: Party Guest List (18 pts)
The purpose of this project is to review and practice:
· designing a class using UML class diagram
· building a class
· working with projects with one class and one driver class
Write a complete program that stores and prints number of
participants in a party. As part of your program, write a Party
class that implements these members:
· An instance constant that holds the maximum number of
guests.
· Two instance variables that hold, respectively, the actual
number of guests and the party host’s name.
· A constructor that stores the maximum number of guests and
the host’s name, and initialize the actual number of guests to
zero.
· An addGuest method that fakes adding a guest to the guest list
by incrementing the actual number of guests by 1 or prints an
error message if there’s no more room on the guest list (already
full).
· A printParty method that prints the party’s host, actual number
of guests, and the max number of spots.
Draw a UML class diagram for this Party class. Refer to the
UML format in Fig 6.12 of our textbook, but without the driver
class portion nor the local variables portion.
15. Provide a separate driver class that tests your Party class. Your
driver class should contain this main method:
public static void main(String[] args)
{
Party party = new Party(3, "David Beckham");
party.addGuest("Roberto Baggio");
party.addGuest("Zinedine Zidane");
party.addGuest("Roberto Baggio");
party.addGuest("Johan Cruyff");
party.addGuest("Diego Maradona");
party.printParty();
} // end main
When compiled and run, your driver class and Party class
together should produce this output:
Johan Cruyff cannot come to the party. The guest list is full.
Diego Maradona cannot come to the party. The guest list is full.
David Beckham's party: 3 guests out of 3 spots.
You may have realized that Party class allows a same guest
added to the guest list for more than once, which is bad! This is
a design flaw due to the limitation of our Java knowledge.
Since we don’t know how to store multiple guest names of a
party yet, there is no way to check if a guest is already
registered. But don’t worry we will come back and fix this in a
16. follow-up assignment after we learn about Array in ch9.
Overall comment your program appropriately. Pay attention to
the standard stuff like coding style, indention, heading, and
curly braces. Double check your code for indentation after you
paste it in your document.
Submission
· In one word document: UML class diagram; source code;
screenshot of running program
· Use the rubric below to check the completeness of your work
before turning it in.
Rubric: CS219, HW1
Item
Points
(Max)
Points (recvd)
Exercise (2 pts)
1
½
2
½
3
½
4
½
17. Project (14 pts)
UML for Party class
3
An instance constant that holds the maximum number of guests
1
Two instance variables that hold, respectively, the actual
number of guests and the party host’s name
2
A constructor that stores the maximum number of guests and the
host’s name, and initialize the actual number of guests to zero
3
An addGuest method that increments the actual number of
guests by 1 or prints an error message if there’s no more room
on the guest list
3
A printParty method that prints info of the party in the given
format
1½
The project contains two classes: Party class and a separate
driver class
½
18. General (4 pts)
Your project compiles and runs
½
Program output: correct result and format
½
Programming style
· Meaningful names for constants and variables
· Correct indentation: 2 spaces for each level
1
Comments
· Prolog
· End of section comments: end of class, end of method, end of
loop etc.
· Document each variable
· Proper comments in the program
2