Spurthy College Of Science And Management Studies
BANGALORE UNIVERSITY
BACHELOR OF COMPUTER APPLICATIONS (BCA)
BCA504P : JAVA PROGRAMMING LAB
PART - A
1. Write a program to find factorial of list of number reading input as command
line argument.
2. Write a program to display all prime numbers between two limits.
3. Write a program to sort list of elements in ascending and descending order and
show the exception handling.
4. Write a program to implement all string operations.
5. Write a program to find area of geometrical figures using method.
6. Write a program to implement constructor overloading by passing different
number of parameter of different types.
7. Write a program to create student report using applet, read the input using text
boxes and display the o/p using buttons.
8. Write a program to calculate bonus for different departments using method
overriding.
9. Write a program to implement thread, applets and graphics by implementing
animation of ball moving.
10. Write a program to implement mouse events and keyboard events.
PART – B
During practical examination the External and Internal examiners may prepare
examquestion paper related to theory syllabus apart from Part-A. (A minimum of
10Programs has to be prepared).
Note :
a) The candidate has to write both the programs One from Part-A and other from
Part-B and execute one program as of External examiner choice.
b) A minimum of 10 Programs has to be done in Part-B and has to be maintained
in
the Practical Record.
c) Scheme of Evaluation is as follows:
Writing two programs - 10 Marks
Execution of one program - 10 Marks
Formatting the Output - 05 Marks
Viva - 05 Marks
Record - 05 Marks
Total - 35 Marks
Dept of BCA Page 1
Spurthy College Of Science And Management Studies
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([Link]==0)
{
[Link]("No Command line arguments");
return;
}
for (int i=0; i<[Link];i++)
{
arr[i]=[Link](args[i]);
}
for(int i=0;i<[Link];i++)
{
fact=1;
while(arr[i]>0)
{
fact=fact*arr[i];
arr[i]--;
}
[Link]("Factorial of "+ args[i]+"is
: "+fact);
}
}
}
Dept of BCA Page 2
Spurthy College Of Science And Management Studies
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([Link]<2)
{
[Link]("No command line Argruments ");
return;
}
int num1=[Link](args[0]);
int num2=[Link](args[1]);
[Link]("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)
{
[Link](" "+i);
}
}
}
3. Write a program to sort list of elements in ascending and descending
order and show the exception handling.
Source Code
class Sorting
Dept of BCA Page 3
Spurthy College Of Science And Management Studies
{
public static void main(String args[])
{
int a[] = new int[5];
try
{
for(int i=0;i<5;i++)
a[i]=[Link](args[i]);
[Link]("Before Sorting\n");
for(int i=0;i<5;i++)
[Link](" " + a[i]);
bubbleSort(a,5);
[Link]("\n\n After Sorting\n");
[Link]("\n\nAscending order \n");
for(int i=0;i<5;i++)
[Link](" "+a[i]);
[Link]("\n\nDescending order \n");
for(int i=4;i>=0;i--)
[Link](" "+a[i]);
}
catch(NumberFormatException e)
{
[Link]("Enter only integers");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("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;
Dept of BCA Page 4
Spurthy College Of Science And Management Studies
}
}
}
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";
[Link]("The strings are "+s1+"and"+s2);
int len1=[Link]();
int len2=[Link]();
[Link]("The length of "+s1+" is :"+len1);
[Link]("The length of "+s2+" is :"+len2);
[Link]("The concatenation of two strings =
"+[Link](s2));
[Link]("First character of
"+s1+"is="+[Link](0));
[Link]("The uppercase of
"+s1+"is="+[Link]());
[Link]("The lower case of
"+s2+"is="+[Link]());
[Link](" the letter e occurs at
position"+[Link]("e")+"in"+s1);
[Link]("Substring of "+s1+"starting from
index 2 and ending at 4 is = "+[Link](2,4));
[Link]("Replacing 'e' with 'o' in "+s1+"is
="+[Link]('e','o'));
boolean check = [Link](s2);
if(check==false)
[Link](""+s1+" and "+s2+" are not
same");
else
[Link]("" + s1+" and " + s2+"are same");
Dept of BCA Page 5
Spurthy College Of Science And Management Studies
5. Write a program to find area of geometrical figures using method.
Source code:
import [Link].*;
class Area
{
public static double circleArea(double r)
{
return [Link]*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([Link]));
try
{
input = [Link]();
}catch(Exception e)
{
[Link]("Error" + e);
}
return input;
}
Dept of BCA Page 6
Spurthy College Of Science And Management Studies
public static void main(String args[])
{
[Link]("Enter the radius");
Double radius=[Link](readLine());
[Link]("Area of circle = " +
circleArea(radius));
[Link]("Enter the side");
Double side=[Link](readLine());
[Link]("Area of square =
"+squareArea(side));
[Link]("Enter the Width");
Double width=[Link](readLine());
[Link]("Enter the height");
Double height=[Link](readLine());
[Link]("Area of Rectangle = " +
rectArea(width,height));
[Link]("Enter the Base");
Double base=[Link](readLine());
[Link]("Enter the Height");
Double height1=[Link](readLine());
[Link]("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;
[Link]("Intialized with default
constructor");
}
Box(int l, int b)
{
length=l;
breadth=b;
Dept of BCA Page 7
Spurthy College Of Science And Management Studies
height=2;
[Link]("Initialized with
parameterized constructor having 2 params");
Box(int l, int b, int h)
{
length=l;
breadth=b;
height=h;
[Link]("Initialized with parameterized
constructor having 3 params");
public int getVolume()
{
return length*breadth*height;
public static void main(String args[])
{
Box box1 = new Box();
[Link]("The volume of Box 1 is :"+
[Link]());
Box box2 = new Box(10,20);
[Link]("Volume of Box 2 is :" +
[Link]());
Box box3 = new Box(10,20,30);
[Link]("Volume of Box 3 is :" +
[Link]());
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 [Link].*;
import [Link].*;
import [Link].*;
Dept of BCA Page 8
Spurthy College Of Science And Management Studies
/* <applet code="[Link]",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
[Link]=2.0;
[Link]=[Link];
[Link]=[Link];
[Link](lblTitle,gbc);
//Anchor most components to the right
[Link]=[Link];
[Link]=[Link];
[Link](lblRegno,gbc);
[Link]=[Link];
[Link](txtRegno,gbc);
[Link]=[Link];
[Link](lblCourse,gbc);
Dept of BCA Page 9
Spurthy College Of Science And Management Studies
[Link]=[Link];
[Link](txtCourse,gbc);
[Link]=[Link];
[Link](lblSemester,gbc);
[Link]=[Link];
[Link](txtSemester,gbc);
[Link]=[Link];
[Link](lblSub1,gbc);
[Link]=[Link];
[Link](txtSub1,gbc);
[Link]=[Link];
[Link](lblSub2,gbc);
[Link]=[Link];
[Link](txtSub2,gbc);
[Link]=[Link];
[Link](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);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
try{
if([Link]() == cmdReport)
{
rno=[Link]().trim();
course=[Link]().trim();
sem=[Link]().trim();
sub1=[Link]().trim();
sub2=[Link]().trim();
avg="Avg Marks:" + (([Link](sub1) +
[Link](sub2))/2);
Dept of BCA Page 10
Spurthy College Of Science And Management Studies
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)
{
[Link](heading,30,30);
[Link](rno,30,80);
[Link](course,30,100);
[Link](sem,30,120);
[Link](sub1,30,140);
[Link](sub2,30,160);
[Link](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)
{
[Link](dept+"\t"+salary+"\t\t"+bonus+"\t"+total
salary);
}
}
class Accounts extends Department
{
Dept of BCA Page 11
Spurthy College Of Science And Management Studies
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();
[Link](10000);
[Link](20000);
[Link]("Department \t Basic Salary \t Bonus
\t Total Salary");
[Link]("-----------------------------------
---------------------------");
[Link]("Accounts Dept");
[Link]("Sales Dept");
[Link]("-----------------------------------
----------------------------");
}
}
9. Write a program to implement thread, applets and graphics by
implementing animation of ball moving.
Source code :
import [Link].*;
import [Link].*;
/* <applet code="[Link]" height=300
width=300></applet> */
public class MovingBall extends Applet implements Runnable
{
Dept of BCA Page 12
Spurthy College Of Science And Management Studies
int x,y,dx,dy,w,h;
Thread t;
boolean flag;
public void init()
{
w=getWidth();
h=getHeight();
setBackground([Link]);
x=100;
y=10;
dx=10;
dy=10;
}
public void start()
{
flag=true;
t=new Thread(this);
[Link]();
}
public void paint(Graphics g)
{
[Link]([Link]);
[Link](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
{
[Link](300);
}
catch(InterruptedException e)
{}
}
}
public void stop()
{
t=null;
flag=false;
}
}
Dept of BCA Page 13
Spurthy College Of Science And Management Studies
10. Write a program to implement keyboard events.
Source code:
import [Link].*;
import [Link].*;
import [Link].*;
/*<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+=[Link]();
repaint(0);
public void keyPressed(KeyEvent e)
{
showStatus("Key Pressed");
}
public void keyReleased(KeyEvent e)
{
showStatus("Key Released");
}
public void paint(Graphics g)
{
[Link](str,15,15);
}
}
Dept of BCA Page 14
Spurthy College Of Science And Management Studies
PART B
1) Write a java program to check whether the given string or
number is palindrome or not.
Source code:
import [Link].*;
class Palindrome
public static void main(String args[])
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner([Link]);
[Link]("Enter a string/number to check if it is a
palindrome");
original = [Link]();
int length = [Link]();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + [Link](i);
if ([Link](reverse))
[Link]("Entered string/number is a
palindrome.");
else
[Link]("Entered string/number isn't a
palindrome.");
2) Write a java program to find whether the given no is Armstrong
or not.
Dept of BCA Page 15
Spurthy College Of Science And Management Studies
Source code:
import [Link];
public class armstrong2
public static void main(String[] args) {
int num, number, temp, total = 0;
[Link]("Ënter 3 Digit Number");
Scanner scanner = new Scanner([Link]);
num = [Link]();
[Link]();
number = num;
for( ;number!=0;number /= 10)
temp = number % 10;
total = total + temp*temp*temp;
if(total == num)
[Link](num + " is an Armstrong number");
else
[Link](num + " is not an Armstrong number");
3) Write a java program to print the given pattern.
1
Dept of BCA Page 16
Spurthy College Of Science And Management Studies
2 4
3 6 9
Source code:
public class pattern2
public static void main(String[] args)
int lines=10;
int i=1;
int j;
for(i=1;i<=lines;i++){// this loop is used to print the lines
for(j=1;j<=i;j++){// this loop is used to print lines
[Link](i*j+" ");
[Link]("");
4. Write a Java Program to calculate simple interest
Source code
import [Link];
public class JavaExample
public static void main(String args[])
float p, r, t, sinterest;
Scanner scan = new Scanner([Link]);
[Link]("Enter the Principal : ");
Dept of BCA Page 17
Spurthy College Of Science And Management Studies
p = [Link]();
[Link]("Enter the Rate of interest : ");
r = [Link]();
[Link]("Enter the Time period : ");
t = [Link]();
[Link]();
sinterest = (p * r * t) / 100;
[Link]("Simple Interest is: " +sinterest);
5. Write a Java program to reverse a number using while Loop
Source code
import [Link];
class ReverseNumberWhile
public static void main(String args[])
int num=0;
int reversenum =0;
[Link]("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner([Link]);
//Captured input would be stored in number num
num = [Link]();
//While Loop: Logic to find out the reverse number
while( num != 0 )
Dept of BCA Page 18
Spurthy College Of Science And Management Studies
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
[Link]("Reverse of input number is: "+reversenum);
6. Write a Java program to sum the elements of an array
Source code
class SumOfArray
public static void main(String args[])
int[] array = {10, 20, 30, 40, 50, 10};
int sum = 0;
//Advanced for loop
for( int num : array)
sum = sum+num;
[Link]("Sum of array elements is:"+sum);
7. Write a Java Program to count the total number of characters in a
string
Source code
Dept of BCA Page 19
Spurthy College Of Science And Management Studies
public class CountCharacter
public static void main(String[] args)
String string = "The best of both worlds";
int count = 0;
//Counts each character except space
for(int i = 0; i < [Link](); i++)
if([Link](i) != ' ')
count++;
//Displays the total number of characters present in the
given string
[Link]("Total number of characters in a string:
" + count);
8. Write a Java Program to check Even or Odd number
Source code
import [Link];
class CheckEvenOdd
public static void main(String args[])
Dept of BCA Page 20
Spurthy College Of Science And Management Studies
int num;
[Link]("Enter an Integer number:");
//The input provided by user is stored in num
Scanner input = new Scanner([Link]);
num = [Link]();
/* If number is divisible by 2 then it's an even number
* else odd number*/
if ( num % 2 == 0 )
[Link]("Entered number is even");
else
[Link]("Entered number is odd");
9. Write a Java program to calculate area of Square
Source code
import [Link];
class SquareAreaDemo
public static void main (String[] args)
[Link]("Enter Side of Square:");
//Capture the user's input
Scanner scanner = new Scanner([Link]);
//Storing the captured value in a variable
double side = [Link]();
//Area of Square = side*side
double area = side*side;
Dept of BCA Page 21
Spurthy College Of Science And Management Studies
[Link]("Area of Square is: "+area);
10. Write a Java program to print Fibonacci series
Source code
class FibonacciExample1
public static void main(String args[])
int n1=0,n2=1,n3,i,count=10;
[Link](n1+" "+n2);//printing 0 and 1
for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are
already printed
n3=n1+n2;
[Link](" "+n3);
n1=n2;
n2=n3;
END OF FILE
Dept of BCA Page 22