Maharshi Dayanand University
COLLEGE NAME
BCA 310
Submitted To: Submitted By
Reg. No
1
INDEX
P.NO. PROGRAM Date Remarks
1 Write a PROGRAM to print your name in java.
2 Write a PROGRAM to implement constructors.
3 Write a PROGRAM to implement method overloading.
4 Write a PROGRAM to implement method overriding.
5 Write a PROGRAM to print mark sheet using scanner class.
6 Write a PROGRAM to pass arguments with a. no need of this
keyword b. using this keyword
7 Write a PROGRAM to converting local variable into instance/
static variable.
8 Write a PROGRAM to implement inheritance.
9 Write a PROGRAM to implement super keyword.
10 Write a PROGRAM to implement abstract keyword.
11 Write a PROGRAM to implement encapsulation.
12 Write a PROGRAM to implement final keyword.
13 Write a PROGRAM to implement garbage collection.
14 Write a PROGRAM to implement interface in java.
15 Write a PROGRAM to handle exception in java using try-
catch.
16 Write a PROGRAM to handle exception in java using try-
finally.
17 Write a PROGRAM to handle exception in java using throw.
18 Write a PROGRAM to handle exception in java using throws.
19 Write a PROGRAM to implement multithreading using
extends keyword.
20 Write a PROGRAM to implement runnable interface using
implement keyword.
2
Q1: Write a PROGRAM to print your name in java
class Nafisha
{
public static void main(String[] args)
{
System.out.println("WELCOME TO MY FIRST PROGRAM");
}
}
3
Q2:Write a PROGRAM to implement constructors.
class Test_N
{
void good()
{
System.out.println("DEFAULT CONSTRUCTOR");
}
public static void main(String args[])
{
Test_N t=new Test_N();
t.good();
}
}
4
Q3: Write a PROGRAM to implement method overloading.
class Adder
{
static int add(int a, int b) {
return a+b;
}
static int add(int a, int b, int c) {
return a+b+c;
}
}
class TestOverloading{
public static void main(String args[])
{
System.out.println(Adder.add(12,14));
System.out.println(Adder.add(24,48,123));
}
}
5
Q4: Write a PROGRAM to implement method overriding.
class Parent
{
final void andhra()
{
System.out.println("Andhra Pradesh.....");
}
}
class Child extends Parent
{
void andhra()
{
//super.andhra();
System.out.println("Overriding the method is possible.....");
}
public static void main(String args[])
{
Child c=new Child();
c.andhra();
}
}
6
Q6: Write a PROGRAM to pass arguments with
a. no need of this keyword
b. using this keyword
no need of this keyword
class Test
int a=10;
int b=20;
void add(int i, int j)
System.out.println(a+b);
System.out.println(i+j);
public static void main(String args[])
Test t= new Test();
t.add(100,200);
};
need of this keyword
class Test
int a=10;
int b=20;
7
void add(int a, int b)
System.out.println(a+b);
System.out.println(this.a+this.b);
public static void main(String args[])
Test t= new Test();
t.add(100,200);
};
8
Q7: Write a PROGRAM to converting local variable into instance/
static variable.
class Test
static int i;
int j;
void values(int a,int b)
i=a;
j=b;
void add()
System.out.println(i+j);
void mul()
System.out.println(i*j);
public static void main(String args[])
Test t=new Test();
t.values(100,200);
t.add();
t.mul();
9
10
Q8 : Write a PROGRAM to implement inheritance.
class Bank
int getRateOfIntrest()
return 0;
class SBI extends Bank
int getRateOfIntrest()
return 8;
class ICICI extends Bank
int getRateOfIntrest()
return 7;
class AXIS extends Bank
int getRateOfIntrest()
return 9;
11
class Test2
public static void main(String args[])
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI intrest rate is: "+s.getRateOfIntrest());
System.out.println("ICICI intrest rate is: "+i.getRateOfIntrest());
System.out.println("AXIS intrest rate is: "+a.getRateOfIntrest());
12
Q9 : Write a PROGRAM to implement super keyword.
class Vehicle
void run()
System.out.println("Vehicle is running");
class Bike extends Vehicle
void run()
//super.run(); now only cild class will run by default
super.run();
System.out.println("Bike is running");
public static void main(String args[])
Bike obj=new Bike();
obj.run();
13
Q10 : Write a PROGRAM to implement abstract keyword.
abstract class Test
abstract void m1();
abstract void m2();
abstract void m3();
class AbstractDemo extends Test
void m1()
System.out.println("m1-method");
void m2()
System.out.println("m2-method");
void m3()
System.out.println("m3-method");
public static void main(String ARGS[])
AbstractDemo ad=new AbstractDemo();
ad.m1();
ad.m2();
ad.m3();
};
14
15
Q11 : Write a PROGRAM to implement encapsulation.
public void setSid(int sid)
this.sid=sid;
public int getSid()
return sid;
public void setSname(String sname)
this.sname=sname;
public String getSname()
return sname;
};
class Test5
public static void main(String args[])
Encapsulation e= new Encapsulation();
e.setSid(100);
e.setSname("arun");
int id=e.getSid();
String name=e.getSname();
16
System.out.println(id);
System.out.println(name);
};
17
Q12 : Write a PROGRAM to implement final keyword.
class Parent
void andhra()
System.out.println("Andhra Pradesh.....");
class Child extends Parent
void andhra()
System.out.println("Overriding the method is possible.....");
public static void main(String args[])
Child c=new Child();
c.andhra();
18
Q13 : Write a PROGRAM to implement garbage collection.
class Garbage
public static void main(String args[])
Garbage g1=new Garbage();
Garbage g2=new Garbage();
Garbage g3=new Garbage();
Garbage g4=new Garbage();
System.out.println(g1);
System.out.println(g2);
System.out.println(g3);
System.out.println(g4);
g1=null; //g1 obj is eligible for garbage collection
g2=null; //g2 obj is eligible for garbage collection
System.out.println(g1);
System.out.println(g2);
System.out.println(g3);
System.out.println(g4);
19
Q14 : Write a PROGRAM to implement interface in java.
interface AnimalEat
void eat();
interface AnimalTravel
void travel();
class Animal implements AnimalEat,AnimalTravel
public void eat()
System.out.println("Animal is eating...");
public void travel()
System.out.println("Animal is travelling...");
public class Interface
20
{
public static void main(String args[])
Animal a= new Animal();
a.eat();
a.travel();
21
Q15 : Write a PROGRAM to handle exception in java using try-catch.
public class TryCatch2 {
public static void main(String args[]) {
try {
int data = 50 / 0;
System.out.println("It will give arithematic error ");
} catch (ArithmeticException e) {
System.out.println(e);
System.out.println("REST OF THE CODE......");
22
Q16: Write a PROGRAM to handle exception in java using try-
finally
class Test_NK
public static void main(String[] args)
try
System.out.println("hello”);
System.out.println(10/0);
catch (ArithmeticException ae)
System.out.println("u r getting ae:"+ae);
finally
System.out.println("finally block is always executed");
System.out.println("rest of the code");
23
Q17: Write a PROGRAM to handle exception in java using
throw.
import java.util.*;
class Throw
static void validate(int age)
if (age<18)
throw new ArithmeticException("not elgible for vote..........");
else
System.out.println("welcome to the voting............");
public static void main(String[] args)
Scanner s=new Scanner(System.in);
System.out.println("please enter your age ");
int n=s.nextInt();
validate(n);
System.out.println("Rest of the code.............");
24
Q18: Write a PROGRAM to handle exception in java using
throws.
import java.io.*;
class Throws
void studentDetails()throws IOException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("please enter student name");
String sname=br.readLine();
System.out.println("u r name is:"+sname);
public static void main(String[] args)throws IOException
Throws t1=new Throws();
t1.studentDetails();
25
Q19: Write a PROGRAM to implement multithreading using extends keyword.
class MyThread extends Thread
public void run()
System.out.println("durga");
System.out.println("PROGRAM");
};
class ThreadExtends
public static void main(String[] args)
MyThread t=new MyThread();
t.start();
26
Q20: Write a PROGRAM to implement runnable interface
using implement keyword
class MyThread implements Runnable
public void run()
System.out.println("durga");
System.out.println("PROGRAM");
class ThreadRunnable
public static void main(String[] args)
MyThread obj=new MyThread();
Thread t=new Thread(obj);
t.start();
27