Вы находитесь на странице: 1из 22

LAB 1

Instructor: Jolanta Soltis

1-1
Save your files on AFS server. Use SSH to connect or H:
drive in the lab.

• Before you can develop an application written in the Java programming


language, you will need the Java Platform Standard Edition (Java SE)
development kit. It has the necessary Java Virtual Machine 1 (JVM), core
Application Programming Interfaces (API)s, and the compiler you'll need for
most and perhaps all of your development.
• Note: Mac users should go to Apple's Mac OS X Java Runtime Environment.

• Download the JDK


– Download java JDK 6 Update 6: http://java.sun.com

• Check where java is installed: C:\ProgramFiles\Java\jdk1.6.0_03\bin

• J2SE 5.0 documentation: http://java.sun.com/j2se/1.5.0/download.jsp


• The Java Tutorial: http://java.sun.com/docs/books/tutorial

1-2
• Java SE: This kit is necessary for developing all applications, except
those designed for consumer devices. Java SE comes bundled with the
compiler, a runtime environment, and core API.
• Java Platform Enterprise Edition (Java EE): This packages includes an
application server, web server, J2EE APIs, support for Enterprise
JavaBeans, Java Servlets API, and JavaServer Pages (JSP) technology.
Use J2EE with the Java SE.
• Java Platform Micro Edition (Java ME): If you are interested in
developing programs for Palm Pilots, screen phones, and other
consumer devices, this kit provides tools for compiling, deployment
and device configuration, and APIs that are specialized for each type
of device.
• JavaFX Script Technology is a highly productive scripting language
that enables content developers to create rich media and content for
deployment on Java environments.

1-3
• The development kits include the APIs necessary to whatever type of
applications you develop in the Java programming language.
• Java APIs are libraries of compiled code let you add ready-made and
customizable functionality to your programs to save coding time.
• Java programs are executed within a program called the JVM.
• Rather than running directly on the native operating system, the
program is interpreted by the JVM for the native operating system.
• This is key to making your programs portable from one platform to
another. In other words, you can develop your programs on a Solaris,
Linux, Macintosh, or Windows, then run it on another server or
platform.

1-4
• Once you have the development kits you need, you
are ready to begin writing code in the Java
programming language.
• Programs are written in three basic flavors: applets,
applications, and servlets/JSP pages.
• Applets run in the JVM built into a web browser;
applications run in the JVM installed on a computer
system; and servlets/JSP run in the JVM installed
on a web server.

1-5
Each release of the Java 2 SDK,
Standard Edition contains:

• Java Compiler
• Java Virtual Machine*
• Java Class Libraries
• Java AppletViewer
• Java Debugger and other tools
• Documentation (in a separate download
bundle)
1-6
1-6
Find the directory where it was
installed

• C:\Program Files\Java\jdk1.6.0_03\bin

1-7
1-7
PATH

• Do you get the following error message?


Exception in thread "main" java.lang.NoSuchMethodError: main
• The Java runtime system needs to know where to find
programs that you want to run and libraries that are
needed.
• It knows where the predefined Java packages are, but if
you are using additional packages, you must tell specify
where they are located.
• PATH tells Java where to search for programs

1-8
1-8
Setting PATH on Windows XP

• The PATH variable can be set on Windows XP with the


following steps.
– Click the Start button in the lower left of the screen.
– Select Control Panel from the pop-up menu.
– Choose System from the submenu.
– Click the Advanced tab.
– Click the Environment Variables button near the bottom and you
will see two lists of variables.
– Look in the System variables list for a variable named PATH. If
you find it, click Edit. If you don't find it, click New and enter
PATH in the Variable name field.

1-9
1-9
1-10
1-10
Compiling a Java program in
DOS
• Create the source program with a text editor (eg, DOS, Eclipse, jEdit,
TextPad, ...).
• DOS:
– Open a DOS command window and cd to the directory containing the source file.
– Compile the source program (Greeting.java in this example) with the following
DOS command:
javac Greeting.java
– This produces one or more ".class" files, which are the object (Java byte code)
form of Java programs. You can produce a ".exe" file from this, but that isn't
normally done.
– Run it with:
java Greeting
– This loads the Greeting.class file and all necessary classes.

1-11
1-11
Programs to use to compile java
applications: TextPad
TextPad:
• Minimum Requirements:
– IBM Compatible PC
– Microsoft Windows 95 (SP#1), NT 4 (SP#6), or later versions of Windows.
– COMCTL32.DLL 4.70 or later (can be downloaded from
http://support.microsoft.com/default.aspx?scid=KB;en-us;q186176).
– 3.5MB free disk space

– 1. From the Configure menu, choose Preferences.


– 2. Select the Tools page on the Preferences dialog box.
– 3. Click Add.
– 4. Select "Java SDK Commands" from the drop down menu.
– 5. Click OK.
– The Java SDK commands are automatically added to the Tools menu, if the
Java SDK is installed before TextPad is first run on a PC.

1-12
1-12
Programs to use to compile java
applications: Eclipse

• Link to download Eclipse


http://www.eclipse.org/

• Tutorial:
http://web.njit.edu/~soltis/cis114.htm
Exercise 1

• What is the problem with the following code


fragment:

num = 50;
while (num >= 0)
{
System.out.println (num);
Num – num + 1;
}
Answer 1:

• This is an infinite loop. The loop will never


terminate because the condition will always
be true.

© 2004 Pearson Addison-Wesley. All rights reserved 1-15


Exercise 2:

• Using a for loop, write a program that


displays that squares of all integers greater
than 0 and less than or equal to a given
number n.

© 2004 Pearson Addison-Wesley. All rights reserved 1-16


Answer 2:

public class ex3


{
public static void main (String[] args)
{
int n = 5;
int square = 1;
for (int i = 1; i <=n; i++)
{
square = i * i;
System.out.println(square);
}
}
}

© 2004 Pearson Addison-Wesley. All rights reserved 1-17


Exercise 3:

• Which of the following is a legal Java


identifier?
– 1ForAll
– oneForAll
– one/4/all
– 1_4_all
– 1forall

© 2004 Pearson Addison-Wesley. All rights reserved 1-18


Answer 3:

• b.
Explanation: Java identifiers cannot start with
a number (so the answers in a, d and e are
illegal) and cannot include the “/” character,
so the answer in c is illegal.

© 2004 Pearson Addison-Wesley. All rights reserved 1-19


Exercise 4:

• Show the output that would occur from the following


code, including proper spacing.
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
if (j!=k) System.out.print(' ');
else System.out.print('*');
System.out.println( );
}
 
Answer:
*
*
*
*
*

© 2004 Pearson Addison-Wesley. All rights reserved 1-21


• Explanation: The outer loop iterates from 0 to 4,
and for each iteration, the inner loop also iterates
from 0 to 4. For iteration of the inner loop, if j
and k are not the same, a blank is output,
otherwise an *. So, if j = 0 and k = 2, then two
blanks are output before an *, followed by two
more blanks. At the end of each iteration of the
inner loop, the cursor is returned to start a new
line.

Вам также может понравиться