0% found this document useful (0 votes)
57 views

University of Petra Faculty of Information Technology: Programming - I Java

The document provides an introduction to Java programming, covering topics such as primitive data types, control flow structures, methods, arrays, strings, object-oriented programming concepts, and Java applets. It also discusses teaching strategies, homework format, and lab software for the course. The instructor is Bassam Haddad from the University of Petra's Faculty of Information Technology.

Uploaded by

CHANDAN JOSHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

University of Petra Faculty of Information Technology: Programming - I Java

The document provides an introduction to Java programming, covering topics such as primitive data types, control flow structures, methods, arrays, strings, object-oriented programming concepts, and Java applets. It also discusses teaching strategies, homework format, and lab software for the course. The instructor is Bassam Haddad from the University of Petra's Faculty of Information Technology.

Uploaded by

CHANDAN JOSHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

University of Petra

Faculty of Information Technology

Bassam Haddad
Associate Professor of Computer Science
Office: 7312 Ext. 340
[email protected]

Programming –I
Java

Part 1
Introduction

Based on:
1.  Introduction to Java Programming eight Edition, Y. Deniel Liang,
Pearson-Prentice Hall, 2011

7) Bassam Haddad AI-2006 Lecture Notes in:


Data Structures, Programming Skills-II,
(https://sites.google.com/site/bassamhaddadlecturenotes/home?pli=1)

B. Haddad 2015 1
Introduction to JAVA

Covering
• Java programming
•  Introduction to Programming Languages and CS
Java, the World Wide Web
The Java Language Specification, installation, JDK, and IDE
A Simple Java Program
Creating, Compiling, and Executing a Java Program
Anatomy of a Java Program

•  Primitive data types and Operations


Identifiers, Variables,Assignment Statements and Assignment Expressions
Constants, Numeric Data Types and Operations, Numeric Type Conversions
Character Data Type and Operations
The String Type, Getting Input from the Console
Getting Input from Input Dialogs
Programming Style and Documentation,Programming Errors,Debugging

•  Java control flow structure


•  Selection Statements
Boolean Data Type and Operations, Simple if Statements
if ... else Statements, Nested if Statements, switch Statements
Conditional Expressions, Formatting Console Output and Strings
Operator Precedence and Associativity, Operand Evaluation Order

•  Loops
The while Loop, The do-while Loop,The for Loop
Nested Loops,Minimizing Numerical Errors,Keywords break and continue

•  Methods
Creating a Method, Calling a Method,Passing Parameters by Values,Overloading Methods
The Scope of Variables,The Math Class
•  Arrays
Declaring Array Variables, Creating Arrays, Array Size and Default Values
Array Indexed Variables, Array Initializers
Processing Arrays
Loops, Copying Arrays,Passing Arrays to Methods
Returning an Array from a Method.Variable-Length Argument Lists
Searching Arrays.Sorting Arrays
The Arrays Class.Two-Dimensional Arrays, Multidimensional Arrays

•  String
The String Class, Constructing a String, Immutable Strings and Interned Strings
String Comparisons,String Length and Retrieving Individual Characters
String Concatenation, Obtaining Substrings
String Conversions,Finding a Character or a Substring in a String,
Conversion Between Strings and Arrays
Converting Characters and Numeric Values to Strings

•  Java Applets

•  Introduction OOP

B. Haddad 2015 2
Introduction to JAVA (Teaching Strategy)

JAVA Language (Advanced Level)

OOP (Level 2)

Fundamentals first (Level 1)

PDT Methods CS Arrays

Classes Objects Inheritance ...

Networking Exp. Handling Multithreading

Servlets DB programming ...

Teaching Strategy

Fundamentals first (Level 1)


In this course the focus will be on
•  PDT
•  Control Structures
•  Methods
•  Arrays

OOP (Level 2)
OOP (Advanced Level)
Networking, Multithreading,

B. Haddad 2015 3
Introduction to JAVA

LAB and Software

•  Notepad for editing your Java Programs


•  In a later stage, you can use
•  TextPad
•  Eclipse or NetBeans & Oracle/Sun JDK software to create,
edit, compile and run our JAVA programs

•  These programs are free and you can download and use them for
your home computer.
•  To download software for home use, follow information posted on
the course website.
•  All of these programs are free.

B. Haddad 2015 4
Introduction to JAVA

Homework's Format

// Assignment Nr #1

// Programming-I Java-I (Dr. Haddad)

// Lab instructor :

// Name: Your Name(s)


// ID:
// Date: Today’s Date
/** Problem description:
* WelcomeToJava is a Java program that simply
* writes a message to the console window
*/

public class WelcomeToJava


{
public static void main(String[] args)
{
System.out.println("Welcome to Java one!");
}
}

B. Haddad 2015 5
Basics of Computer Science Programming Languages

What is a Computer?

§  Computer
§  Device capable of performing computations
and making logical decisions
§  Computers process data under the control
of sets of instructions called computer
programs
§  Hardware
§  Various devices comprising a computer
§  Keyboard, screen, mouse, disks, memory,
CD-ROM, and processing units
§  Software
§  Programs that run on a computer

B. Haddad 2015 6
What is a Computer?

§  Logical units in every computer:


(Neumann Architecture)
§  Input unit
§  Obtains information from input devices
(keyboard, mouse)
§  Output unit
§  Outputs information (to screen, to printer,
to control other devices)
§  Memory unit
§  Rapid access, low capacity, stores input
information (RAM)
§  Arithmetic and logic unit (ALU)
§  Performs arithmetic calculations and logic
decisions
§  Central processing unit (CPU)
§  Supervises and coordinates the other
sections of the computer
§  Secondary storage unit
§  Cheap, long-term, high-capacity storage
§  Stores inactive programs (Hard disks)

B. Haddad 2015 7c
What is a Computer?

§ ALU: Addition, subtraction,


logical AND, OR, NOT
§ CU: executes machine language programs
§ Speed of CPU: determined by a clock speed( emitting
electronic pulse at a constant rate ( the faster speed
the clock speed: more instructions
are executed in a given time)
§ The clock speed is measured by megahertz(MHZ)
§ 1 MHz : 1000.000 pulse per second
§ 1 GHz: 1000 Mhz)
§ Input: E.g. Keyboard, Mouse
§ Output: Monitor, Printer
§ Main Memory: Ram
§ Sec. Memory: Hard drive, flash drive

B. Haddad 2015 8
Memory
§ Memory: storing data and program instruction s for the CPU to
execute
§ Memory Unit: Ordered in a sequence of Bytes
§ Byte: 8 Bits
§ Bit: Binary digits (zero and one)

Memory Address Main memory can be visualized as a column


or row of cells.

A section of memory is called a byte.

2000 A byte is made up of 8 bits.

2001 01100001 Encoding for Char a


2002 01001010 Encoding for Char j
2003 000000101 Encoding for Char 5

A section of two or four bytes is


often called a word.

B. Haddad 2015 9
Operating System

§ Operating System (OS): the most important program that


runs on a computer to manage and control its activities
§ Example: Windows 98, XP, Vista, 7, 9, 10
§ Application programs , such as Word anf Internet Explorer
cannot run without an OS
§ TASKS:
•  Controlling and Monitoring System activities
•  Allocating and Assigning System Recourses
•  Scheduling Operations

User

Application Programs

Operation System

Hardware

B. Haddad 2015 10
Programming Languages

§  Machine languages (native language)


§  Strings of numbers giving machine specific
instructions
§  Example:
Ading two numbers might have the following
form :

1101101010011
§  Assembly languages( Low Level PL)
§  English-like abbreviations representing
(mnemonic ) elementary computer operations
(translated via assemblers)
§  Example:

ADDF3 R1, R2, R3


§  High-level languages
§  Codes similar to everyday English
§  Use mathematical notations (translated via
compilers)
§  Example: area of a circle with radius 4

area= 4 * 4* 3.1415

Pi or π is approximately equal to 3.14159. It represents the ratio of any


circle's circumference to its diameter in Euclidean geometry, which is
the same as the ratio of a circle's area to the square of its radius.

B. Haddad 2015 11
Programming Languages

High-Level PL

Imperative PL OO PL Functional Logic Prog.


(procedural)
• FORTRAN • Java • Lisp • PROLOG
• COBOL • Smalltalk • Scheme • AFL
• C++ (hybrid) • Haskell • FRIL
• Pascal • APL • DATALOG
• Simula
• C • Python
• ML • λ PROLOG
• … • …
• … • VB.NET
• C#
• …

B. Haddad 2015 12
Introduction to Java

§ What Is Java?
§ Getting Started With Java Programming:
§ Creating,
§ Compile and Running a Java Application
What Is Java?
History

§  Developed by a team led by James Gosling at and Sun


Microsystems (now Oracle)
§  Originally called Oak ( in 1991) designed originally for
consumer applications)
§  1995, re-named to Java, and redesigned for developing
internet applications
§  Now: full-featured general purpose Programming
Language
§  Very Important: Open Source and open access
Programming Language
§  Java initially became attractive because Java programs
can be run from a Web browser
§  Applets: Java Programs that run from a Web browser
§  Applets: use GUI( Graphical User Interfaces) such as
User Interfaces with buttons, text fields, text areas,
Multimedia buttons, … to interact with users on the
Web and process their requests
JSP: Java server Pages Java can also be used to develop
applications on the the Sever side to generate dynamic web

