University of Petra Faculty of Information Technology: Programming - I Java
University of Petra Faculty of Information Technology: Programming - I Java
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
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
• 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)
OOP (Level 2)
Teaching Strategy
OOP (Level 2)
OOP (Advanced Level)
Networking, Multithreading,
…
B. Haddad 2015 3
Introduction to JAVA
• 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
// Lab instructor :
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?
B. Haddad 2015 7c
What is a Computer?
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)
B. Haddad 2015 9
Operating System
User
Application Programs
Operation System
Hardware
B. Haddad 2015 10
Programming Languages
1101101010011
§ Assembly languages( Low Level PL)
§ English-like abbreviations representing
(mnemonic ) elementary computer operations
(translated via assemblers)
§ Example:
area= 4 * 4* 3.1415
B. Haddad 2015 11
Programming Languages
High-Level PL
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
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
• A compiler translates the entire source code into a machine-code file, and
the machine-code file is then executed
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++
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
B. Haddad 2015 19
Creating, Compiling, and Executing a Java Program
Hallo, my Petra
Result
If runtime errors
B. Haddad 2015 20
Compiling Java Source Code
B. Haddad 2015 21
The Java Specification, API, JDK, IDE
B. Haddad 2015 22
The Java Specification, API, JDK, IDE
B. Haddad 2015 23
Getting Started with Java Programming
Example: 1.1
Problem: write a java Program display the message:
“Welcome to Java!”
B. Haddad 2015 24
Getting Started with Java Programming
B. Haddad 2015 25
Creating and Editing Using NotePad
B. Haddad 2015 26
Compiling and Running Java from the Command Window
B. Haddad 2015 27
Compiling and Running Java from TextPad
B. Haddad 2015 28