CMIS 102A Lab exercise #1 Create a folder d:/cmis102Ayourname or use My Documents or use your USB key flash memory stick. This is where you should put all your files related to this course. Through the course you will be building up a portfolio of Java programs that you can copy and paste from and be proud of. Best would be to use your own laptop. *************************** Browser irritations: I recommend you use the Firefox web browser instead of Microsoft's Internet Explorer (IE) because: 1. it displays .java files in the browser instead of insisting on running a helper app, [dependant on webs site, unfortunately] 2. won't mess up saved downloaded files, if Saved As "Web page, HTML only" 3. multitabbing browsing ability, (MS has fixed this) 4. blocks popup windows. (MS has fixed this) 5*. most important for us in the class, it won't change the names of the example programs and cause you frustration! IE's only advantage is that it loads fast thanks to its close integration with the Microsoft operating system (whatever its name is this year) and perhaps to Microsoft's ability to cheat and use undisclosed APIs. If you choose to use IE don't ask me to fix the problems it causes. Firefox is installed on all Maryland computers. It can be downloaded from www.getfirefox.com *************************** Go to the class web site: http://davidwills.net/cmis102a JCreator.exe is in C:\Program Files\Xinox software\JCreatorV3LE It's in Start | Programs menu. and/or icon on desktop. *************************** JCreator irritations: 1. Indentation of my programs won't work unless you change the tab size to 8 characters: Configure | Options | Editor Java Tabs Size:8 2. If JCreator starts in a Workspace (you can determine if it does if the title bar has a word before "JCreator") which will compiling and running your program difficult, so do: File|Close Workspace. Then in: Configure | Options | Workspace unclick "Open last workspace at startup". 3. sometimes the Compile and Execute buttons become inactive due to some "Tool" running, so in the Tools menu click Stop Tool. 4. We won't be using the Project or Workspace features of JCreator. Use them if you want but don't ask me anything about them. 5. sometimes someone has "hidden" the syntax error pane, so at the bottom of the JCreator window "pull up" that pane. 6. if .java wasn't associated with JCreator by Windows, then double-clicking a .java file will not start JCreator but some other program such as Visual Java (this is a problem in one of the labs). *************************** Use JCreator to create program HelloWorld, compile it and run it. The HowtoUseJCreator document shows how to compile and run a program (click the "Build File" button, then click the "Run Project" button) Here is the program source code: (It's an application (note that it has main) and displays its output in the General Output pane) Paste this into a new empty java file in JCreator. Make a new file, not a new project, we won't be using JCreator projects in this course. Name the file HelloWorld.java (note the case of the letters) public class HelloWorld { public static void main (String[] args) { System.out.println ("Hello World!"); } } Change the program to display: HelloWorld! This is xxx where xxx should be replaced with your name. Download, compile and run the WageCalculator.java file. Either copy and paste from the browser into a new file in JCreator or, better, SAVE the file from the browser and then open file in JCreator (avoiding the step of creating and naming a new file in JCreator). This is an application. It uses the Java Scanner class to do input. We won't be using Scanner much in this course. Download, compile and run the WageCalculatorGUI.java file. "GUI" means graphical user interface: windows, menus, button, textfieldss. We won't be doing that in this course because it's complicated and there is a lot of more basic stuff we have to cover first. Notice how much Java code there is for a relatively simple GUI program. Download, compile and run the WageCalculatorOptionPane.java file. We will learn how to do input and output (I/O) with these dialog boxes. Notice how much less Java code there is than the GUI program. Modify the messages that the JOptionPanes display. Make some syntax errors in this program and then compile the program to see what error messages you'll get. Note that if you double click the first line of an error message, a red arrow will point to the line that causes the error. The actual syntax problem is at or above the indicated line. Try the following syntax errors (one at a time): omitting a semicolon at the end of a statement, changing the case (i.e. uppercase vs. lowercase) of a letter, misspelling a word, changing the class name, omitting a brace etc. Run the WageCalculatorApplet by clicking its link in the class home page. Applets are Java programs that execute in a web browser, typically as part of a web page. They are what we will spend most of our time on in this course because they are more fun, graphical, and cooler than command-line applications. We'll learn how to make them. For a better idea of the capabilities of an applet, run these: http://davidwills.net/cmis102a/Bouncing2.html http://davidwills.net/cmis141/Bouncing.html http://davidwills.net/cmis141/PolygonsTest3.html ***************************************** Optional, Use Notepad (or some other text editor) to view a .java file, which is a text file whose entire contents are the characters you see in Notepad and in JCreator. In Windows Explorer, or My Computer, or a shell, view the properties (by right clicking the filename) to see the size of the file in bytes (HelloWorld.java will be about 121 bytes). View the .class file in Notepad. It is a binary file, not text, so you see a lot of garbage characters (little boxes and weird symbols which are bytes that Notepad is trying to show as ASCII text characters). The .class file is small too. ***************************************** Optional, to understand what's happening when you compile and run a Java program: When you compile in JCreator, it runs the javac.exe program of the JDK. When you execute a compiled Java program in JCreator, it runs the java.exe program of the JDK. Look in the bin folder of the JDK folder to see these files. Both javac and java can be run manually from the Windows command prompt: 1. Start | Run | cmd starts a command prompt. 2. cd to your cmis102a folder. 3. You will have to give the full path to the javac program: 3a. to compile: "C:\Program Files\Java\jdk1.5.0_01\bin\javac" HelloWorld.java (note the quotaion marks and the space) (note that the path might have to be different depending on the version of the JDK) 3b. to execute: java HelloWorld (note the lack of .class) (java.exe has been put into C:\Windows\system32, which is in the PATH, obviating the need for the fullpath) On your own home computer (you won't be able to do this in the lab): To avoid having to indicate the path to the programs, modify the PATH variable to include the bin folder: (from Sun's documentation) Choose Start, Settings, Control Panel, and double-click System. On Microsoft Windows NT, select the Environment tab; on Microsoft Windows 2000 select the Advanced tab and then Environment Variables. Look for "Path" in the User Variables and System Variables. If you're not sure where to add the path, add it to the right end of the "Path" in the User Variables. A typical value for PATH is: C:\Program Files\Java\jdk1.6.0_\bin Capitalization doesn't matter. Click "Set", "OK" or "Apply". The PATH can be a series of directories separated by semi-colons (;). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should only have one bin directory for a JDK in the path at a time (those following the first are ignored), so if one is already present, you can update it to jdk1.6.0_\bin. 4. Restart the command prompt for the new path to take effect. 5. javac HelloWorld.java java HelloWorld