Java Lab PDF
Java Lab PDF
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 calculate bonus for different departments using method overriding.
8.Write a program to create student report using applet, read the input using text boxes and display
the o/p using buttons.
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
1.Write a program to find the fibonacci series using Command Line Arguments as input.
2.Write a program to find if the number is palindrome or not using Command Line Arguments as input.
3.Write a program for method overloading by calculating the square of different numbers using
method overloading.
4.Write a program to demonstrate the working of Scanner input class.
5.Write a program to implement Hierarchical Inheritance.
6.Write a program to demonstrate a class which implements an interface.
7.Write a program to demonstrate Java packages.
8.Write a program to implement Multi-threading.
9.Write an applet program to demonstrate Graphics Shapes using Graphics Classes in Java.
10.Write an applet program to demonstrate Bar Chart using Graphics Classes in Java.
Part-A
1. Write a program to find factorial of list of numbers in the given range using command line arguments.
class Factorial
{
public static void main(String args[])
{
int i,j;
if(args.length==0)
{
System.out.println("No Command Line Arguments");
return;
}
else
{
String m=args[0],n=args[1];
int m1=Integer.parseInt(m);
int n1=Integer.parseInt(n);
System.out.println("************************************");
System.out.println("Factorial Series b/w "+m1+" and "+n1);
for(i=m1;i<=n1;i++)
{
int fact=1;
for(j=1;j<=i;j++)
{
fact=fact*j;
}
System.out.println("Factorial of "+i+" = "+fact+"\n");
}
}
}
}
2. Write a program to find Prime No series b/w 2 limits and read the limits in command line arguments.
class PrimeNo
{
public static void main(String args[])
{
int i,j;
int m=Integer.parseInt(args[0]);
int n=Integer.parseInt(args[1]);
System.out.println("**********************");
System.out.println("Prime No Series b/w "+m+" and "+n);
for(i=m;i<n;i++)
{
int count=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
count=1;
break;
}
}
if(count==0)
System.out.println("\t"+i);
}
System.out.println("************************");
}
}
3.Write a program to sort list of elements in ascending and descending order and show the
exception handling.
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");
}
}
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;
}
}
}
}
}
}
}
8.Write a program to create student report using applet, read the input using text boxes and display
the o/p using buttons.
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
/* <Applet code="Students.class" width="400" height="500"></Applet> */
9.Write a program to implement thread, applets and graphics by implementing animation of ball
moving.
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;
}
}
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
}
10.B Keyboard Events
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
2.Write a program to find if the number is palindrome or not using command line arguments.
class Palindrome
{
public static void main(String args[])
{
int t,r=0;
int n=Integer.parseInt(args[0]);
int p=n;
while(n>0)
{
t=n%10;
r=r*10+t;
n=n/10;
}
if(r==p)
System.out.println(p+" is Palindrome");
else
System.out.println(p+" is not Palindrome");
}
}
3. Write a program for method overloading by calculating the square of different numbers using
method overloading.
class Calculate
{
void square(int a)
{
System.out.println("The square of integer number is ="+a*a);
}
float square(float b)
{
return (b*b);
}
double square(double c)
{
return (c*c);
}
}
class OverloadingPartB
{
public static void main(String args[])
{
Calculate obj=new Calculate();
obj.square(2);
float result=obj.square(3.2F);
System.out.println("The square of Floating number is ="+result);
double result1=obj.square(5.5d);
System.out.println("The square of Double number is ="+result1);
}
}
String s = in.nextLine();
System.out.println("You entered string " + s);
int a = in.nextInt();
System.out.println("You entered integer " + a);
float b = in.nextFloat();
System.out.println("You entered float " + b);
}
}
5. Write a program to implement Hierarchical Inheritance.
class HierarchialInheritance
{
public static void main(String args[])
{
emp1 obj = new emp1();
obj.displayemp();
obj.eno=10;
obj.ename="Asha";
obj.displayemp1();
emp2 e=new emp2();
e.eno=109;
e.accno=954176894;
e.displayemp2();
}
}
class emp
{
int eno;
void displayemp()
{
System.out.println("Super Class");
System.out.println("");
}
}
class InterfaceDemo
{
public static void main(String args[])
{
Implement_Interface sd=new Implement_Interface();
sd.displayStudent();
sd.displayStdDetails();
}
}
interface Student
{
int regno=10;
String name="Roopa";
public void displayStudent();
}
package package1;
public class PackageTest
{
public void display()
{
System.out.println("Hi, Welcome to Java Packages");
}
}
import package1.PackageTest;
class PackageDemo
{
public static void main(String args[])
{
PackageTest p=new PackageTest();
p.display();
}
}
class MultipleThread
{
public static void main(String args[])
{
ThreadA ta=new ThreadA();
ThreadB tb=new ThreadB();
ThreadC tc=new ThreadC();
Thread t1=new Thread(ta,"Thread One");
Thread t2=new Thread(tb,"Thread Two");
Thread t3=new Thread(tc,"Thread Three");
t1.start();
t2.start();
t3.start();
for(int i=16;i<=20;i++)
System.out.println(Thread.currentThread().getName()+"="+i);
System.out.println("End of Main");
}
}
9. Write an applet program to demonstrate Graphics Shapes using Graphics Classes in Java.
import java.awt.Graphics;
import java.awt.Graphics;
soprotection.com