Goals - to learn how to compile and execute a Java program - to modify a program to enhance it Overview This activity will introduce you to the Java programming language. You will type in the Java program shown, compile it, and execute it. You will submit your project. Creating a Java program (Hello.java) CSCI 161: Introduction to Programming I Lab 1b: Hello, World (Eclipse, Java) /* Hello.java: traditional first program * Author: your name * Date: today's date */ public class Hello { /** * @param args */ } public static void main(string[] args) { // TODO Auto-generated method stub } // Console input and output System.out.println("Hello, world!"); You should still have a terminal window and Eclipse open. If not, open a terminal window and change your working directory to be your ~/161 directory (type cd 161). Now open Eclipse by typing 'eclipse &' (be careful not to omit the ampersand), or just click on the Eclipse icon on the tool bar at the left of the desktop (Not there? Click on the Applications button in the upper left corner of the desktop.). Create a new project by clicking on File New Java Project. o This may pop up a Select a wizard window, as shown in Figure 1. If it does, Click on the arrow to the left of where it says Java, then click on Java Project. Click the Finish button. 1
Figure 1 o A Create a Java Project dialog box should open. For the Project name type in Lab1a (the fourth character is a one; without the quotes, and with no spaces). o Under Project layout, Click on the radio button that says Use project folder as root for sources and class files. Click on Configure default Click on the Project radio button. Click on the OK button. o Click on the Finish button. 2
o A new dialog box should appear as shown in Figure 2. Click on the Remember my decision checkbox (so that you never get this dialog box again in the future), and then click on Yes. Figure 2 If all goes well, you should now see the following screen (you can close the Outline pane on the right side of the window, since we won t be using it): Figure 3 3
Make sure that Lab1a is selected under the Package Explorer pane on the left side of the window (it would be highlighted). Create a new Java class file by clicking on the New Java Class button (it is a green circle with a C and a little +). If by mistake you click the arrow next to the New Java Class button (which causes a drop-down menu to appear), you need to select Class (the top entry). A new dialog box will appear. Make sure that Lab1a is the source folder name (make it so, if it isn t). Type Hello as the Name. Under Which method stubs would you like to create?, click on the box to create public static void main(string[ ] args) so that a check mark appears. Now click the Finish button. In the editor pane, you will see a new tab entitled Hello.java. This file automatically has focus, i.e. it s where things that you type will show up. This file contains the basic outline or stub of a Java class called Hello the first line should read public class Hello {. You will fill in this stub with the rest of the code shown above. Begin typing in the Java program shown above. Include your name in the comments, along with today s date. As you type statements, pay close attention to capitalization (some letters must be capitalized) and spacing, both within a line and between lines. You want your code to look exactly like that shown above. Get into the habit of indenting the program as shown above. The Eclipse editor makes it easy to use consistent indentation by automatically indenting the next line after the enter key is pressed. If you press 'CONTROL-SHIFT-F', Eclipse will automatically indent all of your code. This program has minimal comments. We will expect more on later assignments. Comments and indenting are essential to the readability of your program. Type in the program logic just below the // TODO Auto-generated method stub comment line. Note that Eclipse will attempt to assist you as you are typing in the code by suggesting possible completions to what you have typed so far. You may ignore these suggestions for now. Once you ve typed in the entire program as shown above, Click the Run button (the green circle with the white triangle that looks like a play button). (If you accidently click the down arrow beside the Run button instead, you will need to select Run ). If Eclipse cannot automatically create a launch configuration, a dialog box will appear. Highlight Java Application and click the new button (a page with a plus sign if you hover, it says New launch configuration ). Hello is added below the Java Application and it will be highlighted. The Name should default to Hello, the project to Lab1, and the Main class (which is the name of the class containing the method or function main ) to Hello. Click the Run button to launch your application! If you have not saved the Hello.java file, you will be prompted to save your files you should click the box for Always save resources before launching to avoid being prompted to save in the future. 4
This program is a very simple example of console output. When you run it, the phrase Hello, world! will appear in the console window (located at the bottom of the workbench window in the Console tab). Debugging your program: If Eclipse detects a mistake in your code as you are typing in your program, the line will be marked with a red wavy underscore. You should attempt to correct the error before running your program. If you do run a program and run-time errors occur, error messages will be shown in the Console pane. Continue to debug (correct) your program until you are able to run it without exceptions. Submit your Project as Lab1a: In the terminal window, use submit to submit your updated project. Make sure that you are in your Lab1a directory. If not, use the cd command to get there. Type the command submit When the dialog asks you to enter the name of the grader account, type in liffick161 The file being submitted should be Hello.java. If it isn t, you are probably still in the wrong directory. Reposition the directory using the cd command and try again. Modifying an existing program (Hello2.java) For this next activity, you will modify the program you created above. You don't want to replace your previous program, so you will copy it and modify the copy. Create a New Project: Use the same process as above by clicking on File New Java Project. Name the new project Lab1b. Create a New Class from an Existing Program: Follow the same process as above to create a new class called Hello2 for the Lab1b project. You can copy and paste the Hello program from Lab1a into Lab1b to avoid retyping most of the code for this new project. Make a Few Small Changes: First change the class name to Hello2 (as in public class Hello2 { ). Edit Hello2.java so that the console output greets you by first name, for example, Hello, Jane! instead of Hello, world! Run the Program: This time, you can just click the Run button. When you are done, submit your modified project again as "Lab1b". Congratulations! You ve survived your first 161 lab! 5
Practice Some Terminal Commands: Learning a few terminal (Unix bash shell) commands will make it easier for you to use the system. You'll learn these commands as time goes by. If you are connecting to these machines from outside the lab, you will get a terminal when you connect. In the terminal, type the commands in the left column. The right column describes what you are doing. When you complete that series of commands, you should be back at your 161 folder. If not, figure out how to get there. Remember that you always need to move the terminal into ~/161 to work on files for this course. Command cd pwd ls ls -l cd 161 cd..!p cd 161!! man pwd Meaning move to your home folder (~sabeach) display the current directory name list files in home (161 should be there) list files, last time changed, size, and permissions move into 161 folder move one folder up file hierarchy repeat most recent command that started with p (pwd) move into 161 folder repeat previous command (this gives you an error message because there is no ~/161/161) display the manual for 'pwd' command; press spacebar to get next page if necessary; press 'q' to quit 6
This picture shows the file system for a student with userid sabeach. Her home directory is ~sabeach (or the full path, /home/students/sabeach) with two subdirectories, 140 and 161. Logging Out: From the menu at the top of the screen, choose System -> Log Out userid. You will be asked to confirm; click the Log Out button. You are logged out when you see the login panel on the screen. Normally, before you leave the lab, you should log out this way. Save any work before logging out. You'll log in again to start the next part of the lab activity. Further Exploration: There are many ways to learn about the system and get help. Explore these outside the lab time: - explore the Help menu - use Applications/Internet/Mozilla FireFox to get a web browser and visit Java sites on the Internet. 7