computer programming lecture#2 أ. إلهام باسندوه 1

25
Computer programming Lecture#2 . دوه ن س ا هام ب ل إ إ1

Upload: virgil-gibson

Post on 04-Jan-2016

232 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Computer programming Lecture#2 أ. إلهام باسندوه 1

1

Computer programming

Lecture#2

باسندوه. إلهام أ

Page 2: Computer programming Lecture#2 أ. إلهام باسندوه 1

2

•Primitive Data Types •Reserved Words

• Escape Sequences •Variable Declaration

•Initializing Variable•Constants

•Getting Input

We will learn: We learn last week:

•Print a text• Println

Page 3: Computer programming Lecture#2 أ. إلهام باسندوه 1

3

Primitive Data Types

Primitive data types are predefined types of data, which aresupported by the programming language.

Page 4: Computer programming Lecture#2 أ. إلهام باسندوه 1

4Reserved Words

Page 5: Computer programming Lecture#2 أ. إلهام باسندوه 1

5 Some Escape Sequences For

FormattingEscape sequence Description

\t Horizontal tab

\r Carriage return

\n New line

\” Double quote

\\ Backslash

Page 6: Computer programming Lecture#2 أ. إلهام باسندوه 1

6Escape Sequences Example

Page 7: Computer programming Lecture#2 أ. إلهام باسندوه 1

7General Format: As in C++

DataType var;DataType var1,var2, varn;DataType var=value;DataType var1=value1, var2=value2,….varn;

Examples:int x;int price= 100, w;long u,a;long Population= 243253451L,k, g;

Variable Declaration

Page 8: Computer programming Lecture#2 أ. إلهام باسندوه 1

8

Examples:float avg, t,y;float gpa = 3.5f;double e,n,m;

double v=2.5d;

char g,h;

char gradeLetter = ‘D;’

boolean flag;

boolean b= true, c=false;

Variable Declaration

Page 9: Computer programming Lecture#2 أ. إلهام باسندوه 1

9 Initializing Variables

Always initialize your variables prior to using them!

public class OutputExample1{

public static void main (String [] args){

int num; System.out.print(num) ;

} }

OutputExample1.java:7: error: variable num might not have

been initialized System.out.print(num);

^

Page 10: Computer programming Lecture#2 أ. إلهام باسندوه 1

10Example for Printing variable

Page 11: Computer programming Lecture#2 أ. إلهام باسندوه 1

11 public class declarationEx

{ public static void main ( String[] args )

{ int grade;

grade = 99 ; char gradeLetter='A;'

boolean d=true ;// You cannot write d=1; as in C++ it will be considered as a syntax

ERROR

System.out.print("Your grade= " + grade + ", Your grade letter is: " + gradeLetter+'\n');

System.out.println(d) ;} }

Another Example for Printing variable

Page 12: Computer programming Lecture#2 أ. إلهام باسندوه 1

12

•A constant is similar to a variable except that they keep the same value throughout their existence

•They are specified using the reserved word final in the declaration

•For example:final double PI = 3.14159;final int FullGrade = 100;

Constants

Page 13: Computer programming Lecture#2 أ. إلهام باسندوه 1

13 Constants Example

Page 14: Computer programming Lecture#2 أ. إلهام باسندوه 1

14You can use the pre-written methods (functions) in the Scanner class.General structure:

Getting Input

Page 15: Computer programming Lecture#2 أ. إلهام باسندوه 1

15

Scanner Methods to obtain numbers and Boolean values from the user

Scanner Method Type of input

nextInt ;)( int

nextLong(); long

nextByte(); byte

nextFloat(); float

nextDouble(); double

nextBoolean(); boolean

nextShort(); short

Getting Input

Page 16: Computer programming Lecture#2 أ. إلهام باسندوه 1

16

example

Page 17: Computer programming Lecture#2 أ. إلهام باسندوه 1

17Output

Page 18: Computer programming Lecture#2 أ. إلهام باسندوه 1

18 import declaration Helps the compiler locate a class that is used in this program .You use import declarations to identify the predefined classes used in a Java program .

Explanation of example

Page 19: Computer programming Lecture#2 أ. إلهام باسندوه 1

19Scanner input = new Scanner( System.in );

Specifies the name (input) and type (Scanner) of a variable that is used in this program .

Enables a program to read data for use in a program .

The equals sign (=) in a declaration indicates that the variable should be initialized (i.e., prepared for use in the program) with the result of the expression to the right of the equals sign .

The new keyword creates an object.

Explanation of example

Page 20: Computer programming Lecture#2 أ. إلهام باسندوه 1

20

System is a class .Part of package java.lang .

Class System is not imported with an import declaration at the beginning of the program .

Explanation of example

Page 21: Computer programming Lecture#2 أ. إلهام باسندوه 1

21Java Basic Operators

The Arithmetic Operators : used in mathematical expressions in the same way that they are used in algebraAssume variable A =10 and variable B = 20, then:

Page 22: Computer programming Lecture#2 أ. إلهام باسندوه 1

22 The Relational Operators:Assume variable A =10 and variable B = 20, then:

Java Basic Operators (cont.)

Page 23: Computer programming Lecture#2 أ. إلهام باسندوه 1

23 The Logical Operators:Assume variable A = true and variable B = false, then:

Java Basic Operators (cont.)

Page 24: Computer programming Lecture#2 أ. إلهام باسندوه 1

24

   example: Adding Integers

Page 25: Computer programming Lecture#2 أ. إلهام باسندوه 1

25

   example: Adding Integers