Java File PGDCA Second Semester
Java File PGDCA Second Semester
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.
class Math1
double x;
x = Math.max(12,7);
x = Math.sqrt(81);
x = Math.abs(-77);
x = Math.pow(2,3);
x = Math.min(12,7);
x = Math.log(7);
[3]
Output:
[4]
Program 3: Write a program that reads data from keyboard using
readLine().
class NumberOperation
int num1=0;
int num2=0;
try
num1 = Integer.parseInt(in.readLine());
num2 = Integer.parseInt(in.readLine());
catch(Exception e)
{}
}
[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.
int ip,cg,dcn,edp,maths;
class Result
s1.getdata(80,75,70,65,90);
float per1=(tm1*100)/500;
s2.getdata(92,95,64,75,66);
float per2=(tm2*100)/500;
[7]
System.out.println(" Total Marks of Second Student is " + tm2);
s3.getdata(45,78,64,66,84);
float per3=(tm3*100)/500;
s4.getdata(56,78,75,35,54);
float per4=(tm4*100)/500;
s5.getdata(98,74,78,85,90);
float per5=(tm5*100)/500;
[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.
import java.lang.Math;
class AreaCircum
DataInputStream in = new
DataInputStream(System.in);
try
radius = Integer.parseInt(in.readLine());
catch(Exception e)
{}
}
[10]
Output:
[11]
Program 6: Write a program to use all control statements in one switch
statement.
int choice,r,q,n,p,i,a,b,c;
choice = 1;
switch(choice)
case 1:
p=1; n=5;
p=p*i;
break;
case 2:
[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.
int length,width;
length=x;
width=y;
int rectArea()
int area=length*width;
return(area);
class RectArea
int area1,area2;
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.
class DemoMethod
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.
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
{
[19]
Room r2=new Room(5);
Output:
[20]
Program 10: Write a program to demonstrate nesting of methods.
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[])
nest.display();
Output:
[22]
Program 11: Write a program to demonstrate Method Overriding.
int x;
Super(int x)
this.x=x;
void display()
System.out.println("Super x="+x);
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
s1.display();
Output:
[24]
Program 12: Write a program to demonstrate abstract class and method.
t.numberOfSides();
r.numberOfSides();
h.numberOfSides();
Output:
[26]
Program 13: Write a program to implement of Single inheritance.
int length;
int breadth;
Room(int x,int y)
length=x;
breadth=y;
int area()
return(length*breadth);
int height;
super(x,y);
height=z;
int volume()
[27]
{
return(length*breadth*height);
class InherTest
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.
table[i][j] = (i + 1) * (j + 1);
else
else
System.out.println(" ");
[29]
Output:
[30]
Program 15: Write a program to display the string in alphabetical order.
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.
void display1();
interface Interface2
void display2();
System.out.println("I am in Inetrface1");
System.out.println("I am in Inetrface2");
void display3()
[33]
}
class MultiInterfaces
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.
String name;
int rollno;
name=n;
rollno=r;
void showData()
float m5)
marks1=m1;
[36]
marks2=m2;
marks3=m3;
marks4=m4;
marks5=m5;
void showMarks()
interface Sports
void showsm();
float total;
void display()
total=marks1+marks2+marks3+marks4+marks5+spo
rtsmarks;
showData();
showMarks();
showsm();
class Hybrid
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.
Program 19: Write a program that imports the class classA from package
package1.
class PackageTest1
objectA.displayA();
[40]
Program 20: Write a program which makes a package named Package2
containing a single class ClassB.
[41]
Program 21: Write a program to import classes from other packages.
import package2.ClassB;
class PackageTest2
objectA.displayA();
objectB.displayB();
Output:
[42]
Program 22: Write a program which makes a package named areas
containing a single class Circle.
int radius;
public Circle(int r)
radius=r;
return(Math.PI*radius*radius);
int side;
public Square(int s)
side=s;
[43]
}
return(side * side);
int length;
int width;
length=l;
width=b;
return(length * width);
[44]
Program 25: Write a program that imports all the classes from the package
Areas.
import areas.Square;
import areas.Circle;
class AreaTest
[45]
Output:
[46]
Program 26: Write a program to create Threads using Thread Class.
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
class ThreadTest
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.
for(int i=1;i<5;i++)
if(i==1)
yield();
for(int j=1;j<5;j++)
if(j==3)
stop();
[50]
}
for(int k=1;k<5;k++)
if(k==1)
try
sleep(1000);
catch(Exception e)
}
[51]
}
class ThreadMethods
threadA.start();
threadB.start();
threadC.start();
[52]
Output:
[53]
Program 28: Write a program to illustrate the use of Priority in Threads.
System.out.println("thread A started");
for(int i=1;i<=4;i++)
System.out.println("thread B started");
for(int j=1;j<=4;j++)
}
[54]
}
System.out.println("thread C started");
for(int k=1;k<=4;k++)
class ThreadPriority1
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);
threadB.start();
threadC.start();
Output:
[56]
Program 29: Write a program to illustrate Try and Catch for Exception
Handling.
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);
[57]
Output:
[58]
Program 30: Write a program for throwing our own exception.
MyException(String message)
super(message);
class TestMyException
int x=5,y=1000;
try
float z=(float)x/(float)y;
if(z<0.01)
catch(MyException e)
{
[59]
System.out.println("Caught my exception");
System.out.println(e.getMessage());
finally
Output:
g.drawString("Hello java",10,100);
Solution: <html>
<body>
<center>
<applet code=HelloJava.class
width=400
height=400>
</applet>
</center>
</body>
</html>
Output:
[61]
Program 33: Write an applet HelloJava PARAM.
import java.applet.*;
[62]
public class HelloJavaParam extends Applet
String str;
if(str==null)
g.drawString(str,10,100);
Solution: <html>
<body>
[63]
<applet code=HelloJavaParam.class
width=400
height=400>
</applet>
</body>
</html>
Output:
[64]
[65]
Program 35: Write a program for displaying numerical values.
import java.applet.*;
int value1=10;
int value2=20;
int sum=value1+value2;
g.drawString(s,50,50);
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.
import java.applet.*;
int value1=10;
int value2=20;
int sum=value1+value2;
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.
class WriteBytes
'R','A','S','\n','L','O','N','D','O','N','\n'};
FileOutputStream outfile=null;
try
outfile.write(cities);
outfile.close( );
System.out.println(ioe);
System.exit(-1);
[70]
Output:
[71]
Program 40: Write a program for reading bytes to a file.
class ReadBytes
FileInputStream infile=null;
int b;
try
while((b=infile.read())!=-1)
System.out.print((char)b);
infile.close( );
System.out.println(ioe);
[72]
Output:
[73]
Program 41: Write a program for copying bytes from one file to another.
class CopyBytes
FileInputStream infile=null;
FileOutputStream outfile=null;
byte byteRead;
try
outfile=new FileOutputStream("city1.txt");
do
outfile.write(byteRead);
while(byteRead!=-1);
catch (FileNotFoundException e)
}
[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.
class WriteCharacters
try
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.
class ReadCharacters
int b;
try
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.
class CopyCharacters
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.
class ReadWritePrimitive
throws IOException
dos.writeInt(1999);
dos.writeDouble(375.85);
dos.writeBoolean(false);
dos.writeChar('X');
dos.close();
fos.close();
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]