Transcript Project".

Introduction to Computation and Problem
Solving
Class 2:
Integrated Development
Environments
Prof. Steven R. Lerman
and
Dr. V. Judson Harward
1
Announcements
• Status of web site.
• Needs and Preferences Questionnaire
– totally optional
– we ask for your name so we can correlate your
preferences, experience, and performance
over the course of the semester
– we know we are serving multiple communities
2
Goals
• This is a session about tools you
can use to program in Java®.
• We are going to introduce you to 3:
1. A standard text editor and command line
tools (javac, java).
2. The Forte IDE, a professional programming
tool.
3. The BlueJ integrated development
environment (IDE), a good tool for teaching
3
The Bad Old Days
Until recently, programmers used separate
tools for different stages of the software
process:
– A text editor to create or modify a source file.
– A compiler to turn the source code into byte or
machine code.
– A (often implicit) loader to load the compiled
code and get it to run.
– A debugger to help find errors.
Emacs provides a simple unified environment.
4
The NauticalMile Program
• A nautical mile is defined as the average length of
a 1 minute arc of latitude on the earth's surface.
• So if I told you that the polar circumference of the
earth was 24859.82 miles, you could calculate the
length of a nautical mile in feet, right?
• Let's do it.
• Start by opening Notepad:
• Open Notepad:
Start->Programs->Accessories->Notepad
5
Creating NauticalMile.java
Edit in the following code:
public class NauticalMile {
public static void main( String [] args ) {
double circum = 24859.82*5280;
int minutesInCircle = 360*60;
double nautMile = circum / minutesInCircle;
System.out.println(
"Feet in a nautical mile = " +
nautMile
);
}
}
6
Saving NauticalMile.java
Save the file in Notepad by creating a new
directory
– File -> SaveAs brings up the file dialogue
– Navigate to where you want to create the new
directory
– Click the folder icon with the star in the upper
right corner
– Save the file as NauticalMile.java.
– The name has to match the class name in the
file. The extension for Java® source files must
be .java.
7
Compiling NauticalMile.java
• Start up a Command Prompt:
Start->Programs->Accessories->Command
Prompt
• Navigate in the command window to the directory
you just created using the cd command
• Type the incantation:
set PATH=C:\j2sdk1.4.0_01\bin;%PATH%
• Compile the program
javac NauticalMile.java
• If you get any compile time errors, go back to
Notepad and correct your typos.
8
Running NauticalMile.java
• List the directory using the dir command and
note you now have a NauticalMile.class file
that contains your byte code.
• In the same directory, type:
java NauticalMile
Note you don’t use the .java or .class extension
when you run the compiled file.
9
Why Use an IDE?
• IDEs integrate the command line tools we
have been using in a visual, tightly
coupled environment.
• IDEs are designed to make you more
productive, but there is a learning curve.
• Forte is more powerful, BlueJ more visual
and intuitive
10
Starting Forte
• Fire up Forte by double clicking the icon on your
desktop.
• Identify all the interface areas labeled on the next
slide.
– The Main Window is the command center., holding
menus, tabs, and buttons.
– The Explorer allows you to manage files and assemblies
of files (projects) that form programs.
– The working area holds editor, debugger, or compiler
windows as appropriate.
11
Anatomy of Forte
12
Java® and Files
• Java® source files have the .java extension and
compiled ones the .class extension.
• In general, Java® expects each file to contain a
single class. If the class is called Foo, java®
expects the file to be called Foo.java.
• Java® uses directories and a concept called a
package to group related classes. More on
packages later.
• It is a good practice to work on each separate
programming project in a separate directory.
• Packages allow you to share classes/files
between projects and directories.
13
Forte: Filesystems and Projects
• Forte won't even see a file until you "mount the
filesystem" containing the file. You'll learn how to
do that in a moment.
• A Forte filesystem is a directory and all the
subdirectories underneath it.
• Once you mount a filesystem, you can examine
any Java® source files in it using the Forte editor
and other tools.
• You can also add files from the filesystem to a
project or create a new file within the filesystem.
14
Managing Forte Projects
• Every time you start Forte, it will open the project
you were working on when you shut Forte down.
• The first time you start Forte, it opens the
"Default Project".
• To create a new Forte project, select Project
Manager from the Project drop down menu in
the Main Window, and click the New button.
• To open a different project, open Project
Manager, select the project you want to open,
and click the open button.
15
Mounting a Forte Filesystem
• In the Explorer, click the Filesystem tab.
• Right click the Filesystems label at the top of
the pane and select the menu option
Mount->Local Directory
• Navigate in the dialogue that appears to the
directory you want to mount. Make sure to select
the directory instead of opening it.
16
Looking at NauticalMile in Forte
• Let's practice:
– Create a new Forte project called Units.
– Mount the directory where you saved
NauticalMile as a filesystem.
– Double click the tab to the left of the mounted
filesystem to open it.
– Double click NauticalMile.java in the
filesystems pane, and the file should open in
the editor.
17
Neat Things About Forte
• Notice that key words are highlighted.
• Type into the window to mess up the alignment of
the text lines. Then right click and select Reformat
Code in the editor window. It should realign your
margins.
– You can adjust the formatting options. Select
Tools->Options in the Main Window. Then expand
Editing->Editor Settings->Java® Editor->
Indentation Engines->Java® Indentation Engine in
the resulting Options window. A Properties Pane
should appear to the right of the options tree. Change the
value of Add Newline Before Brace. Then close the
Options window and reformat the code.
18
Add Files to a Project
• Note that NauticalMile.java isn't a part of the
Units project yet. Click the Project Units tab
in the Explorer. No files should be listed.
• Now right click on the Project Units entry on
the Project Units tab. Select Add Existing
and navigate to choose NauticalMile.java.
Click OK.
• Recompile by selecting Project->Build
Project in the Main Window. It should compile
cleanly.
19
Executing in Forte
• Let's run it. Select Project->Execute Project.
• A dialog will appear (Set Project Main Class).
NauticalMile should be the only available
choice. Select it and click OK.
• The project should now run. A new view (the
Running Pane) appears. The program output
appears in the output window.
20
Compile Time Errors
• Return to the Editor by reselecting the Editing tab at the
bottom of the Main Window. Now remove the semicolon
from the end of the line that starts
double circum.
• Look in the Compiler Out window. Pretty smart, eh? You
should see
NauticalMile.java [6:1] ';' expected
double circum = 24859.82*5280
with a caret underneath the space where the error was
detected. Click on error message and the corresponding
line will be highlighted in the source file. Fix the error.
• The second error is an artifact. Without the semicolon, the
compiler missed the definition of minutesInCircle.
21
Stepping Through Your Program
• Rebuild. And note that the menus tell you about
shortcuts. You can use Cntl-Shift-F11 to rebuild
instead of the menu if you want.
• Now click the Step Into button on the
Main
Window
• A whole new view (Debugging) appears.
22
Stepping Through, 2
• The StepInto button now has friends:
– StepInto means stop at every line of code
including following method calls.
– StepOver means stop at every line of code in
the current method but execute method calls
in one step.
– StepOut means hurdle over everything in the
current method and stop when the method
returns.
• Click StepOver
23
Examining Variable Values
• In the left frame of the Debugging View, you'll see
a panel labelled "DebuggerWindow". Go to the
Call Stack panel, and expand the line that starts
NauticalMile:
• You should see all the variables that are currently
defined.
• Click StepOver once more to advance another
line. You should see that you just defined another
variable, minutesInCircle.
• Click the Continue button, the program message
appears, and the program exits.
24
Breakpoints
• So what if you are trying to figure out what is
wrong with a BIG program, and you don't have
time to click through every line.
• Go back to the editor. Right click on the line that
defines minutesInCircle and select Toggle
Breakpoint. A breakpoint acts as a stop sign to
the debugger.
• Select Project->Debug Project. Where does
the program stop? What variables are defined?
25
Exiting the Debugger
• Sometimes you want to exit the debugger without
allowing your program to run to completion.
• Just click the Finish button (the one with the red
dot) and OK when the dialog appears.
26
Deleting Files from a Project
• Go back to the Editor View, and right click
NauticalMile in the Project Units tab. Select
Delete.
• When you do this in the Project tab, it deletes the
file from the project, but not from the filesystem
(directory). In fact, you can still see the text in the
editor.
• But beware. If you do this on the FileSystems tab,
you really will delete the file.
27
Creating a New File in a Project
• Right click Project Units on the Project tab and
choose Add New.
• Select Classes->Main from the first page of the
resulting dialog. Click Next. We have said we
want to create a new class, like NauticalMile, that
can run as a console application.
• On the next page, the only thing you need to
change is the Name field. Change it to
Kilometer.
• Click Finish, and browse the new file that appears
in the Editor.
28
Definition of the Meter
• The French originally defined the meter to be
1/40,000,000 of polar circumference of the earth.
• Using that fact and whatever you want to cut
and
paste from NauticalMile, create the code to
calculate how many kilometers there are in a
nautical mile and print it to the Output
Window.
• Compile and debug. Raise your hand if you need
help.
• If you get 1.609, that's the wrong answer.
29
BlueJ
• Is much simpler than Forte.
• Your installation contains an excellent tutorial
and sample projects.
• To view the tutorial, double click
C:\BlueJ\BlueJTutorial.pdf
• Fire up BlueJ, and work your way through the
tutorial.
• As a simple exercise, open the Shapes project,
compile, and see if you can get a circle, square,
and triangle to overlap by invoking methods on
the associated objects.
Java® is a trademark or registered trademark of Sun Microsystems, Inc. in the United
States and other countries.
30