Lab 1/Corrections added on Sept 7th

ohiofulvousSoftware and s/w Development

Aug 15, 2012 (4 years and 11 months ago)

226 views

Out: The week of the Sept 6
th

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 2
nd

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
environme
nts.
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.


I.

Comma
nd Line Version


a.

Log onto your
munro account
. Create a file
Hello.ja
va

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[] a
rgv)


{


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);


}


}


b.

Save the file but stay i
n 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.


c.

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.


d.

In Java the name of the
class

should match the name of the file. By convention the class
name should be capitalized. Cha
nge the 1
st

line of the program to:
class Hello
and
recompile.

This time it should work.


e.

A successful compile will generate a file of type
.class
.


Run the program using the following command:
java Hello


II.

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.


III.

Creating a Java Server Page

a.

Preliminary Step: At the Unix command prompt issue the fol
lowing command:


chmod o+x ~ ~/public_html


b.

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.jsp

with the following
contents

and set the permissions so t
hat others can read it.


<h1>Hello World Demo</h1>

The time
is now <%= new java.util.Date()

%>


c.

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


IV.

Creating

an Applet:
Hello World on a Web Page

a.

On munro in your public_html directory create

a file JDisplay.java containing the
following code based o
n 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.


b.

In the same directory c
reate 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>


c.

Change the permissions so that both are readable by others.
Use your browser to view
the web page:
http://munro.humber.ca/~
yourid
/
yourFile
.html



V.

NetBeans Version


In t
his 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


a.

Start NetBeans 7.0 from the Windows Start Menu. Select:
File | New Project | Java
Application | Next


b.

C
hange the
Project Location

field to point to the course directory you created earlier
on. Change the
Project Name

field to HelloYourName. Then click Finish.


c.

In the
code window

u
nderneath the comment
// TODO code application logic here

type in:


sout
<
tab key>


The line will automatically expand to:
System.out.println("");


Add “Hello World” inside the quotes. Add a 2
nd

line to print out the current date and
time as you did in the first example.


d.

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.


e.

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
j
ava
ar
chive.

i.

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.

ii.

From the menu select
Run | Cle
an and Build Main Project
. This will create a
new directory called
dist.

iii.

Open up a new cmd window from the Windows Start menu.

iv.

cd to the following directory:
Hello
YourName
/dist. List the contents of the
directory. You should see a file
hello
yourname
.jar.

To run the program:


java
-
jar

hello
yourname.jar


v.

Transfer the jar file to your munro account.
To run it enter the same command.



VI.

Advanced: Using NetBeans to Create an Applet

a.

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.


b.

In the init method add the same code used in Step IV.

c.

To run
, press
F6
. A small window
will show you the output of the applet.



VII.

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 b
e

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 so
urce 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% a
t 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 6
th

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,
n
ew 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.