Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
9/21/2012 Using jGRASP An interactive tool for creating and debugging Java programs Start jGRASP If you haven’t installed jGRASP on your computer yet, do that first, then come back to this presentation. • Locate the big G icon on your desktop or go to the Start Menu, All Programs, then click on jGRASP. • The jGRASP window will open. 1 9/21/2012 Navigation • At the left side of the screen is the navigation pane. • Here’s where you see the list of folders and files on your computer. Navigation • Navigate to your USB drive (or wherever your CS 171G files are stored) by doubleclicking on the drive. • Caution: Before you work on a Java program, always make sure that the correct folder is displayed in this pane. 2 9/21/2012 Navigation Buttons Back button Move up to higher folder level Refresh the folder display Create a new folder Practice • Create a new folder (on your USB or hard drive) just for your Java programs • Navigate to that folder. 3 9/21/2012 Practice: Create a Java Applet • Click the File Menu, choose New, then click on Java • You’ll begin typing your program instructions in the large, blank area on the right Practice: Create a Java Applet • • Type in the Java program exactly as shown. jGRASP will color-code the instructions as you type. Careful! Java is case-sensitive. Make sure you get the capital letters as shown. • Save the file with the file name Example.java Remember the curly braces from JavaScript? You're going to use them in Java, also. Make sure you have them matched up. spelling punctuation capitalization 4 9/21/2012 Practice: Compile a Java Applet • The Java compiler will check your program for errors. • Error messages will display in the bottom area of the screen. • Compile your program by clicking the button on the toolbar. Practice: Compile a Java Applet • A new file called Example.class has been created. • Example.java is your “source” code. • Example.class contains "bytecode". • If the program compiled with no errors, you will see "operation complete". 5 9/21/2012 Practice: Compiler errors? • If you see error messages instead of “Operation Complete”, go back and check what you typed. • Gradually, you will learn what each type of error message means. • Correct any typing errors that you made, then click the Compile button again. • Repeat until you get “Operation Complete”. Practice: View the Java Applet • Once you have successfully compiled the program, you can view the applet. • Applets need HTML and a web browser or “appletviewer” • Applets cannot run by themselves 6 9/21/2012 Practice: View the Java Applet • jGRASP has an easy way to create a temporary HTML file and view the applet. • Click the running man icon on the toolbar Note: In old versions of jGrasp, there was an “apple” button for viewing applets. Practice: View the Java Applet • • • • • Here’s what you should see. The applet program you wrote is displayed in Applet Viewer. It draws one, red line from (0,0) to (100, 100). When you’re finished viewing the applet, close the Applet Viewer with the X button in the top, right corner. The Applet Viewer has to be closed before you can make any changes to the program. You’ll learn all about drawing, setting colors and using the coordinate system in the “Graphics” notes and lab assignment. 7 9/21/2012 A Second Java Example Creating an Application • Close the program (File Menu, then Close). • Now create a new Java program and type in the program shown here. • This program is an application. It can run by itself, without having HTML, a web browser, or applet viewer. Note: this program has a few errors. Type it exactly as shown here. We’ll get rid of the errors shortly. A Second Java Example Debugging • • • Save the program with the name Example2.java Compile the program In the bottom area of the screen, you should see one error message. • Java has a multi-level compiler. It checks for very simple “typo” kinds of errors first, such as this missing semicolon. Click in the program area and put a semicolon at the end of the line. (In my program, it’s line 11.) x = scan.nextInt(); Compile the program again. • • 8 9/21/2012 A Second Java Example Debugging • • Now you should see one error message, but it’s a different error. This error is a level 2 compiler error. Although you might think it’s just a simple typo, Java thinks it’s a more complex error than the missing semicolon. • • • • The level 2 error messages are usually not very helpful. The error message is “cannot find symbol” which doesn’t tell you much. The real problem here is that the “s” on “system” should be a capital “S”. Change it to a capital S. Scanner scan = new Scanner(System.in); Compile the program again. A Second Java Example Debugging • Now you should see a successful compile and the message “Operation Complete”. • Debugging is the process of finding and fixing errors in a program. • The compiler helps you find syntax errors… errors in spelling, punctuation, capitalization, words out of order, etc. • Even though you fix all of the syntax errors in a program, it doesn’t necessarily mean there are no more errors. • The other type of error is called a logic or “runtime” error. When you run the program (or view an applet), it doesn’t work right. Runtime errors are even harder to debug than syntax errors. 9 9/21/2012 A Second Java Example Running the Application • In jGRASP, you can run an application by clicking the Run button • Click run. Notice the cursor next to the two triangles in the message area. It’s waiting for you to input a number. Type some number and press Enter. • When you run an application in jGRASP, the input and output is done in the “Run I/O” tab near the bottom of the screen. I/O is short for “Input and Output. • This program is complete. You can exit from jGRASP now. Summary • jGrasp is a simple interactive program that allows you to edit, compile, and test Java programs • To compile a program use the button. • To run a program (or view an applet) use the button. 10