0% found this document useful (0 votes)
36 views17 pages

Computer Grade IX

Uploaded by

hejekdbdghsnwnb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views17 pages

Computer Grade IX

Uploaded by

hejekdbdghsnwnb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Computer grade IX (revision)

Programming Paradigm-

An approach or style of programming that I used by a programming language

It is a basis of classification of programming languages

1) Procedure Oriented Programming- (POP)

A programming paradigm where a complex problem is solved by dividing it into smaller problems by
using functions/procedures/methods/instructions.

[ A function is a set of instructions to be executed by a computer ]

The program solving aspect lays more emphasis on procedures than objects

Used to create medium sized applications mainly

Follows a top-down approach

[ a big problem is divided into smaller problems, solution of each smaller problem is found and
combined to reach a final solution ]

COBOL, BASIC, FORTRAN, C, Pascal, ALGOL

Features-

 Focuses on procedures
 Functions share global data
 Data items are global and keep floating from one function to another
 Uses a top-down approach to carry out all computations

Limitations-

 Data items are not secure as they are global and can be accessed by any function
 Changes made in data items by one function is reflected on all functions
 Not suitable for creating large applications or solving complex programs
 Limited code reusability
 Limited code extensibility (ability to create new data types)

2) Object Oriented Programming (OOP)-

A programming paradigm based on the concept of using objects to do all computations.

Problem solving aspect lays emphasis on objects rather than functions/procedures

[ An object is any unique identifiable entity with some attributes and behavior ]

C++, Java, Smalltalk, Visual Basic, Simula 67

Features-

 Focuses on objects rather than procedures


 Restricts free movement of data items and functions
 Suitable for solving complex problems
 Follows a bottom-up approach
 Provides for code reusability and extensibility
 Provides easy and simple troubleshooting

Limitations-

 Size of programs written in OOP is more than one written in POP

Object- basic unit of OOP

Any unique and identifiable entity with some attributes and behavior

A component of the program that knows how to perform certain actions and how to interact with
other elements of the program

An instance of a class

Has physical existence

Declared using the keyword ‘new’

Class- Non-primitive or user definer or composite data type

A collection of objects which have some common attributes and behavior

A group of similar objects

A class is defined as an object factory because one can create as many numbers of objects as wanted
within a class which have some common attributes and behavior

A class is called a user defined data type because it is defined by a user with certain characteristics
and behaviors within it

A class is called a composite data type because it is composed of member variables of different data
types

A class is a prototype of an object

A class is a blueprint from which objects can be created

Has logical existence

Declared using the keyword ‘class’

Principles of OOP-

Data Abstraction- refers to the act of representing only the relevant data or essential components
and hiding all other unnecessary background details

Encapsulation- refers to the process of binding/ wrapping up data items and associated functions
together into a single unit to keep them safe from any unauthorized access or misuse

 Makes the program secure


 Improves maintainability and flexibility of the program
Polymorphism- refers to the concept of using the same function name for multiple times in a
program to perform different tasks

Inheritance- is a powerful mechanism by which an object in one class acquires properties from
objects in another class

 Enables building new classes(child/derived/sub classes) from pre-existing classes


(parent/base/super classes)

Java- developed by James Gosling at Sun Microsystems

Later acquired by Oracle Corporation

Earlier Name- Oak

Features of Java-

1) Platform Independent Language-

The compiler coverts the source code of a java program (.java file) to a byte code(.class file) which
can run on any platform with a JVM

2) Object Oriented Language-

3) Simple – because java has a simple, easy to understand and clean syntax

4) Robust – puts lot of emphasis in early checking for possible errors, provides garbage collection,
exception handling and memory allocation

5) Secure – derived from encapsulation

6) Distributed Language – java programs can be distributed on more than one systems that are
connected via an internet connection

7) Supports Multithreading – concurrent execution of more than one part of a program for
maximum utilization of the CPU

Standalone Programs (Java Applications) – java programs that can run independently on a
computer

Internet Applets (Java Applets) – small java programs that are embedded into a web page and
require a web browser to be executed

Compiler –

A program that translates source code into machine code/object code in one go

Can generate an intermediate code

Occupies larger part of memory

Execution of a compiled program is faster

Interpreter –

A program that translates source code into object code, line by line

Never generates an intermediate code


Occupies less part of memory

Execution of an interpreted program is slower

Java Development Kit (JDK)-

A complete set of Java Runtime Environment (JRE), compilers and various tools like Javadoc, Java
debugger, etc. required to create, compile and execute java programs.

