II B.Tech II Sem CSE Java Lab Exercise – 8 (Runtime Polymorphism)

a)Runtime Polymorphism

Aim: To write a JAVA program that implements Runtime polymorphism


Description:



Program:

class A 
{
void display() 
{
System.out.println("Inside A class"); 
}
}
class B extends A 
{
void display() 
{
System.out.println("Inside B class"); 
}
}
class C extends A 
{
void display() 
{
System.out.println("Inside C class"); 
}
}
class runtimedemo 
{
public static void main(String args[]) 
{
A a1=new A(); 
B b1=new B(); 
C c1=new C(); 
A ref;
ref=c1; 
ref.display(); 
ref=b1; 
ref.display(); 
ref=a1; 
ref.display();
}
}

Output:

netaji gandi Tuesday, January 7, 2020
II B.Tech II Sem CSE Java Lab Exercise - 7b (Exception)

b) Illustrating multiple catch classes

Aim:To write a JAVA program Illustrating Multiple catch clauses

Description:


Program:

class multitrydemo 
{
public static void main(String args[]) 
{
try 
{
int a=10,b=5; 
int c=a/b;
int d[]={0,1}; 
System.out.println(d[10]); 
System.out.println(c);
}
catch(ArithmeticException e) 
{
System.out.println(e); 
}
catch(ArrayIndexOutOfBoundsException e) 
{
System.out.println(e); 
}
System.out.println("After the catch statement");
}
}

Output:


netaji gandi
II B.Tech II Sem CSE Java Lab Exercise - 7a (Exception)

a) Exception handling mechanism

Aim: To write a JAVA program that describes exception handling mechanism


Description:


Program:

Usage of Exception Handling:

class trydemo 
{
public static void main(String args[]) 
{
try 
{
int a=10,b=0;
int c=a/b;
System.out.println(c);
}
catch(ArithmeticException e) 
{
System.out.println(e); 
}
System.out.println("After the catch statement"); 
}
}

Output:



netaji gandi Monday, January 6, 2020
II B.Tech II Sem CSE Java Exercise - 6 b(Inheritance - Continued)

b) Implementing interface

Aim:To write a JAVA program to implement Interface.

Description:


Program:

(i) First form of interface implementation

interface A 
{
void display(); 
}
class B implements A 
{
public void display() 
{
System.out.println("B's method"); 
}
}
class C extends B 
{
public void callme() 
{
System.out.println("C's method"); 
}
}
class interfacedemo 
{
public static void main(String args[]) 
{
C c1=new C(); 
c1.display(); 
c1.callme();

}
Output:

(ii) Second form of interface implementation
interface D 
{
void display(); 
}
interface E extends D 
{
void show(); 
}
class A 
{
void callme() 
{
System.out.println("This is in callme method"); 
}
}
class B extends A implements E 
{
public void display() 
{
System.out.println("This is in display method"); 
}
public void show() 
{
System.out.println("This is in show method"); 
}
}
class C extends B 
{
void call() 
{
System.out.println("This is in call method"); 
}
}
class interfacedemo1 
{
public static void main(String args[]) 
{
C c1=new C(); 
c1.display(); 
c1.show(); 
c1.callme(); 
c1.call();

}

Output:



(iii) Third form of interface implementation

interface A
{
void display();
}
class B implements A 
{
public void display() 
{
System.out.println("This is in B's method"); 
}
}
class C implements A 
{
public void display() 
{
System.out.println("This is C's method"); 
}
}
class interfacedemo2 
{
public static void main(String args[]) 
{
B b1=new B(); 
C c1=new C(); 
b1.display(); 
c1.display();
}
}

Output:

(iv) Fourth form of interface implementation

interface A
{
void display(); 
}
interface B 
{
void callme(); 
}
interface C extends A,B 
{
void call(); 
}
class D implements C 
{
public void display() 
{
System.out.println("interface A"); 
}
public void callme() 
{
System.out.println("interface B"); 
}
public void call() 
{
System.out.println("interface C"); 
}
}
class interfacedemo3
{
public static void main(String args[]) 
{
D d1=new D(); 
d1.display(); 
d1.callme(); 
d1.call();
}

}

Output:



netaji gandi
II B.Tech II Sem CSE Java Exercise - 6 a(Inheritance - Continued)

a)super keyword implementation

Aim:  Write a JAVA program give example for “super” keyword

Description:


Program:

(i)Using super to call super class constructor (Without parameters)

class A 
{
int l,b; 
A()
{
l=10;
b=20; 
}
}
class B extends A 
{
int h; 
B()
{
super();
h=30; 
}
int volume() 
{
return l*b*h; 
}
}
class superdemo 
{
public static void main(String args[]) 
{
B b1=new B();
int r=b1.volume(); 
System.out.println("The vol. is: "+r);
}
}
Output:



(ii)Using super to call super class constructor (With parameters)

class A 
{
int l,b;
A(int u,int v) 
{
l=u;
b=v; 
}
}
class B extends A 
{
int h;
B(int u,int v,int w) 
{
super(u,v);
h=w; 
}
int volume() 
{
return l*b*h; 
}
}
class superdemo1 
{
public static void main(String args[])
{
B b1=new B(30,20,30);
int r=b1.volume(); 
System.out.println("The vol. is: "+r);
    }
}

Output:





netaji gandi
II B.Tech II Sem CSE Java Lab Exercise - 5c (Inheritance)

c)Abstract Class

