9th Revise
9th Revise
CLASS 9TH
REVISION
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
TYPE CONVERSION
CLASS 9TH
REVISION
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.
The class that allows to input or read primitive data types(int,short,float,etc.) and strings is known
as Scanner Class.
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)
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
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
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 ()
{
}
}
Note - : While naming a method, just make sure that it should be a legal identifier and should be
meaningful.
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
SCOPE OF VARIABLE
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.
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:
FUNCTION
AND CONSTRUCTOR
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;
}
FUNCTION
AND 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.
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
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
DOUBLE
DIMENSIONAL ARRAY
[ ] [ ]
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