MCSL025 Java Lab Records 3

Session 7 : Multithreading 

 Ex 23: Write a program to launch 10 threads. Each thread increments a counter variable. Run the program
with synchronization. 
 Code:
class s07_02
{
public static void main(String arg[])throws Exception
{
data d1=new data();
data d2=new data();
data d3=new data();
data d4=new data();
data d5=new data();
data d6=new data();
data d7=new data();
data d8=new data();
data d9=new data();
data d10=new data();
System.out.println(d10.count);
}
}
//—————————
class item { static int count=0; }
class data extends item implements Runnable
{
item d=this;
Thread t;
data()
{
t=new Thread(this);
t.start();
}
public void run()
{ d=syn.increment(d); }
}
//==============================
class syn
{
synchronized static item increment(item i)
{
i.count++;
return(i);
}
}   Ex 24: Write a program for generating 2 threads, one for printing even numbers and the other for printing
odd numbers. 
 Code:
class even extends Thread
{
Thread t=null;
even()
{
t=new Thread(this);
start();
}
public void run()
{
try
{
for(int i=2;i<50;i+=2)
System.out.print(i+" ");
Thread.sleep(100);
}
catch(Exception e)
{System.out.println("thread interepted");}
}
}
class odd extends Thread
{
Thread t=null;
odd()
{
t=new Thread(this);
start();
}
public void run()
{
try
{
for(int i=1;i<50;i+=2)
System.out.print(i+" ");
Thread.sleep(100);
}
catch(Exception e)
{System.out.println("thread interepted");}
}
}
class s07_03
{
public static void main(String arg[])
{
even e=new even();
odd o=new odd();
}
}

Session 8 : Reading, Writing & String Handling in Java 

 Ex 26: Writ a program in Java to create a String object. Initialize this object with your name. Find the
length of your name using the appropriate String method. Find whether the character „a‟ is in your name or
not; if yes find the number of times „a‟ appears in your name. Print locations of occurrences of „a‟ .Try the
same for different String objects. 
 Code:
class data
{
String name;
data(String n){ name=n; }
void disp()
{
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Name :"+name);
int c=0;
int len=name.length();
for(int i=0;i<len;i++)
if(name.charAt(i)=='A'||name.charAt(i)=='a')
{
c++;
System.out.println("number of occurance :"+c);
System.out.println("Possition :"+(i+1));
}
if(c==0)
System.out.println("there is no 'A' available in the string");
}
}
class s08_01
{
public static void main(String ar[])
{
data d1=new data("anil kumar");
d1.disp();
data d2=new data("biju");
d2.disp();
}
}  Ex 28: Write a program for searching strings for the first occurrence of a character or substring and for the
last occurrence of a character or substring. 
 Code:
import java.io.*;
class s08_03
{
public static void main(String[]args) throws Exception
{
int len1,len2,last=0;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the string:");
String s1=in.readLine();
System.out.println("Enter searching string:");
String s2=in.readLine();
len1=s1.length();
len2=s2.length();
for(int i=0;i<=(len1-len2);i++)
{
if(s1.substring(i,len2+i).equals(s2))
{
if(last==0)
System.out.println("first occurance is at possition :"+(i+1));
last=i+1;
}
}
if(last!=0)
System.out.println("last occurance is at possition :"+last);
else
System.out.println("the string is not found");
}
}   Ex 29: Write a program in Java to read a statement from console, convert it into upper case and again print
on console. 
 Code:
import java.io.*;
class s08_04
{
public static void main(String a[]) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter file Statement:");
String s1=in.readLine();
System.out.println(s1.toUpperCase());
}
}    Ex 30: Write a program in Java, which takes the name of a file from user, read the contents of the file and
display it on the console. 
 Code:
import java.io.*;
class s08_05
{
public static void main(String args[])
{
FileInputStream fis=null;
Try
{
fis=new FileInputStream(args[0]);
byte ch;
while((ch=(byte)fis.read())!=-1)
{System.out.print((char)ch); }
}
catch(IOException e)
{ System.out.println("interrupted");}
finally
{
try
{fis.close();}
catch(IOException e)
{System.out.println("error in closing");}
}
}
}    Ex 31: Write a Java program to copy a file into another file.  Code:
import java.io.*;
class s08_06
{
public static void main(String a[]) throws IOException
{
int d=0;
FileInputStream fi=null;
FileOutputStream fo=null;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter file name:");
String s1=in.readLine();
System.out.println("Enter new file name:");
String s2=in.readLine();
try
{
fi=new FileInputStream(s1);
fo=new FileOutputStream(s2);
System.out.println("Copying file");
while((d=fi.read())!=-1)
fo.write((byte)d);
System.out.println("File copyied");
}
catch(IOException e)
{System.out.println(e);}
}
}

Session 9 : Applets and its Application

  Ex 32: Write a Java Applet program which reads your name and address in different text fields and when a
