0% found this document useful (0 votes)
11 views16 pages

9th Revise

This document serves as a revision guide for Class 9th programming concepts, covering topics such as creating classes, data types, input methods, operators, loops, functions, and constructors. It explains the syntax and usage of various programming elements, including variable initialization, type conversion, and the structure of control statements like if-else and loops. Additionally, it discusses the importance of methods and constructors in Java, along with examples and explanations of their functionalities.

Uploaded by

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

9th Revise

This document serves as a revision guide for Class 9th programming concepts, covering topics such as creating classes, data types, input methods, operators, loops, functions, and constructors. It explains the syntax and usage of various programming elements, including variable initialization, type conversion, and the structure of control statements like if-else and loops. Additionally, it discusses the importance of methods and constructors in Java, along with examples and explanations of their functionalities.

Uploaded by

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

CHAPTER

CLASS 9TH
REVISION

OBJECT CREATING A CLASS DATA TYPES


The means to recognize or identify
By creating a class we create a memory location in an entity/entities and their operations
Computer's memory. are known as data types.
For eg: To store different types of
class CIE values/data we use different data
types.
CIE Integer Data: There are 4 data
CIE types for storing integer data,
they are arranged in ascending
Class order of their range: byte - short -
int - long
Real Data: There are 2 data types
for storing real data, they are
arranged in ascending order of
their range: float - double
Computer's memory
Character Data: We use char
VARIABLES data type to store characters i.e.
data enclosed in single inverted
Now, inside a class, we need memory allocations for commas(' ').
variables to store data. String Data: We use String data
Syntax to create a variable: type to store characters i.e. data
data_type variable_name=constant/value enclosed in double inverted
For eg: commas(" ").
String var="cons"; Boolean Data: We use boolean
data type to store boolean data
var cons i.e. true and false.

Variable
[String]
CHAPTER

CLASS 9TH
REVISION

HOW TO PRINT ?
OBJECT
IMPLICIT TYPE
System.out.print() : This statement prints the sentence CONVERSION
and keeps the controller in the same line.
System.out.println() : This statement prints the
sentence and brings the controller in the next line. byte char short int long float double

Syntax : It's just like the reactivity series of


System.out.print("I'm Pranay Mishra: "); chemistry, here the lower data types
System.out.println("Founder of Clarify Knowledge"); are more powerful, they will change
System.out.print("Welcome to Code Is Easy"); the above ones into their data type.

Output : char + int = int


I'm Pranay Mishra: Founder of Clarify Knowledge int + long =long
Welcome to Code Is Easy. int + double = double

ESCAPE SEQUENCE CHARACTER EXPLICIT TYPE


CONVERSION
Now, inside a class, we need memory allocations for It is forced coversion of data types
variables to store data. and is also known as Type Casting.
Syntax to create a variable:
data_type variable_name=constant/value For eg:
For eg: int a,float b,char c;
String var="cons"; d= (char)(a+b*c);
\n - New Line char ch='A';
\t - One tab space System.out.print((int)ch);
\' - Prints single inverted commas(') in output = 65
\" - Prints double inverted commas(") in output
\\ - Prints backslash (\) in output
Enter key is also known as Carriage-return key.
One tab space means 5 spaces(cursor shifts 5
spaces).

TYPE CONVERSION

If we want to use a data in real data type variable that is


in integer type then we need to change its data type
and this process is known as type conversion.
CHAPTER

CLASS 9TH
REVISION

INPUT USING INITIALIZATION


OBJECT

The process of assigning data or actual values to a declared variable is known as initialization.

This process is basically not taking input from the user but assigning the values in the variables
during writing the code.

INPUT USING SCANNER

The class that allows to input or read primitive data types(int,short,float,etc.) and strings is known
as Scanner Class.

Syntax :- Scanner variable_name = new Scanner(System.in)


-> Scanner sc = new Scanner(System.in);
-> Scanner ob = new Scanner(System.in);
-> Here ob and sc are the variable names.

