Download import java.applet.

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Q11’a’->WAP TO FIND OCCURRENCE OF A SUBSTRING IN A STRING USING SPLIT METHOD.
import java.io.*;
class Term11
{
public static void main(String s[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string");
String s1=br.readLine();
System.out.println("enter the substring");
String s2=br.readLine();
String s3[]=s1.split(" ");
int cn=0;
for(int i=0;i<s3.length;i++)
{
for(int j=0;j<s3[i].length();j++)
{
if(s3[i].charAt(j)==s2.charAt(0))
{
String sub=s3[i].substring(j,j+s2.length());
if(sub.equals(s2))
{
cn++;j=j+s2.length();
}
}
}
}
System.out.println("the occurence of substring "+s2+" is "+cn);
}
}
OUTPUT:-
Q11’b’->WITHOUT USING SPLIT METHOD.
import java.io.*;
class Term112
{
public static void main(String s[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string");
String s1=br.readLine();
System.out.println("enter the substring");
String s2=br.readLine();
int cn=0;
for(int j=0;j<s1.length();j++)
{
if(s1.charAt(j)==s2.charAt(0))
{
String sub=s1.substring(j,j+s2.length());
if(sub.equals(s2))
{
cn++;j=j+s2.length();
}
}
}
System.out.println("the occurence of substring "+s2+" is "+cn);
}
}
OUTPUT:-
Q13->WAP TO FIND OCCURRENCE OF UR NAME IN A FILE.
import java.io.*;
class Term13
{
public static void main(String s[])throws Exception
{
int cn=0;String m;
String m2="komal";
File fn=new File("D:/my.txt");
fn.createNewFile();
BufferedReader br=new BufferedReader(new FileReader(fn));
while((m=br.readLine())!=null)
{
for(int i=0;i<m.length();i++)
{
if(m.charAt(i)==m2.charAt(0))
{
String sub=m.substring(i,i+m2.length());
if(sub.equals(m2))
{
cn++;i=i+m2.length();
}
}
}
}
br.close();
System.out.println(cn);
}
}
OUTPUT:->
Q14->WAP to copy one file content into another file.
import java.io.*;
public class Question14
{
public static void main(String[] args) throws Exception{
FileReader fr= new FileReader(new File("temp"));
FileWriter fw= new FileWriter(new File("temp1"));
BufferedReader br= new BufferedReader(fr);
String str;
while((str=br.readLine()) != null)
{
fw.write(str);
}
fw.close();
File f= new File("temp1");
if(!f.exists())
System.out.println("file does not exist");
System.out.println("content of copied file=");
FileInputStream fis= new FileInputStream(f);
char data;
while((data=(char)fis.read()) != -1)
{
System.out.print(data);
}
fis.close();
fr.close();
}
}
OUTPUT:-
Q15:->WAP TO DRAW THE GIVEN FIGURE.
import java.awt.*;
import java.applet.*;
import java.util.*;
public class Term15 extends Applet
{
int x,y;
public void init()
{
System.out.println("enter the value of x,y=");
Scanner sc = new Scanner(System.in);
x=sc.nextInt();
y=sc.nextInt();
}
public void paint(Graphics g)
{
g.drawRect(x,y,66,66);
g.drawRect(x-21,y-19,110,110);
g.drawOval(x-14,y-14,97,97);
}
}
/*
<applet code="Term15.class" width=400 height=400>
</applet>
*/
OUTPUT:->
Q16->wap to draw a square with red color. color of this square will
Reduce after each 5ms?
import java.awt.Color;
import java.applet.Applet;
import java.awt.Graphics;
import java.util.Timer;
public class Question16 extends Applet{
int x=0;
public void paint(Graphics g)
{
x=x+25;
Color c= new Color(255-x,255-x,255-x);
g.drawRect(0,0,50,50);
g.setColor(c);
g.fillRect(0,0,50,50);
try{
Thread.sleep(1000);
repaint();
}catch (Exception e) {
}
}
}
/*
<applet code="Question16.java" width=400 height=400>
</applet>
*/
OUTPUT:-
Q17->WAP TO DRAW A CIRCLE AND SQUARE USING BUTTON.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Term17 extends Applet implements ActionListener
{
Button b1,b2;Panel p1;int flag=2;
public void init()
{
p1=new Panel();
b1=new Button("Circle");
b2=new Button("Square");
add(p1);p1.add(b1);p1.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
flag=1;repaint();
}
else
{
flag=0;repaint();
}
}
public void paint(Graphics g)
{
if(flag==1)
g.drawOval(150,150,100,100);
else if(flag==0)
g.drawRect(150,150,100,100);
}
}
/*
<applet code="Term17.java" width=400 height=400>
</applet>
*/
OUTPUT:->
Q18->wap to validate a textfield as email field means only syntax of abc @pqr.com can be
* used in textfield?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;
public class Question18 extends Applet implements ActionListener
{
Pattern p=Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*");
Button b;
TextField tf;
public void init()
{
Panel p= new Panel();
add(p);
Label l= new Label("enter the email");
p.add(l);
tf=new TextField(30);
b= new Button("Validate");
b.addActionListener(this);
p.add(tf);
p.add(b);
}
public void actionPerformed(ActionEvent e)
{
String s= tf.getText();
System.out.println(s);
Matcher m=p.matcher(s);
boolean b1=m.matches();
if(b1==true)
{
System.out.println("Valid Email ID");
}
else
{
System.out.println("InValid Email ID");
}
}
}
/*
<applet code="Question18.java" width=400 height=400>
</applet>
*/
OUTPUT:
Q19->wap to create a scientific calculator that may perform at least 6 calculations
* including power,squareroot, sign?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Question19 extends Applet implements ActionListener{
Panel p1,p2,p3,p4; int d1,d2,op,d=1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bp,bmi,bm,bd,clear,dec,result,pow;
TextField t1; FlowLayout f1,f2;
GridLayout g1,g2; Label l;
public void init()
{
p1= new Panel();
p2 = new Panel();
p3=new Panel(); p4=new Panel();
g2= new GridLayout(4,1); setLayout(g2);
l= new Label("Calculater"); t1= new TextField(45);
result= new Button("=");
add(p4);
add(p1);
add(p3);
p4.add(l); p1.add(t1);
p3.add(result); result.addActionListener(this);
g1= new GridLayout(4,4 );
p2.setLayout(g1);
add(p2);
b1 = new Button("1"); p2.add(b1);b1.addActionListener(this);
b2 = new Button("2"); p2.add(b2);b2.addActionListener(this);
b3 = new Button("3"); p2.add(b3);b3.addActionListener(this);
bp = new Button("+"); p2.add(bp);bp.addActionListener(this);
b4 = new Button("4"); p2.add(b4);b4.addActionListener(this);
b5 = new Button("5"); p2.add(b5);b5.addActionListener(this);
b6 = new Button("6"); p2.add(b6);b6.addActionListener(this);
bmi = new Button("-"); p2.add(bmi);bmi.addActionListener(this);
b7 = new Button("7"); p2.add(b7);b7.addActionListener(this);
b8 = new Button("8"); p2.add(b8);b8.addActionListener(this);
b9 = new Button("9"); p2.add(b9);b9.addActionListener(this);
bm = new Button("*"); p2.add(bm);bm.addActionListener(this);
clear= new Button("c"); p2.add(clear);clear.addActionListener(this);
b0 = new Button("0"); p2.add(b0);b0.addActionListener(this);
pow = new Button("^"); p2.add(pow);pow.addActionListener(this);
bd = new Button("/"); p2.add(bd);bd.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String x=e.getActionCommand();
String x1=t1.getText();
if(x=="0"||x=="1"||x=="2"||x=="3"||x=="4"||x=="5"||x=="6"||x=="7"||x=="9"||x=
="8")
{
if(t1.getText() == "")
t1.setText(x);
else
t1.setText(x1+x);
}
if(x == "c")
t1.setText("");
if(x=="+")
{
try{
d1=Integer.parseInt(x1);
}catch (Exception e1) {
}
t1.setText(""); op=1;
}
else if(x=="-")
{
try{
d1=Integer.parseInt(x1);
}catch (Exception e1) {
}
t1.setText(""); op=2;
}
else if(x=="*")
{
try{
d1=Integer.parseInt(x1);
}catch (Exception e1) {
}
t1.setText(""); op=3;
}
else if(x=="/")
{
try{
d1=Integer.parseInt(x1);
}catch (Exception e1) {
}
t1.setText(""); op=4;
}
else if(x=="^")
{
try{
d1=Integer.parseInt(x1);
}catch (Exception e1) {
}
t1.setText(""); op=5;
}
if(x=="=")
{
try{
d2=Integer.parseInt(t1.getText());
d=1;
}catch (Exception e1) {
}
t1.setText("");
if(op==1)
{
d=d1+d2;
t1.setText(Integer.toString(d));
}
if(op==2){
d=d1-d2;
t1.setText(Integer.toString(d));
}
if(op==3){
d=d1*d2;
t1.setText(Integer.toString(d));
}
if(op==4){
d=d1/d2;
t1.setText(Integer.toString(d));
}
if(op==5){
try{
for(int i=1;i<=d2;i++)
d=d*d1;
t1.setText(Integer.toString(d));
}catch (Exception e1) {
}
}
op=6;
}
}
}
/*
<applet code="Question18.java" width=400 height=400>
</applet>
*/
OUTPUT:-
Q20->wap to create a login form with validation. login name can be only character values
while * password only numeric values using frame and window closing event.
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;
public class Question20 extends Frame implements ActionListener{
Pattern p1 = Pattern.compile("[1-zA-Z]*");
Pattern p2=Pattern.compile("[0-9]*");
public static TextField name,age;
public static void main(String[] s)
{
Question20 q= new Question20();
Frame f = new Frame(); FlowLayout fl = new FlowLayout();
f.setLayout(fl); f.setVisible(true);f.setSize(200, 200);
name = new TextField(30); age= new TextField(30);
Label l1= new Label("enter the name");
Label l2 = new Label("enter the age");
f.add(l1);
f.add(name);f.add(l2);f.add(age);
Button b= new Button("validate");
f.add(b);
b.addActionListener(q);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowAdapter e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
Matcher m1=p1.matcher(name.getText());
boolean b1= m1.matches();
Matcher m2=p2.matcher(age.getText());
boolean b2= m2.matches();
System.out.println(b1+","+b2);
if(b1==true)
System.out.println("valid name");
else
System.out.println("invalid name");
if(b2==true)
System.out.println("valid age");
else
System.out.println("invalid age");
} }
/*
<applet code="Question20.java" width=400 height=400>
</applet>
*/
OUTPUT:Q21->WAP wap to select a country from a list on selection appropriate flag with 5 line
description of that country will display in textarea.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class Term21 extends Applet implements ActionListener
{
String s1,s2,s3,s4,s5;
Label l1;List l=new List(5);TextArea t1;Image im1,im2,im3,im4,im5;int flag;
public void init()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
l1=new Label("SELECT ANY COUNTRY"); t1=new TextArea(10,70);
add(l1);add(l);add(t1);
im1=getImage(getDocumentBase(),getParameter("img1"));
im2=getImage(getDocumentBase(),getParameter("img2"));
im3=getImage(getDocumentBase(),getParameter("img3"));
im4=getImage(getDocumentBase(),getParameter("img4"));
im5=getImage(getDocumentBase(),getParameter("img5"));
l.add("INDIA");l.add("USA");l.add("ENGLAND");l.add("CHINA");l.add("PAKISTAN");
l.addActionListener(this);
s1="India, officially the Republic of India is a country in South Asia,\n"+" It is the
seventh-largest ountry by geographical area, the second-most populous country with over 1.2
billion people,\n"+"and the most populous democracy in the world.";
s2="The United States of America is a federal constitutional republic comprising fifty
states and a federal district.\n"+" The country is situated mostly in central North America,
where its forty-eight contiguous states and Washington,\n"+" D.C., the capital district, lie
between the Pacific and Atlantic Oceans,\n"+" bordered by Canada to the north and Mexico
to the south. ";
s3="England is a country that is part of the United Kingdom.\n"+"It shares land
borders with Scotland to the north and Wales to the west;\n"+" the Irish Sea is to the north
west, the Celtic Sea to the south west,\n"+" while the North Sea to the east and the English
Channel to the south separate it from continental Europe";
s4="Pakistan, officially the Islamic Republic of Pakistan ,is a sovereign country in South
Asia.\n"+"It sits at the crossroads of the strategically important regions of South Asia,\n"+"
Central Asia and the Middle East.";
s5="China, officially the People's Republic of China (PRC),\n"+" is the world's mostpopulous country,\n"+" with a population of over 1.3 billion.\n"+" The East Asian state
covers approximately 9.6 million square kilometres,";
}
public void actionPerformed(ActionEvent e)
{
if(l.getSelectedItem()=="INDIA")
{
flag=0;repaint();t1.setText(s1);
}
else if(l.getSelectedItem()=="USA")
{
flag=1;repaint();t1.setText(s2);
}
else if(l.getSelectedItem()=="ENGLAND")
{
flag=2;repaint();t1.setText(s3);
}
else if(l.getSelectedItem()=="CHINA")
{
flag=3;repaint();t1.setText(s5);
}
else if(l.getSelectedItem()=="PAKISTAN")
{
flag=4;repaint();t1.setText(s4);
}
}
public void paint(Graphics g)
{
if(flag==0)
g.drawImage(im1,250,200,this);
if(flag==1)
g.drawImage(im2,250,200,this);
if(flag==2)
g.drawImage(im3,250,200,this);
if(flag==3)
g.drawImage(im4,250,200,this);
if(flag==4)
g.drawImage(im5,250,200,this);
}
}
/*
<applet code="Term21.java" width=1000 height=1000>
<param name="img1" value="fgg2.jpg">
<param name="img2" value="usa.jpg">
<param name="img3" value="england.jpg">
<param name="img4" value="china.jpg">
<param name="img5" value="pakis.jpg">
</applet>
*/
OUTPUT:
Q23->wap to create a thread based applet which will display a text within a range of a
window.
import java.applet.Applet;
import java.awt.Graphics;
public class Question23 extends Applet
{
int i=9,j=9;
int x=getX();
Graphics g;
public void paint(Graphics g)
{
for(i=0;i<700;i=i+16)
{
try
{
g.drawString("hello",i,i);
Thread.sleep(400);
repaint();
}catch (Exception e) {}
}
}
}
OUTPUT:
Q24-> WAP where a thread can be synchronized.
public class Term24
{
public static void main(String s[])
{
A p=new A();
B b1=new B(p,"Komal");
B b2=new B(p,"Dobhal");
}
}
class A
{
synchronized void show(String s)throws Exception
{
System.out.print("("+s);
Thread.sleep(200);
System.out.print(")");
}
}
class B extends Thread
{
A x; String n;
B(A y, String s)
{
x=y;n=s;
this.start();
}
public void run()
{
try{
x.show(n);
}
catch(Exception e)
{
}
}
}
OUTPUT:
Q26->Wap for connection oriented apps to send a number from client to server will
respond with its square?
//SERVER SIDE
import java.io.*;
import java.net.*;
class Server
{
public static void main(String s[]) throws Exception
{
ServerSocket ss=new ServerSocket(2000);
System.out.println("Start");
Socket s1=ss.accept();
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is);
int st=dis.readInt();
OutputStream os=s1.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeInt(st*st);
dis.close();dos.close();
}
}
//CLIENT SIDE
import java.io.*;
import java.net.*;
import java.util.*;
public class Client
{
public static void main(String s[]) throws Exception
{
Scanner sc=new Scanner(System.in)
Socket s2=new Socket("DeepakSharma-PC",2000);
System.out.println("enter a no");
int n=sc.nextInt();
OutputStream os=s2.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeInt(n);
InputStream is=s2.getInputStream();
DataInputStream dis=new DataInputStream(is);
int st=dis.readInt();
System.out.println("the square is "+st);
dos.close(); dis.close();
}
}
OUTPUT:-
Q26->Wap to read a from URL and then to copy all the content into a file?
import java.io.*;
import java.net.*;
import java.util.Date;
public class Question27 {
public static void main(String[] str) throws Exception
{
URL obj = new URL("http://www.yahoo.com/index.html");
URLConnection conn = obj.openConnection();
System.out.println("Date:"+new Date(conn.getDate()));
System.out.println("Content-type: " +conn.getContentType());
System.out.println("Expiry-date: "+conn.getExpiration());
System.out.println("Last modification time: "+ new Date(conn.getLastModified()));
int l=conn.getContentLength();
System.out.println("length of content: "+l);
if(l==0)
System.out.println("content is not available");
else
{
int ch;
InputStream in = conn.getInputStream();
File f = new File("Index");
FileWriter fw = new FileWriter(f);
while((ch=in.read()) != -1)
fw.write((char)ch);
fw.close();
}
}
}
OUTPUT:-
Q27->IMPLEMENTATION OF A STACK USING VECTOR CLASS.
import java.io.*;
import java.util.*;
class Term28
{
public static void main(String s[])throws Exception
{
Vector v=new Vector(10,3);int n;
Scanner sc=new Scanner(System.in);
System.out.println("A MENU DRIVEN PGM");
do{
System.out.println("1 for PUSH");
System.out.println("2 for POP");
System.out.println("3 for DISPLAY");
System.out.println("4 for EXIT");
System.out.println("enter ur choice");
n=sc.nextInt();
switch(n)
{
case 1:
System.out.println("Enter the element");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
v.addElement(br.readLine());break;
case 2:
System.out.println(v.elementAt(v.size()-1)+" is successfully removed");
v.removeElementAt(v.size()-1);
break;
case 3:
for(int i=v.size()-1;i>=0;i--)
System.out.println(v.elementAt(i));break;
case 4:
System.exit(1);
}
}while(n<5);
}
}
OUTPUT:-
Related documents