by mr. muhammad pervez akhtar

20
DATA STRUCTURES AND ALGORITHMS (PRIMITIVE JAVA) By Mr. Muhammad Pervez Akhtar [email protected] http://pervez.farooka.com

Upload: cuthbert-johnston

Post on 06-Jan-2018

216 views

Category:

Documents


2 download

DESCRIPTION

 Conditional Statements  Relational and equality operators, Logical operators, The if statement, The while statement, The for statement, The do statement, break and continue, The switch statement, The conditional operator  Methods  Overloading of method names  Storage classes

TRANSCRIPT

Page 1: By Mr. Muhammad Pervez Akhtar

DATA STRUCTURES

AND ALGORITHMS

(PRIMITIVE JAVA)

By Mr. Muhammad Pervez [email protected]

http://pervez.farooka.com

Page 2: By Mr. Muhammad Pervez Akhtar

COURSE OUTLINE The general environment The first program

Comments, Main, Terminal Output Primitive types

Primitive types, Constants, Declaration and Initialization, Terminal Input and Output

Basics OperatorsAssignment Operators, Binary Arithmetic

Operators, Unary Operators, Type Conversion Operators

Page 3: By Mr. Muhammad Pervez Akhtar

COURSE OUTLINE Conditional Statements

Relational and equality operators, Logical operators, The if statement, The while statement, The for statement, The do statement, break and continue, The switch statement, The conditional operator

MethodsOverloading of method namesStorage classes

Page 4: By Mr. Muhammad Pervez Akhtar

THE GENERAL ENVIRONMENT

Java Source code resides in files whose names end with the .java suffix

The local compiler, javac, compiles the program and generates .class files, which contain bytecode

Java bytecodes represent the portable intermediate language that is interpreted by running the Java interpreter, java.

The interpreter is also known as the Virtual Machine

Java programs, input can come from one of many places: Standard input, Command Line Arguments, A File, GUI

Component

Page 5: By Mr. Muhammad Pervez Akhtar

THE FIRST PROGRAM

Save program as FirstProgram.java Compile program as javac

FirstProgram.java Run program as javac FirstProgram Four things to note in program

Comments, class, main method, standard output

Page 6: By Mr. Muhammad Pervez Akhtar

COMMENTS Comments are non-compiled and non-

executable code Three types of comments

// : single line comment, inherited from C /*…*/: multi line comments inherited from c /**…*/: information for javadoc utility to

generate documentation form comments Why Comments are needed?

Make code easier for humans to read Helps programmers to modify the program Commented program is a good sign of

programmer

Page 7: By Mr. Muhammad Pervez Akhtar

MAIN() METHOD Java program consists of interacting

classes which contain methods and data elements

Methods can be invoked using object of its class

Static method can be invoked without object of its class

Public static void main(String args[]) is special method with void return type, command line argument, public access modifier and is static

It is the method that executes first of all

Page 8: By Mr. Muhammad Pervez Akhtar

TERMINAL OUTPUT System.out.println: is the primary

output mechanism in java Also called standard output (output on

console) System.out.print: is another way of

output println vs print

println shifts control to start of next line after output

but print does not shifts control to next line Can output integer, floating point,

string, or some other type

Page 9: By Mr. Muhammad Pervez Akhtar

PRIMITIVE TYPES

Page 10: By Mr. Muhammad Pervez Akhtar

CONSTANTS Integer constants can be represented in either

decimal, octal, or hexadecimal notation Octal notation is indicated by a leading 0;

hexadecimal is indicated by a leading 0x or 0X Equivalent ways of representing the integer 37:

37,045,0x25 ::Decimal, Octal and Hexa Character constant is enclosed with a pair of

single quotation marks, as in 'a‘ Internally, this character sequence is interpreted as

a small number String constant consists of a sequence of

characters enclosed within double quotation marks, as in "Hello"

Page 11: By Mr. Muhammad Pervez Akhtar

ESCAPE SEQUENCES Escape sequence is a constant but with

special meanings It starts with a ‘\’ backslash followed by

a character to print Followings are commonly used: ‘\n’ New line character ‘\\’ Backslash character ‘\’’ Single quotation mark ‘\”’ Double quotation mark

Page 12: By Mr. Muhammad Pervez Akhtar

DECLARATION AND INITIALIZATION OF PRIMITIVE TYPES

A primitive type, is declared by providing its type, its name, and optionally its initial value

Type should be primitive type (int, float etc) The name must be an identifier, that

consists of:Any combination of letters, digits, and the

under-score character but it not start with a digit

Reserved words are not allowedNot reuse identifier names that are already

visibly usedAre case sensitive, age or Age are different

Page 13: By Mr. Muhammad Pervez Akhtar

TERMINAL INPUT AND OUTPUT Basic formatted terminal I/O is accomplished by

nextLine and println The standard input stream is System.in The standard output stream is System.out Formatted I/O uses the String type + combines two Strings If one of the argument is not a String, a

temporary String is created for it if it is a primitive type

For input, we associate a Scanner object with System.in.

Scanner has different methods for different primitive types

Page 14: By Mr. Muhammad Pervez Akhtar

BASIC OPERATORS Operators are symbols to perform an

operation Operators are used to form expressions Expression is combination of variables,

constants and operators Following are the types of operators

Assignment Operators =, +=, -=, *=, /=Binary Arithmetic Operators +, -, *, /, %Unary Operators ++, --, - ::prefix or

postfix useType Conversion Operators

Generate a temporary entity of a new type

Page 15: By Mr. Muhammad Pervez Akhtar

CONDITIONAL STATEMENTS Statements that affect the flow of

control Relational and Equality operators

<, <=, >, >=, ==, != Logical Operators

AND, OR, NOT or conjunction, disjunction and negation respectively

&&, ||, ! Is another representation ! Has highest precedence and && has

higher precedence than ||&& and || called short-circuit operators

Page 16: By Mr. Muhammad Pervez Akhtar

CONDITIONAL STATEMENTS The IF, IF THAN ELSE statement

Two common mistakes

Loops the While, For, and Do statements

Page 17: By Mr. Muhammad Pervez Akhtar

CONDITIONAL STATEMENTS Break and Continue statements

Break statement terminate execution in the middle of a repeated statements (loop)

Continue StatementGive up on the current iteration of a loop and go on to the next iteration

Page 18: By Mr. Muhammad Pervez Akhtar

CONDITIONAL STATEMENTS Switch Statement

Suitable when multiple branches against one Condition

The conditional operatora shorthand for simple if-else statements testExpr ? yesExpr : noExpr

Page 19: By Mr. Muhammad Pervez Akhtar

METHODS Known as a function or procedure :: main() A method header consists of a name, a

(possibly empty) list of parameters, and a return type

The actual code to implement the method, sometimes called the method body

A method declaration consists of a header plus the body

Overloading of method Several methods may have the same name and

be declared in the same class scope as long as their signatures(that is, their parameter list types) differ.

Page 20: By Mr. Muhammad Pervez Akhtar

STORAGE CLASSES Local Variables:

Entities that are declared inside the body of a method are local variables

Can be accessed by name only within the method body

Global VariablesA variable declared outside the body of a

method is global to the class if the word static is used, is similar to global

variables in other languages Static Final Variable

If both static and final are used, they are global symbolic constants