button named find is pressed the sum of the length of characters in name and address is displayed in another
text field. Use appropriate colors, layout to make your applet look good.
  Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class s09_01 extends Applet implements ActionListener
{
public void init()
{
label1 = new Label();
label2 = new Label();
label3 = new Label();
t1 = new TextField();
t2 = new TextField();
t3 = new TextField();
b1 = new Button();
setLayout(null);
setBackground(new Color(0, 153, 102));
label1.setAlignment(Label.RIGHT);
label1.setText("Name");
add(label1);
label1.setBounds(140, 60, 50, 20);
label2.setAlignment(Label.RIGHT);
label2.setText("Address");
add(label2);
label2.setBounds(140, 90, 50, 20);
label3.setAlignment(Label.RIGHT);
label3.setText("Total length");
add(label3);
label3.setBounds(130, 120, 60, 20);
add(t1);
t1.setBounds(200, 60, 100, 20);
add(t2);
t2.setBounds(200, 90, 100, 20);
add(t3);
t3.setBounds(200, 120, 100, 20);
b1.setBackground(new Color(255, 204, 153));
b1.setLabel("Total");
b1.addActionListener(this);
add(b1);
b1.setBounds(150, 180, 80, 24);
}
public void actionPerformed(ActionEvent ae)
{
int a=t1.getText().length();
a+=t2.getText().length();
t3.setText(Integer.toString(a));
}
Label label1;
Label label2;
Label label3;
TextField t1;
TextField t2;
TextField t3;
Button b1;
}
<html>
<body>
<applet code="s09_01.class" width=400 height=400>
</applet>
</body>
</html>   Ex 33: Create an applet which displays a rectangle/string with specified color & coordinate passed as
parameter from the HTML file. 
 Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class s09_02 extends Applet implements ActionListener
{
public void init()
{
setLayout(null);
setBackground(new Color(0,10,100));
}
public void paint(Graphics p)
{
String t=null;
int x,y,w,h,r,g,b;
t=getParameter("xx");
x=Integer.parseInt(t);
t=getParameter("yy");
y=Integer.parseInt(t);
t=getParameter("ww");
w=Integer.parseInt(t);
t=getParameter("hh");
h=Integer.parseInt(t);
t=getParameter("rr");
r=Integer.parseInt(t);
t=getParameter("gg");
g=Integer.parseInt(t);
t=getParameter("bb");
b=Integer.parseInt(t);
p.setColor(new Color(r,g,b));
p.fillRect(x,y,w,h);
}
}
<html>
<body>
<applet code="s09_02.class" width=400 height=400>
<param name="xx" value="25">
<param name="yy" value="25">
<param name="ww" value="150">
<param name="hh" value="150">
<param name="rr" value="0">
<param name="gg" value="150">
<param name="bb" value="100">
</applet>
</body>
</html>    Ex 34: Create an applet which will display the calendar of a given date.  Code:
import java.util.*;
import java.awt.*;
import java.applet.*;
public class s9e3 extends Applet
{
GregorianCalendar cal=new GregorianCalendar();
String s,s1,s2,s3,s4;
int a=0,b=0,c=0,d=0;
public void start()
{
s=getParameter("fg");
s1=getParameter("as");
s2=getParameter("as1");
s3=getParameter("as2");
s4=getParameter("as3");
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
d=Integer.parseInt(s4);
}
public void paint(Graphics g)
{
if(s.equals("red"))
g.setColor(Color.red);
g.drawRect(a,b,c,d);
g.drawString("Color = "+"",25,25);
g.drawString("Calendar is"+cal.DATE+"/"+cal.MONTH+"/"+cal.YEAR,34,36);
}
}   Ex 35: Write a program to store student‟s detail using Card Layout.  Code:
class s9e4 extends Applet
{
CardLayout c1;
Panel p;
Label l1;
Label l2;
Label l3;
Label l4;
TextField t1;
TextField t2;
TextField t3;
TextField t4;
public void start()
{ }
public void init()
{
c1=new CardLayout();
l1=new Label("Enter Name :");
l2=new Label("Enter Place :");
l3=new Label("Address :mo(ho)");
l4=new Label("Pin :670571 ");
t1=new TextField(20);
p=new Panel();
p.setLayout(c1);
add(l1);
add(t1);
add(l2);
// add(t2);
add(l3);
// add(t3);
add(l4);
// add(t4);
}
public void paint(Graphics g)
{ }
}

 

Session 10 : Networking & Other Advanced Feature of JAVA 

  Ex 37: Write a Java program to find the numeric address of the following web sites
i. www.ignou.ac.in
ii. www.indiatimes.com
iii. www.rediff.com
iv. www.apple.com
In addition to this, find the Internet Address of your local host. 
 Code:
import java.net.*;
class s10_01
{
public static void main(String ar[])throws Exception
{
InetAddress a=InetAddress.getLocalHost();
System.out.println("The Local Host IP:"+a);
a=InetAddress.getByName("");www.ignou.ac.in
System.out.println("The IP of :"+a);www.ignou.ac.in
a=InetAddress.getByName("");www.indiatimes.com
System.out.println("The IP of :"+a);www.indiatimes.com
a=InetAddress.getByName("");www.apple.com
System.out.println("The IP of :"+a);www.apple.com
InetAddress s[]=InetAddress.getAllByName("");www.rediff.com
for(int i=0;i<s.length;i++)
System.out.println("The IP of :"+s[i]);www.rediff.com
}
}   Ex 38: Create an applet which takes name and age as parameters and display the message “<name> is
<age> year old.”. Print the URL of the class file. 
 Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class s10_02 extends Applet
{
public void init()
{
Font f=new Font("adobe-courier",Font.PLAIN,18);
setFont(f);
setLayout(null);
setForeground(Color.green);
setBackground(new Color(200,0,200));
}
public void paint(Graphics g)
{
String n,a,msg;
n=getParameter("name");
a=getParameter("age");
msg=n+" is "+a+" year old. ";
g.drawString(msg,25,25);
}
}
<html>
<body>
<applet code="s10_02.class" width=400 height=400>
<param name="name" value="Anil kumar">
<param name="age" value="25">
</applet>
</body></html>  

Leave a Reply