Você está na página 1de 45

Q1.

Write a java program to calculate area of a circle and rectangle


by initializing values through constructor overloading and using a
method.

class Area
{
public static void main(String s[])
{
Ar b=new Ar(10);
b.calArea();
Ar b2=new Ar(20,30);
b2.calArea();
}
}
class Ar
{
int r;
int l;
int b;
float area;
Ar(int c)
{
r=c;
}
Ar(int a,int b)
{
l=a;
this.b=b;
}
void calArea()
{
if (r>0)
{
System.out.println("Area of circle is " + (3.14*r*r));
}
else
{
System.out.println("Area of rectangle is " + (l*b));
}
}
}

OUTPUT

Q2. Write a java program to show the use of this and super
keywords in a single program.
class A
{
public static void main(String s[])
{
B b=new B();
b.show();
}
}
class B extends C
{
int i;
B()
{
this(10);
}
B(int a)
{
super(30);
i=a;
}
void show()
{
int i=20;
System.out.println("class B ...i (local variable) = " + i);
System.out.println("class B ...i (instance variable) = " + this.i);
System.out.println("class C ...i (class C variable) = " + super.i);
}
}
class C
{
int i;
C(int a)
{
i=a;
}
void show()
{
System.out.println("C");
}
}

OUTPUT

Q3. Write a java program to show the use of method overriding by


overriding any method of Object class your own class.

class A
{
public static void main(String s[])
{
B b=new B();
System.out.println(b);
}
}

class B
{
public String toString()
{
return "yoyohoneysingh";
}
}

OUTPUT

Q4. Write a java program to check if first command line


arguments reverse is equal to second command line argument.

class A
{
public static void main(String a[])
{
String s=a[0];
String m="";
for(int i=s.length()-1;i>=0;i--)
m=m+s.charAt(i);
String s2=a[1];
if(m.equals(s2))
System.out.println("Arguments are same:");
else
System.out.println("Not same:");
}
}

OUTPUT

Q5. Write a java program to implement string array of size 2*3 by


command line argument and then print the string array as
telephone directory names.
class A
{
public static void main(String a[])
{
int l=a.length;
String c[][]=new String[10][10];
int k=0;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{
c[i][j]=a[k];
k++;
}
String z[]=new String[50];
int s=0;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{
z[s]=c[i][j];
s++;
}
String t=new String();
int q=0;
for(int i=0;i<6;i++)
for(int j=0;j<6-i-1;j++)
{
q=z[j].compareTo(z[j+1]);
if(q>0)
{
t=z[j];
z[j]=z[j+1];
z[j+1]=t;
}
}
for (int i=0;i<6;i++)
System.out.println(z[i]);
}
}

OUTPUT

Q6. Write a java program to show the use of wrapper class object
into Vector. Add elements of different type then count those and
print the number of Integer, Double, Boolean, etc.
import java.util.*;
class A
{
public static void main(String ags[])
{
Vector v=new Vector();
Integer I=new Integer("12");
Double D=new Double("23.32");
A a=new A();
Character C=new Character('c');
Boolean B=new Boolean(true);
v.addElement(I);
v.addElement(D);
v.addElement(I);
v.addElement(D);
v.addElement(a);
v.addElement(C);
v.addElement(B);
v.addElement(C);
System.out.println(v);
int i=0,d=0,c=0,b=0,ad=0;
for(Object o:v)
{
if(o instanceof Integer)
i++;
else if(o instanceof Double)
d++;
else if(o instanceof Character)
c++;
else if(o instanceof Boolean)
b++;
else if(o instanceof A)
ad++;
}
System.out.println("Integer : "+i);
System.out.println("Double : "+d);
System.out.println("Character : "+c);
System.out.println("Boolean : "+b);
System.out.println("A type : "+ad);
}
}

OUTPUT

Q7. Write a java program to initialize a string through command


line argument and then change the value of string as suppose string
is this is amit gupta and the changed value should be
t.i.a.gupta.

class A
{
public static void main(String a[])
{
String n[]=a[0].split(" ");
String s="";
int i;
for(i=0;i<(n.length-1);i++)
{
s=s+n[i].charAt(0)+".";
}
s=s+n[i];
System.out.println(s);
}
}

OUTPUT

Q8. Write a java program to create a package and put an interface


into it. Interface is having a variable and a method. You have this
variable in another package class as well as method in another
package class.

