COS 170 : Lab 1 - Introduction To Editing and Running Java
Programs COS 170 : Lab 1 - Introduction To Editing and Running Java Programs Objectives The objective of this first laboratory is to familiarize you with the Eclipse Integrated Development Environment (IDE), the application in which programs for this course will be developed. Only the basic features of this IDE will be considered in this lab. You will learn how to manage files, edit programs, compile and run programs, and capture output from those programs. YOU SHOULD BRING YOUR USB FLASH DRIVE TO THIS AND ALL FUTURE LABS! Preparation Before you get started, you need to form teams. The professor will instruct you how teams are formed. Only one student per team needs to log into a computer. Log into a University computer with your UMaine System(UMS) account. If you haven't yet activated your account, go to http://mail.maine.edu and choose Account Activation. If you do not have your Activation Code, please visit the HelpDesk in the Portland computer center (first floor of Luther Bonney Hall) or call them at 780-4029. Download the file FirstClass.java to the Desktop. (Right click the link -> Save Target As*... -> Browse to Desktop -> Click Save) Open the file using Notepad. Highlight the entire contents of the file. Copy the file to the clipboard with Ctrl-C Then just paste it into the Lab3 project folder with Ctrl-V With your favorite word processor open a new text file (e.g., Lab1 Solutions.doc) into which you will paste your solutions. (Microsoft Word is fine.) * "Save Target As..." is what you will see with Internet Explorer. In FireFox it will say "Save Link As..." and SeaMonkey will say "Save Link Target as..." . Lab Writeups All labs must be neatly typed and organized as a single solutions document, and only one solution document per team should be turned in. On the first line put: COS170 Lab1 [the Professor's name] [the day & time of your lab section] by [names of all team members] The lab procedure is based on a profesional programming practice called Extreme Programming where programmers work in teams of 2. One is designated the driver and is the one that sits at the keyboard. The other member is called the navigator and should be filling all of the supportive roles: looking for problems, offering suggestions, keeping track of issues that need to be addressed and checking that the exercise is being correctly and fully solved. An effective team is very active and communicates frequently to confirm that each partner understands and agrees with what is being done. The driver and navigator roles should be swapped after each exercise. In your solutions document include the lab exercises in order and well labeled. Create the structure of your document now with labels for each exercise and who the driver will be. This will help remind you to switch roles. For example this lab has 5 exercises, so your solution document might look like: COS 170 Lab 1 Professor Einstein Thurs 12:00 by Bart Simpson & Lisa Simpson Exercise 1 - Bart Exercise 2 - Lisa Exercise 3 - Bart Exercise 4 - Lisa Exercise 5 - Bart As you complete each exercise cut and paste your answers into the solutions file. If you save your solutions file to your USB drive now, it will keep you from accidentally saving it to the local machine later. When you are done with the lab, you should save a final copy and print a copy to turn in. If you finish during the lab period you can turn it in that day, otherwise you will need to make arrangement with your lab partner to meet up to finish it before the start of next week's lab. All labs are expected to be turned in on paper, not emailed. We need paper copies for grading and giving feedback. At the end of the lab there are few quick suggestions for saving paper. Up to two points per lab may be deducted for messy work, and no points will be given if work cannot be found! Hand in ONLY the required materials for those exercises assigned. Please do not enhance the programs with features other than the ones for which we ask. You should feel free to experiment with things on your own, but only submit the requested items. Also please try to conserve paper. If the output of an exercise seems excessively large just cut and paste enough to show us that you have done the exercise. We hope the labs are clearly explained, but please contact the instructor if you have any questions. Exercise 1: (2 points) Now, we want to open FirstClass.java in Eclipse. Open Eclipse (Start/All Programs/Programming Applications/Eclipse...). If this is the first time that you have logged into Eclipse, it will ask you for a default workspace. This will be a main folder to hold all of your projects for this course. If you have a usb flash drive, choose its location and name a folder (if the folder does not already exist it will be created). You can also just use the Desktop, but beware that anything stored to the Desktop will be erased when you logout. If you see a "Welcome" tab, click the X to close it. Go to File -> New -> Java Project (make sure you select Java Project) Under Project Name type: lab1 Under Project Layout choose "Use project folder as root for sources and class files" Then Click Finish and your new java project folder will be created. Now we will load the downloaded java file: FirstClass.java into your new project folder. First make sure your project is selected, by clicking the name of your project (lab1). Go to File -> Import, select [+] General then File System and click Next. Click the Browse button and select the folder containing FirstClass.java (for example select the Desktop if this is where it was downloaded to). Click the checkbox next to FirstClass.java on the right hand listing. Click Finish. The FirstClass.java file will now be copied into your project folder. On the left hand "Package Explorer" panel, click the [+] next to lab1, then again on default package, and finally double click on FirstClass.java to view the code. Look at the program. Since it is well commented you can probably figure out what most of it does. Select all the text by first clicking inside the FirstClass.java source code window then use the menu items Edit -> Select All (Ctrl + A), and then copy the contents from the Eclipse IDE, using Edit -> Copy (Ctrl + C) and paste the copied text into your solutions document using Edit -> Paste (Ctrl + V). Exercise 2: (2 points) There is an error in the code, as shown by the red highlights in the code. If you hover the mouse over the red markings it will tell you information about what it thinks the error is, and sometimes it even has quick fix suggestions about how to fix the problem. I suggest you do not use the quick fixes unless you are 100% sure they are what you want to do! It often has good suggestions, but it only understands the rules for correct syntax. You are the only one who truly understands what the code is supposed to do. The message "j cannot be resolved" means that you are using a variable named j but that it has never been defined earlier in your program. To real problem is that when we declared the variable i earlier in the program we meant to call it j. We will fix it using Find / Relace to get more practice with eclipses. Click at the top of your FirstClass.java program to move the cursor to the top. Go to the Edit -> Find / Replace menu item In the window that pops up, put i in the 'Find:' textbox and j in the 'Replace with:' textbox. Select under Options: Whole Word. This will replace i with j, matching the whole word only (so that the word 'import' does not become 'jmport'). Click on Find to find the first match and click Replace to replace it. This was the only match and the red error markings on the code should disappear. If the search finds no matches, click the "Backwards" tab and try again since your cursor was most likely at the end of the source code. Hit Close to return to the program. Copy the corrected line from FirstClass.java into your solutions document. Exercise 3: (2 points) With the errors all fixed, we are now ready to run the program: Your program is probably already selected (highlighted in the Package Explorer panel), but if not, select it by clicking on it. Select Run -> Run As -> Java Application. Or just use the shortcut icon . If you get the Save and Launch window press OK. This should display output text in the Console tab: Copy this text from the Console and paste it into your solutions document. Exercise 4: (2 points) Find the following line in the FirstClass.java program listing: System.out.println("Powers of two less than or equal to 32:"); Replace this line with the following (you can enter your own message within the double quotes): System.out.println("Your message goes here"); Now run the program again. (When you get tired of the Save and Launch dialog, check "Always save resources before launching" and it will quit asking in the future.) Copy the line you changed in the program and also the output from the Console and paste it into the solutions document. Exercise 5: (2 points) Change the program to make it print the powers of 2 up to 1024. Go ahead and experiment by making changes to the program. If you do something that causes a problem you can always undo it by clicking Edit -> Undo Typing. Copy and paste your changes to the program and the resulting console output to your solutions document. YOU ARE DONE! Save this document document to your USB flash drive, and print a copy to turn in. If you used Microsoft Word, here are a few quick things you can do to save paper: [Home] -> Select all (Ctrl-A) and change the font size to 8. [Page Layout] -> Margins -> Narrow [Page Layout] -> Orientation -> Landscape [Page Layout] -> Columns -> Two Exit eclipse before removing your USB drive. Saving a copy for your partner All of this work has presumably been done on one partner's USB drive. To make a copy for the partner: Close Eclipse Using Windows My Computer look directly on the USB drive, and note what drive it is, probably E: or F:. You will find your files on the USB drive in a workspace folder that you probably called workspace and a project subfolder called lab1. You will also notice files called .metadata, .classpath, and .project. Eclipse creates these hidden files when you create the workspace and the project folders. Don't try to copy these files. The partner should insert their USB drive into the other USB slot. (leave the first USB drive also inserted) Start Eclipse and open or create a workspace on your USB drive. Browse for your USB drive Create your workspace e.g.: E:\workspace Create your new java project called lab1 now import from your partners USB drive all of your .java files and your solution document Exit eclipse before removing your USB drive. Extras - If you are done early, here are some extra things to try. You don't need to turn these in. They are just to enhance your understanding and proficiency. Eclipse is highly customizable. The java perspective icon in the upper right corner shows the current "perspective". This means that the various panels that you currently see were chosen and arranged to be helpful when writing java programs. (In the future you will also be using the debug perspective.) You can minimize or maximize each panel or drag the edges between the panels to change their sizes. Since I never use the "Task List" panel or the "Outline" panel (you might not see them both), I get rid of them by clicking the close icon on those panels. This will give you more room to show your code. Most of the time you wont be actively be using the Package Explorer. You can minimize it with the minimize icon and restore it when you need it with the restore icon that appears along the left edge of the window. These changes will be saved automatically in your workspace when you exit eclipse. If you ever want to restore the original layout of the perspective, just right click on the java perspective icon and choose reset. Short variable names like j are quick to type, but not very descriptive. Use Find / Replace to replace all of the j's with the variable name number Try to change the program to print powers of 3. Try to change the program to print even numbers, e.g.: 2 4 6 8 10 ... Please remember, if you want to save your work and take it with you, you must save it to your flash drive. If you used the Desktop, you can save it by emailing it to yourself.