SlideShare a Scribd company logo
1 of 12
Download to read offline
Faculty of Engineering & Information Technology
Software Engineering Department
Computer Science [2]
Instructor: Eng.Omar Al-Nahal
Copyright March , 2010
WWW.PALINFONET.COM
Lab 1:
Introduction ; First Java programs
Software Lab
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
Lab 1
Introduction; First Java programs
Software Lab
1. Lab Objectives
In this Lab you will learn:
1. Learn how to install the Java SDK and JCreator IDE
2. Get introductory training on the JCreator IDE
3. Learn how to create Java program with JCreator & BlueJ
4. Compile and Run a Java Program
2. Prior to the Laboratory
Before you come to the lab, you should read about simple java programs instructions in the course and solve
the homework below. You should also read this laboratory exercise in detail. Note that you must solve the
homework.
• Read in book , Chapters 1 & 2 , Sections 1.13, 2.1,2.2,2.3,2.4
• Review the Theoretical Lesson.
3. Learn Java Programming by Examples
The Compilation Process
Figure: From Source Code to Running Program
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
2
2
Using the JCreator Integrated Development
What is an IDE?
An IDE (Integrated Development Environment) is an application used to aid a programmer in developing
computer applications or programs.
JCreator uses the JAVAC compiler developed by Sun Microsystems TM to compile the Java source code into
Java Byte code. It is just an editor that provides an easy to use editor and debugger for creating Java source
code. In order to create the JAVA byte code you still need to use the Java Compiler developed by Sun
Microsystems TM.
Using JCreator
To use JCreator the program must first be installed on the computer correctly.
Click on the JCreator icon on the desk top ( IF Dialog boxes appear click next and then ok until the
IDE environment window appears). You should then have a window that looks like
Click on File then New then File (You will have a window that looks like Error! Reference source not
found.)
Figure 2
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
3
3
For Every File in this class you must (These steps are modified each time you create a new File with names and
folders that pertain to each File).
Select an Empty File and Click Next
Figure 3
Provide a Location, Source Path, and Output Path. This should be a folder in your E: drive. You can change
the path for each text box by clicking on the button next to each text box illustrated above.
Give the File a Name1 (Here we are using HelloWorld as the File Name) Be sure that Location, Source and
Output Path have the same folder as the File Name2.
Click Next then Next then Finish
In order to continue you must complete steps 1 through 3 of the Software Development Method found in your
text booki.
Create the first file for your Java program.
Click File then New then File (You should have the window shown in Error! Reference source not found.)
Fill in the name of the file in the Name and Click Finish
Compile the File -> Click Build and then Compile File
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
4
4
Run File -> Click Build and then Execute File When you execute the program you will have a window that
looks like that in Figure 1
Figure 1
Output Pane with a typical Error
Output Pane Without an Error
File View Pane Package View Pane Output Pane Edit Window
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
5
5
A Word about the Java Platform
The Java platform consists of the Java application programming interfaces (APIs) and the Java1 virtual
machine (JVM).
Java APIs are libraries of compiled code that you can use in your programs. They let you add ready-made and
customizable functionality to save you programming time.
The simple program in this lesson uses a Java API to print a line of text to the console. The console printing
capability is provided in the API ready for you to use; you supply the text to be printed.
Java programs are run (or interpreted) by another program called the Java VM. If you are familiar with
Visual Basic or another interpreted language, this concept is probably familiar to you. Rather than running
directly on the native operating system, the program is interpreted by the Java VM for the native operating
system. This means that any computer system with the Java VM installed can run Java programs regardless of
the computer system on which the applications were originally developed.
For example, a Java program developed on a Personal Computer (PC) with the Windows NT operating system
should run equally well without modification on a Sun Ultra workstation with the Solaris operating system, and
vice versa.
Setting Up Your Computer
Before you can write and run the simple Java program in this lesson, you need to install the Java platform on
your computer system.
The Java platform is available free of charge from the java.sun.com web site. You can choose between the
Java® 2 Platform software for Windows 98/2000/XP and 7 or for Solaris. The download page contains the
information you need to install and configure the Java platform for writing and running Java programs.
Note: Make sure you have the Java platform installed and configured for your system before you try to write
and run the simple program presented next.
Writing a Program
The easiest way to write a simple program is with a text editor. So, using the text editor of your choice, create a
text file with the following text, and be sure to name the text file ExampleProgram.java. Java programs are case
sensitive, so if you type the code in yourself, pay particular attention to the capitalization.
//A Very Simple Example
class ExampleProgram {
public static void main(String args [ ]){
System.out.println("I'm a Simple Program");
}
}
Here is the ExampleProgram.java source code file if you do not want to type the program text in yourself.
Compiling the Program
A program has to be converted to a form the Java VM can understand so any computer with a Java VM can
interpret and run the program. Compiling a Java program means taking the programmer-readable text in your
program file (also called source code) and converting it to bytecodes, which are platform-independent
instructions for the Java VM.
The Java compiler is invoked at the command line on Unix and DOS shell operating systems as follows:
javac ExampleProgram.java
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
6
6
Note: Part of the configuration process for setting up the Java platform is setting the class path. The class path
can be set using either the -classpath option with the javac compiler command and java interpreter command,
or by setting the CLASSPATH environment variable. You need to set the class path to point to the directory
where the ExampleProgram class is so the compiler and interpreter commands can find it. See Java 2 SDK
Tools for more information.
Interpreting and Running the Program
Once your program successfully compiles into Java bytecodes, you can interpret and run applications on any
Java VM, or interpret and run applets in any Web browser with a Java VM built in such as Netscape or Internet
Explorer. Interpreting and running a Java program means invoking the Java VM byte code interpreter, which
converts the Java byte codes to platform-dependent machine codes so your computer can understand and run
the program.
The Java interpreter is invoked at the command line on Unix and DOS shell operating systems as follows:
java ExampleProgram
Common Compiler and Interpreter Problems
If you have trouble compiling or running the simple example in this lesson, refer to the Common Compiler and
Interpreter Problems lesson in The Java Tutorial for troubleshooting help.
Code Comments
Code comments are placed in source files to describe what is happening in the code to someone who might be
reading the file, to comment-out lines of code to isolate the source of a problem for debugging purposes, or to
generate API documentation. To these ends, the Java language supports three kinds of comments: double
slashes, C-style, and doc comments.
Double Slashes
Double slashes (//) are used in the C++ programming language, and tell the compiler to treat everything from
the slashes to the end of the line as text.
//A Very Simple Example
class ExampleProgram {
public static void main(String[] args){
System.out.println("I'm a Simple Program");
} }
C-Style Comments
Instead of double slashes, you can use C-style comments (/* */) to enclose one or more lines of code to be
treated as text.
/* These are
C-style comments
*/
class ExampleProgram {
public static void main(String args []){
System.out.println("I'm a Simple Program"); } }
At the command line, you should see:
I'm a Simple Program
Here is how the entire sequence looks in a terminal window:
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
7
7
Doc Comments
To generate documentation for your program, use the doc comments (/** */) to enclose lines of text for the
javadoc tool to find. The javadoc tool locates the doc comments embedded in source files and uses those
comments to generate API documentation.
/** This class displays a text string at
* the console.
*/
class ExampleProgram {
public static void main(String args []){
System.out.println("I'm a Simple Program");
} }
Class
• Every java program includes at least one class definition. The class is the fundamental component of all
Java programs.
• A class definition contains all the variables and methods that make the program work. This is contained in
the class body indicated by the opening and closing braces.
Braces
• The left brace ( { ) indicates the beginning of a class body, which contains any variables and methods the
class needs.
• The left brace also indicates the beginning of a method body.
• For every left brace that opens a class or method you need a corresponding right brace ( } ) to close the
class or method.
• A right brace always closes its nearest left brace.
main() method
• This line begins the main() method. This is the line at which the program will begin executing.
String args[]
• Declares a parameter named args, which is an array of String. It represents command-line arguments.
System.out.println
• This line outputs the string “I'm a Simple Program!” followed by a new line on the screen.
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
8
8
Some common escape sequences.
Escape
sequence Description
n Newline. Position the screen cursor at the beginning of the next line.
t Horizontal tab. Move the screen cursor to the next tab stop.
r Carriage return. Position the screen cursor at the beginning of the current linedo not
advance to the next line. Any characters output after the carriage return overwrite
the characters previously output on that line.
 Backslash. Used to print a backslash character.
" Double quote. Used to print a double-quote character. For example,
System.out.println( ""in quotes"" );
displays
"in quotes"
Displaying multiple lines with method System.out.printf.
1 // Fig. 2.6: Welcome4.java
2 // Printing multiple lines in a dialog box.
3
4 public class Welcome4
5 {
6 // main method begins execution of Java application
7 public static void main( String args[] )
8 {
9 System.out.printf( "%sn%sn",
10 "Welcome to", "Java Programming!" );
11
12 } // end method main
13
14 } // end class Welcome4
Welcome to
Java Programming!
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
9
9
Displaying Text in a Dialog Box by using JOptionPane
Although the programs presented in this book thus far display output in the command window, many Java
applications use windows or dialog boxes (also called dialogs) to display output.
For example, World Wide Web browsers such as Netscape or Microsoft Internet Explorer display Web pages
in their own windows. E-mail programs allow you to type and read messages in a window. Typically, dialog
boxes are windows in which programs display important messages to the user of the program.
Class JOptionPane provides prepackaged dialog boxes that enable programs to display windows containing
messages to users—such windows are called message dialogs.
Figure 2.10 displays the string "WelcomentonJava" in a message dialog.
Line 3 indicates that our program uses class JOptionPane from package javax.swing.
This package contains many classes that help Java programmers create graphical user interfaces (GUIs) for
applications. GUI components facilitate data entry by a program's user, and formatting or presenting data
outputs to the user. In method main, line 10 calls method showMessageDialog of class JOptionPane to display
a dialog box containing a message. The method requires two arguments.
The first argument helps the Java application determine where to position the dialog box. When the first
argument is null, the dialog box appears in the center of the computer screen. The second argument is the
String to display in the dialog box.
Method showMessageDialog is a special method of class JOptionPane called a static method. Such methods
often define frequently used tasks that do not explicitly require creating an object. For example, many
programs display messages to users in dialog boxes. Rather than require programmers to create code that
performs this task, the designers of Java's JOptionPane class declared a static method for this purpose. Now,
with a simple method call, all programmers can make a program display a dialog box containing a message. A
static method typically is called by using its class name followed by a dot (.)
Figure 2.10. Using JOptionPane to display multiple lines in a dialog box.
1 // Fig. 3.17: Dialog1.java
2 // Printing multiple lines in dialog box.
3 import javax.swing.JOptionPane; // import class JOptionPane
4
5 public class Dialog1
6 {
7 public static void main( String args[] )
8 {
9 // display a dialog with the message
10 JOptionPane.showMessageDialog( null, "WelcomentonJava" );
11 } // end main
12 } // end class Dialog1
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
10
10
4. Activities/Exercises
• Write a program TenHelloWorlds.java that prints "Hello, World" ten times.
• Write an application that displays a checkerboard pattern, as follows:
5. Activities/Homework
• Write an application that displays a box, an oval, an arrow and a diamond using asterisks (*), as follows:
• I typed in the following program. It compiles fine, but when I execute it, I get the error
java.lang.NoSuchMethodError: main. What am I doing wrong?
.
** *** ********** ** * **
** *** ** ** ** *** **
** *** ** ** ** ** ** **
** *** ** ** ** ** ** **
***** ** ** ** ** ** **
** *** ** ** ** ** ** **
** *** ** ** ** ** ** **
** *** ** ** *** ***
** *** ********** * *
public class Hello {
public static void main() {
System.out.println("Doesn't execute");
}
}
Al-Azhar University – Gaza Faculty of Engineering & IT
Java Lab Manual Eng.Omar H. Al-Nahal
11
11
6. Web Resources
# The Deitel & Associates home page for Java How to Program, Sixth Edition.
www.deitel.com/books/jHTP6/index.html
# Sun's home page for Java technology. Here you will find downloads, reference guides for developers,
community forums, online tutorials and many other valuable Java resources.
www.java.sun.com/j2se The home page for the Java 2 Platform, Standard Edition.
# The home page for JCreatora popular Java IDE. JCreator Lite Edition is available as a free
download. A 30-day trial version of JCreator Pro Edition is also available.
www.jcreator.com
# The home page for the BlueJ environmenta tool designed to help teach object-oriented Java to new
programmers. BlueJ is available as a free download. www.blueJ.org
# Free Software Online:-
- JDK-V5 Java SE Development Kit
www.palinfonet.com/pin/java/jdkv5.exe
- JCreator – V4 JCreator is a powerful Free IDE for Java
www.palinfonet.com/pin/java/jcpro400.exe
- Java JCreator: Register with the following
Name: mazuki
Serial: MPEXF3-CE2759-WZ9P34-5YFAZW-MQYK1C-89B44V-G30B5E-P8YNJP
# Free Book - Java How to Program, Sixth Edition
www.palinfonet.com/pin/cs3.htm
# Arab Academy for Information Technology
‫اﻟﻤﻌﻠﻮﻣﺎت‬ ‫ﻟﺘﻜﻨﻮﻟﻮﺟﻴﺎ‬ ‫اﻷآﺎدﻳﻤﻴﺔ‬‫اﻟﻌﺮﺑﻴﺔ‬
www.palinfonet.com

More Related Content

What's hot

Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsMahika Tutorials
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7Deniz Oguz
 
Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz SAurabh PRajapati
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaSanjeev Tripathi
 
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...Make Mannan
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New FeaturesNaveen Hegde
 
Introduction to java Programming
Introduction to java ProgrammingIntroduction to java Programming
Introduction to java ProgrammingAhmed Ayman
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Kernel Training
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My HeartBui Kiet
 

What's hot (20)

02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
 
Java programming-examples
Java programming-examplesJava programming-examples
Java programming-examples
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Core java
Core java Core java
Core java
 
Presentation to java
Presentation  to  javaPresentation  to  java
Presentation to java
 
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New Features
 
Introduction to java Programming
Introduction to java ProgrammingIntroduction to java Programming
Introduction to java Programming
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 

Viewers also liked

Viewers also liked (7)

Lvp jcreator
Lvp jcreatorLvp jcreator
Lvp jcreator
 
Interfaz+grafica+de+usuario
Interfaz+grafica+de+usuarioInterfaz+grafica+de+usuario
Interfaz+grafica+de+usuario
 
Pengenalan Jcreator
Pengenalan JcreatorPengenalan Jcreator
Pengenalan Jcreator
 
Inicio jcreator practica1
Inicio jcreator practica1Inicio jcreator practica1
Inicio jcreator practica1
 
Tutorial jcreator claudia laguna coronel
Tutorial jcreator  claudia laguna coronelTutorial jcreator  claudia laguna coronel
Tutorial jcreator claudia laguna coronel
 
Instalación JDK & Jcreator
Instalación JDK & JcreatorInstalación JDK & Jcreator
Instalación JDK & Jcreator
 
Ejercicios resueltos de java
Ejercicios resueltos de javaEjercicios resueltos de java
Ejercicios resueltos de java
 

Similar to Java lab1 manual

(Ebook pdf) java programming language basics
(Ebook pdf)   java programming language basics(Ebook pdf)   java programming language basics
(Ebook pdf) java programming language basicsRaffaella D'angelo
 
Java interview question
Java interview questionJava interview question
Java interview questionsimplidigital
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1sotlsoc
 
java 1 new.pdf
java 1 new.pdfjava 1 new.pdf
java 1 new.pdfSulSya
 
Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Mohamed Essam
 
Unit of competency
Unit of competencyUnit of competency
Unit of competencyloidasacueza
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javaAli Baba
 
Java for Mainframers
Java for MainframersJava for Mainframers
Java for MainframersRich Helton
 
Introduction
IntroductionIntroduction
Introductionrichsoden
 
INTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATIONINTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATIONAjit Yadav
 
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 basicsthe_wumberlog
 

Similar to Java lab1 manual (20)

Java programming language basics
Java programming language basicsJava programming language basics
Java programming language basics
 
(Ebook pdf) java programming language basics
(Ebook pdf)   java programming language basics(Ebook pdf)   java programming language basics
(Ebook pdf) java programming language basics
 
Java interview question
Java interview questionJava interview question
Java interview question
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
 
java 1 new.pdf
java 1 new.pdfjava 1 new.pdf
java 1 new.pdf
 
JAVA Program Examples
JAVA Program ExamplesJAVA Program Examples
JAVA Program Examples
 
Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1
 
Unit of competency
Unit of competencyUnit of competency
Unit of competency
 
J introtojava1-pdf
J introtojava1-pdfJ introtojava1-pdf
J introtojava1-pdf
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Java for Mainframers
Java for MainframersJava for Mainframers
Java for Mainframers
 
Introduction
IntroductionIntroduction
Introduction
 
INTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATIONINTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATION
 
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)
 