INCREMENT(++) AND DECREMENT(--) OPERATORS

The increment operator increases the value by 1 and the decrement operator decreases the value by 1.
Prefix Notation
The presence of an increment or decrement operator before the operand or variable is known as prefix
notation. For eg: ++A or --A
It first changes the value then uses it. (change-then-use rule)
Postfix Notation
The presence of an increment or decrement operator after the operand or variable is known as postfix
notation. For eg: A++ or A--
It first uses the original value then changes it. (use-then-change rule)

LET’S MAKE A CALCULATOR

Step 1: Step 2:
CREATE A CLASS CREATE A SCANNER OBJECT
CHAPTER

CLASS 9TH
REVISION

Step 3:
CREATE A SCANNER OBJECT

Step 4:
ASK FOR USER'S CHOICE

Step 5:
ASK FOR NUMBERS

Step 6:
CREATE CONDITIONS FOR PROPER WORKING

Step 7:
JUST PRINT THE RESULT NOW
CHAPTER

CLASS 9TH
REVISION

MATHS CLASS TABLE

Functions Purpose Syntax Examples

Returns absolute value of a


Math.abs(-73)
abs() numeric data by ignoring the Math.abs(argument)
-> 73
sign

Returns the square root of a Math.sqrt(81)


sqrt() Math.sqrt(argument)
positive number -> 9.0

Used to find the power of a Math.pow(5,3)


pow() Math.pow(arg,power)
numeric data -> 125.0

It rounds of the number to Math.round(1.5) -> 2


round() Math.round(arg)
closest integer value Math.round(3.2) -> 3

Returns truncated value of an


Math.rint(30.35) ->
argument by rounding it off to
rint() Math.rint(argument) 30.0
greater number if iit is 0.5 or
Math.rint(5.5) -> 6.0
more.

floor() Returns the lower number Math.floor(argument) Math.floor(1.8) -> 1.0

ceil() Returns the upper value Math.ceil(argument) Math.ceil(1.2) -> 2.0

Returns the largest from two Math.max(36,40.5)


max() Math.max(arg1,arg2)
arguments ->40.5

Returns the smallest from two Math.min(10,17)


min() Math.min(arg1,arg2)
arguments ->10

Math.cbrt(125) ->
Returns the cube root of the 5.0
cbrt() Math.cbrt(argument)
number Math.cbrt(-8) ->
-2.0

Math.random()
Generates a random number
random() Math.random() ->
between 0.0 to 1..0
0.7134361215296077
CHAPTER

CLASS 9TH
REVISION

IF ELSE

SIMPLE IF STATEMENT

The process of assigning data or actual values to a declared variable is known as initialization.
This process is basically not taking input from the user but assigning the values in the variables
during writing the code.

IF ELSE STATEMENT

Syntax:-
if(condition-1)
{
if(condition-2)
{
Statement-2;
}
else()
{
Statement-3;
}
}
else()
{
Statement-3;
}
CHAPTER

CLASS 9TH
REVISION

NESTED IF ELSE

NESTED IF STATEMENT
True
Syntax:- Condition-1 Statement-1
if(condition-1)
{
False
Statement-1
if(condition-2) False
Exit Condition-2
{
Statement-2;
True
}
}
Statement-2

NESTED IF-ELSE() STATEMENT

Syntax:-
if(condition or relational statements)
{
if-code;
}
else(conditional or relational statements)
{
else-code;
}
CHAPTER

CLASS 9TH
REVISION

LOOPS

For Loop :
The loop that executes one or more statements upto a finite limit is known as for ( ) loop.
SYNTAX :
for ( Initial-Value ; Test Condition ; Update-statement )
{
Statement-blocks;
}

While Loop :
The loop that executes one or more statements till the condition is TRUE
and terminates the iteration when the condition is FALSE .

SYNTAX :
while ( expression or condition or relational expression)
{
Statements-block
}

