[1]
Program 1: Write a program to show the use of Command Line
Arguments.
Solution: class ComLine
public static void main(String args[])
int count,i=0;
String string;
count=[Link];
[Link]("Number of arguments="+count);
while(i<count)
string=args[i];
i=i+1;
[Link](i+":"+"Java is"+string+"!");
Output:
[2]
Program 2: Write a program that illustrates Math function supported by
java language.
Solution: import [Link];
class Math1
public static void main(String args[])
double x;
x = [Link](12,7);
[Link]("The Maximum among 12,7 is " +x);
x = [Link](81);
[Link]("The Square root of 81 is " +x);
x = [Link](-77);
[Link]("The Absolute value of -77 is " +x);
x = [Link](2,3);
[Link]("The cube root of 2 is equal to " +x);
x = [Link](12,7);
[Link]("The Minimum among 12,7 is " +x);
x = [Link](7);
[Link]("The Natural log of 7 is " +x);
[3]
Output:
[4]
Program 3: Write a program that reads data from keyboard using
readLine().
Solution: import [Link];
class NumberOperation
public static void main(String args[])
DataInputStream in = new DataInputStream([Link]);
int num1=0;
int num2=0;
try
[Link]("Enter first number");
num1 = [Link]([Link]());
[Link]("Enter Second number");
num2 = [Link]([Link]());
catch(Exception e)
{}
[Link]("ADDITION of Two numbers =" +(num1 + num2));
[Link]("SUBTRACTION of Two numbers =" +(num1 - num2));
[Link]("MULTIPLICATION of Two numbers =" +(num1 *
num2));
[Link]("DIVISION of Two numbers =" +(num1 / num2));
}
[5]
}
Output:
[6]
Program 4: Write a program to input the marks of 5 students in 5 subjects
of BCA Final. Calculate total and percentage of students.
Solution: class Student
int ip,cg,dcn,edp,maths;
void getdata(int i, int c, int d, int e, int m)
ip=i; cg=c; dcn=d; maths=m; edp=e;
class Result
public static void main(String args[])
Student s1 = new Student();
[Link](80,75,70,65,90);
int tm1= [Link]+[Link]+[Link]+[Link]+[Link];
float per1=(tm1*100)/500;
[Link](" Total Marks of First Student is " + tm1);
[Link]("Percentage Marks of First Student is " +per1);
Student s2 = new Student();
[Link](92,95,64,75,66);
int tm2= [Link]+[Link]+[Link]+[Link]+[Link];
float per2=(tm2*100)/500;
[7]
[Link](" Total Marks of Second Student is " + tm2);
[Link]("Percentage Marks of Second Student is " +per2);
Student s3 = new Student();
[Link](45,78,64,66,84);
int tm3= [Link]+[Link]+[Link]+[Link]+[Link];
float per3=(tm3*100)/500;
[Link](" Total Marks of Third Student is " + tm3);
[Link]("Percentage Marks of Third Student is " +per3);
Student s4 = new Student();
[Link](56,78,75,35,54);
int tm4= [Link]+[Link]+[Link]+[Link]+[Link];
float per4=(tm4*100)/500;
[Link](" Total Marks of Fourth Student is " + tm4);
[Link]("Percentage Marks of Fourth Student is " +per4);
Student s5 = new Student();
[Link](98,74,78,85,90);
int tm5= [Link]+[Link]+[Link]+[Link]+[Link];
float per5=(tm5*100)/500;
[Link](" Total Marks of Fifth Student is " + tm5);
[Link]("Percentage Marks of Fifth Student is " +per5);
[8]
Output:
[9]
Program 5: Write a program to calculate the area and circumference of
circle. You are supposed to get the radius from keyboard.
Solution: import [Link];
import [Link];
class AreaCircum
public static void main(String args[])
DataInputStream in = new
DataInputStream([Link]);
int radius =0;
try
[Link]("Enter the radius: ");
radius = [Link]([Link]());
catch(Exception e)
{}
double area = [Link]*radius*radius;
double circumference = 2*[Link]*radius;
[Link]("Area of circle "+ area);
[Link]("Circumference of circle " + circumference);
}
[10]
Output:
[11]
Program 6: Write a program to use all control statements in one switch
statement.
Solution: class ControlState
public static void main(String args[])
int choice,r,q,n,p,i,a,b,c;
[Link]("-------------Control statements options--------------");
[Link]("1: For Factorial");
[Link]("2: For Fibonacci series");
[Link]("3: For Reverse of number");
choice = 1;
switch(choice)
case 1:
p=1; n=5;
for(i=1; i<=n; i++)
p=p*i;
[Link]("Factorial of " + n + " is " + p);
break;
case 2:
a=0; b=1; i=1; n=7;
[12]
while(i<=n)
if(i==1)
[Link](a);
else if(i==2)
[Link](b);
else
c=a+b;
[Link](c);
a=b;
b=c;
i++;
break;
case 3:
n=345;
do
{
[13]
r=n%10; q=n/10;
[Link](r);
n=q;
while(n>0);
break;
default:
[Link]("WRONG INPUT");
break;
Output:
[14]
Program 7: Write a program for the Application of classes and objects.
Solution: class Rectangle
int length,width;
void getdata(int x,int y)
length=x;
width=y;
int rectArea()
int area=length*width;
return(area);
class RectArea
public static void main(String args[])
int area1,area2;
Rectangle rect1=new Rectangle();
Rectangle rect2=new Rectangle();
[Link]=10;
[Link]=10;
[15]
area1=[Link]*[Link];
[Link](20,20);
area2=[Link]();
[Link]("Area1 ="+area1);
[Link]("Area2 ="+area2);
Output:
[16]
Program 8: Write a program to calculate sum of 2 number demonstrating
method overloading.
Solution: class SumNum
void sum(int num1, int num2)
[Link]("Sum of two integers = "+(num1+num2));
void sum(float num1, float num2)
[Link]("Sum of two float numbers = "+(num1+num2));
void sum(double num1, double num2)
[Link]("Sum of two double numbers = "+(num1+num2));
class DemoMethod
public static void main(String args[])
SumNum s = new SumNum();
[Link](20,10);
[Link](20.5f,10.5f);
[17]
[Link](20.5F,10.5F);
Output:
[18]
Program 9: Write a program for overloading a constructor.
Solution: class Room
int length;
int breath;
Room(int x)
length=breath=x;
Room(int x,int y)
length=x;
breath=y;
int cal_area()
int area;
return(length*breath);
class Overloading
public static void main(String args[])
{
[19]
Room r2=new Room(5);
Room r3=new Room(5,3);
int area2= r2.cal_area();
int area3= r3.cal_area();
[Link]("area of room is"+area2);
[Link]("area of room is"+area3);
Output:
[20]
Program 10: Write a program to demonstrate nesting of methods.
Solution: class Nesting
int m,n;
Nesting(int x,int y)
m=x;
n=y;
int largest()
if(m>=n)
return(m);
else
return(n);
void display()
int large=largest();
[Link]("Largest value="+large);
class NestingTest
{
[21]
public static void main(String args[])
Nesting nest=new Nesting(50,40);
[Link]();
Output:
[22]
Program 11: Write a program to demonstrate Method Overriding.
Solution: class Super
int x;
Super(int x)
this.x=x;
void display()
[Link]("I am in Super class");
[Link]("Super x="+x);
class Sub extends Super
int y;
Sub(int x,int y)
super(x);
this.y=y;
void display()
{
[23]
[Link]("I am in Sub Class");
[Link]("Super x="+x);
[Link]("Sub y="+y);
class OverrideTest
public static void main(String args[])
Sub s1=new Sub(100,200);
[Link]();
Output:
[24]
Program 12: Write a program to demonstrate abstract class and method.
Solution: abstract class Shape
abstract void numberOfSides();
class Triangle extends Shape
public void numberOfSides()
[Link]("Number of sides of Triangle : Three");
class Rectangle extends Shape
public void numberOfSides()
[Link]("Number of sides of Rectangle : Four");
class Hexagon extends Shape
public void numberOfSides()
[Link]("Number of sides of Hexagon: Six");
[25]
}
public class Sides
public static void main(String args[])
Triangle t= new Triangle();
Rectangle r = new Rectangle();
Hexagon h = new Hexagon();
[Link]();
[Link]();
[Link]();
Output:
[26]
Program 13: Write a program to implement of Single inheritance.
Solution: class Room
int length;
int breadth;
Room(int x,int y)
length=x;
breadth=y;
int area()
return(length*breadth);
class BedRoom extends Room
int height;
BedRoom(int x, int y,int z)
super(x,y);
height=z;
int volume()
[27]
{
return(length*breadth*height);
class InherTest
public static void main(String args[])
BedRoom room1=new BedRoom(14,12,10);
int area1=[Link]();
int volume1=[Link]();
[Link]("Area="+area1);
[Link]("Volume="+volume1);
Output:
[28]
Program 14: Write a program for the application of Two-dimensional
array.
Solution: public class TableMul
public static void main(String[] args)
int[][] table = new int[12][12];
for(int i=0; i <= [Link]-1; i++)
for (int j=0; j <= table[0].length-1; j++)
table[i][j] = (i + 1) * (j + 1);
if (table[i][j] < 10)
[Link](" " + table[i][j] + " ");
else
if (table[i][j] > 10 && table[i][j] < 100)
[Link](" " + table[i][j] + " ");
else
[Link](table[i][j] + " ");
[Link](" ");
[29]
Output:
[30]
Program 15: Write a program to display the string in alphabetical order.
Solution: class StringOrdering
static String name[] = {"Madras","Delhi","Ahmedabad","Calcutta","Bombay"};
public static void main(String args[])
int size = [Link];
String temp = null;
for(int i=0;i<size;i++)
for(int j=i+1;j<size;j++)
if(name[j].compareTo(name[i])<0)
temp=name[i];
name[i]=name[j];
name[j]=temp;
for(int i=0;i<size;i++)
[Link](name[i]);
}
[31]
}
Output:
[32]
Program 16: Write a program to demonstrate multiple inheritance by way
of interface.
Solution: interface Interface1
void display1();
interface Interface2
void display2();
class A implements Interface1,Interface2
public void display1()
[Link]("I am in Inetrface1");
public void display2()
[Link]("I am in Inetrface2");
void display3()
[Link]("I am in class A");
[33]
}
class MultiInterfaces
public static void main(String args[])
Interface1 inter1;
Interface2 inter2;
A a = new A();
A b = new A();
A c = new A();
inter1 = a;
inter2 = b;
inter1.display1();
inter2.display2();
c.display3();
[34]
Output:
[35]
Program 17: Write a program to implement a multiple Inheritance.
Solution: class Student
String name;
int rollno;
void getData(String n, int r)
name=n;
rollno=r;
void showData()
[Link]("Name of student =" +name);
[Link]("Rollno of student =" +rollno);
class Test extends Student
float marks1, marks2, marks3, marks4, marks5;
void getMarks(float m1, float m2, float m3, float m4,
float m5)
marks1=m1;
[36]
marks2=m2;
marks3=m3;
marks4=m4;
marks5=m5;
void showMarks()
[Link]("Marks obtained are as follows:");
[Link]("Marks1 =" +marks1);
[Link]("Marks2 =" +marks2);
[Link]("Marks3 =" +marks3);
[Link]("Marks4 =" +marks4);
[Link]("Marks5 =" +marks5);
interface Sports
static final float sportsmarks=5.0f;
void showsm();
class Result extends Test implements Sports
float total;
public void showsm()
[37]
{
[Link]("sports marks =" +sportsmarks);
void display()
total=marks1+marks2+marks3+marks4+marks5+spo
rtsmarks;
showData();
showMarks();
showsm();
[Link]("Total marks =" +total);
class Hybrid
public static void main(String args[])
Result st=new Result();
[Link]("Raman",5201);
[Link](80.0f,70.0f,60.0f,75.0f,85.0f);
[Link]();
[38]
Output:
[39]
Program 18: Write a program to make package named Package1
containing a single class classA.
Solution: package package1;
public class ClassA
public void displayA()
[Link]("I am in Class A");
Program 19: Write a program that imports the class classA from package
package1.
Solution: import [Link];
class PackageTest1
public static void main(String args[])
ClassA objectA = new ClassA();
[Link]();
[40]
Program 20: Write a program which makes a package named Package2
containing a single class ClassB.
Solution: package package2;
public class ClassB
protected int m=10;
public void displayB()
[Link]("I am in Class B");
[Link]("m = " +m);
[41]
Program 21: Write a program to import classes from other packages.
Solution: import [Link];
import [Link];
class PackageTest2
public static void main(String args[])
ClassA objectA = new ClassA();
ClassB objectB = new ClassB();
[Link]();
[Link]();
Output:
[42]
Program 22: Write a program which makes a package named areas
containing a single class Circle.
Solution: package areas;
public class Circle
int radius;
public Circle(int r)
radius=r;
public double cirarea()
return([Link]*radius*radius);
Program 23: Write a program which makes a package named areas
containing a single class Square.
Solution: package areas;
public class Square
int side;
public Square(int s)
side=s;
[43]
}
public int sqarea()
return(side * side);
Program 24: Write a program which makes a package named areas
containing a single class Rectangle.
Solution: package areas;
public class Rectangle
int length;
int width;
public Rectangle(int l,int b)
length=l;
width=b;
public int rectarea()
return(length * width);
[44]
Program 25: Write a program that imports all the classes from the package
Areas.
Solution: import [Link];
import [Link];
import [Link];
class AreaTest
public static void main(String args[])
Rectangle r=new Rectangle(10,5);
Square s=new Square(10);
Circle c=new Circle(5);
[Link]("Area of Rectangle=" +[Link]());
[Link]("Area of Square=" +[Link]());
[Link]("Area of Circle=" +[Link]());
[45]
Output:
[46]
Program 26: Write a program to create Threads using Thread Class.
Solution: class A extends Thread
public void run()
for(int i=1;i<=5;i++)
[Link]("\t from threadA : i=" +i);
[Link]("exit from A");
class B extends Thread
public void run()
for(int j=1;j<=5;j++)
[Link]("\t from thread B : j= " +j);
[Link]("exit from B");
class C extends Thread
[47]
{
public void run ()
for(int k=1; k<=5; k++)
[Link]("\t from thread C : k=" +k);
[Link]("exit from C");
class ThreadTest
public static void main(String args[])
new A().start();
new B().start();
new C().start();
[48]
Output:
[49]
Program 27: Write a program to illustrate use of yield(), stop(), sleep()
methods.
Solution: class A extends Thread
public void run()
for(int i=1;i<5;i++)
if(i==1)
yield();
[Link]("\t From Thread A : i ="+i);
[Link]("exit from A");
class B extends Thread
public void run()
for(int j=1;j<5;j++)
if(j==3)
stop();
[Link]("\t From Thread B : j ="+j);
[50]
}
[Link]("exit from B");
class C extends Thread
public void run()
for(int k=1;k<5;k++)
[Link]("\t From Thread C : k ="+k);
if(k==1)
try
sleep(1000);
catch(Exception e)
[Link]("exit from C");
}
[51]
}
class ThreadMethods
public static void main(String args[])
A threadA = new A();
B threadB = new B();
C threadC = new C();
[Link]("Start thread A");
[Link]();
[Link]("Start thread B");
[Link]();
[Link]("Start thread C");
[Link]();
[Link]("End of main thread");
[52]
Output:
[53]
Program 28: Write a program to illustrate the use of Priority in Threads.
Solution: class A extends Thread
public void run()
[Link]("thread A started");
for(int i=1;i<=4;i++)
[Link]("\n From thread A:i="+i);
[Link]("Exit from A");
class B extends Thread
public void run()
[Link]("thread B started");
for(int j=1;j<=4;j++)
[Link]("\n From thread B:j="+j);
[Link]("Exit from B");
}
[54]
}
class C extends Thread
public void run()
[Link]("thread C started");
for(int k=1;k<=4;k++)
[Link]("\n From thread C:k="+k);
[Link]("Exit from C");
class ThreadPriority1
public static void main(String args[])
A threadA=new A();
B threadB=new B();
C threadC=new C();
[Link](Thread.MAX_PRIORITY);
[Link]([Link]()+1);
[Link](Thread.MIN_PRIORITY);
[Link]("Start thread A");
[55]
[Link]();
[Link]("Start thread B");
[Link]();
[Link]("Start thread C");
[Link]();
[Link]("end of main thread");
Output:
[56]
Program 29: Write a program to illustrate Try and Catch for Exception
Handling.
Solution: class Error3
public static void main(String args[])
int a = 10;
int b = 5;
int c = 5;
int x,y;try
x = a / (b - c);
catch(ArithmeticException e)
[Link]("Division by zero");
y = a/(b+c);
[Link]("y = " + y);
[57]
Output:
[58]
Program 30: Write a program for throwing our own exception.
Solution: import [Link];
class MyException extends Exception
MyException(String message)
super(message);
class TestMyException
public static void main(String args[])
int x=5,y=1000;
try
float z=(float)x/(float)y;
if(z<0.01)
throw new MyException("Number is too small");
catch(MyException e)
{
[59]
[Link]("Caught my exception");
[Link]([Link]());
finally
[Link]("I am always here");
Output:
Program 31: Write a simple HelloJava Applet.
Solution: import [Link].*;
[60]
import [Link].*;
public class HelloJava extends Applet
public void paint(Graphics g)
[Link]("Hello java",10,100);
Program 32: Write a program for HelloJava Applet.
Solution: <html>
<head><title>Welcome to java applet</title></head>
<body>
<center>
<applet code=[Link]
width=400
height=400>
</applet>
</center>
</body>
</html>
Output:
[61]
Program 33: Write an applet HelloJava PARAM.
Solution: import [Link].*;
import [Link].*;
[62]
public class HelloJavaParam extends Applet
String str;
public void init()
str=getParameter("Enter your name: ");
if(str==null)
str="Miss International Love";
str="Hello "+ str;
public void paint(Graphics g)
[Link](str,10,100);
Program 34: Write a program for HelloJava PARAM applet.
Solution: <html>
<head><title>Welcome to java applets</title></head>
<body>
[63]
<applet code=[Link]
width=400
height=400>
<param name="Enter your name:"
value=" Miss. International Love">
</applet>
</body>
</html>
Output:
[64]
[65]
Program 35: Write a program for displaying numerical values.
Solution: import [Link].*;
import [Link].*;
public class NumValues extends Applet
public void paint(Graphics g)
int value1=10;
int value2=20;
int sum=value1+value2;
String s="sum of two numbers: "+[Link](sum);
[Link](s,50,50);
Program 36: Write a program to make HTML file for displaying
numerical values.
Solution: <html>
<applet code=[Link]
width=300
height=300>
</applet>
</html>
[66]
Output:
[67]
Program 37: Write a program for interactive input to an applet.
Solution: import [Link].*;
import [Link].*;
public class NumValues extends Applet
public void paint(Graphics g)
int value1=10;
int value2=20;
int sum=value1+value2;
String s="sum of two numbers: "+[Link](sum);
[Link](s,50,50);
[68]
Program 38: Write a program for interactive input to userin applet.
Solution: <html>
<applet code=[Link]
width=300
height=300>
</applet>
</html>
Output:
[69]
Program 39: Write a program for writing bytes to a file.
Solution: import [Link].*;
class WriteBytes
public static void main(String args[ ])
byte cities[ ]={'D','E','L','H','I','\n','M','A','D',
'R','A','S','\n','L','O','N','D','O','N','\n'};
FileOutputStream outfile=null;
try
outfile = new FileOutputStream("[Link]");
[Link](cities);
[Link]( );
catch (IOException ioe)
[Link](ioe);
[Link](-1);
[70]
Output:
[71]
Program 40: Write a program for reading bytes to a file.
Solution: import [Link].*;
class ReadBytes
public static void main(String args[ ])
FileInputStream infile=null;
int b;
try
infile = new FileInputStream(args [0]);
while((b=[Link]())!=-1)
[Link]((char)b);
[Link]( );
catch (IOException ioe)
[Link](ioe);
[72]
Output:
[73]
Program 41: Write a program for copying bytes from one file to another.
Solution: import [Link].*;
class CopyBytes
public static void main(String args[])
FileInputStream infile=null;
FileOutputStream outfile=null;
byte byteRead;
try
infile =new FileInputStream("[Link]");
outfile=new FileOutputStream("[Link]");
do
byteRead = (byte) [Link]( );
[Link](byteRead);
while(byteRead!=-1);
catch (FileNotFoundException e)
[Link]( "File not found");
}
[74]
catch(IOException e)
[Link]([Link] ( ));
finally
try
[Link]( );
[Link]();
catch (IOException e)
[75]
Output:
[76]
Program 42: Write a program for writing characters to a file.
Solution: import [Link].*;
class WriteCharacters
public static void main(String args[])
char alpha[] = {'A','B','C','D','\n','E','F','G','H','\n'};
FileWriter outfile = null;
try
outfile = new FileWriter("[Link]");
[Link](alpha);
[Link]();
catch(IOException ioe)
[Link](ioe);
[Link](-1);
[77]
Output:
[78]
Program 43: Write a program for reading characters to a file.
Solution: import [Link].*;
class ReadCharacters
public static void main(String args[])
FileReader infile = null;
int b;
try
infile = new FileReader (args [ 0 ] ) ;
while( (b = [Link] ( ) ) != -1)
[Link]( ( char ) b);
[Link]();
catch(IOException ioe)
[Link](ioe);
[79]
Output:
[80]
Program 44: Write a program for copying characters from one file to
another.
Solution: import [Link].*;
class CopyCharacters
public static void main(String args[])
File inFile=new File("[Link]");
File outFile=new File("[Link]");
FileReader ins=null;
FileWriter outs=null;
try
ins=new FileReader(inFile);
outs=new FileWriter(outFile);
int ch;
while((ch=[Link]())!= -1)
[Link](ch);
catch(IOException e)
[Link](e);
[81]
[Link](-1);
finally
try
[Link]();
[Link]();
catch(IOException e){ }
Output:
[82]
Program 45: Write a program for reading and writing primitive data.
Solution: import [Link].*;
class ReadWritePrimitive
public static void main(String args[])
throws IOException
File primitive=new File("[Link]");
FileOutputStream fos=new FileOutputStream(primitive);
DataOutputStream dos=new DataOutputStream(fos);
[Link](1999);
[Link](375.85);
[Link](false);
[Link]('X');
[Link]();
[Link]();
FileInputStream fis=new FileInputStream(primitive);
DataInputStream dis=new DataInputStream(fis);
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]();
[Link]();
[83]
}
Output:
[84]