introduction to programming - rana atef tarabishi€¦ · 03/06/1438 1 introduction to programming...

20
03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 2 Programs Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs. Programs are written using programming languages. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them.

Upload: others

Post on 12-Jul-2020

31 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

1

Introduction to

Programming

Java Language

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 2

Programs

Computer programs, known as software, are instructions to

the computer.

You tell a computer what to do through programs.

Programs are written using programming languages.

Without programs, a computer is an empty machine.

Computers do not understand human languages, so you

need to use computer languages to communicate with them.

Page 2: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

2

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 3

Programming Languages

Machine Language Assembly Language High-Level Language

Machine language is a set of primitive instructions, in the

form of binary code.

Program with native machine language is a tedious process

and it is highly difficult to read and modify.

Example, to add two numbers, you might write an instruction

in binary like this:

1101101010011010

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 4

Programming Languages

Machine Language Assembly Language High-Level Language

Assembly languages were developed to make programming easy.

Since the computer cannot understand assembly language, a program called

assembler is used to convert assembly language programs into machine code.

Example, to add two numbers, you might write an instruction in assembly code

like this:

ADDF3 R1, R2, R3

… ADDF3 R1, R2, R3

Assembly Source File

Assembler

… 1101101010011010

Machine Code File

Page 3: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

3

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 5

Programming Languages

Machine Language Assembly Language High-Level Language

The high-level languages are English-like and easy to

learn and program.

Example, the following is a high-level language statement

that computes the area of a circle with radius 5:

area = 5 * 5 * 3.1415;

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 6

Interpreting/Compiling Source Code

A program written in a high-level language is called a source

program or source code.

Because a computer cannot understand a source program, a source

program must be translated into machine code for execution.

The translation can be done using another programming tool called

an interpreter or a compiler.

Page 4: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

4

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 7

Interpreting Source Code

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.

Note that a statement from the source code may be translated into

several machine instructions.

area = 5 * 5 * 3.1415;

...

High-level Source File

Interpreter Output

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 8

Compiling Source Code

A compiler translates the entire source code into a machine-code file,

and the machine-code file is then executed.

area = 5 * 5 * 3.1415;

...

High-level Source File

Compiler Executor Output …

0101100011011100

1111100011000100

...

Machine-code File

Page 5: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

5

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 9

Popular High-Level Languages

Language Description

Ada

BASIC

C

C++

C#

COBOL

FORTRAN

Java

Pascal

Python

Visual

Basic

Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada

language was developed for the Department of Defense and is used mainly in defense projects.

Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily

by beginners.

Developed at Bell Laboratories. C combines the power of an assembly language with the ease of

use and portability of a high-level language.

C++ is an object-oriented language, based on C.

Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft.

COmmon Business Oriented Language. Used for business applications.

FORmula TRANslation. Popular for scientific and mathematical applications.

Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform-

independent Internet applications.

Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a

simple, structured, general-purpose language primarily for teaching programming.

A simple general-purpose scripting language good for writing short programs.

Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop

graphical user interfaces.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 10

Why Java?

One of the most popular programming languages in use.

Java enables users to develop and deploy applications on the

Internet for servers, desktop computers, and small hand-held devices.

"write once, run anywhere": code that runs on one platform does

not need to be recompiled to run on another.

Java applications are typically compiled to bytecode (class file) that

can run on any Java Virtual Machine (JVM).

Page 6: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

6

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 11

Characteristics of Java

Java is concurrent, class-based, object-oriented, and specifically

designed to have as few implementation dependencies as possible.

was originally developed by James Gosling at Sun Microsystems

(which has since merged into Oracle Corporation).

derives much of its syntax from C and C++.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 12

JDK Versions

JDK 1.02 (1995)

JDK 1.1 (1996)

JDK 1.2 (1998)

JDK 1.3 (2000)

JDK 1.4 (2002)

JDK 1.5 (2004) a. k. a. JDK 5 or Java 5

JDK 1.6 (2006) a. k. a. JDK 6 or Java 6

JDK 1.7 (2011) a. k. a. JDK 7 or Java 7

Page 7: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

7

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 13

JDK Editions

Java Standard Edition (J2SE)

– J2SE can be used to develop client-side standalone

applications or applets.

Java Enterprise Edition (J2EE)

– J2EE can be used to develop server-side applications

such as Java servlets, Java ServerPages, and Java

ServerFaces.

Java Micro Edition (J2ME).

– J2ME can be used to develop applications for mobile

devices such as cell phones.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 14

Popular Java IDEs

NetBeans

– https://netbeans.org/

Eclipse

– http://www.eclipse.org/

Page 8: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

8

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 15

A Simple Java Program

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Run

Welcome

Listing 1.1

