BCA504P – JAVA Programming Lab Manual
Part - A
1. Write a program to find factorial of list of number reading input as
command line argument.
Source Code
public class Factorial
{
public static void main(String args[])
{
int[] arr = new int[10];
int fact;
if(args.length==0)
{
System.out.println("No Command line arguments");
return;
}
for (int i=0; i<args.length;i++)
{
arr[i]=Integer.parseInt(args[i]);
}
for(int i=0;i<args.length;i++)
{
fact=1;
while(arr[i]>0)
{
fact=fact*arr[i];
arr[i]--;
}
System.out.println("Factorial of "+ args[i]+"is :
"+fact);
}
}
}
2. Write a program to display all prime numbers between two limits.
Source Code
class Prime
{
public static void main(String args[])
{
int i,j;
if(args.length<2)
{
System.out.println("No command line Argruments ");
return;
}
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
System.out.println("Prime number between"+num1+"and" +num2+"
are:");
for(i=num1;i<=num2;i++)
{
for(j=2;j<i;j++)
{
int n=i%j;
if(n==0)
{
break;
}
}
if(i==j)
{
System.out.println(" "+i);
}
}
}
}
3. Write a program to sort list of elements in ascending and descending order
and show the exception handling.
Source Code
class Sorting
{
public static void main(String args[])
{
int a[] = new int[5];
try
{
for(int i=0;i<5;i++)
a[i]=Integer.parseInt(args[i]);
System.out.println("Before Sorting\n");
for(int i=0;i<5;i++)
System.out.println(" " + a[i]);
bubbleSort(a,5);
System.out.println("\n\n After Sorting\n");
System.out.println("\n\nAscending order \n");
for(int i=0;i<5;i++)
System.out.print(" "+a[i]);
System.out.println("\n\nDescending order \n");
for(int i=4;i>=0;i--)
System.out.print(" "+a[i]);
}
catch(NumberFormatException e)
{
System.out.println("Enter only integers");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Enter only 5 integers");
private static void bubbleSort(int [] arr, int length)
{
int temp,i,j;
for(i=0;i<length-1;i++)
{
for(j=0;j<length-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
4. Write a program to implement all string operations.
Source code
class StringOperation
{
public static void main(String args[])
{
String s1="Hello";
String s2="World";
System.out.println("The strings are "+s1+"and"+s2);
int len1=s1.length();
int len2=s2.length();
System.out.println("The length of "+s1+" is :"+len1);
System.out.println("The length of "+s2+" is :"+len2);
System.out.println("The concatenation of two strings =
"+s1.concat(s2));
System.out.println("First character of
"+s1+"is="+s1.charAt(0));
System.out.println("The uppercase of
"+s1+"is="+s1.toUpperCase());
System.out.println("The lower case of
"+s2+"is="+s2.toLowerCase());
System.out.println(" the letter e occurs at
position"+s1.indexOf("e")+"in"+s1);
System.out.println("Substring of "+s1+"starting from index 2
and ending at 4 is = "+s1.substring(2,4));
System.out.println("Replacing 'e' with 'o' in "+s1+"is
="+s1.replace('e','o'));
boolean check = s1.equals(s2);
if(check==false)
System.out.println(""+s1+" and "+s2+" are not same");
else
System.out.println("" + s1+" and " + s2+"are same");
5. Write a program to find area of geometrical figures using method.
Source code:
import java.io.*;
class Area
{
public static double circleArea(double r)
{
return Math.PI*r*r;
}
public static double squareArea(double side)
{
return side*side;
}
public static double rectArea(double width, double height)
{
return width*height;
}
public static double triArea(double base, double height1)
{
return 0.5*base*height1;
}
public static String readLine()
{
String input=" ";
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
try
{
input = in.readLine();
}catch(Exception e)
{
System.out.println("Error" + e);
}
return input;
}
public static void main(String args[])
{
System.out.println("Enter the radius");
Double radius=Double.parseDouble(readLine());
System.out.println("Area of circle = " + circleArea(radius));
System.out.println("Enter the side");
Double side=Double.parseDouble(readLine());
System.out.println("Area of square = "+squareArea(side));
System.out.println("Enter the Width");
Double width=Double.parseDouble(readLine());
System.out.println("Enter the height");
Double height=Double.parseDouble(readLine());
System.out.println("Area of Rectangle = " +
rectArea(width,height));
System.out.println("Enter the Base");
Double base=Double.parseDouble(readLine());
System.out.println("Enter the Height");
Double height1=Double.parseDouble(readLine());
System.out.println("Area of traingle
="+triArea(base,height1));
}
6. Write a program to implement constructor overloading by passing different
number of parameter of different types.
Source code
public class Box
{
int length,breadth,height;
Box()
{
length=breadth=height=2;
System.out.println("Intialized with default constructor");
}
Box(int l, int b)
{
length=l;
breadth=b;
height=2;
System.out.println("Initialized with parameterized
constructor having 2 params");
Box(int l, int b, int h)
{
length=l;
breadth=b;
height=h;
System.out.println("Initialized with parameterized
constructor having 3 params");
public int getVolume()
{
return length*breadth*height;
public static void main(String args[])
{
Box box1 = new Box();
System.out.println("The volume of Box 1 is :"+
box1.getVolume());
Box box2 = new Box(10,20);
System.out.println("Volume of Box 2 is :" +
box2.getVolume());
Box box3 = new Box(10,20,30);
System.out.println("Volume of Box 3 is :" +
box3.getVolume());
7. Write a program to create student report using applet, read the input using
text boxes and display the o/p using buttons.
Source code
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="StudentReport.class",width=500 height=500>
</applet>*/
public class StudentReport extends Applet implements ActionListener
{
Label lblTitle,lblRegno,lblCourse,lblSemester,lblSub1, lblSub2;
TextField txtRegno,txtCourse,txtSemester,txtSub1,txtSub2;
Button cmdReport;
String rno="", course="",
sem="",sub1="",sub2="",avg="",heading="";
public void init()
{
GridBagLayout gbag= new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbag);
lblTitle = new Label("Enter Student Details");
lblRegno= new Label("Register Number");
txtRegno=new TextField(25);
lblCourse=new Label("Course Name");
txtCourse=new TextField(25);
lblSemester=new Label("Semester ");
txtSemester=new TextField(25);
lblSub1=new Label("Marks of Subject1");
txtSub1=new TextField(25);
lblSub2=new Label("Marks of Subject2");
txtSub2=new TextField(25);
cmdReport = new Button("View Report");
// Define the grid bag
gbc.weighty=2.0;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor=GridBagConstraints.NORTH;
gbag.setConstraints(lblTitle,gbc);
//Anchor most components to the right
gbc.anchor=GridBagConstraints.EAST;
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblRegno,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtRegno,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblCourse,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtCourse,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblSemester,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtSemester,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblSub1,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtSub1,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblSub2,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtSub2,gbc);
gbc.anchor=GridBagConstraints.CENTER;
gbag.setConstraints(cmdReport,gbc);
add(lblTitle);
add(lblRegno);
add(txtRegno);
add(lblCourse);
add(txtCourse);
add(lblSemester);
add(txtSemester);
add(lblSub1);
add(txtSub1);
add(lblSub2);
add(txtSub2);
add(cmdReport);
cmdReport.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
try{
if(ae.getSource() == cmdReport)
{
rno=txtRegno.getText().trim();
course=txtCourse.getText().trim();
sem=txtSemester.getText().trim();
sub1=txtSub1.getText().trim();
sub2=txtSub2.getText().trim();
avg="Avg Marks:" + ((Integer.parseInt(sub1) +
Integer.parseInt(sub2))/2);
rno="Register No:" + rno;
course="Course :"+ course;
sem="Semester :"+sem;
sub1="Subject1 :"+sub1;
sub2="Subject2 :"+sub2;
heading="Student Report";
removeAll();
showStatus("");
repaint();
}
}catch(NumberFormatException e)
{
showStatus("Invalid Data");
}
}
public void paint(Graphics g)
{
g.drawString(heading,30,30);
g.drawString(rno,30,80);
g.drawString(course,30,100);
g.drawString(sem,30,120);
g.drawString(sub1,30,140);
g.drawString(sub2,30,160);
g.drawString(avg,30,180);
}
8. Write a program to calculate bonus for different departments using method
overriding.
Source code
abstract class Department
{
double salary,bonus,totalsalary;
public abstract void calBonus(double salary);
public void displayTotalSalary(String dept)
{
System.out.println(dept+"\t"+salary+"\t\t"+bonus+"\
t"+totalsalary);
}
}
class Accounts extends Department
{
public void calBonus(double sal)
{
salary = sal;
bonus = sal * 0.2;
totalsalary=salary+bonus;
}
}
class Sales extends Department
{
public void calBonus(double sal)
{
salary = sal;
bonus = sal * 0.3;
totalsalary=salary+bonus;
}
}
public class BonusCalculate
{
public static void main(String args[])
{
Department acc = new Accounts();
Department sales = new Sales();
acc.calBonus(10000);
sales.calBonus(20000);
System.out.println("Department \t Basic Salary \t Bonus \t
Total Salary");
System.out.println("-----------------------------------------------
---------------");
acc.displayTotalSalary("Accounts Dept");
sales.displayTotalSalary("Sales Dept");
System.out.println("-----------------------------------------------
----------------");
}
}
9. Write a program to implement thread priorities.
Source code
class A extends Thread
{
public void run()
{
System.out.println(" Thread A started");
for(int i=1;i<5;i++)
System.out.println(" Thread A : i = "+i);
System.out.println("Exit from Thread A");
}
}
class B extends Thread
{
public void run()
{
System.out.println(" Thread B started");
for(int i=1;i<5;i++)
System.out.println(" Thread B : i = "+i);
System.out.println("Exit from Thread B");
}
}
class C extends Thread
{
public void run()
{
System.out.println(" Thread C started");
for(int i=1;i<5;i++)
System.out.println(" Thread C : i = "+i);
System.out.println("Exit from Thread C");
class ThreadPriority
{
public static void main(String args[])
{
A threadA = new A();
B threadB = new B();
C threadC = new C();
threadA.setPriority(Thread.NORM_PRIORITY);
threadB.setPriority(Thread.MAX_PRIORITY);
threadC.setPriority(Thread.MIN_PRIORITY);
System.out.println("Start Thread A");
threadA.start();
System.out.println("Start Thread B");
threadB.start();
System.out.println("Start Thread C");
threadC.start();
System.out.println("End of main Thread");
}
10. Write a program to implement thread, applets and graphics by
implementing animation of ball moving.
Source code :
import java.awt.*;
import java.applet.*;
/* <applet code="MovingBall.class" height=300 width=300></applet>
*/
public class MovingBall extends Applet implements Runnable
{
int x,y,dx,dy,w,h;
Thread t;
boolean flag;
public void init()
{
w=getWidth();
h=getHeight();
setBackground(Color.yellow);
x=100;
y=10;
dx=10;
dy=10;
}
public void start()
{
flag=true;
t=new Thread(this);
t.start();
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(x,y,50,50);
}
public void run()
{
while(flag)
{
if((x+dx<=0)||(x+dx>=w))
dx=-dx;
if((y+dy<=0)||(y+dy>=h))
dy=-dy;
x+=dx;
y+=dy;
repaint();
try
{
Thread.sleep(300);
}
catch(InterruptedException e)
{}
}
}
public void stop()
{
t=null;
flag=false;
}
}
11.Write a program to implement mouse events.
Source code
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="KeyBoardEvents" width=400 height=400></applet>*/
public class KeyBoardEvents extends Applet implements KeyListener
{
String str="";
public void init()
{
addKeyListener(this);
requestFocus();
public void keyTyped(KeyEvent e)
{
str+=e.getKeyChar();
repaint(0);
}
public void keyPressed(KeyEvent e)
{
showStatus("Key Pressed");
}
public void keyReleased(KeyEvent e)
{
showStatus("Key Released");
}
public void paint(Graphics g)
{
g.drawString(str,15,15);
}
12.Write a program to implement keyboard events.
Source code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="KeyBoardEvents" width=400 height=400></applet>*/
public class KeyBoardEvents extends Applet implements KeyListener
{
String str="";
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyTyped(KeyEvent e)
{
str+=e.getKeyChar();
repaint(0);
public void keyPressed(KeyEvent e)
{
showStatus("Key Pressed");
}
public void keyReleased(KeyEvent e)
{
showStatus("Key Released");
}
public void paint(Graphics g)
{
g.drawString(str,15,15);
}
}
Part – B
13.Program to find the sum of digits of a given number.
import java.io.*;
public class DigitSum
{
public static void main(String args[]) throws
IOException
{
int dig=0, sum=0,num;
BufferedReader br = new BufferedReader (new
InputStreamReader (System.in));
/* To read integer */
System.out.println("Enter number:");
num=Integer.parseInt(br.readLine());
while(num>0)
{
dig=num%10;
sum=sum+dig;
num=num/10;
System.out.println("Sum of digits is : " +sum);
}
}
14.Write a program to demonstrate This Key word.
class ThisDemo
{
/* Declare two instance variables*/
int i,j;
/* Constructor with no arguments */
ThisDemo()
{
this(100);
ThisDemo(int a)
{
this(a,200);
ThisDemo(int i, int j)
{
this.i=i;
this.j=j;
void display()
{
System.out.println(" i = "+i);
System.out.println(" j = "+j);
public static void main(String args[])
{
ThisDemo a1 = new ThisDemo();
a1.display();
}
15. Write a program to demonstrate Multi level inheritance.
class First
{
First()
{
System.out.println("Called zero argument constructor of
First ");
First( int a)
{
System.out.println("Called parameterized constructor of
First ");
}
class Second extends First
{
Second()
{
System.out.println("Called zero argument constructor of
Second ");
Second( int a)
{
System.out.println("Called parameterized constructor of
Second ");
}
class Third extends Second
{
Third()
{
System.out.println("Called zero argument constructor of
Third ");
}
Third( int a)
{
System.out.println("Called parameterized constructor of
Third ");
public static void main(String args[])
{
Third t1 = new Third();
Third t2 = new Third();
}
16.Write a program to copy elements from one array to another.
/* Copy elements from one array to another */
public class CopyArray
{
public static void main(String args[])
{
int [] array1 = {10,20,30,40,50};
int [] array2 = new int[5];
System.out.println("The elements of the Array 1 :");
for(int x : array1)
{
System.out.println(" "+x);
// Copying elements
for(int i=0; i<array1.length;i++)
array2[i]=array1[i];
System.out.println("The elements of the Array 2 :");
for(int x : array2)
{
System.out.println(" "+x);
}
}
17.Write a program to demonstrate command line arguments
public class CommandLine
{
public static void main(String args[])
{
if(args.length>=2)
{
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
int sum=num1+num2;
System.out.println("Sum="+sum);
}
else
{
System.out.println("Insufficient number of command
line arguments");
18.Write a program to demonstrate vector and its operations.
import java.util.*;
public class VectorDemo
{
public static void main(String args[])
{
Vector v = new Vector();
v.add("C");
v.add("c++");
v.add("Java");
v.add("VB6");
System.out.println("Intially the vector
contents"+v.toString());
System.out.println("The last element :"+ v.lastElement());
System.out.println("The Element at Second position :"+
v.elementAt(2));
v.insertElementAt("Python",1);
v.insertElementAt("c#",0);
System.out.println("After inserting contents"+v.toString());
v.removeElementAt(3);
System.out.println("After removing element at 3 the contents
are " + v.toString());
v.setElementAt("c++",1);
v.remove("VB6");
System.out.println(" After removing VB the vector
contents"+v.toString());
}
}
19. Write a program to demonstrate wrapper classes.
public class WrapperDemo
{
public static void main(String args[])
{
int i=100;
Integer i1 = new Integer(i);
Integer i2 = Integer.valueOf("200");
System.out.println("The primitive value of i1 = "
+i1.intValue());
System.out.println("The primitive value of i2 = "
+i2.intValue());
String str="12345";
int num2 = Integer.parseInt(str);
System.out.println("The value of num2 = "+num2);
System.out.println("The string value of i1
="+i1.toString());
System.out.println("The string value of i1
="+i2.toString());
}
}
20.Write a program to implement multiple inheritance using interfaces.
interface XYZ
{
public void functionX();
interface MSD
{
public void functionM();
interface PQR extends XYZ,MSD
{
public void functionP();
class ABC implements PQR
{
public void functionX()
{
System.out.println("Implemeting functionx ");
public void functionM()
{
System.out.println("Implemeting functionM ");
public void functionP()
{
System.out.println("Implemeting function P ");
class InterfaceMulti
{
public static void main(String args[])
{
ABC a1 = new ABC();
a1.functionX();
a1.functionM();
a1.functionP();
****