Java Exception Handling

/ * 

Create a class Student with attributes roll no, name, age and course. Initialize values through parameterized constructor. If age of student is not in between 15 and 21 then generate user-defined exception “AgeNotWithinRangeException”. If name contains numbers or special symbols raise exception “NameNotValidException”. Define the two exception classes.

*/

import java.lang.Exception;
class AgeNotWithinRange extends Exception{
String msg; AgeNotWithinRange(String m) { 
this.msg = m; 
}
public String toString(){ 
return this.getClass().getName()+":"+msg;
}
}

class NameNotValidException extends Exception{
String msg; NameNotValidException(String m) { 
this.msg = m; 
}
public String toString(){ 
return this.getClass().getName()+":"+msg;
}
}

class Student{

 
int rollno;
int age;
String name;
String course;

Student(int r,String n, int a, String c) throws AgeNotWithinRange,NameNotValidException{ 
if(a < 15 || a > 21){  
throw new AgeNotWithinRange(String.valueOf(a)); 
for(int i = 0;i < n.length();i++){  
if(!(Character.isLetter(n.charAt(i)))){      
throw new NameNotValidException(String.valueOf(i));  
this.rollno = r; 
this.name = n; 
this.age = a;
this.course = c; 
System.out.println("Object Created successfully.");
}

 public static void main(String args[]){

 
try{   
Student s1 = new Student(Integer.parseInt(args[0]),args[1],Integer.parseInt(args[2]),args[3]);
          }
catch(AgeNotWithinRange abc){ 
System.out.println(abc);  
}
catch(NameNotValidException def){ 
System.out.println(def);  
}
finally{  
System.out.println("Congratulation now you are UG.");
}

 
try{   
Student s2 = new Student(Integer.parseInt(args[4]),args[5],Integer.parseInt(args[6]),args[7]);
 }
catch(AgeNotWithinRange abc){ 
System.out.println(abc);  
}
catch(NameNotValidException def){ 
System.out.println(def);  
}
finally{  
System.out.println("Congratulation now you are PG.");
} 
}
}

Developed at : MCA - 3 (A), Department of MCA, B H Gardi College, Rajkot

Example of Multiple Inheritance in JAVA

abstract class A
{
            {
            System.out.println("Abstract Class Object is Created");
            }
           
abstract void display();
}

interface T
{
int i=10;
void print();
}

class B extends A implements T
{

void display()
            {
            System.out.println("Display from Derived Class");
            }

public void print()
            {
            System.out.println("Interface Implementation");
            }

public static void main(String s[])
            {
            B b=new B();
            b.display();
            b.print();
            System.out.println(T.i);
            }

}

Shadow Variable Concept

class A
{
int k; 
            {
            System.out.println("Super Class Object is Created");
            }

            A(int j)
            {
            this.k=j;
            System.out.println("Super Class Object is Created with Value K= "+k);
            }
}

class B extends A
{
int k; //shadow variable

            {
            System.out.println("Derived Class Object is Created");
            }
           
            B(int i,int j)
            {
            super(i);
            this.k=j;
            System.out.println("Base Value = "+ super.k+" Derived Value = "+k);
            }
public static void main(String a[])
            {
            System.out.println("Test - 1 : Constructor");
            B b2=new B(20,30);
            }
}

OUTPUT:
E:\java\2015\Lecture\Lecture-2\Shadow Variable>javac A.java

E:\java\2015\Lecture\Lecture-2\Shadow Variable>java B
Test - 1 : Constructor
Super Class Object is Created
Super Class Object is Created with Value K= 20
Derived Class Object is Created

Base Value = 20 Derived Value = 30

Single Inheritance and Super Keyword

class A
{
int k;
            A()
            {
            System.out.println("Super Class Object is Created");
            }
            A(int j)
            {
            this.k=j;
            System.out.println("Super Class Object is Created with Value K= "+this.k);
            }
}

class B extends A
{
int val;
            B()
            {
            System.out.println("Derived Class Object is Created");
            }
            B(int i)
            {
            this.val=i;
            System.out.println("Derived Class Object is Created with Value = "+this.val);
            }
            B(int i,int j)
            {
            super(i);
            this.val=j;
            System.out.println("Derived Class Object is Created with Value = "+this.val);
            }
public static void main(String a[])
            {
            System.out.println("Test - 1 : Default Constructor");
            B b=new B();
            System.out.println();
            System.out.println("Test - 2 : Parameterized - Default Constructor");
            B b1=new B(10);
            System.out.println();
            System.out.println("Test - 3 : Parameterized - Parameterized Constructor");
            B b2=new B(20,30);
            }
}

OUTPUT:
E:\java\2015\Lecture\Lecture-2>javac A.java

E:\java\2015\Lecture\Lecture-2>java B
Test - 1 : Default Constructor
Super Class Object is Created
Derived Class Object is Created

Test - 2 : Parameterized - Default Constructor
Super Class Object is Created
Derived Class Object is Created with Value = 10

Test - 3 : Parameterized - Parameterized Constructor
Super Class Object is Created with Value K= 20

Derived Class Object is Created with Value = 30

LAB - 4 Object Conversion in JAVA

//Fixed Class Given in Package
class Dollar
{
int dol;

Dollar()
{
this.dol=5;
}

Dollar(int i)
{
this.dol=5;
}
void printDollar()
{
System.out.println(" Dollar : "+this.dol);
}
}

//Newly Designed Class to Implement Conversion
class INR
{
int rs;
INR()
{
this.rs=600;
}
//Convert Dollar Object into INR Object
INR(Dollar d)
{
this.rs=d.dol * 60;
}

//Create Dollar Object from INR Object
Dollar convert()
{
return new Dollar(this.rs / 60 );
}

void printINR()
{
System.out.println(" INR = "+this.rs);
}

public static void main(String args[])
{
Dollar d1 = new Dollar();
d1.printDollar();
INR i1 = new INR(d1);
i1.printINR();

INR i2 = new INR();
i2.printINR();
Dollar d2 = i2.convert();
d2.printDollar();
}
}

LAB - 3 Revision LAB

JAVA LAB Program

1) Revise Concept of Static and Non-Static Properties of Class

2) Revise Concept of Initializer and Class Initializer Block

3) Revise Concept of "this".