B. Haddad 2015 13
Characteristics of Java

§ Java is simple
§  based on C++, eliminates Pointers and
multiple inheritance (using Interface),
Garbage collection
§ Java is object-oriented (pure)
§ Java is distributed(Networking)
§ Java is interpreted
§ Complier ( translation into machine code,
native machine language) and therefore
machine- dependent
§  Interpreter: Java Complier produce Byte
Codes(intermediate code instead of machine
code) which is machine- independent and able
to run on a JVM as an interpreter
§ JIT: Just in Time to increase performance
§ Java is robust (reliable)
§ Type Checking (nearly strong)
§ Pointer problems(Dangling pointers and
Object)
§ Run-time exception-handling feature
§ Java is secure (provided with security falsities)
§ Java is architecture-neutral (Platform-
Independent, JVM)
§ Java is portable (Artitecture-Neutral)

B. Haddad 2015 14
Characteristics of Java

§ Java’s performance
§ Interpreter Vs Compiler
§ JIT compilation better performance: Byte-code
into native machine code
§ Java is multithreaded
§ Capability to perform several tasks simultaneously
i.e. downloading a file while printing and paying a
video
§ Necessary for Multimedia and Networking
Programming
§ Java is dynamic
( adopting eve loving environment, new features can be
incorporated transparently)

