Вы находитесь на странице: 1из 64

Web Technology Lab Record

Exercise 1 Date: Title: Creation of Static Webpages using HTML Aim: To design Static Webpages using HTML

Source code: a) 1a.html


<html> <body> <h1> Table example page </h1> <p align="center"> Here is a more complex sample table </p> <table border="1"> <tr> <td rowspan="2" align="center"> <img src="Forest.jpg" width="40%" height="40%"/> </td> <td colspan="4" align="center"> <h2> Camelid comparison </h2> Approximate as of 9/2002 </td> </tr> <tr> <td> # of Humps </td> <td> Indigenious region </td> <td> Spits? </td> <td> Produces wool? </td> </tr> <tr> <td align="right"> Camels (bactrian) </td> <td> 2 </td> <td> Africa/Asia </td> <td rowspan="2"> Llama </td> <td rowspan="2"> Llama </td> </tr> <tr> <td align="right"> Llamas </td> <td> 1 </td> <td> Andes Mountains </td> </tr> </table> </body> </html>

Output:

b) 1b.html
<html> <body> <a href="profiles.html"> <img src="Venkat.jpg" width="40%" height="60%"/> </a> </body> </html>

profiles.html
<html> <body> <a <a <a <a </body> </html> href="acad.html"> Academic profile </a> <br> href="projects.html"> List of projects done </a> <br> href="personal.html"> Personal profile </a> <br> href="1b.html"> Home </a>

Output:

c) 1c.html

<html> <frameset cols="25%,*"> <frame src="left.html"/> <frame src="right1.html" name="abc"/> </frameset> </html>

left.html
<html> <body> MENU <UL> <Li> <a href="rose.html" target="abc">Rose </a> </Li> <Li> <a href="jasmine.html" target="abc">Jasmine </a> </Li> <Li> <a href="lotus.html" target="abc">Lotus </a> </Li> </UL> </body> </html>

right1.html
<html> <body> Click on the choice of your favorite flower links in the left frame in order to view the details of your favorite flower in the right frame. </body> </html>

rose.html
<html> <body> Rose is a beautiful flower which denotes the symbol of love. </body> </html>

jasmine.html
<html> <body> Jasmine is a white colored flower. </body> </html>

lotus.html
<html> <body> Lotus is a white colored flower. It is the national flower of India. </body> </html>

Output:

d) 1d.html

<html> <body> <UL type="disc"> <Li> Coffee </Li> <Li> Tea </Li> <UL type="circle"> <LI> Black tea </li> <li> Green tea </li> <UL type="square"> <li> China </li> <li> Africa </li> </UL> </UL> <Li> Milk </Li> </UL> </body> </html>

Output:

e) 1e.html

<html> <body> <img src="Forest.jpg" width="80%" height="80%" usemap="#nam"/> <map name="nam"> <area shape="rect" coords="0,0,200,200" href="red.html"/> <area shape="circle" coords="500,500,100" href="green.html"/> </map> </body> </html>

Green.html
<html> <body> Green </body> </html>

Red.html
<html> <body> Red </body> </html>

Output:

Result: Static webpages were created successfully using HTML.

Exercise 2

Date: Title: Creation of web pages using CSS Aim: To create web pages using CSS Source code: a) 2a.html
<html> <style> em{color:red;} #id1{margin-left:75px;} #id2{margin-left:15px;} a{text-decoration:none;} a:hover {text-decoration:underline;color:red;backgroundcolor:aqua;} </style> <body> <p align="center"> <b>Shopping List For <em>Monday</em></b> </p> <br> <ul id="id1" type="disc"> <li>Milk</li> <li>Bread</li> <ul id="id2" type="circle"> <li><a href="">White bread</a></li> <li><a href="">Rye bread</a></li> <li><a href="">Whole Wheat bread</a></li> </ul> <li>Rice</li> <li>Potatoes</li> <li>Pizza <font size=2><i>with mushroom</i></font></li> </ul> <p align="center> Go to grocery store </p> </body> </html>

Output:

b) 2b.html

