Download presentation
Presentation is loading. Please wait.
Published byVirgil Gibson Modified over 8 years ago
1
Computer programming Lecture#2 أ. إلهام باسندوه 1
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 3 Primitive data types are predefined types of data, which are supported by the programming language.
4
4 Reserved Words
5
5 Some Escape Sequences For Formatting Escape sequenceDescription \tHorizontal tab \rCarriage return \nNew line \”Double quote \\Backslash
6
6 Escape Sequences Example
7
7 General 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 8
9
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); ^
10
10 Example 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); } 11 Another Example for Printing variable
12
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
13 Constants Example
14
14 You 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 MethodType of input nextInt();int nextLong();long nextByte();byte nextFloat();float nextDouble();double nextBoolean();boolean nextShort();short 15 Getting Input
16
example 16
17
Output 17
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 18
19
Scanner 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 19
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 20
21
21 Java Basic Operators The Arithmetic Operators : used in mathematical expressions in the same way that they are used in algebra Assume variable A =10 and variable B = 20, then:
22
22 The Relational Operators: Assume variable A =10 and variable B = 20, then: Java Basic Operators (cont.)
23
23 The Logical Operators: Assume variable A = true and variable B = false, then: Java Basic Operators (cont.)
24
24 example: Adding Integers
25
25 example: Adding Integers
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.