Do - While Loop :
The loop that executes one or more statements till the condition is TRUE and terminates the
iteration when the condition is FALSE, is known as do-while( ) loop.

SYNTAX :
do
{
Statement-block
}
while( condition or relational expression);
CHAPTER

CLASS 9TH
REVISION

LOOPS

For Loop :
The loop that executes one or more statements upto a finite limit is known as for ( ) loop.
SYNTAX :
for ( Initial-Value ; Test Condition ; Update-statement )
{
Statement-blocks;
}

While Loop :
The loop that executes one or more statements till the condition is TRUE
and terminates the iteration when the condition is FALSE .

SYNTAX :
while ( expression or condition or relational expression)
{
Statements-block
}

Do - While Loop :
The loop that executes one or more statements till the condition is TRUE and terminates the
iteration when the condition is FALSE, is known as do-while( ) loop.

SYNTAX :
do
{
Statement-block
}
while( condition or relational expression);
CHAPTER

FUNCTION
AND CONSTRUCTOR

FUNCTIONS

A method is a named piece of code within a program and executes when it is called from another
section of code. In JAVA, the methods can exists only inside class.

class abc
{
void main ()
{
}
}

There are three reasons why we should use methods :


(i) They provide us a way to invoke the same operation from many places in a program, avoiding
code repetition. (Reusing the code)
(ii) They hide implementation details from the component using methods. (Abstraction)
(iii) They help us manage complex and bigger programs into smaller manageable and
understandable sub-parts . (Modularity)

Note - : While naming a method, just make sure that it should be a legal identifier and should be
meaningful.

Method prototype and signature


The first line of the method definition is the prototype of the method .
A method prototype describes the method interface to the compiler by giving details such as
the numer and type of agruments and types of return values.

METHOD TYPES
ACCESSING A
METHOD/FUNCTION
JAVA METHODS
A method is called or invoked by
providing the method name,
followed by parameters being
STATIC NON STATIC sent enclosed in paranthesis.
They are created The are created by
float area (float a, float b)
by keyword static absence of area (x,y);
in their prototype. keyword static in
their prototype.
CHAPTER

FUNCTION
AND CONSTRUCTOR

FUNCTIONS

Invoking static and non static methods

Static Non Static

They are called class They are called instance


methods and are invoked methods and are invoked
using class name. using objects.
<ClassName>. <ObjectName>.<non-
<staticMethodName>() staticMethodName>()

SCOPE OF VARIABLE

Instance Any method in class definition can assess these variables.


Parameter only the method where parameters appear can access these.
Local Only the bloc where the variable is declared, can access these.

CONSTRUCTOR

A constructor is a special member method of a class without a return type. It is used for
initialising and constructing the data members of an object when it is created. It is
automatically called (invoked) when the object is created using new operator. It cannot be
invoked by the user like normal methods.

Characteristics
A constructor will have the same name as that of the class.
A constructor does not have a return type not even void.
A constructor cannot be static or final.
A constructor must be declared public (if it has to be accessed outside the class).
It is automatically called (invoked) when an object is created.
CHAPTER

FUNCTION
AND CONSTRUCTOR

NEED OF A CONSTRUCTOR

A constructor is used for initialising and constructing the data members of an object with legal
values when it is created. Data members are mostly declared as private members, to
implement data hiding and abstraction. Due to this, data members cannot be initialised
outside the class with values. So, constructors are used for initialising the objects implicitly as
they are created.

Declaring and defining -:


Being a method, it also gives the programmer the ability to define, declare and modify. Since, a
constructor has no return type (not even void), it also provides the programmer with the liberty to
define a constructor with or without a parameter.
A simple constructor is created using the new operator followed by the name of the class.
Syntax:
<access specifier> class_name (parameter list/void)
{
Data_member1 = value1 ;
Data_member2 = value2;
}

