S.S. and S.
S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 1. Write a Program in Java to print Fibonacci series.
import java.io.*;
import java.io.*;
class fibonacci2
{
public static void main(String args[])
{
int a,b,c,no;
Scanner s = new Scanner(System.in);
System.out.println("Enter how many numbers you want to
print\n");
no = s.nextInt();
a=0;
b=1;
System.out.println("\t"+a+"\n\t"+
b);
for(int i=1;i<=no-2;i++)
{
c=a+b;
System.out.println("\t"+
c);
a=b;
b=c;
}
}
}
OUTPUT:
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 2. Write a Program in Java to print Factorial of a number.
import java.io.*;
importjava.util.*;
class fact
{
public static void main(String args[])
{
int fact, no;
Scanner S = new Scanner(System.in);
System.out.println("Enter number to calculate
factorial\t");
no = S.nextInt();
fact = factorial(no);
System.out.println("factorial of "+no+"is="+fact);
}
public static int factorial(int n)
{
if(n==1)
return(1);
return(factorial(n-1)*n);
}
}
OUTPUT:
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 3. Write a Program in Java to demonstrate command line arguments.
import java.io.*;
class square
{
Public static void main(String args[])
{
int no, sq;
no=integer.ParseInt(args[0]);
sq=no*no;
System.out.println(“Square=”+sq);
}
}
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 4. Write a Program in Java to create student information using array.
import java.io.*;
class studinfo
{
public static void main(String arg[])
{
String names[] = { "Rajesh", "Suresh", "Ramesh",
"Kamlesh", "Vignesh" };
int marks[] = { 45, 78, 83, 77, 93 };
char sections[] = { 'A', 'B', 'A', 'A', 'B' };
for(int i=0;i<names.length;i++)
{
System.out.println( names[i] + " in section " +
sections[i] + " got " + marks[i] + " marks." );
}
}
}
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 5. Write a Program in Java to implement user defined package.
//save by
A.java
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
//save by B.java
package mypac;
import pack.*;
class B
{
public static void main(String
args[])
{
A obj = new A();
obj.msg();
}
}
OUTPUT:
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 6. Write a Program in Java to implement default & parametrized
constructor.
import java.io.*;
class square
int no,sq;
public square()
no=4;
public square(int p)
no=p;
public void process()
sq=no*no;
System.out.println("Square="+sq);
class construct
public static void main(String args[])
square s1=new square();
square s2=new square();
s1.process();
s2.process();
OUTPUT:
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 7. Write a Program in Java to demonstrate various operations on string
functions.
import java.io.*;
class string
public static void main(String args[])
String a="SACHIN";
String b="lokesh";
System.out.println(a.toLowerCase());
System.out.println(b.toUpperCase());
System.out.println(b.concat(a));
System.out.println(b.length());
String c=" Rahul ";
String d="";
System.out.println(c.trim());
System.out.println(d.isEmpty());
System.out.println(b.charAt(2));
System.out.println(a.indexOf('c'));
System.out.println(b.equals(a));
System.out.println(b.replace('o','a'));
}
}
OUTPUT:
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 8. Write a Program in Java to demonstrate wrapper class.
import java.io.*;
public class wrapper
public static void main(String args[])
Integer obj1 = new Integer(25);
Integer obj2 = new Integer("25");
Integer obj3 = new Integer(35);
System.out.println("Compairing using compare to obj1 &obJ2="+obj1.compareTo(obj2));
System.out.println("Compairing using compare to obj1 &obj3="+obj1.compareTo(obj3));
System.out.println("Compairing using equals to obj1 &obj2="+obj1.equals(obj2));
System.out.println("Compairing using equals to obj1 &obj3="+obj1.equals(obj3));
Float f1 = new Float("2.25f");
Float f2 = new Float("20.5f");
Float f3 = new Float("2.25f");
System.out.println("Comparing using compare to f1 &f2="+Float.compare(f1,f2));
System.out.println("Comparing using compare to f1 &f2="+Float.compare(f1,f3));
Float f = obj1+f1.floatValue();
System.out.println("Addition of obj1 and f1="+obj1+"+"+f1+"="+f);
}
OUTPUT:
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 9. Write a Program in Java to implement inheritance.
import java.io.*;
class A
void show()
System.out.println("This is parent class");
class B extends A
void display()
System.out.println("This is child class function");
class inherites
public static void main(String args[])
B obj = new B();
obj.show();
obj.display();
}
OUTPUT:
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 10. Write a Program in Java to demonstrate exception handling.
import java.io.*;
class exception
public static void main (String[] args)
int a=5;
int b=0;
try
System.out.println(a/b);
catch(ArithmeticException e)
System.out.println(e.toString());
}
OUTPUT:
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 12. Write AWT/Swing Program in Java to demonstrate different
events.
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current instance
//add components and set size, layout and visibility
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
}
}
OUTPUT:
S.S. and S.S’s
Vidhyadhan Commerce College Wadel Road Dhule
Department of Computer Science
Class:- SYBCA SEM-IV Subject:-BCA 406 Java Programming
Student Name:-
Practical 13. Write AWT/Swing Program in Java to demonstrate different
events.
import java.io.FileOutputStream;
public class FileOutputStreamExample
{
public static void main(String args[])
{
Try
{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
fout.write(65);
fout.close();
System.out.println("success...");
}
catch(Exception e){System.out.println(e);}
}
}