3. Try-catch block
Try – catch block is useful to try statement or syntax if
there is any error catch block will handle exception
Syntax Exception
try int a=10;
{ int b=0;
int c;
// Body--------------
try{
} c = a/b;
catch(Exception e) }
{ catch(Exception e)
// Body------------ {
System.out.println(e);
}
}
3
4. Read Integer Input
From Keyboard
try
{
int a;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter value of a= ");
a = Integer.parseInt(br.readLine());
//float y = Float.parseFloat(stdin.readLine());-- For float
}
catch(Exception e){}
4
5. Read String Input
From Keyboard
try{
while( (strLine = br.readLine()) != null)
{ if(strLine.equals("exit"))
break;
System.out.println("Line entered : " + strLine);
}
br.close();
}
catch(Exception e)
{
System.out.println("Error while reading line from console : " + e);
}
5
12. Do while loop
Syntax
Do{
<Block of statements>;
}while(<boolean condition>);
Example
do
{
System.out.println("i is : " + i);
i++;
}while(i < 5);
12
13. Switch Statement
Syntax
The switch statement is Java’s
switch (expression)
multiway branch statement. It
{ provides an easy way to dispatch
case value 1 : execution to different parts of
statement 1 ; break; your code based on the value of
... an expression.
case value N :
statement N ; break;
default :
statements ; break;
}
13
14. Array in Java
Syntax
dataType[] arrayRefVar = new dataType[arraySize];
dataType[] arrayRefVar = {value0, value1, ..., valuek};
Example
Double[] myList = new double[10];
14
15. Design car
• Design car parts 4 tyre (1- 2 front tyre and 3-4 back
tyre), 4 doors (1- 2 front doors and 3-4 back doors), 4
headlight (2 front light and 2 break light), 1 body, 2
Glass (front and rear glass), 1 Mirror, 4 seat (2 front
seat (Left is driver seat), and 2 back seat) .
15
16. Java Exampe
Make saperate class for below mentioned all functionality
and attach all classes using interitance.
Note : Make super class Area with 2 memeber variable and
extend it another classes.
Triangle
Square
Area = ½ × b × h
Area = a2
b = base
a = length of side
h = vertical height
Rectangle Parallelogram
Area = w × h Area = b × h
w = width b = base
h = height h = vertical height
16
17. Java Overriding
• The benefit of overriding is: ability to define a
behavior that's specific to the sub class type. Which
means a subclass can implement a parent calss
method based on its requirement.
• In object oriented terms, overriding means to
override the functionality of any existing method.
17