4) Revise Concept of Constructor Overloading

Program:
Create Class Book with attributes like bookid, bookname, authorname, price etc.

Implement some methods like printBookInfo, printPrice…etc using appropriate parameter and return value.

Write 3 Constructors and implement fundamentals of Overloading.

Write one methods which counts no of objects which are created in program and print it.

LAB - 2 Initializer Block in JAVA

class Test
{
static
{
System.out.println("From Class Initializer Block");
}

{
System.out.println("From Initializer Block");
}

Test()
{
System.out.println("From Constructor");
}

public static void main(String a[])
{
System.out.println("START of MAIN");
Test t = new Test();
System.out.println("END of MAIN");
}
}

//OUTPUT
//E:\java\2015\LAB-2>java Test
//From Class Intializer Block
//START of MAIN
//From Intializer Block
//From Constructor
//END of MAIN

LAB - 1 JAVA Basic Fundamentals

JAVA LAB-1 Content


1) Basic Java Program "Hello World"

2) How to access normal Variable and Static Variable?

3) How to access Normal Function and Static Function?

4) How to add multiple Class in same program?

Common Program


class C
{
//Class without main()
static int k=20;
}

class A
{
//static variable can be accessed with Class Name
static int i=10;

//Normal Variable
int j=15;

public static void main(String args[])
{
//Print First Argument
System.out.println("String - 1 "+args[0]);
// How to Concatenate multiple string using +
                System.out.println(" i = " + i + "  String - 2" +args[1]);
}
}

class B
{
public static void main(String args[])
{
//Accessing variable of Class A using anonymous Object
System.out.println(" J = "+new A().j);

//Declaring Array of String
String s[]={"abc","def"};

//Calling main function of Class A with Parameter
A.main(s);

//Accessing static variable of Class C 
System.out.println(" C = "+C.k);
}
}

class D
{
public static void main(String args[])
{
//Calling main()
main();
}

//Overloading main()
public static int main()
{
System.out.println("Class D");
return 0;
}
}

class E
{
public static void main(String args[])
{
//Calling All main()
A.main(new String[2]);
B.main(new String[2]);
D.main(new String[2]);
}
}

How to Run & Compile JAVA Program ?


Compile :

Filepath > javac filename.java

Run :

Filepath > java classfilename