Download Lab 1/Corrections added on Sept 7th

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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.
I.
Command Line Version
a. 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);
}
}
b. 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.
c. Compile the program by typing in the following command:
javac Hello.java
Record any error messages. There should be one error message.
than one error then check your code for any typos.
If you have more
d. 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.
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 following 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 that 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 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.
b. 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>
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 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
a. Start NetBeans 7.0 from the Windows Start Menu. Select: File | New Project | Java
Application | Next
b. 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.
c. In the code window underneath 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 2nd 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 java
archive.
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 | Clean 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: HelloYourName/dist. List the contents of the
directory. You should see a file helloyourname.jar. To run the program:
java -jar helloyourname.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 be to not show up for the lab or not to demonstrate each of
the exercises.
1.
2.
3.
4.
5.
6.
7.
8.
9.
Demonstrate the program Hello both on munro and at the command line in Windows. (25%)
Demonstrate the java server page: 10%
Demonstrate the applet running in a Web Page where the source is in Windows. (25%)
Demonstrate the applet running in a Web Page where source is in Unix: 10%
Demostrate running your project in NetBeans (both programs): 10%
Demonstrate running the jar file in Windows and in Unix: 10%
Completion of lab notes: 10%
10% bonus for completing and demonstrating in the first week’s lab.
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.