B. Haddad 2015 15
Assembly, Interpreter, Complier

•  Assembly languages were developed to make programming easy. Since the


computer cannot understand assembly language, however, a program called
assembler is used to convert assembly language programs into machine

•  A compiler translates the entire source code into a machine-code file, and
the machine-code file is then executed

•  An interpreter reads one statement from the source code, translates it to


the machine code or virtual machine code, and then executes it right away

B. Haddad 2015 16
Execution Programs (again)

Source File

Compiler

Object File

Linker

Executable File

B. Haddad 2015 17
Compiling a C/C++ Program

#include
<iostream>

C/C++ program int main()


{
float x;
cout << “
...

C/C++
Compiler

00010100
Machine language 00110010
program(object “.obj” file) 10000100
01001001
01010101
01010010
00010100
00110010
10000100 C/C++
01001001 Linker
01010101
01010010
00010100
Precompiled Libraries 00110010
(e.g. iostream) 10000100
01001001
01010101
01010010
Machine language program(executable “.exe” file)

B. Haddad 2015 18
A Simple Application

Note: the produced programs are platform DEPENENT

B. Haddad 2015 19
Creating, Compiling, and Executing a Java Program

Create Source Code


Saved on the Disk
public class prog1 {
public static void main (String[ ] args)
{ System.out.println( “Hallo, my Petra”); Source Code
}
}

Compile Source Code


>> Javac prog1.java

… Stored on the Disk Compiler errors


Method prog1 ()
0 aloa_0
… Byte Code
Method void (java.lang, String[ ])
0 getstatic #2 …
3 ldc #3 String ““Hallo, Petra”
5 invokevirtual #4
8 Return Run Byte Code
>> java prog1

Hallo, my Petra
Result

If runtime errors

B. Haddad 2015 20
Compiling Java Source Code

•  You can port a source program to any machine with


appropriate compilers.
•  The source program must be recompiled, however, because
the object program can only run on a specific machine.
•  Nowadays computers are networked to work together.
•  Java was designed to run object programs on any platform.
•  With Java, you write the program once, and compile the
source program into a special type of object code, known as
bytecode.
•  The bytecode can then run on any computer with a Java
Virtual Machine, as shown below.
•  Java Virtual Machine is a software that interprets Java
bytecode.

B. Haddad 2015 21
The Java Specification, API, JDK, IDE

§ The Java specification and API (Application Program


Interface) define the JAVA Standard
(by Sun Microsystems/ now oracle)

§ JAVA specification: Syntax and semantic of JAVA


§ API: contains predefined Classes and Interface for developing
Java Programs.
§ The Java specification is stable but the API is still expanding (at
java.Sun.com/ Oracle ) you can download the latest version of
Java API.
§ Java introduced in 1995 but Java 2 platform announced in 1998
§ There are different edition of Java API:

J2SE: Java 2 Standard Edition


(standalone, Applets, Clint Side applications)
J2 EE: Java 2 Enterprise Edition
Server side (selvlets, JavaServer Pages)
J2ME: Java 2 Micro Edition
developing mobile application (mobile telefon)
§ There are many version of J2SE
such as J2SE 6.0 with JDK 6.0
§ JDK: Sun release each version of J2SE with a Java Development Kit (JDK)
E.g. J2SE 5.0 with JDK 5.0 and previously known as JDK 1.5

JDK 1.02 (1995)


JDK 1.1 (1996)
JDK 1.2 (1998)
JDK 1.3 (2000)
JDK 1.4 (2002)
JDK 1.5 (2004) known JDK 5 or Java 5
JDK 1.6 (2006) known JDK 6 or Java 6
JDK 1.7 (2011) known JDK 7 or Java 7
JDK 1.8 (2014) known JDK 8 or Java 8

B. Haddad 2015 22
The Java Specification, API, JDK, IDE

§ JDK: consists of a set of separate programs for developing


Java Programs

invoked from the command line


>> javac prog1.java
to compile a java source code

§ There are many Java development tools with


integrated Environment:

§ NetBeans Open Source (www.netbeans.org)


§ Sun One ( commercial version of Netbeans)
§ Eclipse Open Source)
§ Java Creator (Java Text editor)
§ TextPad (Text-Editor)

