[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=args.length;
System.out.println("Number of arguments="+count);
while(i<count)
string=args[i];
i=i+1;
System.out.println(i+":"+"Java is"+string+"!");
Output:
[2]
Program 2: Write a program that illustrates Math function supported by
java language.
Solution: import java.lang.Math;
class Math1
public static void main(String args[])
double x;
x = Math.max(12,7);
System.out.println("The Maximum among 12,7 is " +x);
x = Math.sqrt(81);
System.out.println("The Square root of 81 is " +x);
x = Math.abs(-77);
System.out.println("The Absolute value of -77 is " +x);
x = Math.pow(2,3);
System.out.println("The cube root of 2 is equal to " +x);
x = Math.min(12,7);
System.out.println("The Minimum among 12,7 is " +x);
x = Math.log(7);
System.out.println("The Natural log of 7 is " +x);
[3]
Output:
[4]
Program 3: Write a program that reads data from keyboard using
readLine().
Solution: import java.io.DataInputStream;
class NumberOperation
public static void main(String args[])
DataInputStream in = new DataInputStream(System.in);
int num1=0;
int num2=0;
try
System.out.println("Enter first number");
num1 = Integer.parseInt(in.readLine());
System.out.println("Enter Second number");
num2 = Integer.parseInt(in.readLine());
catch(Exception e)
{}
System.out.println("ADDITION of Two numbers =" +(num1 + num2));
System.out.println("SUBTRACTION of Two numbers =" +(num1 - num2));
System.out.println("MULTIPLICATION of Two numbers =" +(num1 *
num2));
System.out.println("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();
s1.getdata(80,75,70,65,90);
int tm1= s1.ip+s1.cg+s1.dcn+s1.edp+s1.maths;
float per1=(tm1*100)/500;
System.out.println(" Total Marks of First Student is " + tm1);
System.out.println("Percentage Marks of First Student is " +per1);
Student s2 = new Student();
s2.getdata(92,95,64,75,66);
int tm2= s2.ip+s2.cg+s2.dcn+s2.edp+s2.maths;
float per2=(tm2*100)/500;
[7]
System.out.println(" Total Marks of Second Student is " + tm2);
System.out.println("Percentage Marks of Second Student is " +per2);
Student s3 = new Student();
s3.getdata(45,78,64,66,84);
int tm3= s3.ip+s3.cg+s3.dcn+s3.edp+s3.maths;
float per3=(tm3*100)/500;
System.out.println(" Total Marks of Third Student is " + tm3);
System.out.println("Percentage Marks of Third Student is " +per3);
Student s4 = new Student();
s4.getdata(56,78,75,35,54);
int tm4= s4.ip+s4.cg+s4.dcn+s4.edp+s4.maths;
float per4=(tm4*100)/500;
System.out.println(" Total Marks of Fourth Student is " + tm4);
System.out.println("Percentage Marks of Fourth Student is " +per4);
Student s5 = new Student();
s5.getdata(98,74,78,85,90);
int tm5= s5.ip+s5.cg+s5.dcn+s5.edp+s5.maths;
float per5=(tm5*100)/500;
System.out.println(" Total Marks of Fifth Student is " + tm5);
System.out.println("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 java.io.DataInputStream;
import java.lang.Math;
class AreaCircum
public static void main(String args[])
DataInputStream in = new
DataInputStream(System.in);
int radius =0;
try
System.out.println("Enter the radius: ");
radius = Integer.parseInt(in.readLine());
catch(Exception e)
{}
double area = Math.PI*radius*radius;
double circumference = 2*Math.PI*radius;
System.out.println("Area of circle "+ area);
System.out.println("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;
System.out.println("-------------Control statements options--------------");
System.out.println("1: For Factorial");
System.out.println("2: For Fibonacci series");
System.out.println("3: For Reverse of number");
choice = 1;
switch(choice)
case 1:
p=1; n=5;
for(i=1; i<=n; i++)
p=p*i;
System.out.println("Factorial of " + n + " is " + p);
break;
case 2:
a=0; b=1; i=1; n=7;
[12]
while(i<=n)
if(i==1)
System.out.println(a);
else if(i==2)
System.out.println(b);
else
c=a+b;
System.out.println(c);
a=b;
b=c;
i++;
break;
case 3:
n=345;
do
{
[13]
r=n%10; q=n/10;
System.out.println(r);
n=q;
while(n>0);
break;
default:
System.out.println("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();
rect1.length=10;
rect1.width=10;
[15]
area1=rect1.length*rect1.width;
rect2.getdata(20,20);
area2=rect2.rectArea();
System.out.println("Area1 ="+area1);
System.out.println("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)
System.out.println("Sum of two integers = "+(num1+num2));
void sum(float num1, float num2)
System.out.println("Sum of two float numbers = "+(num1+num2));
void sum(double num1, double num2)
System.out.println("Sum of two double numbers = "+(num1+num2));
class DemoMethod
public static void main(String args[])
SumNum s = new SumNum();
s.sum(20,10);
s.sum(20.5f,10.5f);
[17]
s.sum(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();
System.out.println("area of room is"+area2);
System.out.println("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();
System.out.println("Largest value="+large);
class NestingTest
{
[21]
public static void main(String args[])
Nesting nest=new Nesting(50,40);
nest.display();
Output:
[22]
Program 11: Write a program to demonstrate Method Overriding.
Solution: class Super
int x;
Super(int x)
this.x=x;
void display()
System.out.println("I am in Super class");
System.out.println("Super x="+x);
class Sub extends Super
int y;
Sub(int x,int y)
super(x);
this.y=y;
void display()
{
[23]
System.out.println("I am in Sub Class");
System.out.println("Super x="+x);
System.out.println("Sub y="+y);
class OverrideTest
public static void main(String args[])
Sub s1=new Sub(100,200);
s1.display();
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()
System.out.println("Number of sides of Triangle : Three");
class Rectangle extends Shape
public void numberOfSides()
System.out.println("Number of sides of Rectangle : Four");
class Hexagon extends Shape
public void numberOfSides()
System.out.println("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();
t.numberOfSides();
r.numberOfSides();
h.numberOfSides();
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=room1.area();
int volume1=room1.volume();
System.out.println("Area="+area1);
System.out.println("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 <= table.length-1; i++)
for (int j=0; j <= table[0].length-1; j++)
table[i][j] = (i + 1) * (j + 1);
if (table[i][j] < 10)
System.out.print(" " + table[i][j] + " ");
else
if (table[i][j] > 10 && table[i][j] < 100)
System.out.print(" " + table[i][j] + " ");
else
System.out.print(table[i][j] + " ");
System.out.println(" ");
[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 = name.length;
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++)
System.out.println(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()
System.out.println("I am in Inetrface1");
public void display2()
System.out.println("I am in Inetrface2");
void display3()
System.out.println("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()
System.out.println("Name of student =" +name);
System.out.println("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()
System.out.println("Marks obtained are as follows:");
System.out.println("Marks1 =" +marks1);
System.out.println("Marks2 =" +marks2);
System.out.println("Marks3 =" +marks3);
System.out.println("Marks4 =" +marks4);
System.out.println("Marks5 =" +marks5);
interface Sports
static final float sportsmarks=5.0f;
void showsm();
class Result extends Test implements Sports
float total;
public void showsm()
[37]
{
System.out.println("sports marks =" +sportsmarks);
void display()
total=marks1+marks2+marks3+marks4+marks5+spo
rtsmarks;
showData();
showMarks();
showsm();
System.out.println("Total marks =" +total);
class Hybrid
public static void main(String args[])
Result st=new Result();
st.getData("Raman",5201);
st.getMarks(80.0f,70.0f,60.0f,75.0f,85.0f);
st.display();
[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()
System.out.println("I am in Class A");
Program 19: Write a program that imports the class classA from package
package1.
Solution: import package1.ClassA;
class PackageTest1
public static void main(String args[])
ClassA objectA = new ClassA();
objectA.displayA();
[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()
System.out.println("I am in Class B");
System.out.println("m = " +m);
[41]
Program 21: Write a program to import classes from other packages.
Solution: import package1.ClassA;
import package2.ClassB;
class PackageTest2
public static void main(String args[])
ClassA objectA = new ClassA();
ClassB objectB = new ClassB();
objectA.displayA();
objectB.displayB();
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(Math.PI*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 areas.Rectangle;
import areas.Square;
import areas.Circle;
class AreaTest
public static void main(String args[])
Rectangle r=new Rectangle(10,5);
Square s=new Square(10);
Circle c=new Circle(5);
System.out.println("Area of Rectangle=" +r.rectarea());
System.out.println("Area of Square=" +s.sqarea());
System.out.println("Area of Circle=" +c.cirarea());
[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++)
System.out.println("\t from threadA : i=" +i);
System.out.println("exit from A");
class B extends Thread
public void run()
for(int j=1;j<=5;j++)
System.out.println("\t from thread B : j= " +j);
System.out.println("exit from B");
class C extends Thread
[47]
{
public void run ()
for(int k=1; k<=5; k++)
System.out.println("\t from thread C : k=" +k);
System.out.println("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();
System.out.println("\t From Thread A : i ="+i);
System.out.println("exit from A");
class B extends Thread
public void run()
for(int j=1;j<5;j++)
if(j==3)
stop();
System.out.println("\t From Thread B : j ="+j);
[50]
}
System.out.println("exit from B");
class C extends Thread
public void run()
for(int k=1;k<5;k++)
System.out.println("\t From Thread C : k ="+k);
if(k==1)
try
sleep(1000);
catch(Exception e)
System.out.println("exit from C");
}
[51]
}
class ThreadMethods
public static void main(String args[])
A threadA = new A();
B threadB = new B();
C threadC = new C();
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");
[52]
Output:
[53]
Program 28: Write a program to illustrate the use of Priority in Threads.
Solution: class A extends Thread
public void run()
System.out.println("thread A started");
for(int i=1;i<=4;i++)
System.out.println("\n From thread A:i="+i);
System.out.println("Exit from A");
class B extends Thread
public void run()
System.out.println("thread B started");
for(int j=1;j<=4;j++)
System.out.println("\n From thread B:j="+j);
System.out.println("Exit from B");
}
[54]
}
class C extends Thread
public void run()
System.out.println("thread C started");
for(int k=1;k<=4;k++)
System.out.println("\n From thread C:k="+k);
System.out.println("Exit from C");
class ThreadPriority1
public static void main(String args[])
A threadA=new A();
B threadB=new B();
C threadC=new C();
threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority()+1);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println("Start thread A");
[55]
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");
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)
System.out.println("Division by zero");
y = a/(b+c);
System.out.println("y = " + y);
[57]
Output:
[58]
Program 30: Write a program for throwing our own exception.
Solution: import java.lang.Exception;
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]
System.out.println("Caught my exception");
System.out.println(e.getMessage());
finally
System.out.println("I am always here");
Output:
Program 31: Write a simple HelloJava Applet.
Solution: import java.awt.*;
[60]
import java.applet.*;
public class HelloJava extends Applet
public void paint(Graphics g)
g.drawString("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=HelloJava.class
width=400
height=400>
</applet>
</center>
</body>
</html>
Output:
[61]
Program 33: Write an applet HelloJava PARAM.
Solution: import java.awt.*;
import java.applet.*;
[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)
g.drawString(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=HelloJavaParam.class
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 java.awt.*;
import java.applet.*;
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: "+String.valueOf(sum);
g.drawString(s,50,50);
Program 36: Write a program to make HTML file for displaying
numerical values.
Solution: <html>
<applet code=NumValues.class
width=300
height=300>
</applet>
</html>
[66]
Output:
[67]
Program 37: Write a program for interactive input to an applet.
Solution: import java.awt.*;
import java.applet.*;
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: "+String.valueOf(sum);
g.drawString(s,50,50);
[68]
Program 38: Write a program for interactive input to userin applet.
Solution: <html>
<applet code=NumValues.class
width=300
height=300>
</applet>
</html>
Output:
[69]
Program 39: Write a program for writing bytes to a file.
Solution: import java.io.*;
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("city.txt");
outfile.write(cities);
outfile.close( );
catch (IOException ioe)
System.out.println(ioe);
System.exit(-1);
[70]
Output:
[71]
Program 40: Write a program for reading bytes to a file.
Solution: import java.io.*;
class ReadBytes
public static void main(String args[ ])
FileInputStream infile=null;
int b;
try
infile = new FileInputStream(args [0]);
while((b=infile.read())!=-1)
System.out.print((char)b);
infile.close( );
catch (IOException ioe)
System.out.println(ioe);
[72]
Output:
[73]
Program 41: Write a program for copying bytes from one file to another.
Solution: import java.io.*;
class CopyBytes
public static void main(String args[])
FileInputStream infile=null;
FileOutputStream outfile=null;
byte byteRead;
try
infile =new FileInputStream("city.txt");
outfile=new FileOutputStream("city1.txt");
do
byteRead = (byte) infile.read( );
outfile.write(byteRead);
while(byteRead!=-1);
catch (FileNotFoundException e)
System.out.println( "File not found");
}
[74]
catch(IOException e)
System.out.println(e.getMessage ( ));
finally
try
infile.close( );
outfile.close();
catch (IOException e)
[75]
Output:
[76]
Program 42: Write a program for writing characters to a file.
Solution: import java.io.*;
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("alphabets.txt");
outfile.write(alpha);
outfile.close();
catch(IOException ioe)
System.out.println(ioe);
System.exit(-1);
[77]
Output:
[78]
Program 43: Write a program for reading characters to a file.
Solution: import java.io.*;
class ReadCharacters
public static void main(String args[])
FileReader infile = null;
int b;
try
infile = new FileReader (args [ 0 ] ) ;
while( (b = infile.read ( ) ) != -1)
System.out.print( ( char ) b);
infile.close();
catch(IOException ioe)
System.out.println(ioe);
[79]
Output:
[80]
Program 44: Write a program for copying characters from one file to
another.
Solution: import java.io.*;
class CopyCharacters
public static void main(String args[])
File inFile=new File("city.txt");
File outFile=new File("city1.txt");
FileReader ins=null;
FileWriter outs=null;
try
ins=new FileReader(inFile);
outs=new FileWriter(outFile);
int ch;
while((ch=ins.read())!= -1)
outs.write(ch);
catch(IOException e)
System.out.println(e);
[81]
System.exit(-1);
finally
try
ins.close();
outs.close();
catch(IOException e){ }
Output:
[82]
Program 45: Write a program for reading and writing primitive data.
Solution: import java.io.*;
class ReadWritePrimitive
public static void main(String args[])
throws IOException
File primitive=new File("prim.dat");
FileOutputStream fos=new FileOutputStream(primitive);
DataOutputStream dos=new DataOutputStream(fos);
dos.writeInt(1999);
dos.writeDouble(375.85);
dos.writeBoolean(false);
dos.writeChar('X');
dos.close();
fos.close();
FileInputStream fis=new FileInputStream(primitive);
DataInputStream dis=new DataInputStream(fis);
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
System.out.println(dis.readChar());
dis.close();
fis.close();
[83]
}
Output:
[84]