A product of Sun Microsystems

Java Bytecode-

An intermediate code produced by the javac compiler as a result of compilation of a Java program

A platform independent code that can run on all platforms which have a JVM

Makes Java a platform independent code

(.class) file extension

Java Virtual Machine (JVM)-

The interpreter of Java which executes the intermediate bytecode produced by the javac compiler
and converts it into machine/object code

A character set in java refers to a set of all letters of the English alphabet, digits and few special
symbols which are valid in Java

American Standard Code for Information Interchange or ASCII code-

Developed by Bob Bemer (also developed backslash)

A 7 bit character encoding scheme which uses numbers to represent characters

Each character is assigned a number between 0 and 127

27 = 128 characters

UNICODE-

A 16 bit character encoding standard which is internationally accepted and is capable of


representing almost every character in well-known languages around the world

Each character holds 2 bytes of memory (Therefore, char in java = 2 bytes)

216 = 65536 characters

Escape Sequence-

An escape sequence is a combination of a backslash (\) and one or more characters which has a
special meaning to the Java compiler

Used to represent a character or a sequence of characters that may be difficult to represent directly
in the program
Examples-

\b = Backspace

\n = new line

\t = Horizontal tab

\v = vertical tab

\’ = single quote

\” = double quotes

\\ = backslash

\a = audible bell

\f = form feed

\r = carriage return

Tokens-

A token is the smallest meaningful unit of a Java program

Recognized by the compiler

Basic building block of programs

5 types- keywords, identifiers, literals, operators, punctuators, separators

(i) Keywords-

Reserved words in Java that convey a special meaning to the compiler and can be used for a
particular purpose only

Cannot be used as variable names or identifiers because that would try to change its existing
meaning, which is not allowed

Example- ‘class’, ‘new’, ‘byte’, ‘int’, ‘long’, ‘char’, ‘public’, ‘protected’, ‘default’, ‘private’ ‘return’,
‘void’

‘const’ and ‘goto’ are no longer in use as keywords, yet, they are reserved and cannot be used as
identifiers

(ii) Identifiers-

Name assigned to variables, methods, packages, classes or interfaces

Rules-

1. Must start with either a letter or a recognized special character ( $ or _ )

2. Must not begin with a digit

3. Must not be a keyword


4. After the first character, an identifier can have any combination of characters

5. Are case sensitive

6. Can be of any length

(iii) Literal-

A sequence of characters used in a program to represent a constant value

Integer literals: represent integer values

Real literals: Floating point literals, represent values with decimal points

Character literals: represent exactly one character inside single quote

String literals: represent more than one characters inside double quotes

Boolean literals: represent logical values ‘true’ or ‘false’

Null literals: represented by the keyword ‘null’ or escape sequence ‘\0’

(iv) Operators-

Special symbols that carry out operations on one or more operands

Operands are data values on which operations are performed

3 types of operators in Java-

1. Unary Operators – require only a single operand

Increment (++), decrement (--), unary plus (+), unary minus (-) => negate the value, logical not (!)

Pre fix, Post fix increment/decrement

2. Binary Operators – work on two operands

i. Arithmetic Operators (4) – perform basic mathematical operation (+, -, *, /, %)

ii. Relational Operators (6) – compare two values and give Boolean result (>, <, ==, !=, >=, <= )

iii. Logical Operators (3) – compare two or more relational or Boolean expressions to give Boolean
result ( &&, ||, ! )

3. Assignment Operator – used to assign a value to a variable (=)

4. Dot operator – Member Operator, used to invoke a member of a class or package (like methods)

5. Ternary Operators – Conditional Operators,

work on three or more operands, condensed form of if else statement, return a value

(statement_1)?(statement_2):(Statement_3) ;

If 1 => true, 2 gets executed else 3 gets executed


(v) Punctuators-

. (period or dot)

=> to separate package name from sub-package name and class name

=> to access member variables and methods of a class or package

; (semicolon)

=> to terminate a statement

(vi)Separators- help to define the structure of a program

() (parenthesis)

=> to hold list of parameters in method definition

=> used in control statements and type casting

{} (curly brackets)

=> to define a block/scope of a code, class, method

[] (square brackets)

=> used in array declaration

, (comma)

=> to separate consecutive parameters in method definition

=> to separate multiple variables under the same declaration

Data types-

(i) Primitive Data types-

Intrinsic data types

Inbuilt data types

*Predefined in Java

*Always have a value

*Independent of any other data type

*Size is fixed

8 in number

byte = 1 byte (8 bits) = default value = 0 = -27 to 27-1

