computer programming lecture#2 أ. إلهام باسندوه 1
TRANSCRIPT
1
Computer programming
Lecture#2
باسندوه. إلهام أ
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
3
Primitive Data Types
Primitive data types are predefined types of data, which aresupported by the programming language.
4Reserved Words
5 Some Escape Sequences For
FormattingEscape sequence Description
\t Horizontal tab
\r Carriage return
\n New line
\” Double quote
\\ Backslash
6Escape Sequences Example
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
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
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);
^
10Example for Printing variable
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
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
13 Constants Example
14You can use the pre-written methods (functions) in the Scanner class.General structure:
Getting Input
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
16
example
17Output
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
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
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
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:
22 The Relational Operators:Assume variable A =10 and variable B = 20, then:
Java Basic Operators (cont.)
23 The Logical Operators:Assume variable A = true and variable B = false, then:
Java Basic Operators (cont.)
24
example: Adding Integers
25
example: Adding Integers