<html> <link rel="stylesheet" href="s.css"> <body> <p align="center"> <b>Shopping List For <em>Monday</em></b> </p> <br> <ul id="id1" type="disc"> <li>Milk</li> <li>Bread</li> <ul id="id2" type="circle"> <li><a href="">White bread</a></li> <li><a href="">Rye bread</a></li> <li><a href="">Whole Wheat bread</a></li> </ul> <li>Rice</li> <li>Potatoes</li> <li>Pizza <font size=2><i>with mushroom</i></font></li> </ul> <p align="center> Go to grocery store </p> </body> </html>

s.css
em{color:red;} #id1{margin-left:75px;} #id2{margin-left:15px;} a{text-decoration:none;} a:hover {text-decoration:underline;color:red;background-color:aqua;}

Output:

c) 2c.html

<html> <link rel="stylesheet" href="s1.css"> <body> <p>Thanks for visiting my web page. I hope you enjoy a lot.<br> Please note: This site will be moving soon. Check periodically for updates.</p> </body> </html>

S1.css p{font-size:20pt; color:yellow;background-color:blue;}

d) 2d.html

<html> <body> <input <input <input <input <br> <table> <tr <tr <tr <tr </table> </body> </html> bgcolor="black"><td>20%</td></tr> bgcolor="orange"><td>40%</td></tr> bgcolor="pink"><td>20%</td></tr> bgcolor="cyan"><td>20%</td></tr> type="radio" type="radio" type="radio" type="radio" name="color" name="color" name="color" name="color" id="r" id="y" id="b" id="g" value="b">Black<br> value="o">Orange<br> value="p">Pink<br> value="c">Cyan<br>

Output:

e) 2e1.html

<html> <style> .appl{pad:0.5ems;border-style:groove;margin-top:0.5ems} </style> <body> <div> <h1 class="appl"> This is heading 1 </h1> <h2 class="appl"> This is heading 2 </h2> </div> </body> </html>

Output:

2e2.html

<html> <style> .greenMove{color:green;position:absolute;top:25px;left:15px;} </style> <body> <p class="greenMove"> This text will be shifted </p> <p> hello world </p> </body> </html>

Output:

Result: Web pages were created using CSS

Exercise 3 Date: Title: Programs using javascript Aim: To write programs using javascript Source code: a) 3a.html
<html> <head> <script type="text/javascript"> function print(n,a,b){ var c=6-a-b; if(n!=0){ print(n-1,a,c); document.writeln("Move disc " + n + " from pole " + a + " to pole " + b +"\n <br>"); print(n-1,c,b); } } var n=window.prompt("Enter the number:"); print(n,1,2); </script> </head> <body> </body> </html>

Output:

b) 3b.html
<html> <head> <script type="text/javascript"> function perfect(input){ var x,s=0,f=0;; for(x=1;x<=input/2;x++){ if(input%x == 0){ s+=x; } } if(s == input) return input; else return 0; } var x; input=window.prompt("Enter the value of input :",0); input=parseInt(input); document.writeln("Perfect numbers less than input are: "); for(x=1;x<=input;x++){ if(perfect(x) != 0) document.writeln(x); } </script> </head> <body> </body> </html>

Output:

c) 3c.html
<html> <head> <script type="text/javascript"> function checkpal(inp){ var l=inp.length; for(var i=0;i<l;i++) if(inp[i]!=inp[l-i-1]) return 0; return 1; } var a=window.prompt("Enter the string:","0"); if(checkpal(a)==0) document.writeln("Not a palindrome\n"); else document.writeln("Palindrome\n"); </script> </head> <body> </body> </html>

Output:

d) 3d.html
<html> <head> <script type="text/javascript"> var n; n=window.prompt("Enter the no of salesman : ",0); n=parseInt(n); var i; var a = new Array(100); for(i=0;i<n;i++){ a[i]=window.prompt("Enter the gross sales of salesman"+(i+1)+" : ",1); a[i]=parseInt(a[i]); a[i]=200+(9*a[i]/100); } var p=0,q=0,r=0; for(i=0;i<n;i++){ if(a[i]>=200 && a[i]< 400) p++; else if(a[i]>=400 && a[i]< 700) else if(a[i]>=700 && a[i]< 1000) } document.writeln("200-400:"+p+"; "); document.writeln("400-700:"+q+"; "); document.writeln("700-1000:"+r+"; "); </script> </head> </html>