short = 2 bytes (16 bits) = default value = 0 = -217 to 217-1

int = 4 bytes (32 bits) = default value = 0 = -231 to 231-1

long = 8 bytes (64 bits) = default value = 0 = -263 to 263-1


float = 4 bytes (32 bits) = default value = 0.0f

double = 8 bytes (64 bits) = default value = 0.0d or 0.0

char = 2 bytes (16 bits) = default value = ‘/u0000’

boolean = occupies 1 byte, uses 1 bit = default value = false

(ii) Non-Primitive Data Types-

Composite data types

Reference data types

*Defined by the user

*Can be null also

*Dependent of other data types

*Size depends on the number and types of primitive data types used to create them

Composed of various primitive data types

Examples- classes, interface, arrays, objects, Strings

Variables-

A named location in a computer’s memory which stores data temporarily

The value stored in it can be changed during the execution of the program

Note - ‘String’ is an object type data type, not a primitive type (thus a non-primitive data type)

Provided by java.lang package which is preloaded in a class, hence; can be used as a primitive data
type

Constants-

Data values which never change during the execution of a program

Declared using keyword ‘final’ before data type name, e.g. => final float g = 9.8 ;

Precedence-

The order in which a program evaluates the various operators in an expression

Associativity-

The direction in which an expression is evaluated if it contains many operators with the same
precedence value

print() –

* doesn’t adds a new line after printing a statement


* can’t be used with an argument

println() –

* adds a new line after printing a statement

* can be used without an argument

Initialization is the process of assigning value to a variable for the first time in a program

Type conversion – process of converting one predefined data type into another

(i) Implicit Type Conversion-

Widening Casting

Coercion

Default type conversion

When an expression contains more than one data type, the java compiler automatically converts all
data types to the highest data type in the expression

Only a smaller data type can be converted to a bigger data type

e.g.- double a = int b + int c ;

b and c are automatically converted to double

(ii) Explicit Type Conversion-

Narrowing casting

Performed manually through user intervention

A smaller data type can be converted to a bigger data type and also, a bigger data type can be
converted to a smaller data type

e.g.- int a = (int)b / (int)c ; if b and c are double type they are manually being converted to int

Package – group of classes that are packed together on basis of their functionality

* prevent naming conflicts

* make searching/locating and using classes easier

* provide controlled access to data items

* can be considered as data encapsulation

Scanner class – used to read data dynamically from the keyboard

(JDK 1.5 or above with Bluej 2.5 or above)

In java.util package

Advantages of using the Scanner class –

* It accepts data in token form so there is no need to mention the type of data
* String manipulation is quite easy as the Scanner class provides various functions

* InputStreamReader and BufferedReader are not needed

* Can be used for powerful pattern matching

* No need for exception handling

Delimiter –

A character used to separate two tokens (data items)

Whitespace = default delimiter

e.g. = spaces, commas

Methods of the Scanner class – methods don’t include ‘sc.’ But only ‘int()’, ‘Double()’, etc.

sc.nextInt() ;

sc.nextDouble() ;

sc.next() ; single word

sc.nextLine(); a sentence

sc.next().charAt(0) ;

Errors – illegal operations in a program that result in abnormal working of the program

Runtime Errors –

When the program is syntactically is correct but has a logical issue that is detected during the
program execution and is clearly reflected in the output screen

Not detected during compilation but during execution, (abnormal execution)

‘try-catch’ block is used to handle run-time errors

e.g. – division by zero, square root of a negative number, etc.

Syntax Errors –

Compile time errors

Due to incorrect use of syntax in a program

Error message is displayed on the compiling screen

Detected during compiling

Easy to spot and rectify


e.g. – missing semi-colon, wrong variable or method or class name, etc.

Logical Errors –

Semantic errors

When program gets compiled and executed but results in unexpected output (normal execution)

Not detected by the compiler

Found by the user only

Comments –

Explanation of the source code of the program or parts of the program that make the source code
easy to understand

Ignored by the compiler

Single line comments – ‘ //………… ’

Multi line comments – ‘ /*………….*/ ’

‘java.lang’ package is preloaded in every java class

The ‘Math’ class of the ‘java.lang’ package contains essential mathematical functions of java

R = (int) ( min + (max-min)*Math.random() ) ;

max is exclusive….. min is inclusive

so if we want to use it as a dice =>

min = 1 (incuded)

max = 7 (excluded)

less than 7 will be included, (6.9999…….)

if int (6.0)

R = (int) (1 + (7-1)*Math.random() );

