Escolar Documentos
Profissional Documentos
Cultura Documentos
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
class A
{
public static void main(String s[])
{
B b=new B();
System.out.println(b);
}
}
class B
{
public String toString()
{
return "yoyohoneysingh";
}
}
OUTPUT
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
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
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
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
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 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
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
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
OUTPUT
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