Note: Java source programs are case-sensitive

B. Haddad 2015 23
Getting Started with Java Programming

§ A Simple Java Application


§ Compiling Programs
§ Executing Applications
A Simple Application

Example: 1.1
Problem: write a java Program display the message:

“Welcome to Java!”

Java programs are written in a text editor and saved with a


“.java” filename extension such as Welcome1.java

//This application program prints welcome


//to Java!

public class Welcome1 {


public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

B. Haddad 2015 24
Getting Started with Java Programming

This is a Save this file as Welcome1.java


comment (names must match): the Class Name

//This application program prints


//welcome to Java!

public class Welcome1


{
Curly brackets public static void main(String[]args)
must match up {
System.out.println("Welcome to Java");
}
}

B. Haddad 2015 25
Creating and Editing Using NotePad

To use NotePad, type


notepad Welcome.java
from the DOS prompt.

B. Haddad 2015 26
Compiling and Running Java from the Command Window

•  Set path to JDK bin directory


–  set path=c:\Program Files\java\jdk1.8.0\bin
•  Set classpath to include the current directory
–  set classpath=.
•  Compile
–  javac Welcome.java
•  Run
–  java Welcome

B. Haddad 2015 27
Compiling and Running Java from TextPad

B. Haddad 2015 28

You might also like