SlideShare a Scribd company logo
1 of 22
LAB 1
Instructor: Jolanta Soltis
1-1
• 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 Machine1 (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:ProgramFilesJavajdk1.6.0_03bin
• 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
Save your files on AFS server. Use SSH to connect
or H: drive in the lab.
• 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
1-6
1-6
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-7
1-7
Find the directory where it was
installed
• C:Program FilesJavajdk1.6.0_03bin
1-8
1-8
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-9
1-9
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-10
1-10
1-11
1-11
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-12
1-12
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.
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.

More Related Content

Similar to lab1.ppt

Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
Pratima Parida
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
Pratima Parida
 
Lecture 3 java basics
Lecture 3 java basicsLecture 3 java basics
Lecture 3 java basics
the_wumberlog
 

Similar to lab1.ppt (20)

java slides
java slidesjava slides
java slides
 
Introduction to java programming tutorial
Introduction to java programming   tutorialIntroduction to java programming   tutorial
Introduction to java programming tutorial
 
Java introduction
Java introductionJava introduction
Java introduction
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
 
Java Intro: Unit1. Hello World
Java Intro: Unit1. Hello WorldJava Intro: Unit1. Hello World
Java Intro: Unit1. Hello World
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
 
java:characteristics, classpath, compliation
java:characteristics, classpath, compliationjava:characteristics, classpath, compliation
java:characteristics, classpath, compliation
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
CORE JAVA
CORE JAVACORE JAVA
CORE JAVA
 
Programming
ProgrammingProgramming
Programming
 
JAVA CORE
JAVA COREJAVA CORE
JAVA CORE
 
Installing d space on windows
Installing d space on windowsInstalling d space on windows
Installing d space on windows
 
Lecture 3 java basics
Lecture 3 java basicsLecture 3 java basics
Lecture 3 java basics
 
JAVA First Day
JAVA First DayJAVA First Day
JAVA First Day
 
How java works
How java worksHow java works
How java works
 
How java works
How java worksHow java works
How java works
 
Java introduction
Java introductionJava introduction
Java introduction
 
Big Java Chapter 1
Big Java Chapter 1Big Java Chapter 1
Big Java Chapter 1
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

lab1.ppt

  • 2. • 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 Machine1 (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:ProgramFilesJavajdk1.6.0_03bin • 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 Save your files on AFS server. Use SSH to connect or H: drive in the lab.
  • 3. • 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
  • 4. • 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
  • 5. • 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
  • 6. 1-6 1-6 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)
  • 7. 1-7 1-7 Find the directory where it was installed • C:Program FilesJavajdk1.6.0_03bin
  • 8. 1-8 1-8 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
  • 9. 1-9 1-9 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.
  • 11. 1-11 1-11 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.
  • 12. 1-12 1-12 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.
  • 13. Programs to use to compile java applications: Eclipse • Link to download Eclipse http://www.eclipse.org/ • Tutorial: http://web.njit.edu/~soltis/cis114.htm
  • 14. Exercise 1 • What is the problem with the following code fragment: num = 50; while (num >= 0) { System.out.println (num); Num – num + 1; }
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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( ); }
  • 21. Answer: * * * * * © 2004 Pearson Addison-Wesley. All rights reserved 1-21
  • 22. • 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.