Mittwoch, 10. September 2008

EE5805 Java Programming (1)

EE5805 Java Programming and Applications
http://www.ee.cityu.edu.hk/

The Tutorial notes of EE5805 or EE3206 is shown as follows:

EE3206 or EE5805 Tutorial 1 – Java Programming Environments

The programming exercises in this course require the following software to be installedon your system:

JDK 6 (or latest stable version), http://java.sun.com/javase/downloads/index.jsp

NetBeans 6.1 (or latest stable version), http://www.netbeans.org/


1. Read the online training below and follow the steps to create your first “Hello World”program.

NetBeans IDE Java Quick Start Tutorial
http://www.netbeans.org/kb/60/java/quickstart.html


2. To compile and run your program using the command-line tools of JDK:

1). Choose [Run…] from the Start Menu of your Windows. Type in “cmd” and
then click [OK] to open a DOS command prompt.
2). Enter “path” to display your current path setting.
3). Add the path, if necessary, of the JDK compiler (javac.exe) by typing “set
path=%path%;BIN_PATH” where BIN_PATH is the location of the
compiler (usually as C:\Program Files\Java\jdkx.y.z\bin).

4). Go to the source directory of your project folder by entering “cd SRC_PATH”
where SRC_PATH is the actual path in your system.
5). Type “javac HelloWorldApp.java” to compile your program source.
Type “dir” and see if the class file is correctly generated.
6). Run the class file by entering “java HelloWorldApp”.


3. Under the project folder, the IDE tool created a JAR (Java Archive) file in the
“dist” subdirectory. The JAR file is in a ZIP-compatible format.

To explore the content of a JAR, you may use any ZIP programs (e.g. WinZip) to extract it or use the command-line tool (jar.exe) with this command “jar xfv yourjarfile.jar”.

Write down what you find inside the JAR file.


4. For each of the following code fragments, decide whether it can be compiled. If yes,determine the value of x after the execution of the code. If no, explain why.

a) byte x = 0;
x++;
b) byte x = 127;
c) byte x = 128;

d) char x = '0';
x = x + 1;
e) char x = '0' + 1;
f) char y = '0';
char x = y + 1;

g) final char y = '0';
char x = y + 1;
h) int x = 3f * 4;
i) int x = (int) 3f * 4;
j) int x = (int) 3f * 4f;
k) int x = 'f' * 4;

l) float x = 1.0;
m) float x;
x = 1.0;
n) double x;
x = 1f;

5. For each of the following code fragments, decide whether it can be compiled.
If yes,determine the output display of the execution of the code. If no, explain why.

a) int x = 1, y = 2, z = 3;
if (x > y) if (z > y)
System.out.println(“y is the smallest”);
else
System.out.println(“y is not the smallest”);

b.) boolean t = true, f = false;
if (t && f)
System.out.println(“t && f”);
if (t && !f)
System.out.println(“t && !f”);
if (t || f)
System.out.println(“t || f”);
if (!!t & !f)
System.out.println(“!!t & !f”);


c.) int x = 0, y = 0, z = 0;
switch (x) {
case 0:
y = 1;
z = 2;
break;
case 1:
y = 2;
break;
z = 1;
default:
}
System.out.println(y);
System.out.println(z);


d.) for (int i=0; i<10; i++)
for (int j=0; j<10; j++)
if (j == 5)
break;
System.out.print(i * j + “ ”);

e.) for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
if (j == 5)
continue;
System.out.print(i * j + “ ”);
}
}

f.) for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
if (i == 5)
break;
System.out.print(i * j + “ ”);
}
}

g.) for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
if (i == 5)
continue;
System.out.print(i * j + ' ');
}
}


6. Create a project “JTutor01”. Write a class “PrintPyramid” that prompts the user toenter an integer ranging from 1 to 15 and displays a digit-pyramid.

For example, if the input integer is 12, the output is shown below.


- END -

Keine Kommentare: