Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Workshop: Lab 1                                                                                                           Engineering Software 1 
Lab 1: 
Introduction to Java Programming Language and  
Java Development Kit (JDK) 
 
Learning Objectives 
1. To become familiar with Java Environment 
2. To become familiar with Java installation 
3. To be able to create, edit, compile and run Java programs 
4. To learn how to set the PATH environment variable to indicate where the JDK’s 
tools are installed. 
 
Lab Work 
General Note: Study every exercise and for each of them draw a structure diagram. Type 
in all these programs and run them inside your Java development kit.  
In your log-book take note of all the results from the execution of these programs and 
write down any problems that you had to solve. 
For each program of the exercises, explain clearly what each section of the program is 
doing. You may need to perform a walk-through, line by line, where this is appropriate. 
Record all your explanations in the log-book. 
Remember to put a date in your log-book and also to offer some discussions and a brief 
conclusion after every week’s work. 
Finally, complete the programming assignments at the end of the exercises, then enter 
comments in your logbook as you are doing this and save to your floppy disk/CD for 
submission with your logbook. 
 
Part A: Introduction to Java 
The Java programming language is a general-purpose object-oriented concurrent 
language. Its syntax is similar to C and C++, but it omits many of the features that make 
C and C++ complex, confusing, and unsafe. The Java platform was initially developed to 
address the problems of building software for networked consumer devices. It was 
designed to support multiple host architectures and to allow secure delivery of software 
components. To meet these requirements, compiled code had to survive transport across 
networks, operate on any client, and assure the client that it was safe to run.  
The popularization of the World Wide Web made these attributes much more interesting. 
The Internet demonstrated how media-rich content could be made accessible in simple 
ways. Web browsers such as Mosaic enabled millions of people to roam the Net and 
made Web surfing part of popular culture. At last there was a medium where what you 
saw and heard was essentially the same whether you were using a Mac, PC, or UNIX 
machine, and whether you were connected to a high-speed network or a slow modem.  
Web enthusiasts soon discovered that the content supported by the Web's HTML 
document format was too limited. HTML extensions, such as forms, only highlighted 
those limitations, while making it clear that no browser could include all the features 
users wanted. Extensibility was the answer.  
Stavros Dimitriou                                   ©London South Bank University 1
Workshop: Lab 1                                                                                                           Engineering Software 1 
Sun's HotJava browser showcases the interesting properties of the Java programming 
language and platform by making it possible to embed programs inside HTML pages. 
These programs are transparently downloaded into the HotJava browser along with the 
HTML pages in which they appear. Before being accepted by the browser, the programs 
are carefully checked to make sure they are safe. Like HTML pages, compiled programs 
are network - and host-independent. The programs behave the same way regardless of 
where they come from or what kind of machine they are being loaded into and run on.  
A Web browser incorporating the Java or Java 2 platform is no longer limited to a 
predetermined set of capabilities. Visitors to Web pages incorporating dynamic content 
can be assured that their machines cannot be damaged by that content. Programmers can 
write a program once, and it will run on any machine supplying a Java or Java 2 runtime 
environment. 
The Java virtual machine is the cornerstone of the Java and Java 2 platforms. It is the 
component of the technology responsible for its hardware- and operating system- 
independence, the small size of its compiled code, and its ability to protect users from 
malicious programs.  
The Java virtual machine is an abstract computing machine. Like a real computing 
machine, it has an instruction set and manipulates various memory areas at run time. It is 
reasonably common to implement a programming language using a virtual machine; the 
best-known virtual machine may be the P-Code machine of UCSD Pascal.  
The first prototype implementation of the Java virtual machine, done at Sun 
Microsystems, Inc., emulated the Java virtual machine instruction set in software hosted 
by a handheld device that resembled a contemporary Personal Digital Assistant (PDA). 
Sun's current Java virtual machine implementations, components of its JavaTM 2 SDK and 
JavaTM 2 Runtime Environment products, emulate the Java virtual machine on Win32 and 
Solaris hosts in much more sophisticated ways. However, the Java virtual machine does 
not assume any particular implementation technology, host hardware, or host operating 
system. It is not inherently interpreted, but can just as well be implemented by compiling 
its instruction set to that of a silicon CPU. It may also be implemented in microcode or 
directly in silicon.  
The Java virtual machine knows nothing of the Java programming language, only of a 
particular binary format, the class file format. A class file contains Java virtual 
machine instructions (or bytecodes) and a symbol table, as well as other ancillary 
information.  
For the sake of security, the Java virtual machine imposes strong format and structural 
constraints on the code in a class file. However, any language with functionality that can 
be expressed in terms of a valid class file can be hosted by the Java virtual machine. 
Attracted by a generally available, machine-independent platform, implementors of other 
languages are turning to the Java virtual machine as a delivery vehicle for their languages. 
 