q++; r++;

Output:

e) 3e.html
<html> <head> <script type="text/javascript"> var len; function ssort(a){ var x,temp,y; for(x=0;x<len;x++){ for(y=0;y<len-1;y++){ if(a[y]>a[y+1]){ temp=a[y]; a[y]=a[y+1]; a[y+1]=temp; } } } for(x=0;x<len;x++) document.writeln(a[x]); } var a=new Array(100); len=window.prompt("Enter the length :",0); len=parseInt(len); var z; for(z=0;z<len;z++){ a[z]=window.prompt("Enter the element :",0); a[z]=parseInt(a[z]); } ssort(a); </script> </head> <body> </body> </html>

Output:

Result: Programs were written using javascript

Exercise 4 Date: Title: Programs using VBScript Aim: To write programs using VBScript Source code: a) 4a.html
<html> <head> <script type="text/vbscript"> function start() dim principle,time,rate,ci,amt principle = f1.principle.value time = f1.time.value rate = f1.rate.value if ( len(principle) = 0) then call msgbox ("Invalid principle" ) end if if ( len(time) = 0 ) then call msgbox ("Invalid time" ) end if if ( len(rate) = 0) then call msgbox ("Invalid rate" ) end if if ( not IsNumeric(principle) ) then call msgbox ("Invalid principle" ) end if if ( not IsNumeric(time) ) then call msgbox ("Invalid time" ) end if if ( not IsNumeric(rate) ) then call msgbox ("Invalid rate" ) end if principle = cint(principle) time = cint(time) rate = cint(rate) amt = 1 dim i i=0 for i=1 to time step 1 amt = amt* ( 1 + (rate/100) ) next amt = amt*principle call msgbox ( "amt = "&amt ) end function </script> </head> <body> <h1> Compound Interest </h1> <form name="f1"> Principle : <input type="text" name="principle"> Time : <input type="text" name="time"> Rate : <input type="text" name="rate"><br><br> <input type="submit" onClick="start()"> </body> </html>

Output:

b) 4b.html <html> <head> <script type="text/vbscript"> function start() dim strng , uppercase , lowercase strng = f1.strng.value lowercase = lcase(strng) call msgbox("Lower case string is : " & lowercase) uppercase = ucase (strng) call msgbox("Upper case string is : " & uppercase) end function </script> </head> <body> <h1> Case Conversion </h1> <form name="f1"> Enter the string : <input type="text" name="strng"> <input type="submit" onClick="start()"> </body> </html> Output:

c) 4c.html <html> <head> <script type="text/vbscript"> function start() dim a dim str,str2 str = f1.str.value a = split( str , " ") for each x in a str1=str1+strreverse(x)+" " next call msgbox ("Reversed : " & str1) end function </script> </head> <body> <h1> String Token Reversal </h1> <form name="f1"> Enter the string : <input type="text" name="str"> <input type="submit" onClick="start()"> </body> </html> Output:

d) 4d.html <html> <head> <script type="text/vbscript"> function start() dim num,key key = f1.key.value num = f1.num.value if not isnumeric(key) then call msgbox("Invalid key" ) end if dim arr , i , flag arr = split ( num , " ") i=0 flag=0 for each x in arr if not isnumeric(x) then call msgbox("Invalid input" ) end if if x = key then call msgbox ( "found at the index : " & i ) flag=1 end if i = i+1 next if flag = 0 then call msgbox ("Element not found" ) end if end function </script> </head> <body> <h1> Linear Search </h1> <form name="f1"> Array : <input type="text" name="num"> Search : <input type="text" name="key"> <input type="submit" onClick="start()"> </body> </html> Output:

e) 4e.html <html> <head> <script type="text/vbscript"> function start( ) dim input input = f1.sites.value document.location=input end function </script> </head> <body> <form name="f1"> <h1>Favorites</h1> <select name="sites" onchange="start()"> <option value="websites">Websites</option> <option value="http:\\www.google.com">Google</option> <option value="http:\\www.facebook.com">Facebook</option> <option value="http:\\www.cricinfo.com">Cricinfo</option> <option value="http:\\www.gmail.edu">Gmail</option> </select> </form> </body> </html> Output:

Result: Programs were written using VBscript

Exercise 5 Date: Title: Creation of dynamic web pages using DHTML Aim: To create dynamic web pages using DHTML Source code: a) 5a.html <html> <head> <script type="text/javascript"> window.setInterval("bounce()",500); function bounce() { pic.style.position="absolute"; var x = Math.floor(Math.random()*800); var y = Math.floor(Math.random()*400); pic.style.left=x; pic.style.top=y; } </script> </head> <body> <img id="pic" src="Venkat.jpg" width="100" height="100" style="position:absolute; top:500; left:500"> </body> </html> Output:

b) 5b.html <html> <script type="text/javascript"> function add() { var x,y; x=f.input1.value; y=f.input2.value; x=parseInt(x); y=parseInt(y); alert("x+y="+(x+y)); } function sub() { var x,y; x=f.input1.value; y=f.input2.value; x=parseInt(x); y=parseInt(y); alert("x-y="+(x-y)); } function mul() { var x,y; x=f.input1.value; y=f.input2.value; x=parseInt(x); y=parseInt(y); alert("x*y="+(x*y)); } function div() { var x,y; x=f.input1.value; y=f.input2.value; x=parseInt(x); y=parseInt(y); alert("x/y="+(x/y)); } function mod() { var x,y; x=f.input1.value; y=f.input2.value; x=parseInt(x); y=parseInt(y); alert("x%y="+(x%y)); }

function sin() { var x,y; x=f.input1.value; y=f.input2.value; x=parseInt(x); y=parseInt(y); alert("sin x="+Math.sin(x)); } function cos() { var x,y; x=f.input1.value; y=f.input2.value; x=parseInt(x); y=parseInt(y); alert("cos x="+Math.cos(x)); } function log() { var x,y; x=f.input1.value; y=f.input2.value; x=parseInt(x); y=parseInt(y); alert("log x ="+Math.log(x)); } </script> <body> <form name="f"> <input type="text" value="1" name="input1"> <input type="text" value="2" name="input2"> <input type="submit" value="add" bgcolor="red" onclick=add()> <input type="button" value="sub" onclick=sub()> <input type="button" value="mul" onclick=mul()> <input type="button" value="div" onclick=div()> <input type="button" value="mod" onclick=mod()> <input type="button" value="sin" onclick=sin()> <input type="button" value="cos" onclick=cos()> <input type="button" value="log" onclick=log()> </form> </body> </html>

Output:

c) 5c.html <html> <head> <script type="text/vbscript"> option explicit public sub validate() dim name,age,address,sex,hobbies,phone,email dim namevalidation, agevalidation, phonevalidation, emailvalidation name = applnform.name.value age = applnform.age.value address = applnform.address.value 'sex = applnform.sex.checked hobbies = applnform.hobbies.value phone = applnform.phone.value email = applnform.email.value dim regularExpression Set regularExpression = new RegExp regularExpression.Pattern = "^[a-zA-Z ]+$" namevalidation = regularExpression.Test(name) if( not namevalidation) then MsgBox "Name cant be Empty. Only letters allowed." exit sub end if if( not isnumeric(age) or not age >=1 or not age <= 100) then MsgBox "Age has to be between 1 and 100" exit sub end if regularExpression.Pattern = "^0\d{2} \d{8}$" phonevalidation = regularExpression.Test(phone) regularExpression.Pattern = "^0\d{3} \d{7}$" phonevalidation = phonevalidation or regularExpression.Test(phone) regularExpression.Pattern = "^0\d{4} \d{6}$" phonevalidation = phonevalidation or regularExpression.Test(phone) if( not phonevalidation) then MsgBox "Invalid Phone Number. Enter STD Code followed by Space and Phone Number.10 chars in all max!!" exit sub end if regularExpression.Pattern = "^[a-zA-Z.]+@[a-zA-Z.]+$" emailvalidation = regularExpression.Test(email) if( not emailvalidation) then MsgBox "Invalid Email. Enter username@domainname" exit sub end if

