Out: The week of the Sept 6th
Due: A progress check is needed at the end of the first lab. If you haven’t finished by then the exercises will be checked off at the very beginning of the 2nd lab. If you aren’t ready to demonstrate the exercise on request at that time then a 10% late penalty will apply.
CENG 310 – Lab 1: Hello World In Java
The purpose of this exercise is to show you how to implement a Java program in different environments. All of the java code has been given to you – the only modification should be to modify strings so that your name is displayed. How the code works will be described briefly at the beginning of the lab and in more detail in the first lecture.

  1. Command Line Version
  2. Log onto your munro account. Create a file Hello.java and enter the following code – where italics appear put in actual values. This is similar to Listing 1.1 in the course text.
    class hello
    {
    public static void main(String[] argv)
    {
    System.out.print(“Hello world! The time is now: “);
    System.out.println(new java.util.Date());
    System.out.format(“My name is: %s. My age is %d.\n”, “Your name”, your age);
    }
    }
  3. Save the file but stay in the editor. Open a second terminal window on munro. Rather than entering and exiting the editor it saves time (and is therefore good practice) to use multiple windows.
  4. Compile the program by typing in the following command:
    javac Hello.java
    Record any error messages. There should be one error message. If you have more than one error then check your code for any typos.
  5. In Java the name of the class should match the name of the file. By convention the class name should be capitalized. Change the 1st line of the program to: class Hello and recompile. This time it should work.
  6. A successful compile will generate a file of type .class.
    Run the program using the following command: java Hello
  7. Compile Once, Run Everywhere
    A class file consists of java bytecode which consists of assembler type instructions that are interpretted by a java runtime environment which is what the java command that you ran in the previous step is. You will demonstrate this by moving a class file from Unix to Windows and running it without recompiling.
    In Windows, on your H: drive or a USB key, create a directory for this course. Use FileZilla or any other file transfer program to copy the Hello.class file to this directory.
    Open a Windows cmd window, cd to the directory containing Hello.class and reenter the command: java Hello
    Aside from a different value for the time, you should get the same output.
  8. Creating a Java Server Page
  9. Preliminary Step: At the Unix command prompt issue the following command:
    chmod o+x ~ ~/public_html
  10. Java server pages allow you to mix html and java code contained in .jsp files together.
    On munro, in your public_html directory, create a file hello.jspwith the following contents and set the permissions so that others can read it.
    <h1>Hello World Demo</h1>
    The time is now <%= new java.util.Date() %>
  11. Display the page in a browser. There is no need to compile it – the file is automatically compiled by the server when you request it from the browser
  12. Creating an Applet: Hello World on a Web Page
  13. On munro in your public_html directory create a file JDisplay.java containing the following code based on the example in Liang on pp615 and save it in your directory.
    import javax.swing.JApplet;

import javax.swing.JLabel;

public class JDisplay extends JApplet {

public void init() {

add(new JLabel("Great!", JLabel.CENTER));

System.out.println("Applet has begun");

}

}

Use the javac command to compile the file. Move on to the text step when you’ve compiled with no errors and have been able to generate a class file.

  1. In the same directory create a web page. This is based on listing 18.2 on pp615.
    <html>

head

title>MyNameHere Java Applet Demo</title>

</head>

body

<applet code=JDisplay.class height=50 width=250>

</applet>

</body>

</html>

  1. Change the permissions so that both are readable by others. Use your browser to view the web page: yourid/yourFile.html
  1. NetBeans Version
    In this last exercise you’ll be using the NetBeans IDE in a Windows environment to develop your program – this is very much a point-and-click exercise
  2. Start NetBeans 7.0 from the Windows Start Menu. Select: File | New Project | Java Application | Next
  3. Change the Project Location field to point to the course directory you created earlier on. Change the Project Name field to HelloYourName. Then click Finish.
  4. In the code window underneath the comment // TODO code application logic heretype in:
    sout<tab key>
    The line will automatically expand to: System.out.println("");
    Add “Hello World” inside the quotes. Add a 2nd line to print out the current date and time as you did in the first example.
  5. To compile and run the program either press the green arrow above the code or press F6 or select Run | Run Main Project from the menu bar or right click on the name the the project in the Project Window and select Run. Try all 4 methods.
    Note that the output will appear in a tab in the Output window.
  6. Deploying your project outside of NetBeans
    Notice in the Code Window that not only is a class generated, but there is also a package statement. Even though we have only created a single class, packages are able to contain multiple class files. The type of file generated is .jar which stands for java archive.
  7. Click on the Files tab next to the Project window. Click on the + icon next to the name of your project to display all the files and directories in your project.
  8. From the menu select Run | Clean and Build Main Project. This will create a new directory called dist.
  9. Open up a new cmd window from the Windows Start menu.
  10. cd to the following directory: HelloYourName/dist. List the contents of the directory. You should see a file helloyourname.jar. To run the program:
    java -jar helloyourname.jar
  11. Transfer the jar file to your munro account. To run it enter the same command.
  12. Advanced: Using NetBeans to Create an Applet
  13. In the Project window right click on the name of the project and select New | Other | Java | JApplet | Next. Give the JApplet a name and click the Finish button.
  14. In the init method add the same code used in Step IV.
  15. To run, press F6. A small window will show you the output of the applet.
  16. When you have completed the lab review what you have done and write up a page for your notes as to what you have learned. I may ask to see your notes in the lab.

Evaluation

The only way not to get 100% would be to not show up for the lab or not to demonstrate each of the exercises.

  1. Demonstrate the program Hello both on munro and at the command line in Windows. (25%)
  2. Demonstrate the java server page: 10%
  3. Demonstrate the applet running in a Web Page where the source is in Windows. (25%)
  4. Demonstrate the applet running in a Web Page where source is in Unix: 10%
  5. Demostrate running your project in NetBeans (both programs): 10%
  6. Demonstrate running the jar file in Windows and in Unix: 10%
  7. Completion of lab notes: 10%
  8. 10% bonus for completing and demonstrating in the first week’s lab.
  9. 10% penalty for not completing the demonstration when requested at the start of the Week 2 lab. (Not being there or being late is not an excuse)
  10. Further penalties in units of 10% at the discretion of the instructor, however since everyone will have this done on time it’s not something we should need to go into. ;-)

Modifications
This is a new lab for 2011. There were some minor problems with the instructors that were uncovered in the Sept 6th session. If you have a printed copy of the earlier version, here are the corrections that were mentioned in the lab. This document already includes these changes.

  1. Your accounts on munro have not been set up to display web pages. You need to add “navigation” permission to your main and public_html directories by making them executable by others:
    chmod o+r ~ ~/public_html
  2. In step Ib, new Date() is replaced by new java.util.Date()
  3. In step III a semicolor after new java.util.Date()was removed
  4. The javac compiler was not installed in Windows in the lab. Instead of entering the code for the applet and compiling it in windows, the development was moved to Unix.
  5. In part VI the path of clicks to create a JApplet was not complete.