all java programs
TRANSCRIPT
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 1/46
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 2/46
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 3/46
JAVA keywords:
+++++++++++++
Write a program to get an input from the user:-
=============================================
package string_packg;
import java.util.Scanner;
public class takeinputfromtheuser {
/**
* @param args
*/
public static void main(String[] args) {
int a;
// TODO Auto-generated method stub
Scanner obj = new Scanner(System.in);
System.out.println("Enter the value of integer");
a = obj.nextInt();
System.out.println(" You entered the integer " +a);
}
}
OUTPUT:-
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 4/46
======
Enter the value of integer
1
You entered the integer 1
NOTES:
======
iF YOU ARE GOING TO TAE A STRING AS AN INPUT THEN USE
s = obj.nextLine();
===========
IF YOU ARE ENTERING A FLOAT ValUE THEN USE
s = obj.nextFloat();
===========
CASTING:
=======
Implicit and Explicit.
1) IMPLICIT CASTING:-
===================
package string_packg;
public class Casting {
/**
* @param args
*/
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 5/46
public static void main(String[] args) {
short short_ID = 12345;
int integer_ID = short_ID;// LOWER DATA TYPE VALUE IS BEING ASSIGNED TO A
VARIABLE OF HIGHER DATA TYPE
System.out.print(integer_ID);
// TODO Auto-generated method stub
}
}
2) EXPLICIT CASTING:-
====================
package string_packg;
public class EXPLICITCASTING {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 123445;
short s = (short) i ;
System.out.print(s+"id");
}
}
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 6/46
output:
======
-7627id
package string_packg;
public class EXPLICITCASTING {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 123445;
short s = i ; // (short) i is missing!
System.out.print(s+"id"); // '+' IS USED FOR CONCATENATION
}
}
OUTPUT:
======
ERROR!!!!
Type mismatch: cannot convert from int to short!
operators AND operands:
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 7/46
++++++++++++++++++++++
1)ARITHMETIC
Addn, Subn, Multpn, Divn, Modulo
2)UNARY ARITHMETIC:
==================
INCREMENT: ++
DECREMENT: --
<<<<PREFIX AND POSTFIX.
3)RELATIONAL:
============
==,!=,<,<=,>,>=.
4)LOGICAL:-
=========
cONDITIONAL AND && (BINARY OPERATOR) ( a k a sHORT CIRCUIT AND)
cONDITIONAL OR || ("")
and & (does the same function as double-and && in an if-else)
OR | (bitwise OPERATORS ,ARE THEY THERE???? BHAGAVANTHANIGE GOTTU!!!)
XOR ^
not !
USES &:
=======
package string_packg;
public class LOGICAL {
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 8/46
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 9/46
*/
public static void main(String[] args) {
int i= 234;
int j=235;
int k=300;
int l =299;
if ((i>j)&&(k>l))
System.out.print("TRUE");
else
System.out.print("falsE");
// TODO Auto-generated method stub
}
}
output:
======
falsE ( SAME O/P AS IN THE PREVIOUS CASE )
INCREMENT AND DECREMENT OPERATORS:
==================================
package string_packg;
public class INCREMENTPOSTANDPRE {
/**
* @param args
*/
public static void main(String[] args) {
int i=7,j = 7;
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 10/46
int s,d;
s= i++ + i++ + i++;
System.out.print(s);
d= ++j + ++j + ++j;
System.out.print(d);
// TODO Auto-generated method stub
}
}
OUTPUT:
======
24 27
SWITCH CASE
============
package string_packg;
public class Switchcases {
/**
* @param args
*/
public static void main(String[] args) {
switch(2*3+4+5)
{
case 1:
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 11/46
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 12/46
case 4: System.out.print(" Right Choice");
break;
default: System.out.print("WRONG");
break;
}
// TODO Auto-generated method stub
}
}
OUTPUT:
======
nter the value of integer
545565667
You entered the integer 545565667
WRONG
OUTPUT:
=======
Enter the value of integer
4
You entered the integer 4
Right Choice
FOR LOOP:
========
package string_packg;
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 13/46
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 14/46
b=--i+--i+--i;
System.out.print(a);
System.out.print(b);
// TODO Auto-generated method stub
}
}
O/P:
===
24 24
Why do we go for while and do- while loop when we know that for loop is easier?
A: When we do no t know the no of iterations that have to be done we go for while or do while
loop
Difference between while and dop while loop ?
While checks the condition and then executes whereas do-while executes aleast once even though
the condition is not satisfied.
ARRAYS:
======
String parkingspace= {"Car111","Car 222"," car333"};
String parkingspace [5]; THIS IS INVALID
String [] s = new String [5];
array.length gives the no of elements in the array.
Array index always starts from zero.
-----------------------------------------------------------------------------------------------------------------------------------
Activity1:
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 15/46
==========
take input from the user for no of subjects and then find the average marks of the marks entered
for the no of subjects chosen.
package string_packg;
import java.util.Scanner;
import java.io.*;
public class CalculateaverageArray {
/**
* @param args
*/
public static void main(String[] args) {
Scanner objt = new Scanner(System.in);
int i,n , sum=0, temp;
System.out.print("Enter the no of elements in the array");
n= objt.nextInt();
System.out.print(" You have entered the no of elements in the array");
int []a = new int [n];
System.out.print("Enter the marks");
for ( i=0; i<n; i++)
{
a[i]= objt.nextInt();
temp = a[i];
sum= sum +temp;
}
System.out.print(sum/n);
// TODO Auto-generated method stub
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 16/46
}
}
OUTPUT:
=======
Enter the no of elements in the array 3
You have entered the no of elements in the arrayEnter the marks34
43
100
59
Array out of bound -TRYING TO STORE MORE ELEMENTS THAN THE ARRAY7 C AN ACCOMODATE.
mETHODS:-
========
Methods define the behavior of a class.
Class A
{
int a=20;
String name ="VIT";
Sum() // Method
{
s= 1+2+3+4+5;
Syso(s);----------------------------------> System.out.print( a+ " "+b); Here value of a is printed and then
value of b is printed after a space mark.
------------------------------------------> System.out.print( " " +a+b); Here value of a is printed followed by
b without space
------------------------------------------->System.out.print( a+b) this will sum up thwe values of a and b and
print it.
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 17/46
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 18/46
AREA OF A RECTANGLE- COMPUTE IN THE CLASS RECTANGLE AND DISPLAY IT IN THE CLASS THAT
CONTAINS THE MAIN FUNCTION:
----------------------------------------------------------------------------------------------------------------
==================================================================================
==============================
(A) package string_packg;
import java.util.Scanner;
public class RECTANGLE
{ void entries()
{int l, b, area;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the value of breadth");
b = obj.nextInt();
System.out.println("Enter the value of length");
l = obj.nextInt();
area = l* b;
System.out.print(" The area of the rectangle:" +area);
}
}
(B) package string_packg;
public class Mainer {
/**
* @param args
*/
public static void main(String[] args) {
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 19/46
// TODO Auto-generated method stub
RECTANGLE obj = new RECTANGLE();
obj.entries();
}
}
OUTPUT:-
======
Enter the value of breadth
2
Enter the value of length
3
The area of the rectangle:6
Area of a triangle
==================
package string_packg;
import java.util.Scanner;
public class Triangle {
int a,b,c,s; double area;
void Area()
{
System.out.print("Enter the dimensions of the triangle");
Scanner obj = new Scanner(System.in);
a= obj.nextInt();
b= obj.nextInt();
c= obj.nextInt();
if(a>b+c||b>c+a||c>b+a )
{
System.out.print("Triangle does not exist");
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 20/46
}
else
{s= (a+b+c)/2;
area= Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.print("Area of the triangle is "+area);
}
}
}
InClassStaticMethodCall:
========================
package staticmethodinclasscall;
public class Class1 {
public static void main(String[] args) {
greet("Java teacher");
}
public static void greet(String someperson)
{System.out.println("Hello"+someperson+"!");
}
}
output:
++++++
HelloJava teacher!
PASSING BY VALUE:
=================
STRING ARGS[] of MAIN METHOD:
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 21/46
=============================
package staticmethodinclasscall;
public class StringArguments {
public static void main(String[] args) {
int j=0;
while (j<2)
{
System.out.println(args[j]);
j++;
}
}
}
CODE TO SHOW THAT STATIC BLOCK OF CODE GETS EXE4CUTED EVEN BEFORE THE MAIN
METHOD:(INITIALIZATION IS DIONE BEFORE STAIC BLK EXECUTION)
=================================================================================
package newpackage;
public class Statvaraccess {
public static int a=5;
public static int b;
static void display()
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 22/46
{
System.out.print("You just called a static method");
}
static{
b=a*8;
System.out.print(b);
System.out.print("this is the first satic block of code");
}
public void method()
{
System.out.print("this is not a static block");
}
static
{
System.out.print("This is the second static block of code");
}
public static void main(String[] args) {
Statvaraccess obj = new Statvaraccess();
System.out.println(Statvaraccess.a);-------->(2)
System.out.println(obj.b);-------->(1)
obj.method();
display();
}
OUTPUT:
40this is the first satic block of codeThis is the second static block of code5
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 23/46
40
this is not a static blockYou just called a static method
======
NOTES=||
======
(1) tHERE IS NO NECESSITY TO CREATE AN OBJECT TO ACCESS A STATIC VARIABLE INSIDE THE SAME
CLASS
IN LIEU IT CAN BE SIMPLY WRITTEN AS
System.out.println(b);
(2) ALSO, WE REQUIRE CLASSNAME.VARIABLE NAME ONLY WHEN WE TRY TIO ACCESS THE STAT VAR
IN SOME OTHER CK
CLASS.SO, WRITE IT DIRECTLY AS
System.out.println(a);
(3) static block of code is called even befor ethe main method is called.
FUNCTIONING O F A STATIC VARIABLE
=================================
package newpackage;
public class Animal {
static int animalcount=0;
public Animal()
{
animalcount+=1;
}
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 24/46
public static void main(String[] args) {
System.out.println(" The no of animals are"+""+animalcount);// note had the
variable animalcount BEEN NON-STATIC THEN WE NEED AN INSTANCE ( REFERENCE VARIABLE;
OBJECT)
new Animal();//---> When the JVM goes for garbage collection it actually looks for
objects without refernce variable so these 3 objects are chosen for garbage collection
new Animal();----^
new Animal();----|
System.out.println(" The no of animals are"+""+animalcount);
}
}
OUTPUT:
=======
The no of animals are0
The no of animals are3
THERE CAN ONLY BE ONE PUBLIC CLASS INSIDE A .jAVA FILE.and that has the same name as the dot
java file.
Extends
======= One class extending another
+++++++++++++++++++++++++++
package newpackage;
public class EXTENSION extends ClassExtends{
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 25/46
int s,t; // 2 non-static vars
public static void main(String[] args)
{
EXTENSION obj = new EXTENSION ();
/*instantiation: note here
to access the attributes and methods
of the PARENT class the object of
CHILD class
will suffice*/
obj.s =a+6;
obj.t= obj.b +5;
somemethod();
obj.somemethod();
/*WARNING: A static method of the parent class
+++++++ can be accessed simply by name
instead of creating an obj
to do the same*/
obj.met2();
}
}
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 26/46
class ClassExtends {
static int a=6; int b=7;// variable a is static and b NON-static
static void somemethod()// static method
{
System.out.print("Sample statement");
}
void met2()// NON-static mthd
{
System.out.print(" Yet another sample");
}
}
OUTPUT:
======
Sample statementSample statement Yet another sample
CANNOT ASSIGN A FINAL VARIABLE:
==================================
package newpackage;
public class FinalKeyWord {
final static int a =5;
public static void main(String[] args) {
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 27/46
//void bullshit() cannot create a method inside main method
++++++
{ a=9;// +ERROR+// Also, note that inside main you cannot declare static but FINAL
IS POSSIBLE.
++++++
}
}
}
FINAL METHOD:
============= THIS METHOD CAN BE INHERITED BUT CANNOT BE OVERriddEN.
OVERRIDE: SAME SIGNATURE BUT DIFFERENT RETURN TYPE.
OVERLOADING : DONE DURING COMPILE TIME
OVERRIDING AND POLYMORPHISM IS DONE DURING RUN TIME
OVERRIDING:
==========
package newpackage;
public class FInal extends B {
void Method1()
{
System.out.println("I am Lucky");
}
public static void main(String[] args) {
FInal obj = new FInal();
obj.Method1();// This object is going to call the method from the child class rather
than the parent class this is a property called overriding
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 28/46
}
}
class B
{
void Method1()
{
System.out.println("Let us override!!!");
}
}
output:
======
I am Lucky
Now see this magic !!!! Mudi Magix!!!!
package newpackage;+
public class FInal extends B {
void Method1()
{
System.out.println("I am Lucky");
}
public static void main(String[] args) {
FInal obj = new FInal();
obj.Method1();// YOU CANNOT BULLSHIT THIS TO OVERRIDE BECAUSE IT (method1)
IS MADE FINAL IN THE PARENT CLASS
// lOOK FOR IT YOURSELF HA HA HA !!! |
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 29/46
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 30/46
========================================
Source -----> Generate getters and setters.
Default value for string is null.
If you try to get before set then null value is returned .
The default value remains even after setting a new value.
When you set a value to the private variable using a set method then the object stores the newly set
value
but ther degfault values will remain in the blueprint of the class.effectively one cannot actually
change the
value of a private variable.
+++++++
CODE====
++++++++
package newpackage;
class GetterSetter {
private String name = "Akshay";
private String regNo = "10bec0004";
//Getter
public String getName()
{
return name;
}
public String getRegNo()
{
return regNo;
}
//sETTER
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 31/46
public void setName( String n)
{
name = n;
}
public void setRegNo(String r){
regNo=r;
}
//bsilfo-BYTE SHORT INT LONG FLOAT DOUBLE
}
public class Getter_Setter {
public static void main(String[] args) {
GetterSetter obj = new GetterSetter();
obj.setName("Bhargav");
obj.setRegNo("iitmee4556");
System.out.println(obj.getName());
System.out.println(obj.getRegNo());
}
}
output:
======
Bhargav
iitmee4556
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 32/46
long a = 123679891112141518// though the assigned no is withion the range of a long var. it throws
an error!!
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[SOME BULLSHIT PROBLEM WITH THE COMPILER!!!!!]]]]]]]]]
sO,kindly excuse them for the inconvenience caused and do as follows( Not break the System!!)
long a = 123679891112141518L;//Now it recognizes this as a long variable and not as "int" as it had
done so previously.
Similarly for "float" write explicitly as "numer"f otherwise it considers it tp be double.
Inheritance allows you to extend an existing class to make a more splzed class.
Allows us to reuse the existing code.All classes in Java inherit from the object class.
constructors are not inherited'.
SUPER AND THIS:
==============
iNHERITANCE IS DESCRIOBED AS "IS-A" RELATIONSHIP. e.g; Dog is an Animal.
Aggregation HAS-A relationship.
Object class is the root class of asll classes.
Extends keyword is used for inhritance.
Multilevel inheritance:
=======================
Class A{
}
Class B extends A{ }
Class C extends B{} Here the class B is extended to C but not Class A as you may think.
Multiple inheritrance is not supported in Java:
===============================================
A child class having two or more parents is multiple inheritance.tO ACHIEVE THE SAME THEY USE
IONTERFACE IN JAVA.
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 33/46
AN EXAMPLE OF MULTILEVEL INHERITANCE:
====================================
package newpackage;
import java.util.Scanner;
public class MultileV_inheritance extends passerine{
public static void main(String[] args) {
MultileV_inheritance obj = new MultileV_inheritance();
obj.bird();
obj.game_bird();
}
}
class Bird
{
int legs = 2;
static int wings =2;
void bird(){
System.out.println("Aves have many phyla ");
}
}
class passerine extends Bird
{
String game_birds;
Scanner gb = new Scanner(System.in);
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 34/46
void sample_passerine()
{
System.out.println("Sparrow is an example of a passerine bird");
}
void game_bird()
{ System.out.println("Enter the game_bird_instance");
game_birds= gb.nextLine();
System.out.println(game_birds);
}
}
OUTPUT:
======
Aves have many phyla
Enter the game_bird_instance
peacock
peacock
ORDER OF CONSTRUCTOR INVOCATION:
===============================
Notes annu refer madiri;;;;;;
SUPER MATTU THIS:
==================
Notes annu saha refer madiri
package newpackage;
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 35/46
public class DiffofThisAndSuper {
public static void main(String[] args) {
Animalia a = new Animalia();
a.eat();
Dog d = new Dog();
d.eat();
d. anotherEssen();
}
}
class Animalia
{
int i =90;
void eat()
{
System.out.println("animal:eat");
}
}
class Dog extends Animalia
{
int i= 45;
void eat()
{
System.out.println("dOGGY:ISST:dEUTSCH");
}
void anotherEssen()
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 36/46
{
this.eat();
super.eat();
System.out.println(this.i);
}
}
output:
++==++
animal:eat
dOGGY:ISST:dEUTSCH
dOGGY:ISST:dEUTSCH
animal:eat
45
Say, a class B extends class A , then , there exists automatically a super constructor in the default
constructor of class B -
But, if we explicitly write a parametrized constr. then the compiler does not create a default constr
and so ,there is no super() thus
creating a problem on extending class A.
Access modifier can be made broader when iot is narrow in the parent class.
A Jugglery of access modifiers:
==============================
package string_packg;
class ConstrSuperThis extends MotherClass {
public void drive()// if this is private and the parent class mthd is public then error again because you
cannot modify the parent class access modifier
{
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 37/46
System.out.println("Hi! I am a KILLER whale!!??!!:) ");
}
public static void main(String[] args) {
ConstrSuperThis obj = new ConstrSuperThis ();
obj.drive();
MotherClass obj1= new MotherClass();
obj1.drive();//error
MotherClass obj2= new ConstrSuperThis();
obj2.drive(); // error parent class mthsd not visible
}
}
class MotherClass
{
private void drive()
{
System.out.println("Hi! I am a beluga whale!!");
}
}
package string_packg;
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 38/46
class ConstrSuperThis extends MotherClass {
public void drive()
{
System.out.println("Hi! I am a KILLER whale!!??!!:) ");
}
public static void main(String[] args) {
ConstrSuperThis obj = new ConstrSuperThis ();
obj.drive();
MotherClass obj1= new MotherClass();
obj1.drive();
MotherClass obj2= new ConstrSuperThis();
obj2.drive();
}
}
class MotherClass
{
void drive()
{
System.out.println("Hi! I am a beluga whale!!");
}
}
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 39/46
o/p
===
Hi! I am a KILLER whale!!??!!:)
Hi! I am a beluga whale!!
Hi! I am a KILLER whale!!??!!:)
fOR I NTERFACE CLASSES WHEN WE PLAN TO IMPLEMENT SOME OF THE METHIODS OF THE
INTERFACE CLASS IN
THE NEXT CLASS TRHEN IT should BE DONE FULLY IN THAT CLASS.
ABSTRACT CLASS CANNOT BE INSTANTIATED.
iF WE TRY TO IMPLEMENT THE METHOD OF AN ABSTRACT CLASS IN THE NEXT CLASS AND SOMEMETHOD OF THE PARENT ABSTR CLASS IS STILL UNIMPLEMENTED THEN THE CHILDCLASS IS ONCE
AGAIN ABSTR.
oH! AN INTERFACE CLASS IS A PURE ABSTRACT CLASS
INTERFACE VARIABLES ARE BY DEFAULT PUBLIC STATIC FINAL.
==================================================================================
==================================================================================
====================
+++++++++++++++++++***************************************************************
++++++++++++++++++++++++++++++++++++++++++***********************************+++++
++++++++++++++++++++
JUNIT TESTING:
=\=*-+/-**/+*-
package string_packg;
public class JunitTesting {
int l;
int m;
public JunitTesting ()
{
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 40/46
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 41/46
obj.mul(a, b);
}
}
package string_packg;
import junit.framework.TestCase;
public class JunitTestingTest extends TestCase {
public JunitTesting v;
protected void setUp() throws Exception {
JunitTesting v = new JunitTesting();
v.l=5;
v.m= 9;
}
public void testAdd() {
assertEquals("Sorry valyue does not match",14,v.add(v.l,v.m));// 14 is the expected
value.
}// a.l and a.m are actual values
/* public void testSub() {
fail("Not yet implemented");
}
public void testMul() {
fail("Not yet implemented");
}*/
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 42/46
}
TRY CATCH FINALLY:
==================
package string_packg;
public class TryAndCatch_Without {
public static void main(String[] args) {
int a=10;
int b=5;
int c=5;
int x,y;
try{
x=a/(b-c);
}catch(ArithmeticException ex)
{
System.out.println("Divide by zero");
}
y=a/(b+c);
System.out.println("y="+ y);
}
}
O/P:
===
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 43/46
Divide by zero
y=1
NESTED TRY CATCH:
================
package string_packg;
import java.util.InputMismatchException;
public class NestedTryPrint {
public static void main(String[] args) {
try
{
System.out.println("Outer Try block atarts");
try{
System.out.println("Inner Try block starts");
int res=5/0;
}catch(InputMismatchException e){
System.out.println("InputMismatchException caught");/*HERE THERE IS NO CATCH BLOCK
THAT CATCHES ARITHMETIC EXCEPTION SO IT GOES TO INNER FINAL OF FINALLY
}finally
{
System.out.println("inner final");
}}catch(ArithmeticException e)
{System.out.println("Arithmetic exception caught");
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 44/46
} finally
{
System.out.println("Outer finally");
}
}
}
O/P:
====
Outer Try block atarts
Inner Try block starts
inner final
Arithmetic exception caught
Outer finally
never goes to inner try block becos it doesn ot get a chance
==================================================================
package string_packg;
import java.util.InputMismatchException;
public class NestedTryPrint {
public static void main(String[] args) {
try
{
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 45/46
System.out.println("Outer Try block atarts");
int R= 6/0;
try{
System.out.println("Inner Try block starts");
int res=5/0;
}catch(InputMismatchException e){
System.out.println("InputMismatchException caught");
}finally
{
System.out.println("inner final");
}}catch(ArithmeticException e)
{System.out.println("Arithmetic exception caught");
} finally
{
System.out.println("Outer finally");
}
}
}
o/p:
====
Outer Try block atarts
Arithmetic exception caught
Outer finally
oops concepts:
============
8/13/2019 All Java Programs
http://slidepdf.com/reader/full/all-java-programs 46/46
Classes
Encapsulation
Inheritance
Association(GENLN, SPL.N && COMPOSITION AND AGGREGATION)
Interface Interface a{}
Implemet the methods in as ubclass as sollows
ClASS B IMPLEMENTS A
{
}AND NOTE THAT THE MTD IMPL SHUD BE COMPLETE.
Polymorphism( METHIDS CAN BE OVERLOADED AND OVERRIDDEN)