IMPORTANT NOTE: (1) To enable the buttons, you must download the entire slide file slide.zip and unzip the files into a directory (e.g., c:\slide) . (2) You must have installed JDK and set JDK’s bin directory in your environment path (e.g., c:\Program Files\java\jdk1.7.0\bin in your environment path. (3) If you are using Office 2010, check PowerPoint2010.doc located in the same folder with this ppt file.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 16

Creating and Editing Using NotePad

To use NotePad, type

notepad Welcome.java

from the DOS prompt.

Page 9: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

9

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 17

Creating and Editing Using WordPad

To use WordPad, type

write Welcome.java

from the DOS prompt.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 18

Creating, Compiling, and

Running Programs

Source Code

Create/Modify Source Code

Compile Source Code

i.e., javac Welcome.java

Bytecode

Run Byteode

i.e., java Welcome

Result

If compilation errors

If runtime errors or incorrect result

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!"); }

}

Method Welcome()

0 aload_0

Method void main(java.lang.String[])

0 getstatic #2 …

3 ldc #3 <String "Welcome to

Java!">

5 invokevirtual #4 …

8 return

Saved on the disk

stored on the disk

Source code (developed by the programmer)

Byte code (generated by the compiler for JVM

to read and interpret, not for you to understand)

Page 10: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

10

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 19

Compiling Java Source Code

When using another programming language, you can port a source program to any

machine with appropriate compilers but the source program must be recompiled,

because the object program can only run on a specific machine.

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 JVM.

JVM is a software that interprets Java bytecode.

Java Bytecode

Java Virtual

Machine

Any

Computer

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 20

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Trace a Program ExecutionEnter main method

Page 11: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

11

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 21

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Trace a Program Execution

Execute statement

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 22

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Trace a Program Execution

print a message to the

console

Page 12: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

12

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 23

Anatomy of a Java Program

Class name

Main method

Statements

Statement terminator

Reserved words

Comments

Blocks

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 24

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Class Name

Every Java program must have at least one class.

Each class has a name.

By convention, class names start with an uppercase letter.

In the following example, the class name is Welcome:

Page 13: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

13

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 25

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Main Method

Line 2 defines the main method.

In order to run a class, the class must contain a method named

main.

The program is executed from the main method.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 26

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Statement

A statement represents an action or a sequence of

actions.

The statement System.out.println("Welcome to Java!")

in the program is a statement to display the greeting

"Welcome to Java! ".

Page 14: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

14

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 27

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Statement Terminator

Every statement in Java ends with a semicolon (;).

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 28

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Reserved words

Reserved words or keywords are words that have a specific

meaning to the compiler and cannot be used for other purposes

in the program.

Example, when the compiler sees the word class, it

understands that the word after class is the name for the class.

Page 15: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

15

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 29

Blocks

A pair of braces in a program forms a block that groups components of a program.

public class Test {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Class block

Method block

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 30

Special Symbols

Character Name Description

{}

()

[]

//

" "

;

Opening and closing

braces

Opening and closing

parentheses

Opening and closing

brackets

Double slashes

Opening and closing

quotation marks

Semicolon

Denotes a block to enclose statements.

Used with methods.

Denotes an array.

Precedes a comment line.

Enclosing a string (i.e., sequence of characters).

Marks the end of a statement.

Page 16: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

16

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 31

Programming Errors

Syntax Errors

– Detected by the compiler

Runtime Errors

– Causes the program to abort

Logic Errors

– Produces incorrect result

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 32

Syntax Errors

public class ShowSyntaxErrors {

public static main(String[] args) {

System.out.println("Welcome to Java);

}

}

Page 17: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

17

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 33

Runtime Errors

public class ShowRuntimeErrors {

public static void main(String[] args) {

System.out.println(1 / 0);

}

}

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 34

Logic Errors

public class ShowLogicErrors {

public static void main(String[] args) {

for (i=1; i<=10; i++) ; {

System.out.println("Number is " + i);

}

}

}

Page 18: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

18

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 35

Lab

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 36

Compiling and Running Java from

the Command Window

Set path to JDK bin directory

– set path=c:\Program Files\java\jdk1.6.0\bin

Set classpath to include the current directory

– set classpath=.

Compile

– javac Welcome.java

Run

– java Welcome

Page 19: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

19

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 37

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Exercise 1

Learn the NetBeans Environment.

Create a project and write the following program.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 38

public class MyClass {

static int i;

public static void main(String args[]){

System.out.println(i);

}

}

Exercise 2

Test the following program:

Page 20: Introduction to Programming - Rana Atef Tarabishi€¦ · 03/06/1438 1 Introduction to Programming Java Language Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson

03/06/1438

20

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 39

public class MultiplicationTable

{

public static void main(String[] args)

{

System.out.println("Multiplication Table for the number 2 ");

for(int i=0; i<=2; i++)

System.out.println("2 * "+i+" = "+ 2*i);

}

}

Exercise 3

Test the following program:

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

rights reserved. 40

Two more simple exercises

RunWelcomeWithThreeMessages

RunComputeExpression