1 <= R <= 6

Dangling else problem –

The nested if statement introduces a source of potential ambiguity referred to as the ‘dangling else’
problem. This problem arises when in a nested if structure the number of ‘if’ clauses is more than
the number of ‘else’ clauses.

e.g.-
if(condition_1)

if(condition_2)

statement_1 ;

else(condition_2)

statement_2 ;

switch-case statement-

used for creating menu-driven programs where the user has to select an option from given multiple
choices

used to select among multiple alternatives

can have values based on user’s choice

expression used must be an integer, a character or a String

can be used only for a single value an not a range of values

if-else-

used to select between two alternatives

values used are based on constraints

any data type can be used

can be used to check for a range of values

Fall through – refers to the way the switch statement executes its various case sections

Every statement that follows the selected case will be executed unless a break statement is
encountered

Entry controlled loops –

The test condition is checked before executing the body of the loop

Body of the loop will not be executed ever if the test condition evaluates to false

e.g. – ‘for’ loop and ‘while’ loop

Exit controlled loops –

The test condition is evaluated after executing the body of the loop

Body of the loop will be executed at least once even if the test condition evaluates to false

e.g. – ‘do-while’ loop


for (initialization; condition; updation)

……………………………. ;

initialization ;

while (condition)

……………………………… ;

updation ;

initialization ;

do

…………………

updation ;

while(condition) ;

Jump statements –

Statements used in programming languages to control the flow of execution of a program

i. ‘break’ –

used to terminate a loop or a switch-case statement whenever encountered

takes the program control out of the loop body or switch-case body

ii. ‘continue’ –

used to skip the rest of the current iteration of a loop

takes the program flow to the beginning of the loop and continues with the next iteration
doesn’t terminate the loop

for –

entry controlled loop

used for a fixed number of iterations

the control variable (counter variable) is initialized along with the loop

while –

entry controlled loop

used for an unknown number of iterations

the control variable is initialized before the beginning of the loop

do-while –

exit controlled loop

the control variable is initialized before entering the body of the loop

Note – V.V.I.P

S.o.pln (a+b) ; => addition

S.o.pln (“Hello”+a+b) ; => concatenation

S.o.pln (“Hello”+(a+b)) ; => a+b addition and concatenation with “Hello”

S.o.pln (“Hello”+a+(-b) ) ; => concatenation

S.o.pln (“Hello”+a-b) ; => syntax error

S.o.pln (“Hello”+(a-b) ) ; => a-b subtraction and concatenation with “Hello”

int a ;

char b ; (int>char)

a = b ; (correct, as ‘b’ is char and isko ‘a’ me daal rahe hai, b < a) char = 2bytes, int = 4bytes

b = a ; (incorrect, as ‘a’ is int and isko ‘b’ me daalne ka try kar rahe hai lekin a(int) > b(char) )

b = (char) a; (correct, => explicit type conversion)

‘A’ => 65

‘Z’ => 90 ( Z is 26th letter, hence; Z = A + 25 = 65 + 25 = 90)


‘a’ => 97

‘z’ => 122

‘0’ => 48

‘5’ => 53

‘9’ => 57

'5' has the int value 53


if we write '5'-'0' it evaluates to 53-48, or the int 5
if we write char c = 'B'+32; then c stores 'b', as 66+32 = 98 = ‘b’

int a = sc.nextInt() ;

String b = sc.next() ;

(no problem)

String b = sc.next() ;

int a = sc.nextInt() ;

(no problem)

Int a = sc.nextInt() ;

String b = sc.nextLine() ;

(no syntax problem but….. String input gets skipped)

String b = sc.nextLine() ;

Int a = sc.nextInt() ;

(no problem)

Int a = sc.nextInt() ;

sc.nextLine() ;
String b = sc.nextLine() ;

(no problem…… String input skipping problem rectified)

Int a = sc.nextInt() ;

Sc.next() ;

String b = sc.nextLine() ;

(complex problem……… After input of int, b’s printing statement not printed, directly input option
but single word only that too 2nd word; but we want sentence input through b )

e.g.-

System.out.println("Enter int a- ");

int a = sc.nextInt() ;

sc.next()

System.out.println("Enter String b- ");

String b = sc.nextLine() ;

System.out.println("int a = "+a);

System.out.println("String b = "+b);

Output => (1)

Enter int a-

101

Aditya Agarwal

Enter String b-

int a = 101

String b = Agarwal
Output => (2)

Enter int a-

101

Aditya

Enter String b-

int a = 101

String b =

You might also like