package p;
import q.I;
class A implements I
{
public static void main(String args[])
{
System.out.println(I.i);
A a=new A();
a.show();
}
public void show()
{
System.out.println("Interface OverRidden");
}
}

package q;
public interface I
{
int i=10;
void show();
}

OUTPUT

Q9. Write a java program to show the use of jar file in static
import.

package p;
import static q.B.*;
class A
{
public static void main(String a[])
{
show();
}
}

package q;
public class B
{
public static void show()
{
System.out.println("B");
}
}

OUTPUT

Q10. Write a java program to throw a custom exception if first two


command line arguments are not same with value not same else
same.
class A
{
public static void main(String a[])
{
B b=new B(a[0],a[1]);
try
{
b.show();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("End..");
}
}
class B
{
String a=new String();
String b=new String();
B(String x,String y)
{
a=x;
b=y;
}
void show()throws Exception
{
C n=new C("Not Same");
if (a.equals(b))
System.out.println("Same");
else
throw n;
}
}
class C extends Exception
{
C(String s)
{
super(s);
}
}

OUTPUT

Q11. Write a java program to show the use of try with resource
after creating multiple resource.
import java.io.*;
class A
{
public static void main(String a[])throws Exception
{
try(B b=new B();C c=new C();)
{
}
}
}

class B implements Closeable


{
public void close()
{
try
{
String file="b.txt";
File f=new File(file);
if(f.exists()==false)
{
f.createNewFile();
System.out.println(file+" File does not exists: Creating new file");
}
else
System.out.println(file+" File already exists:");
File d=new File("C:/Users/yogesh singh negi/Desktop/class");
String m[]=d.list();
for(String n:m)
{
if(n.contains(".html"))
{
FileReader fr2=new FileReader(n);
int s;
FileWriter fw=new FileWriter(f,true);
while((s=fr2.read()) != -1)
fw.write((char)s);
fw.close();
}}}catch(Exception e){}

}
}
class C implements Closeable
{
public void close()
{
try
{
String file="a.txt";
File f=new File(file);
if(f.exists()==false)
{
f.createNewFile();
System.out.println(file+" File does not exists: Creating new file");
}
else
System.out.println(file+" File already exists:");
File d=new File("C:/Users/yogesh singh negi/Desktop/class");
String m[]=d.list();
for(String n:m)
{
if(n.contains(".java"))
{
FileReader fr2=new FileReader(n);
//BufferedReader br=new BufferedReader(fr2);
int s;
FileWriter fw=new FileWriter(f,true);
//BufferedWriter bw=new BufferedWriter(fw);
while((s=fr2.read()) != -1)
fw.write((char)s);
fw.close();
}}}catch(Exception e){}
}
}

OUTPUT

Q12. Write a java program to check whether a.txt file exists in


C:\Users\yogesh singh negi\Desktop\yogesh_mca_java or not. If
not exists then create it in order to copy all java extension file of this
directory into it.
import java.io.*;
class A
{
public static void main(String a[])throws Exception
{
String file="a.txt";
File f=new File(file);
if(f.exists()==false)
{
f.createNewFile();
System.out.println(file+" File does not exists: Creating new file");
}
else
System.out.println(file+" File already exists:");
try
{
File d=new File("C:/Users/yogesh singh negi/Desktop/yogesh_mca_java");
String m[]=d.list();
for(String n:m)
{
if(n.contains(".java"))
{
FileReader fr2=new FileReader(n);
//BufferedReader br=new BufferedReader(fr2);
int s;
FileWriter fw=new FileWriter(f,true);
//BufferedWriter bw=new BufferedWriter(fw);
while((s=fr2.read()) != -1)
fw.write((char)s);
fw.close();
}}}catch(Exception e){}
}
}

OUTPUT

Q13. Write a java program to show the use of transient keyword


when we writing an object state into a file.
import java.io.*;
class A
{
public static void main(String a[])throws Exception
{
B b=new B();
String s="b.txt";
FileOutputStream fos=new FileOutputStream(s);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(b);
fos.close();
FileInputStream fis=new FileInputStream(s);
ObjectInputStream ois=new ObjectInputStream(fis);
B b1=(B)ois.readObject();
System.out.println(b1.i);
System.out.println(b1.j);
System.out.println(b1.k);
System.out.println(b1.l);
}
}
class B implements Serializable
{
int i=10;
static int j=20;
final int k=30;
transient int l=50;
}

OUTPUT

Q14. Write a java program to show the use of Runnable interface


in Thread synchronization.
import java.io.*;
class A
{
public static void main(String a[])throws Exception
{
B b=new B(2);
B b1=new B(3);
B b2=new B(4);
}
}
class B implements Runnable
{
int x;
B(int x)
{
this.x=x;
Thread t=new Thread(this);
t.start();
}
public void run()
{
C.print(x);
}
}
class C
{
static synchronized void print(int x)
{
for(int i=0;i<=5;i++)
{
System.out.println(i*x);
try
{
Thread.sleep(500);
}
catch(Exception e){}
}
}
}

OUTPUT

Q15. Write a java program to understand the concept of inter


Thread communication.
import java.io.*;
import java.util.*;
class A
{
public static void main(String a[])throws Exception
{
final customer c=new customer();
new Thread(){public void run(){c.withdraw(500);}}.start();
new Thread(){public void run(){c.deposite(220);}}.start();
}
}
class customer
{
int amt=100,w=0;
synchronized void withdraw(int amt)
{
w=amt;
System.out.println("Going to Withdraw "+amt+" "+this.amt);
if(this.amt<amt)
{
System.out.println("Less Balance: Waiting for depsite "+amt+" "+this.amt);
try
{
wait();
}
catch(Exception e)
{}
}
this.amt-=amt;
System.out.println("Withdraw Compeleted "+amt);
}
synchronized void deposite(int amt)
{
System.out.println("Going to Deposite "+amt+" "+this.amt);
this.amt+=amt;
System.out.println("Deposite compeleted "+amt+" "+this.amt);
while(this.amt<w)
{
deposite(amt);
}
notify();
}
}

OUTPUT

Q16. Write a java program to implement GUI application that


calculates the factorial of a number on button click extended into
first textbox.
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class A extends Applet implements ActionListener
{
Button b;
TextField t1,t2;
public void init()
{
b=new Button("FACTORIAL");
b.addActionListener(this);
t1=new TextField(20);
t2=new TextField(20);
add(b);
add(t1);
add(t2);
}
public void actionPerformed(ActionEvent e)
{
int fact=1;
int n=Integer.parseInt(t1.getText());
for(int i=1;i<=n;i++)
fact=fact*i;
t2.setText(""+fact);
}
}
//<applet code=A height=500 width=500></applet>

OUTPUT

Q17. Write a java program to implement GUI application that


shows us the use of CardLayout, GridLayout and BorderLayout in
a single program.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Y extends Applet implements ActionListener
{
Label l;
Button b;
Panel p2;
CardLayout cd;
CheckboxGroup cbg1,cbg2,cbg3;
public void init()
{
GridLayout gd=new GridLayout(3,1,50,50);
setLayout(gd);
Panel p5=new Panel();
p5.setLayout(new BorderLayout());
l=new Label("Computer Quiz");
p5.add(l,BorderLayout.NORTH);
add(p5);
p2=new Panel();
cd=new CardLayout();
p2.setLayout(cd);
Panel p1=new Panel();
Panel p=new Panel();
p.setLayout(new GridLayout(5,1));
p.add(new Label("Who is the CEO of GOOGLE COPR."));
p.add(new Panel().add(new Checkbox(" Sundar Pichai")));
p.add(new Panel().add(new Checkbox(" Steve Jobs")));
p.add(new Panel().add(new Checkbox(" Bill Gates")));
p.add(new Panel().add(new Checkbox(" Satya Nadela")));
p1.add(p);
Panel p3=new Panel();
Panel p4=new Panel();
p4.setLayout(new GridLayout(5,1));
p4.add(new Label("Who is the founder of APPLE"));
p4.add(new Panel().add(new Checkbox(" Sundar Pichai")));

p4.add(new Panel().add(new Checkbox(" Steve Jobs")));


p4.add(new Panel().add(new Checkbox(" Satya Nadela")));
p4.add(new Panel().add(new Checkbox(" Bill Gates")));
p3.add(p4);
Panel p7=new Panel();
Panel p8=new Panel();
p8.setLayout(new GridLayout(5,1));
p8.add(new Label("Which amoung the following is not an OS"));
p8.add(new Panel().add(new Checkbox(" Win7")));
p8.add(new Panel().add(new Checkbox(" Linux")));
p8.add(new Panel().add(new Checkbox(" i3")));
p8.add(new Panel().add(new Checkbox(" Vista")));
p7.add(p8);
p2.add(p1);
p2.add(p3);
p2.add(p7);
add(p2);

Panel p6=new Panel();


b=new Button("CLICK TO NEXT");
b.addActionListener(this);
p6.add(b);
add(p6);
}
public void actionPerformed(ActionEvent e)
{
cd.next(p2);
}
}
/*
<applet code=Y width=500 height=500></applet>
*/

OUTPUT

Q18. Write a java program to implement GUI application similar to


login window which have two textboxes and a button. If the entered
value in the textboxes are same then a frame window will open with
a label on it saying Welcome else a frame window will open with
again two textboxes and a button to re-login.
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class G extends Applet implements ActionListener
{
Label l1,l2;
Button b;
TextField t1,t2;
Panel p2;
public void init()
{
p2=new Panel();
p2.setLayout(new GridLayout(3,2,10,10));
l1=new Label("LogIn");
l2=new Label("Password");
b=new Button("OK");
b.addActionListener(this);
t1=new TextField(20);
t2=new TextField(20);
p2.add(l1);
p2.add(t1);
p2.add(l2);
p2.add(t2);
p2.add(b);
add(p2);
}
public void actionPerformed(ActionEvent e)
{
String s=new String(t1.getText());
String m=new String(t2.getText());
if(s.equals(m)==true)
{
H h=new H();
h.show();
}
else if(s.equals(m)==false)
{

H j=new H();
j.show2();
}
}
}
class H extends WindowAdapter
{
int i;
Frame f=null,f1=null;
Label l3,l4;
Button b1;
TextField t3,t4;
Panel p;
void show()
{
i=0;
f=new Frame("Same");
f.setVisible(true);
f.setSize(400,400);
f.add(new Label("WELCOME"));
f.addWindowListener(this);
}
void show2()
{
i=1;
f1=new Frame("Not Same");
f1.setVisible(true);
f1.setSize(400,400);
f1.setLayout(new FlowLayout());
p=new Panel();
p.setLayout(new GridLayout(3,2,10,10));
l3=new Label("LogIn");
l4=new Label("Password");
b1=new Button("SAVE");
t3=new TextField(20);
t4=new TextField(20);
p.add(l3);
p.add(t3);
p.add(l4);
p.add(t4);
p.add(b1);
f1.add(p);
f1.addWindowListener(this);

}
public void windowClosing(WindowEvent e)
{
if(i==0)
f.dispose();
else if(i==1)
f1.dispose();
}
}
//<applet code="G" width=500 height=500></applet>

OUTPUT

Q19. Write a java program in which one machine will send a


number to another machine and another machine will return
factorial of that number.
import java.io.*;
import java.net.*;
public class X
{
public static void main(String a[])throws Exception
{
ServerSocket ss=new ServerSocket(2000);
System.out.println("Start");
Socket s=ss.accept();
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
String m=dis.readUTF();
int f=Integer.parseInt(m);
int fact=1;
for(int i=1;i<=f;i++)
fact=fact*i;
m=""+fact;
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeUTF(m);
s.close();
}
}
import java.io.*;
import java.net.*;
public class Y
{
public static void main(String a[])throws Exception
{
Socket s=new Socket("localhost",2000);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeUTF("5");
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
String m=dis.readUTF();
System.out.println(m);
s.close();
}
}

OUTPUT

Q20. Write a java program to the use of simple RMI concept.


import java.rmi.*;
public interface Adder extends Remote
{
public int fact(int x)throws RemoteException;
}

import java.rmi.*;
import java.rmi.server.*;
public class AdderRemote extends UnicastRemoteObject implements Adder
{
AdderRemote()throws RemoteException{}
public int fact(int x)
{int f=1;
for (int i=1;i<=x;i++)
f*=i;
return f;
}
}

import java.rmi.*;
import java.net.*;
import java.rmi.registry.*;
public class MyClient
{
public static void main(String a[])
{
try
{
Adder s=(Adder)Naming.lookup("rmi://localhost/yo");
System.out.println(s.fact(5));
}
catch(Exception e)
{}
}
}

import java.rmi.*;
import java.net.*;
import java.rmi.server.*;
public class MyServer
{
public static void main(String a[])
{
try
{
Adder s=new AdderRemote();
Naming.rebind("yo",s);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT

Você também pode gostar