MsgBox "All entries are Valid. Application Successful." end sub </script> </head> <body> <h1>On-Line Applicaiton</h1> <form name="applnform"> <table> <tr> <td> Name: </td> <td><input type = "text" name="name"></td> </tr> <tr> <td> Age: </td> <td><input type = "text" name="age"></td> </tr> <tr> <td> Address: </td> <td><input type = "text" name="address"></td> </tr> <tr> <td> Sex: </td> <td><input type = "radio" name="sex" value="male">Male <br><input type = "radio" name="sex" value="female">Female</td> </tr> <tr> <td> Hobbies: </td> <td><input type = "text" name="hobbies"></td> </tr> <tr> <td> Phone Number: </td> <td><input type = "text" name="phone"></td> </tr> <tr> <td> E-mail ID:

</td> <td><input type = "text" name="email"></td> </tr> </table> <input type="button" value="Apply" onclick="validate()"> </form> </body> </html> Output:

d) 5d.html <html> <head> <script type="text/javascript"> function start() { alert(ptext.innerHTML); ptext.innerHTML="THANKS"; alert(ptext.innerHTML); } </script> </head> <body onload="start()"> <p id="ptext"> WELCOME</p> </body> </html> Output:

Result: Dynamic webpages were created using DHTML

Exercise 6 Date: Title: Chat applications using socket programming in Java Aim: To write chat applications using socket programming in Java Source code: a) 6aclient.java
import import import import import java.io.*; java.net.*; java.util.*; java.util.logging.Level; java.util.logging.Logger;

class senderThread implements Runnable { Thread t; PrintWriter output; senderThread(PrintWriter out) { output = out; t = new Thread(this,"ClientSender"); t.start(); } public void run() { try { BufferedReader in1 = new BufferedReader(new InputStreamReader(System.in)); String I; I = in1.readLine(); while (true) { output.println(I); if (I.compareTo("quit") == 0) { break; } I = in1.readLine(); } in1.close(); } catch (IOException ex) { Logger.getLogger(senderThread.class.getName()).log(Level.SEVERE, null, ex); } } } class receiverThread implements Runnable { Thread t; BufferedReader input; receiverThread(BufferedReader in) { //System.out.println("Inside receiver's constructor of client");

input = in; t = new Thread(this,"ClientReceiverThread"); //System.out.println("going to start receiver's thread in client"); t.start(); } public void run() { //System.out.println("Run() in receiver of Client"); try { String I; I = input.readLine(); while (true) { System.out.println("RECD MSG: "+I); if (I.compareTo("quit") == 0) { break; } I = input.readLine(); } } catch (IOException ex) { Logger.getLogger(receiverThread.class.getName()).log(Level.SEVERE, null, ex); } catch (NullPointerException nle) { System.out.println(nle); } } } public class client { public static void main(String args[]) { try { Socket c1 = new Socket(InetAddress.getLocalHost(),562); Socket c2 = new Socket(InetAddress.getLocalHost(),563); BufferedReader in = new BufferedReader(new InputStreamReader(c1.getInputStream())); PrintWriter out = new PrintWriter(c2.getOutputStream(),true); senderThread st = new senderThread(out); //System.out.println("Going to start reciver thread in client"); receiverThread rt = new receiverThread(in); //System.out.println("Receiver thread in client started"); st.t.join(); rt.t.join(); //System.out.println("receiver thread in client completed"); in.close(); out.close(); c1.close(); c2.close(); } catch(Exception e) { System.out.println("Error in server2 : "+e); }

} }

6aserver.java
import import import import import java.io.*; java.net.*; java.util.*; java.util.logging.Level; java.util.logging.Logger;

