1 introduction to computation and problem solving class 2: integrated development environments prof....

30
1 Introduction to Computation and Pr oblem Solving Class 2: Class 2: Integrated Development Integrated Development Environments Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

Upload: kerry-franklin

Post on 12-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

1

Introduction to Computation and Problem Solving

Class 2: Class 2: Integrated Development Integrated Development

EnvironmentsEnvironments

Prof. Steven R. Lerman and

Dr. V. Judson Harward

Page 2: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

2

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

Page 3: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

3

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

Page 4: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

4

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.

Page 5: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

5

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

Page 6: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

6

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 ); } }

Page 7: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

7

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.

Page 8: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

8

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.

Page 9: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

9

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.

Page 10: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

10

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

Page 11: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

11

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.

Page 12: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

12

Anatomy of Forte

Page 13: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

13

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.

Page 14: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

14

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.

Page 15: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

15

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.

Page 16: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

16

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.

Page 17: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

17

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.

Page 18: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

18

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.

Page 19: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

19

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.

Page 20: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

20

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.

Page 21: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

21

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.

Page 22: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

22

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.

Page 23: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

23

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

Page 24: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

24

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.

Page 25: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

25

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?

Page 26: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

26

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.

Page 27: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

27

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.

Page 28: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

28

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.

Page 29: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

29

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.

Page 30: 1 Introduction to Computation and Problem Solving Class 2: Integrated Development Environments Prof. Steven R. Lerman and Dr. V. Judson Harward

30

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.