CSCI161: Introduction to Programming I

Spring 2009

Lab 1b: Hello, World (Eclipse, Java)

 

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. Then, you will copy your program file, make some changes to it, compile it, and resubmit the project.

  /* Hello.java: traditional first program
   * Author: your name 
   * Date: today's date 
   */
  import java.util.Scanner;
  import javax.swing.JOptionPane;
 
  public class Hello {
     
  
 
     public static void main(String[] args) {
         
         String personName;
 
         // Console input and output
         Scanner input = new Scanner(System.in);
         System.out.print("Please type your name: ");
         personName = input.nextLine();
         System.out.println("Hello, " + personName);
 
         // Graphical input and output
         personName = JOptionPane.showInputDialog("Please type your name: ");
         JOptionPane.showMessageDialog(null, "Hello, " + personName);
     }
  }

Creating a Java program (Hello.java)

Open a terminal window and change your working directory to be your ~/161/Lab1 directory (type cd 161/Lab1).  Now open Eclipse, e.g. by typing eclipse &   or by clicking on the Eclipse icon. When the Eclipse workbench appears, your Lab1 project will appear in the Package Explorer at the left of the window.  Your myinfo.txt file will be open in the editor pane.

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 you click the arrow just to the right of the icon, this produces a drop-down menu and you need to select ‘Class’ (the top entry).  A new dialog box will appear.  Leave the source folder as ‘Lab1’ and leave the package blank.  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)’.  Now click the ‘Finish’ button.

In the editor pane, you will see a new tab entitled ‘Hello.java.’  This file automatically has focus.  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 code shown above.

Begin typing in the Java program shown above. Include your name in the comments, along with today’s date. Type the “import” statements before the “public class Hello {“ line.  As you type statements in, 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.

Click the ‘Run’ button (the green circle with the white triangle that looks like a play button).  (If you click the down arrow, you will need to select ‘Run…’). 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 can click the box for ‘Always save resources before launching’ to avoid being prompted to save in the future.

This program combines 1) console input/output and 2) graphical components/GUI (graphical user interface).  For the console input and output, the program asks the user to enter their name from the console (located at the bottom of the workbench window in the Console tab).  After the user enters their name in the Console and hits ENTER, the program greets the user, saying ‘Hello, ’ followed by the name that the user has entered. 

The program then displays an input dialog box prompting the user to enter their name.  After the user types a name and hits the ‘OK’ button (or hits ENTER), a message box appears, greeting the user and using the provided name.

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: In the terminal, use submit to submit your updated project.  Make sure that you are in your Lab1 directory. The files being submitted should be myinfo.txt and Hello.java.

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 Program from an Existing Program: In Eclipse, create a new project called Hello2, following the same steps you did in creating Hello (if you forget, the steps are shown in the Computing Tips document Creating a New Program).

Next, create a new class in the Hello2 project by selecting the Hello2 project name in the Package Explorer pane (along the left side of the Eclipse window) and then clicking on the New Java Class icon. Call this new class Hello2 also. Once you've created this new class, you can copy all of the code from the Hello program (with just a copy and paste operation)...make sure you replace ALL of the contents of the Hello2 class with the code from the Hello class.

Make a Few Small Changes: First change the class name to Hello2 (as in ‘public class Hello2 {‘). Edit Hello2.java so that the console input and output asks for the user’s first name, reads the first name, then asks for the user’s last name, and reads the last name.  The program should then greet the user by first and last name, with proper spacing between the names. Note that you will need two string objects, one for the first name and one for the last name.

Run the Program: Now run ‘Hello2’.  Click on the down arrow beside the ‘Run’ button and select ‘Run…’.  Click the ‘New’ button (the one with the page and the ‘+’ sign) and a ‘Hello2’ launch configuration will be created (Name is ‘Hello2’ and main class is ‘Hello2’).  Click the ‘Run’ button.

Make a Few More Changes: Now change the graphical code to mirror the console changes.  You will display two input message boxes (one each for first and last name) and then a single message box greeting the user with their full name.

Run the Program: This time, you can just click the ‘Run’ button.

Submitting Your Files: When you are done, submit your modified projects. The files being submitted should be ‘myinfo.txt’, ‘Hello.java’ and ‘Hello2.java’. You will need to use the terminal window to do the submissions. First, change the directory to the 161/Lab1 directory to submit both the info.txt and Hello.java files. Then change to the Hello2 directory to submit the Hello2.java file.

Congratulations! You’ve survived your first 161 lab!