Example :
class Book
{ int bookno;
String name;
float price;
public Book() //constructor is created, having same name Book as
{ bookno = 0; that of its class name Book, with public access specifier
name = "ABC"; and no return type.

TYPES OF CONSTRUCTORS
The main function of a constructor is to initialise the data members of an object while it is created.
Constructors can be broadly categorised into two categories based on the parameters it takes:

Default constructor : A constructor that takes no parameters.


Parameterised constructor : A constructor that takes one or more parameters.
CHAPTER

FUNCTION
AND CONSTRUCTOR

MORE ABOUT CONSTRUCTOR

Default Constructor/Non Parameterised Constructor


A constructor that takes no parameters is called a default constructor. It will have the name of the
class and have no return type. If you do not write a constructor in your program, the compiler will
supply a default constructor and initialise values with the default values of the primitive data types i.e.
integer variables to zero, floating point variables to 0.0 and String to null.
Syntax:
class_name()
{
Data_member1 = value1;
Data_member2 = value2;
}

Parameterised Constructor
Constructor that takes one or more parameters or arguments is called as a parameterised
constructor. Such a constructor will accept values and initialise the data members with the
corresponding values. There is no limitation to number of parameters.

Syntax:
class_name (type1 val 1, type2 val 2...)// parameter list
{
Data_member1 = val 1;
Data_member2 = val 2;
}

Declaring and defining -:


Being a method, it also gives the programmer the ability to define, declare and modify. Since, a
constructor has no return type (not even void), it also provides the programmer with the liberty to
define a constructor with or without a parameter.
A simple constructor is created using the new operator followed by the name of the class.
Syntax:
<access specifier> class_name (parameter list/void)
{
Data_member1 = value1 ;
Data_member2 = value2;
}
CHAPTER

FUNCTION
AND CONSTRUCTOR

MORE ABOUT CONSTRUCTOR

'This' keyword
'This' keyword is used within a constructor to refer to the current object. It is actually a reference to
the object that invokes the constructor. In fact, even if you do not use the this keyword, the compiler
normally implicitly converts it by prefixing this to the data members.

CONSTRUCTOR MEMBER METHODS

name: always similar to class name: according to user


name return type: void or valid
return type: no return type need: to avoid repetition and to
need: to initilalise member increase code reusability.
variables

Constructors within Constructors using 'this''


Constructors can be chained together by invoking a constructor from within another constructor. The
this keyword is used to invoke another constructor from within the class. When constructors are
chained this way, the control just moves from one constructor to the other till the data members are
initialised and the object is fully constructed.
CHAPTER

DOUBLE
DIMENSIONAL ARRAY

[ ]
SINGLE
OBJECTDIMENSION AL ARRAY:
00 0 1 201 02
It has a single subscript subscript
int ar[]=new int[3]; of array 10 3 4 511 12

DOUBLE
OBJECT DIMENSION AL ARRAY: 20
6 7 821 22

It has double subscript


int ar[][]=new int[3][3];

Row column

[ ] [ ]
00

10

20
0 1 2
01

3 4 5
11

6 7 8
21
02

12

22
00

10

20
0 1 2
01

3 4 5
11

6 7 8
21
02

12

22

logic To enter elements/digits in the matrix


for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
Outer loop {
ar[i][j]=in.nextInt();
} inner loop
}
CHAPTER

DOUBLE
DIMENSIONAL ARRAY

logic To print the matrix


for(int i=0;i<3;i++)
{
it prints the row for(int j=0;j<3;j++)
and as the loop {
terminates,
System.out.print(ar[i][j] + “ “);
it starts }
printing the next
row in the next System.out.println();
line }
and the process repeats until the outer loop is terminated

[ ] [ ]
LEFT DIAGONAL RIGHT DIAGNOAL

00 0 1 2
01 02 00 0 1 2
01 02

10 3 4 5
11 12 10 3 4 5
11 12

20
6 7 8
21 22 20
6 7 8
21 22

when i==j when i+j==2


left diagonal elements are found right diagonal elements are found

logic To print the diagonals

You might also like