Java Platform, Standard Edition (J2SE) provides a complete environment for applications 
development on desktops and servers and for deployment in embedded environments. It 
provides the basis for security, database connectivity and more. There are two principal 
products in the J2SE platform family: J2SE Runtime Environment (JRE) and J2SE 
Development Kit (JDK). The JRE provides the Java APIs, Java virtual machine, and 
other components necessary to run applets and applications written in the Java 
Stavros Dimitriou                                   ©London South Bank University 2
Workshop: Lab 1                                                                                                           Engineering Software 1 
programming language. It is also the foundation for the technologies in the Java 2 
Platform, Enterprise Edition (J2EE) for enterprise software development and deployment. 
The JRE does not contain tools and utilities such as compilers or debuggers for 
developing applets and applications.  
 
The JDK is a superset of the JRE, and contains everything that is in the JRE, plus tools 
such as the compilers and debuggers necessary for developing applets and applications. 
 
This conceptual diagram illustrates all the component technologies in J2SE platform and 
how they fit together. 
 
 
 
J2SE 5.0 Installation notes 
 
Installation notes for the J2SE Development Kit 5.0 (JDK), including system 
requirements and installation instructions, are available for the Microsoft Windows 
platform. 
 
J2SE Development Kit 5.0 (JDK 5.0) is supported on Microsoft Windows running Intel 
IA32, 100% compatible processors, and AMD Opteron 32. The minimum physical RAM 
is required to run graphically based applications. More RAM is recommended for applets 
running within a browser using the Java Plug-in. Running with less memory may cause 
disk swapping which has a severe effect on performance. Very large programs may 
require more RAM for adequate performance. 
 
Note - The JDK has two version numbers -- external version number 5.0 and internal version 
number 1.5.0. 
Note - Trying to install the JDK on a non-supported version of Microsoft Windows or on a 
machine that doesn't have a sufficiently up-to-date Service Pack will cause the installer to 
generate this warning: "We recommend that you do not install this Java platform for the following 
reasons: This Java platform does not support the operating system or operating-system service 
pack on this machine." 
Stavros Dimitriou                                   ©London South Bank University 3
Workshop: Lab 1                                                                                                           Engineering Software 1 
 
The Java 2 Platform Standard Edition 5.0 - J2SE Development Kit (JDK) - can be 
downloaded from the http://java.sun.com web site. 
 
J2SE 5.0 Installation Instructions 
System Requirements 
J2SE Development Kit 5.0 (JDK 5.0) is supported on Microsoft Windows running Intel 
IA32, 100% compatible processors, and AMD Opteron 32. 
The minimum physical RAM is required to run graphically based applications. More 
RAM is recommended for applets running within a browser using the Java Plug-in. 
Running with less memory may cause disk swapping, which has a severe effect on 
performance. Very large programs may require more RAM for adequate performance.  
The Java 2 SDK can be installed on the following versions of Windows: 
ƒ Windows XP (with Service Pack 2)  
ƒ Windows XP Home  
ƒ Windows 2000 Professional (with Service Pack 4)  
ƒ Windows 98 (1st and 2nd Editions)  
ƒ Windows ME  
ƒ Windows Server 2003, Web Edition  
ƒ Windows Server 2003, Standard Edition  
ƒ Windows Server 2003, Enterprise Edition  
ƒ Windows Server 2003, DataCenter Edition  
 
Note: Trying to install the JDK on a non-supported version of Microsoft Windows or on a 
machine that doesn't have a sufficiently up-to-date Service Pack will cause the installer to 
generate this warning:  
"We recommend that you do not install this Java Platform for the following reasons: This 
Java Platform does not support the operating system or operating-system service pack on 
this machine." 
 
In this procedure, you will run the self-installing executable to unpack and install the JDK 
software bundle. As part of the JDK, this installation includes the Java Plug-in and Java 
Web Start, as well as an option to include the public Java 2 Runtime Environment. 
(The JDK also contains a private JRE for use only by its tools.). For example, if you are 
downloading the installer for update 1.5.0_05, the following file name: jdk-
1_5_0_-windows-i586.exe would become: jdk-1_5_0_05-windows-
i586.exe 
 
If you save the self-installing executable to disk without running it from the download 
page at the web site, notice that its byte size is provided on the download page. Once the 
download has completed, check that you have downloaded the full, uncorrupted software 
file. 
 
Note - (1) You must have administrative permissions in order to install the JDK on Microsoft 
Windows 2000 and XP. (2) For users doing this installation on non-Western 32-bit machines, 
select "Custom" for Setup Type. Then, in Custom Setup under feature 2 ("Support for Additional 
Languages"), select "This feature will be installed on local hard drive." 
Stavros Dimitriou                                   ©London South Bank University 4
Workshop: Lab 1                                                                                                           Engineering Software 1 
 
The file jdk-1_5_0_-windows-i586-i.exe is the JDK installer. If you 
downloaded it instead of running it directly from the web site, double-click on the 
installer's icon. Then follow the instructions the installer provides. The installer may ask 
you to reboot your computer. When done with the installation, you can delete the 
download file to recover disk space. 
 
Installed Directory Tree 
The JDK has the directory structure shown below.  
 
                  jdk1.5.0_ 
  ____________________|___________________ 
 |    |    |    |    |   |     |      |   | 
 |    |    |    |   bin lib    |    demo  | 
 |    |    | LICENSE |   |     |         jre 
 |    | COPYRIGHT              |        __|__ 
 |  README.txt              include    |     | 
readme.html                           bin   lib 
 
 
In addition, the Java Plug-in and Java Web Start will automatically be installed. Look for 
a Java Web Start icon on your desktop. There will also be an entry for Java Web Start in 
the Start -> Programs menu.  
Java Web Start --  
 Compatibility: The release of Java Web Start that comes with this JDK/JRE can be 
run on JDK/JRE 1.2.2 or later. It will not work with JDK/JRE 1.1.x or earlier.  
 Upgrading from Previous Versions: If you have a previous release of Java Web 
Start, do not uninstall it. Uninstalling it will cause the download cache to be cleared, 
and all previously installed Java Web Start application data will have to be 
downloaded again. This new release will overwrite previous installations and 
automatically update browsers to use this new release. The configuration files and 
program files folder used by Java Web Start have changed, but all your settings will 
remain intact after the upgrade, since Java Web Start will translate your settings to the 
new form.  
 Uninstalling JDK/JRE 5.0: (1) The only way to uninstall Java Web Start 1.5.0 is to 
uninstall JDK/JRE 5.0. But note that doing so will remove the 1.5.0 Java Web Start 
cache, which stores the Java Web Start application data. Uninstalling the JDK/JRE 
will not, however, remove the cache for previous releases of Java Web Start (1.0.1 and 
1.2). Previous releases have separate uninstallers for Java Web Start. (2) You may see 
a misleading message if you do the following: first download and cache a Java Web 
Start (JNLP) application with JDK/JRE 5.x; next remove JDK/JRE 5.x using "Add or 
Remove Programs" from the Windows Control Panel; then try to remove the Java 
Web Start application using "Add or Remove Programs." This misleading message 
can occur as follows: When you select to remove the application, you will see an 
"Uninstaller Error" dialog saying "An error occurred while trying to remove Java-
Application:  App. It may have already been uninstalled. Would you like to 
remove Java-Application:  App from the Add or Remove program list?" If 
you say yes to this, then you will see another "Uninstaller Error" dialog saying "You 
do not have sufficient access to remove Java-Application:  App from the Add 
or Remove Program list. Please contact your system administrator." This is the 
misleading message. It implies that the problem is due to privileges. It is not. The 
problem is that you have already removed the Java Web Start application when you 
removed JDK/JRE 5.x, but this is not reflected in the "Add or Remove Programs" 
dialog until it is refreshed by pressing F5 or it is closed and reopened. To avoid seeing 
this misleading message, either press F5 or close and reopen the dialog. Any Java 
Web Start application that was downloaded and cached with JDK/JRE 5.x will no 
longer appear in the list of currently installed programs.  
 Using Java Web Start with Netscape 6.x/7.x: For Netscape 6.x/7.x users, setup the 
Java Web Start MIME type (JNLP) in the Edit->Preferences->Navigator->Helper 
Stavros Dimitriou                                   ©London South Bank University 5
Workshop: Lab 1                                                                                                           Engineering Software 1 
Applications section. The file extension is jnlp; MIME Type is application/x-java-
jnlp-file. It should be handled by the javaws executable file in your Java Web Start 
directory. Also note that, due to a problem with the JavaScript in Netscape 6.x/7.x, 
you must use the non-JavaScript version of the demos page . 
 
Update the PATH variable 
You can run the JDK without setting the PATH variable, or you can optionally set it as a 
convenience.  
Should I set the PATH variable? 
Set the PATH variable if you want to be able to conveniently run the JDK executables (javac.exe, 
java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the 
command. If you don't set the PATH variable, you need to specify the full path to the executable 
every time you run it, such as:  
    C:> "\Program Files\Java\jdk1.5.0_\bin\javac" MyClass.java 
It's useful to set the PATH permanently so it will persist after rebooting.  
How do I set the PATH permanently?  
To set the PATH permanently, add the full path of the jdk1.5.0_\bin directory to the 
PATH variable. Typically this full path looks something like C:\Program 
Files\Java\jdk1.5.0_\bin. Set the PATH as follows, according to whether you are on 
Microsoft Windows NT or 98/2000/ME.  
Microsoft Windows NT, 2000, and XP - To set the PATH permanently:  
a. Choose Start, Settings, Control Panel, and double-click System. On Microsoft 
Windows NT, select the Environment tab; on Microsoft Windows 2000 select the 
Advanced tab and then Environment Variables. Look for "Path" in the User Variables 
and System Variables. If you're not sure where to add the path, add it to the right end 
of the "Path" in the User Variables. A typical value for PATH is:  
b.      C:\Program Files\Java\jdk1.5.0_\bin  
Capitalization doesn't matter. Click "Set", "OK" or "Apply".  
The PATH can be a series of directories separated by semi-colons (;). Microsoft 
Windows looks for programs in the PATH directories in order, from left to right. You 
should only have one bin directory for a JDK in the path at a time (those following the 
first are ignored), so if one is already present, you can update it to 
jdk1.5.0_\bin.  
c. The new path takes effect in each new Command Prompt window you open after 
setting the PATH variable.  
 
Uninstalling the JDK  
If you should ever want to uninstall the JDK, use the "Add/Remove Programs" utility in 
the Microsoft Windows Control Panel. As an alternative method, if you still have the 
original installation program that you used to install the JDK, you can double click on it 
to launch an uninstall program.  
 
 
Stavros Dimitriou                                   ©London South Bank University 6
Workshop: Lab 1                                                                                                           Engineering Software 1 
Part B: Installing the latest version of the J2SE Development Kit (JDK) 
(This is an exercise to help you on how to install JDK at your own PC) 
1. Follow the procedure above. Download from the http://java.sun.com web site the latest 
version of java - J2SE(TM) Development Kit 5.0 Update 6. 
 
2. You will download this file: jdk-1_5_0_06-windows-i586-p.exe (size: 59.86 MB) 
 
3. At the time of publication of this course, the jdk-1_5_0_06 is the latest version. 
However, any time you can download the most recent version of the installer from Sun’s 
Web site. Go to http://java.sun.com/j2se/1.5.0/download.jsp and then click the 
following link Download JDK 5.0 Update 6. Then click the DOWNLOAD link under 
the “Windows Platform - J2SE(TM) Development Kit 5.0 Update 6”. You must 
accept the license agreement before downloading. Once you accept the license agreement, 
you are directed to a download page, where you can select and download the installer for 
your computer platform. Save the installer on your hard disk and keep track of where you 
save it. The following steps show the installation procedure on the Windows platform. 
 
4. Starting installation of the JDK. After downloading the JDK installer double click the 
installer program to begin installing the JDK. The InstallShield Wizard window (Fig. 1) 
is displayed. Wait for the wizard to finish the configuration. 
 
 
 
Fig. 1: InstallShield Wizard for the JDK 
Stavros Dimitriou                                   ©London South Bank University 7
Workshop: Lab 1                                                                                                           Engineering Software 1 
 
 
 
 
Fig. 2: JDK license agreement 
 
5. Accepting the license agreement. Carefully read the license agreement. Select Yes to 
agree to the terms (Fig. 2). [Note: If you choose not to accept the license agreement, the 
software will not install and you will not be able to execute or create Java applications.] 
 
6. Choosing the installation directory for the JDK. Select the directory in which you 
want the JDK to be installed (Fig. 3). If you change the default installation directory, be 
sure to write down the exact name and location of the directory you choose, as you will 
need this information later in the installation process. After selecting a directory, click the 
Next> button to install the development tool. 
Stavros Dimitriou                                   ©London South Bank University 8
Workshop: Lab 1                                                                                                           Engineering Software 1 
 
 
Fig. 3: Choosing the installation directory for the JDK 
 
7. Choosing the installation directory for the Java Runtime Environment (JRE). Select 
the directory in which you want the JRE to be installed (Fig. 4). We recommend that you 
choose the default installation directory. After you have selected a directory, click the 
Next> button to install the JRE. 
 
8. Selecting browsers. As part of the installation, you have the option of making the Java 
plug-in the default Java runtime environment for Java programs called applets that run in 
Web browsers (Fig. 5). After you select the desired browsers click the Next> button.  
 
 
Stavros Dimitriou                                   ©London South Bank University 9
Workshop: Lab 1                                                                                                           Engineering Software 1 
Wait while the Install Wizard installs J2SE development Kit 5.0 Update 6. 
 
Fig. 4: Selecting the components of the JDK to install 
 
 
 
Fig. 5: Selecting Browser(s) 
 
9. Finishing the installation. The program will now install the JRE. Click the Finish 
button to complete the installation process (Fig. 6). 
 
 
Stavros Dimitriou                                   ©London South Bank University 10
Workshop: Lab 1                                                                                                           Engineering Software 1 
 
 
 
 
Fig. 6: Completing the installation 
 
The PATH environment variable on your computer designates which directories the 
computer searches when looking for applications, such as the applications that enable you 
to compile and run your Java applications (called javac.exe and java.exe, respectively). 
 
You will now practically learn how to set the PATH environment variable on your 
computer. 
 
 
Stavros Dimitriou                                   ©London South Bank University 11
Workshop: Lab 1                                                                                                           Engineering Software 1 
Part C: Installing Setting the PATH Variable 
(This is an exercise to help you on how to set the PATH variable at your own PC) 
 
Workshop 
The last step before you can use the JDK is to set the PATH environment variable to in-
dicate where the JDK’s tools are installed. 
 
1. Opening the System Properties dialog. Right click on the My Computer icon on 
your desktop and select Properties from the menu. The System Properties dialog (Fig. 
7) appears. [Note: Your System Properties dialog may appear different than the one 
shown in Fig. 7, depending on your version of Microsoft Windows. This particular dialog 
is from a computer running Microsoft Windows XP Service pack 2. Your dialog might 
include different information.] 
 
 
 
Fig. 7: System Properties dialog 
 
 
2. Opening the Environment Variables dialog. Select the Advanced tab at the top of the 
System Properties dialog (Fig. 8). Click the Environment Variables button to display 
the Environment Variables dialog (Fig. 9). 
 
Stavros Dimitriou                                   ©London South Bank University 12
Workshop: Lab 1                                                                                                           Engineering Software 1 
 
Select the 
Advanced tab 
Click the 
Environment 
Variables button 
 
Fig. 8: Advanced tab System Properties dialog 
 
 
 
Fig. 9: Environment Variables dialog 
 
3. Editing the PATH variable. Scroll down inside the System variables box to select the 
PATH variable. Click the Edit button. This will cause the Edit System Variable dialog 
to appear (Fig. 10). 
 
Stavros Dimitriou                                   ©London South Bank University 13
Workshop: Lab 1                                                                                                           Engineering Software 1 
 
 
Fig. 10: Edit System Variable dialog 
 
4. Changing the contents of the PATH variable. Place the cursor inside the Variable 
Value field. Use the left-arrow key to move the cursor to the beginning of the list. At the 
beginning of the list, type the name of the directory in which you placed the JDK 
followed by \bin; (Fig. 11). If you chose the default installation directory in Step 6 of the 
previous section (see Introduction: Installing the latest version of the J2SE 
Development Kit (JDK)), you will add C:\Program Files\Java\jdk1.5.0_06\bin; to the 
PATH variable here. 
Your PATH variable is: C:\Program Files\Java\jdk1.5.0_06\bin 
 
Fig. 11: Editing the PATH variable 
 
Click the OK button to complete the modification of the PATH variable. [Note: the 
default installation directory is named jdk1.5.0_06, even though the JDK is now called 
version 5.0.] 
You are now ready to begin with your Java programs! 
 
 
 
 
 
Stavros Dimitriou                                   ©London South Bank University 14
Workshop: Lab 1                                                                                                           Engineering Software 1 
Part D: Compiling and Executing Your First Java Application 
 
You are now ready to compile and execute our program. For this purpose, you will use 
the Sun Microsystems’ J2SE Development Kit. 
To prepare to compile the program, open a command window and change to the directory 
where the program is stored. You learned in part C how to set the PATH variable. 
 
Assume that the name of the class is Welcome. 
 
To compile a program called Welcome.java, type 
 
javac Welcome.java
 
in the command window of your system. 
 
If the program contains no syntax errors, the preceding command creates a new file called 
Welcome.class (known as the class file for Welcome) containing the Java 
bytecodes that represent our application. When we use the java command to execute the 
application, these bytecodes will be executed by the JVM. 
 
 
Note:  When attempting to compile a program, if you receive a message such as “bad command or 
filename,” “javac: command not found” or “'javac' is not recognized as an 
internal or external command, operable program or batch file,” then your Java 
software installation was not completed properly. If you are using the J2SE Development Kit, this indicates 
that the system’s PATH environment variable was not set properly.
 
 
To execute a Java application called Welcome, type 
 
java Welcome 
 
in the command window of your system. 
 
This launches the JVM, which loads the “.class” file for class Welcome. Note that the 
“.class” file-name extension is omitted from the preceding command; otherwise, the 
JVM will not execute the program. The JVM calls method main. 
 
 
 
 
 
 
Stavros Dimitriou                                   ©London South Bank University 15
Workshop: Lab 1                                                                                                           Engineering Software 1 
Exercise 1: A simple Java Program 
 
A Java program can be written in many ways. Let us begin with a simple Java program 
that displays the message “Welcome to Java!” 
 
Problem  
Write a program that displays the message “Welcome to Java!” on the console. 
 
Solution 
The following listing gives the solution to the problem. 
 
___________________________________________________________ 
// Welcome.java 
// Text-printing program. 
 
public class Welcome   
{ 
   // main method begins execution of Java application 
   public static void main( String args[] ) 
   { 
      System.out.println("Welcome to Java!"); 
 
   } // end method main 
 
} // end class Welcome 
__________________________________________________________ 
 
1. Create, Compile and Run the program.  
2. Explain how you did it, demonstrate the output you got and put all this 
information in your logbook. 
 
 
 
Stavros Dimitriou                                   ©London South Bank University 16