class senderThread implements Runnable { Thread t; PrintWriter output; senderThread(PrintWriter out) { //System.out.println("Inside sender's constructor of server"); output = out; t = new Thread(this,"ServerSenderThread"); //System.out.println("goign to start sender of server"); t.start(); } public void run() { // System.out.println("Inside run() of sender of server"); try { BufferedReader in1 = new BufferedReader(new InputStreamReader(System.in)); String I; I = in1.readLine(); while (true) { output.println(I); if (I.compareTo("quit") == 0) { break; } I = in1.readLine(); } in1.close(); } catch (IOException ex) { Logger.getLogger(senderThread.class.getName()).log(Level.SEVERE, null, ex); } } } class receiverThread implements Runnable { Thread t; BufferedReader input; receiverThread(BufferedReader in) { input = in; t = new Thread(this,"ServerReceiver"); t.start();

} public void run() { try { String I; I = input.readLine(); while (true) { System.out.println("RECD MSG: "+I); if (I.compareTo("quit") == 0) { break; } I = input.readLine(); } } catch (IOException ex) { Logger.getLogger(receiverThread.class.getName()).log(Level.SEVERE, null, ex); } } } public class server { public static void main(String args[]) { try { ServerSocket s1 = new ServerSocket(562); ServerSocket s2 = new ServerSocket(563); //System.out.println("Waiting for Client to connect"); Socket c1 = s1.accept(); Socket c2 = s2.accept(); //System.out.println("Clients Connected"); PrintWriter out = new PrintWriter(c1.getOutputStream(),true); BufferedReader in = new BufferedReader(new InputStreamReader(c2.getInputStream())); //System.out.println("Going to call constrcutor of server sender"); senderThread st = new senderThread(out); receiverThread rt = new receiverThread(in); //System.out.println("Sender thread in server started"); st.t.join(); rt.t.join(); //System.out.println("Sender thread in server completed"); //in.close(); out.close(); s1.close(); s2.close(); c1.close(); c2.close(); } catch(Exception e) { System.out.println("Error in server2 : "+e); } } }

Output:

b) 6bclient.java
import import import import import java.io.*; java.net.*; java.util.*; java.util.logging.Level; java.util.logging.Logger;

class senderThread implements Runnable { Thread t; DatagramSocket soc; senderThread(DatagramSocket ds) { //System.out.println("Inside sender's constructor of server"); soc = ds; t = new Thread(this,"ClientSenderThread"); //System.out.println("goign to start sender of server"); t.start(); } public void run() { // System.out.println("Inside run() of sender of server"); try { byte buffer[] = new byte[1024]; int pos = 0; int c; while (true) { c = System.in.read(); switch(c) { case -1: System.out.println("Server Quits"); break; case '\n' : soc.send(new DatagramPacket(buffer,pos,InetAddress.getLocalHost(),580)); pos = 0; break; default: buffer[pos++] = (byte)c; } } } catch (IOException ex) { Logger.getLogger(senderThread.class.getName()).log(Level.SEVERE, null, ex); } } } class receiverThread implements Runnable { Thread t; DatagramSocket soc; receiverThread(DatagramSocket ds)

{ soc = ds; t = new Thread(this,"ClientReceiver"); t.start(); } public void run() { try { String I; byte []temp=new byte[1000]; while (true) { DatagramPacket dp = new DatagramPacket(temp,1000); soc.receive(dp); System.out.println("RECD MSG: "+new String(dp.getData())); } } catch (IOException ex) { Logger.getLogger(receiverThread.class.getName()).log(Level.SEVERE, null, ex); } } } public class client { public static void main(String args[]) { try { DatagramSocket ds1 = new DatagramSocket(571); DatagramSocket ds2 = new DatagramSocket(581); senderThread st = new senderThread(ds1); receiverThread rt = new receiverThread(ds2); st.t.join(); rt.t.join(); ds1.close(); ds2.close(); } catch(Exception e) { System.out.println("Error in server2 : "+e); } } }

6bserver.java
import import import import import java.io.*; java.net.*; java.util.*; java.util.logging.Level; java.util.logging.Logger;

class senderThread implements Runnable { Thread t; DatagramSocket soc; senderThread(DatagramSocket ds) { //System.out.println("Inside sender's constructor of server"); soc = ds; t = new Thread(this,"ServerSenderThread"); //System.out.println("goign to start sender of server"); t.start(); } public void run() { // System.out.println("Inside run() of sender of server"); try { byte buffer[] = new byte[1024]; int pos = 0; int c; while (true) { c = System.in.read(); switch(c) { case -1: System.out.println("Server Quits"); break; case '\n' : soc.send(new DatagramPacket(buffer,pos,InetAddress.getLocalHost(),581)); pos = 0; break; default: buffer[pos++] = (byte)c; } } } catch (IOException ex) { Logger.getLogger(senderThread.class.getName()).log(Level.SEVERE, null, ex); } } } class receiverThread implements Runnable { Thread t; DatagramSocket soc; receiverThread(DatagramSocket ds) { soc = ds; t = new Thread(this,"ServerReceiver"); t.start(); } public void run() { try { String I; byte []temp=new byte[1000]; while (true) {

DatagramPacket dp = new DatagramPacket(temp,1000); soc.receive(dp); System.out.println("RECD MSG: "+new String(dp.getData())); } } catch (IOException ex) { Logger.getLogger(receiverThread.class.getName()).log(Level.SEVERE, null, ex); } } } public class server { public static void main(String args[]) { try { DatagramSocket ds1 = new DatagramSocket(570); DatagramSocket ds2 = new DatagramSocket(580); senderThread st = new senderThread(ds1); receiverThread rt = new receiverThread(ds2); st.t.join(); rt.t.join(); ds1.close(); ds2.close(); } catch(Exception e) { System.out.println("Error in server2 : "+e); } } }

Output:

Exercise 7 Date: Title: Event driven programs using java applet Aim: To write event driven programs using java applet Source code: a) 7a.java
/* <applet code="applet1" width = "1000" height="600"> </applet> */ import java.applet.Applet; import java.awt.Graphics; public class applet1 extends Applet { String s; public void init() { // TODO start asynchronous download of heavy resources s = "\nInside Init"; } public void start(){ s += "\nInside Start"; } public void paint(Graphics g){ s += "\nIndise Paint"; g.drawString(s,50,50); g.drawLine(50, 60, 100, 60); g.drawOval(50,80,60,60); g.fillOval(50,150,60,60); g.drawOval(50,210,100,60); g.fillOval(50,300,100,60); g.drawRect(200,210,100, 60); g.fillRect(200,300,100, 60); g.drawArc(350, 210, 50, 50, 0, 90); g.fillArc(350, 300, 50, 50, 0, 45); int x[] = {400, 450,500,500,450}; int y[] = {400,400,500,550,450}; g.drawPolygon(x,y,5); } }

Output:

b) 7b. java
import java.applet.Applet; import java.awt.*; /* <applet code="applet2" width="1000" height="600"> </applet> */ public class applet2 extends Applet { String s; public void paint(Graphics g){ g.drawString("The following are the names of avaialble font familes", 10, 20); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String fontList[] = ge.getAvailableFontFamilyNames(); for(int i=0;i<fontList.length;i++) g.drawString(fontList[i],10,40+ 15*i); } }

Output:

c) 7c. java
import java.applet.Applet; import java.awt.*; /* <applet code="applet3" width="1000" height="600"> </applet> */ public class applet3 extends Applet { String s; public void init() { setBackground(Color.CYAN); setForeground(Color.RED); } public void paint(Graphics g){ g.drawString("The is the text in the applet Window.", 10, 20); showStatus("This is the text in the stauts bar"); } }