Aim: To write a java program for abstract class to find areas of different shapes

Description:



Program:

abstract class shape
{
abstract double area();
}
class rectangle extends shape
{
double l=12.5,b=2.5;
double area()
{
return l*b;
}
}
class triangle extends shape
{
double b=4.2,h=6.5;
double area()
{
return 0.5*b*h;
}
}
class square extends shape
{
double s=6.5; double area()
{
return 4*s;
}
}
class shapedemo
{
public static void main(String[] args)
{
rectangle r1=new rectangle();
triangle t1=new triangle();
square s1=new square();
System.out.println("The area of rectangle is: "+r1.area());
System.out.println("The area of triangle is: "+t1.area());
System.out.println("The area of square is: "+s1.area());
}

}

Output:


netaji gandi
II B.Tech II Sem CSE Java Lab Exercise - 5b (Inheritance)

b)Multi level Inheritance

Aim: To write a JAVA program to implement multi level Inheritance

Description:


Program:

class Car
{
   public Car()
   {
System.out.println("Class Car");
   }
   public void vehicleType()
   {
System.out.println("Vehicle Type: Car");
   }
}
class Maruti extends Car
{
   public Maruti()
   {
System.out.println("Class Maruti");
   }
   public void brand()
   {
System.out.println("Brand: Maruti");
   }
   public void speed()
   {
System.out.println("Max: 90Kmph");
   }
}
public class Maruti800 extends Maruti
{

   public Maruti800()
   {
System.out.println("Maruti Model: 800");
   }
   public void speed()
   {
System.out.println("Max: 80Kmph");
   }
   public static void main(String args[])
   {
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
   }
}

Output:

netaji gandi
II B.Tech II Sem CSE Java Lab Exercise - 5a (Inheritance)

a)Implementing Single Inheritance

Aim: To write a JAVA program to implement Single Inheritance

Description:




Program:

class Shape 
{
    int length;
    int breadth;
}
class Rectangle extends Shape
{
    int area;
    public void calcualteArea()
    {
        area = length*breadth;
    }
    public static void main(String args[])
    {
        Rectangle r = new Rectangle();
        r.length = 10;
        r.breadth = 20;
        r.calcualteArea();
        System.out.println("The Area of rectangle of length \""
                +r.length+"\" and breadth \""+r.breadth+"\" is \""+r.area+"\"");
    }
}

Output:


netaji gandi

NPTEL Programming in Java Jan 2024 Week 11

  Week 11 : Programming Assignment 1 Due on 2024-04-11, 23:59 IST The following code is missing some information needed to run the code. Add...