Lecture 3 java basics
Lecture 3 java basicsLecture 3 java basics
Lecture 3 java basics
 
Basics of java 1
Basics of java 1Basics of java 1
Basics of java 1
 
Core java introduction
Core java introduction Core java introduction
Core java introduction
 
01slide
01slide01slide
01slide
 

Recently uploaded

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 

Recently uploaded (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 

Java lab1 manual

  • 1. Faculty of Engineering & Information Technology Software Engineering Department Computer Science [2] Instructor: Eng.Omar Al-Nahal Copyright March , 2010 WWW.PALINFONET.COM Lab 1: Introduction ; First Java programs Software Lab
  • 2. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal Lab 1 Introduction; First Java programs Software Lab 1. Lab Objectives In this Lab you will learn: 1. Learn how to install the Java SDK and JCreator IDE 2. Get introductory training on the JCreator IDE 3. Learn how to create Java program with JCreator & BlueJ 4. Compile and Run a Java Program 2. Prior to the Laboratory Before you come to the lab, you should read about simple java programs instructions in the course and solve the homework below. You should also read this laboratory exercise in detail. Note that you must solve the homework. • Read in book , Chapters 1 & 2 , Sections 1.13, 2.1,2.2,2.3,2.4 • Review the Theoretical Lesson. 3. Learn Java Programming by Examples The Compilation Process Figure: From Source Code to Running Program
  • 3. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal 2 2 Using the JCreator Integrated Development What is an IDE? An IDE (Integrated Development Environment) is an application used to aid a programmer in developing computer applications or programs. JCreator uses the JAVAC compiler developed by Sun Microsystems TM to compile the Java source code into Java Byte code. It is just an editor that provides an easy to use editor and debugger for creating Java source code. In order to create the JAVA byte code you still need to use the Java Compiler developed by Sun Microsystems TM. Using JCreator To use JCreator the program must first be installed on the computer correctly. Click on the JCreator icon on the desk top ( IF Dialog boxes appear click next and then ok until the IDE environment window appears). You should then have a window that looks like Click on File then New then File (You will have a window that looks like Error! Reference source not found.) Figure 2
  • 4. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal 3 3 For Every File in this class you must (These steps are modified each time you create a new File with names and folders that pertain to each File). Select an Empty File and Click Next Figure 3 Provide a Location, Source Path, and Output Path. This should be a folder in your E: drive. You can change the path for each text box by clicking on the button next to each text box illustrated above. Give the File a Name1 (Here we are using HelloWorld as the File Name) Be sure that Location, Source and Output Path have the same folder as the File Name2. Click Next then Next then Finish In order to continue you must complete steps 1 through 3 of the Software Development Method found in your text booki. Create the first file for your Java program. Click File then New then File (You should have the window shown in Error! Reference source not found.) Fill in the name of the file in the Name and Click Finish Compile the File -> Click Build and then Compile File
  • 5. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal 4 4 Run File -> Click Build and then Execute File When you execute the program you will have a window that looks like that in Figure 1 Figure 1 Output Pane with a typical Error Output Pane Without an Error File View Pane Package View Pane Output Pane Edit Window
  • 6. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal 5 5 A Word about the Java Platform The Java platform consists of the Java application programming interfaces (APIs) and the Java1 virtual machine (JVM). Java APIs are libraries of compiled code that you can use in your programs. They let you add ready-made and customizable functionality to save you programming time. The simple program in this lesson uses a Java API to print a line of text to the console. The console printing capability is provided in the API ready for you to use; you supply the text to be printed. Java programs are run (or interpreted) by another program called the Java VM. If you are familiar with Visual Basic or another interpreted language, this concept is probably familiar to you. Rather than running directly on the native operating system, the program is interpreted by the Java VM for the native operating system. This means that any computer system with the Java VM installed can run Java programs regardless of the computer system on which the applications were originally developed. For example, a Java program developed on a Personal Computer (PC) with the Windows NT operating system should run equally well without modification on a Sun Ultra workstation with the Solaris operating system, and vice versa. Setting Up Your Computer Before you can write and run the simple Java program in this lesson, you need to install the Java platform on your computer system. The Java platform is available free of charge from the java.sun.com web site. You can choose between the Java® 2 Platform software for Windows 98/2000/XP and 7 or for Solaris. The download page contains the information you need to install and configure the Java platform for writing and running Java programs. Note: Make sure you have the Java platform installed and configured for your system before you try to write and run the simple program presented next. Writing a Program The easiest way to write a simple program is with a text editor. So, using the text editor of your choice, create a text file with the following text, and be sure to name the text file ExampleProgram.java. Java programs are case sensitive, so if you type the code in yourself, pay particular attention to the capitalization. //A Very Simple Example class ExampleProgram { public static void main(String args [ ]){ System.out.println("I'm a Simple Program"); } } Here is the ExampleProgram.java source code file if you do not want to type the program text in yourself. Compiling the Program A program has to be converted to a form the Java VM can understand so any computer with a Java VM can interpret and run the program. Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the Java VM. The Java compiler is invoked at the command line on Unix and DOS shell operating systems as follows: javac ExampleProgram.java
  • 7. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal 6 6 Note: Part of the configuration process for setting up the Java platform is setting the class path. The class path can be set using either the -classpath option with the javac compiler command and java interpreter command, or by setting the CLASSPATH environment variable. You need to set the class path to point to the directory where the ExampleProgram class is so the compiler and interpreter commands can find it. See Java 2 SDK Tools for more information. Interpreting and Running the Program Once your program successfully compiles into Java bytecodes, you can interpret and run applications on any Java VM, or interpret and run applets in any Web browser with a Java VM built in such as Netscape or Internet Explorer. Interpreting and running a Java program means invoking the Java VM byte code interpreter, which converts the Java byte codes to platform-dependent machine codes so your computer can understand and run the program. The Java interpreter is invoked at the command line on Unix and DOS shell operating systems as follows: java ExampleProgram Common Compiler and Interpreter Problems If you have trouble compiling or running the simple example in this lesson, refer to the Common Compiler and Interpreter Problems lesson in The Java Tutorial for troubleshooting help. Code Comments Code comments are placed in source files to describe what is happening in the code to someone who might be reading the file, to comment-out lines of code to isolate the source of a problem for debugging purposes, or to generate API documentation. To these ends, the Java language supports three kinds of comments: double slashes, C-style, and doc comments. Double Slashes Double slashes (//) are used in the C++ programming language, and tell the compiler to treat everything from the slashes to the end of the line as text. //A Very Simple Example class ExampleProgram { public static void main(String[] args){ System.out.println("I'm a Simple Program"); } } C-Style Comments Instead of double slashes, you can use C-style comments (/* */) to enclose one or more lines of code to be treated as text. /* These are C-style comments */ class ExampleProgram { public static void main(String args []){ System.out.println("I'm a Simple Program"); } } At the command line, you should see: I'm a Simple Program Here is how the entire sequence looks in a terminal window:
  • 8. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal 7 7 Doc Comments To generate documentation for your program, use the doc comments (/** */) to enclose lines of text for the javadoc tool to find. The javadoc tool locates the doc comments embedded in source files and uses those comments to generate API documentation. /** This class displays a text string at * the console. */ class ExampleProgram { public static void main(String args []){ System.out.println("I'm a Simple Program"); } } Class • Every java program includes at least one class definition. The class is the fundamental component of all Java programs. • A class definition contains all the variables and methods that make the program work. This is contained in the class body indicated by the opening and closing braces. Braces • The left brace ( { ) indicates the beginning of a class body, which contains any variables and methods the class needs. • The left brace also indicates the beginning of a method body. • For every left brace that opens a class or method you need a corresponding right brace ( } ) to close the class or method. • A right brace always closes its nearest left brace. main() method • This line begins the main() method. This is the line at which the program will begin executing. String args[] • Declares a parameter named args, which is an array of String. It represents command-line arguments. System.out.println • This line outputs the string “I'm a Simple Program!” followed by a new line on the screen.
  • 9. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal 8 8 Some common escape sequences. Escape sequence Description n Newline. Position the screen cursor at the beginning of the next line. t Horizontal tab. Move the screen cursor to the next tab stop. r Carriage return. Position the screen cursor at the beginning of the current linedo not advance to the next line. Any characters output after the carriage return overwrite the characters previously output on that line. Backslash. Used to print a backslash character. " Double quote. Used to print a double-quote character. For example, System.out.println( ""in quotes"" ); displays "in quotes" Displaying multiple lines with method System.out.printf. 1 // Fig. 2.6: Welcome4.java 2 // Printing multiple lines in a dialog box. 3 4 public class Welcome4 5 { 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.printf( "%sn%sn", 10 "Welcome to", "Java Programming!" ); 11 12 } // end method main 13 14 } // end class Welcome4 Welcome to Java Programming!
  • 10. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal 9 9 Displaying Text in a Dialog Box by using JOptionPane Although the programs presented in this book thus far display output in the command window, many Java applications use windows or dialog boxes (also called dialogs) to display output. For example, World Wide Web browsers such as Netscape or Microsoft Internet Explorer display Web pages in their own windows. E-mail programs allow you to type and read messages in a window. Typically, dialog boxes are windows in which programs display important messages to the user of the program. Class JOptionPane provides prepackaged dialog boxes that enable programs to display windows containing messages to users—such windows are called message dialogs. Figure 2.10 displays the string "WelcomentonJava" in a message dialog. Line 3 indicates that our program uses class JOptionPane from package javax.swing. This package contains many classes that help Java programmers create graphical user interfaces (GUIs) for applications. GUI components facilitate data entry by a program's user, and formatting or presenting data outputs to the user. In method main, line 10 calls method showMessageDialog of class JOptionPane to display a dialog box containing a message. The method requires two arguments. The first argument helps the Java application determine where to position the dialog box. When the first argument is null, the dialog box appears in the center of the computer screen. The second argument is the String to display in the dialog box. Method showMessageDialog is a special method of class JOptionPane called a static method. Such methods often define frequently used tasks that do not explicitly require creating an object. For example, many programs display messages to users in dialog boxes. Rather than require programmers to create code that performs this task, the designers of Java's JOptionPane class declared a static method for this purpose. Now, with a simple method call, all programmers can make a program display a dialog box containing a message. A static method typically is called by using its class name followed by a dot (.) Figure 2.10. Using JOptionPane to display multiple lines in a dialog box. 1 // Fig. 3.17: Dialog1.java 2 // Printing multiple lines in dialog box. 3 import javax.swing.JOptionPane; // import class JOptionPane 4 5 public class Dialog1 6 { 7 public static void main( String args[] ) 8 { 9 // display a dialog with the message 10 JOptionPane.showMessageDialog( null, "WelcomentonJava" ); 11 } // end main 12 } // end class Dialog1
  • 11. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal 10 10 4. Activities/Exercises • Write a program TenHelloWorlds.java that prints "Hello, World" ten times. • Write an application that displays a checkerboard pattern, as follows: 5. Activities/Homework • Write an application that displays a box, an oval, an arrow and a diamond using asterisks (*), as follows: • I typed in the following program. It compiles fine, but when I execute it, I get the error java.lang.NoSuchMethodError: main. What am I doing wrong? . ** *** ********** ** * ** ** *** ** ** ** *** ** ** *** ** ** ** ** ** ** ** *** ** ** ** ** ** ** ***** ** ** ** ** ** ** ** *** ** ** ** ** ** ** ** *** ** ** ** ** ** ** ** *** ** ** *** *** ** *** ********** * * public class Hello { public static void main() { System.out.println("Doesn't execute"); } }
  • 12. Al-Azhar University – Gaza Faculty of Engineering & IT Java Lab Manual Eng.Omar H. Al-Nahal 11 11 6. Web Resources # The Deitel & Associates home page for Java How to Program, Sixth Edition. www.deitel.com/books/jHTP6/index.html # Sun's home page for Java technology. Here you will find downloads, reference guides for developers, community forums, online tutorials and many other valuable Java resources. www.java.sun.com/j2se The home page for the Java 2 Platform, Standard Edition. # The home page for JCreatora popular Java IDE. JCreator Lite Edition is available as a free download. A 30-day trial version of JCreator Pro Edition is also available. www.jcreator.com # The home page for the BlueJ environmenta tool designed to help teach object-oriented Java to new programmers. BlueJ is available as a free download. www.blueJ.org # Free Software Online:- - JDK-V5 Java SE Development Kit www.palinfonet.com/pin/java/jdkv5.exe - JCreator – V4 JCreator is a powerful Free IDE for Java www.palinfonet.com/pin/java/jcpro400.exe - Java JCreator: Register with the following Name: mazuki Serial: MPEXF3-CE2759-WZ9P34-5YFAZW-MQYK1C-89B44V-G30B5E-P8YNJP # Free Book - Java How to Program, Sixth Edition www.palinfonet.com/pin/cs3.htm # Arab Academy for Information Technology ‫اﻟﻤﻌﻠﻮﻣﺎت‬ ‫ﻟﺘﻜﻨﻮﻟﻮﺟﻴﺎ‬ ‫اﻷآﺎدﻳﻤﻴﺔ‬‫اﻟﻌﺮﺑﻴﺔ‬ www.palinfonet.com