Output:

d) 7d. java
import java.applet.Applet; import java.awt.*; import java.net.*; /* <applet code="applet4" width="1000" height="600"> </applet> */ public class applet4 extends Applet { URL u1,u2; String s1,s2; public void init() { u1 = getCodeBase(); s1 = "URL of Class File : "+u1.toString(); u2 = getDocumentBase(); s2 = "URL of Applet Code : "+u1.toString(); } public void paint(Graphics g){ g.drawString(s1, 10, 20); g.drawString(s2, 10, 40); } }

Output:

Result: Event driven programs were written using java applets

Exercise 8 Date: Title: Programs using AWT controls and layouts in Java Aim: To write programs using AWT controls and layouts in Java Source code: a) 8a. java
/*<Applet code="eight_a" width="2500" height="600"></applet>*/ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class eight_a extends Applet implements ActionListener{ Button b1,b2; String mssg=" "; public void init() { b1=new Button("Yes"); b2=new Button("No"); add(b1); add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); if(str.equals("Yes")) mssg="You have pressed yes"; else mssg="You have pressed no"; repaint(); } public void paint(Graphics g) { g.drawString(mssg, 6,40); } }

Output:

b) 8b. java
/*<Applet code="eight_b" width="2500" height="600"></applet>*/ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class eight_b extends Applet implements ItemListener{ Checkbox c1,c2, c3, c4; String mssg=""; public void init() { c2=new Checkbox("WINDOWS NT", null,false); c3=new Checkbox("SOLARIS", null,false); c4=new Checkbox("MACOS", null,false); c1=new Checkbox("WINDOWS 98", null,true); add(c1); add(c2); add(c3); add(c4); c1.addItemListener(this); c2.addItemListener(this); c3.addItemListener(this); c4.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { mssg="Current status of checkboxes:"; g.drawString(mssg, 6,120); mssg="Windows 98:" + c1.getState(); g.drawString(mssg, 6, 140); mssg="Windows NT:" + c2.getState(); g.drawString(mssg, 6, 160); mssg="Solaris:" + c3.getState(); g.drawString(mssg, 6, 180); mssg="MACOS" + c4.getState(); g.drawString(mssg, 6, 200); } }

Output:

c) 8c. java
/*<Applet code="eight_c" width="2500" height="600"></applet>*/ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class eight_c extends Applet implements ItemListener{ CheckboxGroup cbg; Checkbox c1,c2,c3; String mssg=""; public void init() { cbg=new CheckboxGroup(); c1=new Checkbox("DOT MATRIX PRINTER",cbg,true); c2=new Checkbox("LASER PRINTER",cbg,false); c3=new Checkbox("COLOR PRINTER",cbg,false); add(c1); add(c2); add(c3); c1.addItemListener(this); c2.addItemListener(this); c3.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { mssg="Current Selection State:"; g.drawString(mssg,6,140); mssg=cbg.getSelectedCheckbox().getLabel(); g.drawString(mssg,6, 160); } }

Output:

d) 8d.java
/*<Applet code="eight_d" width="2500" height="600"></applet>*/ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class eight_d extends Applet implements ActionListener{ List l1,l2; public void init() { l1=new List(5,false); l2=new List(6,false); l1.add("WINDOWS 98"); l1.add("WINDOWS NT"); l1.add("LINUX"); l1.add("SOLARIS"); l1.add("MACOS"); l2.add("INTERNET EXPLORER"); l2.add("NETSCAPE NAVIGATOR"); l2.add("LYNX"); l2.add("FIREFOX"); l2.add("OPERA"); add(l1); add(l2); l1.addActionListener(this); l2.addActionListener(this); } public void actionPerformed(ActionEvent ie) { repaint(); } public void paint(Graphics g) { int n1 =l1.getItemCount(); int n2 = l2.getItemCount(); String mssg =l1.getSelectedItem(); g.drawString("Selected Item in list 1 :"+ mssg, 6, 140); g.drawString("Total No of Items in List 1: " +n1 ,6, 160); mssg = l2.getSelectedItem(); g.drawString("Selected item in list 2:"+mssg, 6, 200); g.drawString("Total no of items in list 2: "+n2, 6, 220); } }

Output:

e) 8e.java
/*<Applet code="eight_e" width="2500" height="600"></applet>*/ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class eight_e extends Applet implements ActionListener{ static final int n=4; String mssg=""; public void init() { setLayout(new GridLayout(n,n)); for(int i=0;i<n;i++) { for(int j=1;j<=n;j++) { int k=i*n+j; Button b = new Button(" "+ k); add(b); b.addActionListener(this); } } } public void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand(); mssg ="You pressed " + str; repaint(); } public void paint(Graphics g) { showStatus(mssg); } }

Output:

Result: Programs were written using AWT controls and Layouts

Вам также может понравиться