Computer Grade IX
Computer Grade IX
Programming Paradigm-
A programming paradigm where a complex problem is solved by dividing it into smaller problems by
using functions/procedures/methods/instructions.
The program solving aspect lays more emphasis on procedures than objects
[ a big problem is divided into smaller problems, solution of each smaller problem is found and
combined to reach a final solution ]
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)
[ An object is any unique identifiable entity with some attributes and behavior ]
Features-
Limitations-
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
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
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
Inheritance- is a powerful mechanism by which an object in one class acquires properties from
objects in another class
Features of Java-
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
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
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
Interpreter –
A program that translates source code into object code, line by line
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.
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
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
27 = 128 characters
UNICODE-
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-
(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-
Rules-
(iii) Literal-
Real literals: Floating point literals, represent values with decimal points
String literals: represent more than one characters inside double quotes
(iv) Operators-
Increment (++), decrement (--), unary plus (+), unary minus (-) => negate the value, logical not (!)
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 ( &&, ||, ! )
4. Dot operator – Member Operator, used to invoke a member of a class or package (like methods)
work on three or more operands, condensed form of if else statement, return a value
(statement_1)?(statement_2):(Statement_3) ;
. (period or dot)
=> to separate package name from sub-package name and class name
; (semicolon)
() (parenthesis)
{} (curly brackets)
[] (square brackets)
, (comma)
Data types-
*Predefined in Java
*Size is fixed
8 in number
*Size depends on the number and types of primitive data types used to create them
Variables-
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-
Declared using keyword ‘final’ before data type name, e.g. => final float g = 9.8 ;
Precedence-
Associativity-
The direction in which an expression is evaluated if it contains many operators with the same
precedence value
print() –
println() –
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
Widening Casting
Coercion
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
Narrowing casting
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
In java.util package
* 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
Delimiter –
Methods of the Scanner class – methods don’t include ‘sc.’ But only ‘int()’, ‘Double()’, etc.
sc.nextInt() ;
sc.nextDouble() ;
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
Syntax Errors –
Logical Errors –
Semantic errors
When program gets compiled and executed but results in unexpected output (normal execution)
Comments –
Explanation of the source code of the program or parts of the program that make the source code
easy to understand
The ‘Math’ class of the ‘java.lang’ package contains essential mathematical functions of java
min = 1 (incuded)
max = 7 (excluded)
if int (6.0)
R = (int) (1 + (7-1)*Math.random() );
1 <= R <= 6
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
if-else-
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
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
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
……………………………. ;
initialization ;
while (condition)
……………………………… ;
updation ;
initialization ;
do
…………………
updation ;
while(condition) ;
Jump statements –
i. ‘break’ –
takes the program control out of the loop body or switch-case body
ii. ‘continue’ –
takes the program flow to the beginning of the loop and continues with the next iteration
doesn’t terminate the loop
for –
the control variable (counter variable) is initialized along with the loop
while –
do-while –
the control variable is initialized before entering the body of the loop
Note – V.V.I.P
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) )
‘A’ => 65
‘0’ => 48
‘5’ => 53
‘9’ => 57
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() ;
String b = sc.nextLine() ;
Int a = sc.nextInt() ;
(no problem)
Int a = sc.nextInt() ;
sc.nextLine() ;
String b = sc.nextLine() ;
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.-
int a = sc.nextInt() ;
sc.next()
String b = sc.nextLine() ;
System.out.println("int a = "+a);
System.out.println("String b = "+b);
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 =