Showing posts with label SDTL. Show all posts

Write a program to demonstrate applet

, by Prashant Gunjal


import java.lang.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class pr_8 extends Applet implements ActionListener
{
Label l1,l2;
TextField t1,t2;
Button b1,b2;
public void init()
{
setLayout(null);
l1=new Label("String one : ");
l2=new Label("String two : ");
t1=new TextField(30);
t2=new TextField(30);
b1=new Button("Check");
b2=new Button("Clear");
l1.setBounds(50,50,100,50);
t1.setBounds(200,50,100,50);
l2.setBounds(50,150,100,50);
t2.setBounds(200,150,100,50);
b1.setBounds(50,250,70,70);
b2.setBounds(200,250,70,70);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
b1.addActionListener(this);
add(b2);
b2.addActionListener(this);

}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
if(t1.getText().equals(t2.getText()))
t1.setText("Strings are Equals");
else
t1.setText("Strings are not Equals");
t2.setText("");
}
if(ae.getSource()==b2)
{
t1.setText("");
t2.setText("");
}

}
}
/*
<applet code="pr_8.class" width=500 height=500></applet>
*/
/******** END of Code ***************/

OUTPUT : (Run in this way ....)
C:\jdk\bin>javac pr_8.java
C:\jdk\bin>appletviewer pr_8.java



read more

Write a program for producer consumer using thread

, by Prashant Gunjal


/* Aim : Write a program for producer consumer using thread */
import java.lang.*;

class Q
{
int n;
synchronized int get()
{
System.out.println("\n\tGot : "+n);
return(n);
}
synchronized void put(int n)
{
this.n=n;
System.out.println("\n\tPut : "+n);
}
}

class producer implements Runnable
{
Q q;
producer(Q q)
{
this.q=q;
new Thread(this,"producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // We've been interrupted: no more messages.
        return;
   }
}
}
}

class consumer implements Runnable
{
Q q;
consumer(Q q)
{
this.q=q;
new Thread(this,"consumer").start();
}
public void run()
{
while(true)
{
q.get();
try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // We've been interrupted: no more messages.
        return;
   }

}
}
}

class pr_7
{
public static void main(String argv[]) throws Exception
{
Q q= new Q();
new producer(q);
new consumer(q);

System.out.println("\nPress ctrl + c to stop");
}
}

/* OUTPUT
C:\jdk\bin>javac pr_7.java

C:\jdk\bin>java pr_7

        Put : 0

Press ctrl + c to stop

        Got : 0

        Put : 1

        Got : 1

        Put : 2

        Got : 2

        Put : 3

        Got : 3

        Put : 4

        Got : 4

        Put : 5

        Got : 5

C:\jdk\bin>
*/
read more

Aim Write a program to implement Swing .

, by Prashant Gunjal


/*
Aim Write a program to implement Swing .
*/
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class swing extends JFrame implements ActionListener
{
JButton on,off;
JLabel water_level,Fuse_status,Line_status;
JTextField msg;
Font fnt;
JCheckBox line1,line2,line3,fuse1,fuse2,fuse3;
swing()
{
JPanel p1 = (JPanel)(getContentPane());
setBounds(50,50,500,500);

p1.setLayout(null);

fnt = new Font ("arial",Font.BOLD+Font.ITALIC,20);
on=new JButton("ON");
off=new JButton("off");
water_level=new JLabel("Water Status ");
Fuse_status=new JLabel("Fuse status ");
Line_status=new JLabel("Line status ");
msg = new JTextField(50);

UIManager.put("ProgressBar.background", Color.RED);
UIManager.put("ProgressBar.foreground", Color.GREEN);
//UIManager.put("ProgressBar.selectionBackground",Color.YELLOW);
JProgressBar water_progress=new JProgressBar(JProgressBar.VERTICAL,0,100);
water_progress.setValue(50);
water_progress.setStringPainted(true);

line1=new JCheckBox("Line 1");
line2=new JCheckBox("Line 2");
line3=new JCheckBox("Line 3");
fuse1=new JCheckBox("Fuse 1");
fuse2=new JCheckBox("Fuse 2");
fuse3=new JCheckBox("Fuse 3");

water_level.setBounds(30,30,150,50);
water_progress.setBounds(30,80,100,130);

Line_status.setBounds(200,30,150,50);
line1.setBounds(200,100,150,20);
line2.setBounds(200,140,150,20);
line3.setBounds(200,180,150,20);

Fuse_status.setBounds(350,30,120,50);
fuse1.setBounds(350,100,120,20);
fuse2.setBounds(350,140,120,20);
fuse3.setBounds(350,180,120,20);

msg.setBounds(30,250,440,100);
on.setBounds(30,370,219,70);
off.setBounds(251,370,219,70);
on.addActionListener(this);
off.addActionListener(this);

p1.setFont(fnt);
p1.add(on);
p1.add(off);
p1.add(water_level);
p1.add(Fuse_status);
p1.add(Line_status);
p1.add(msg);
p1.add(line1);
p1.add(line2);
p1.add(line3);
p1.add(fuse1);
p1.add(fuse2);
p1.add(fuse3);
p1.add(water_progress);

//drawLine(50,50,150,150);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

}
public void actionPerformed(ActionEvent ae)
{

if(ae.getSource()==on)
if(line1.isSelected())
if(line2.isSelected())
if(line3.isSelected())
if(fuse1.isSelected())
if(fuse2.isSelected())
if(fuse3.isSelected())
msg.setText("     !!!!! Motor is ON !!!!!");
else
msg.setText("ERROR!!! Fuse 3 goan");
else
if(fuse3.isSelected())
msg.setText("ERROR!!! Fuse 2 goan");
else
msg.setText("ERROR!!! Fuse 2 and 3 are goan");
else
if(fuse2.isSelected())
if(fuse3.isSelected())
msg.setText("ERROR!!! Fuse 1 goan");
else
msg.setText("ERROR!!! Fuse 1 and 3 are goan");
else
if(fuse3.isSelected())
msg.setText("ERROR!!! Fuse 1 and 2 are NOT present");
else
msg.setText("ERROR!!! All Fuse are goan");
else
msg.setText("ERROR!!! Line 3 is NOT present");
else
if(line3.isSelected())
msg.setText("ERROR!!! Line 2 is NOT present");
else
msg.setText("ERROR!!! Line 2 and 3 are NOT present");
else
if(line2.isSelected())
if(line3.isSelected())
msg.setText("ERROR!!! Line 1 is NOT present");
else
msg.setText("ERROR!!! Line 1 and 3 are NOT present");
else
if(line3.isSelected())
msg.setText("ERROR!!! Line 1 and 2 are NOT present");
else
msg.setText("ERROR!!! ALL 3 Lines are NOT present");
else
if(ae.getSource()==off)
msg.setText("     !!!!! Motor is OFF !!!!!");
}
public static void main(String argv[])
{
swing obj=new swing();
}
}

OUTPUT :   1) COMPILE IT WITH JAVAC
                    2) RUN BY JAVA
you will found following output on screen..



read more

Write a program to demonsrtate concept on networking.

, by Prashant Gunjal


/*
Aim Write a program to demonsrtate concept on networking.
*/

Program : service.java

import java.net.*;
import java.io.*;
class service
{
public static void main(String arg[])
{
try
{
ServerSocket ss=new ServerSocket(123);
Socket s=null;
s=ss.accept();
System.out.println("\n\tAccepted connectivity");
System.out.println("\n\tClient is :  "+s);
String str;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);

System.out.println("\n\t To Initialize two way communication press Enter");
str=br.readLine();

InetAddress ia =InetAddress.getLocalHost();
Socket cs=new Socket(ia,124);
System.out.println("\n\tconnect to server ");
BufferedReader br1= new BufferedReader(new InputStreamReader(cs.getInputStream()));
System.out.println("\n\t\tChating..................");
do
{
System.out.print("Enter Message : ");
str=br.readLine();
pw.write(str+"\n");
pw.flush();

str=br1.readLine();
System.out.println("Reply is : "+str);
}while(!str.equals("exit"));
s.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

Program : ( client.java )

import java.net.*;
import java.io.*;
class client
{
public static void main(String arg[])
{
try
{
InetAddress ia =InetAddress.getLocalHost();
System.out.println("\n\tRequest to server");
Socket cs=new Socket(ia,123);
System.out.println("\n\tconnect to server ");
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1= new BufferedReader(new InputStreamReader(cs.getInputStream()));
String str;
System.out.println("\n\t To Initialize two way communication press Enter");
str=br.readLine();
ServerSocket ss=new ServerSocket(124);
Socket s=null;
s=ss.accept();
System.out.println("\n\tAccepted connectivity");
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
System.out.println("\n\t\tChating..................");
do
{
str=br1.readLine();
System.out.println("Message is : "+str);

System.out.print("Reply : ");
str=br.readLine();
pw.write(str+"\n");
pw.flush();

}while(!str.equals("exit"));
cs.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

1 ) now compile server first and run it in one command prompt
2 ) then compile client and run it on another command prompt





read more

Write a program to demonstrate concept of exception handling.

, by Prashant Gunjal


/*
Aim : Write a program to demonstrate concept of exception handling.
*/
import java.lang.*;
import java.io.*;
class MyException extends Exception
{
MyException(int a)
{
System.out.println("\n\t\tYou r not eligeble because your age is "+a+" (greater)");
}
}

class pr_6
{
static void check(int a)throws MyException
{
if(a>20)
throw new MyException(a);
else
System.out.println("\n\t\tYou are eligible to apply...");
}
public static void main(String args[])throws Exception
{
String ch;
int a;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
do
{
System.out.print("\n\t\tEnter your age : ");
a=Integer.parseInt(br.readLine());
check(a);
System.out.print("\n\t\tDo u want to continue \n\t\t\t1) yes \n\t\t\t2) no \n\t\t\tEnter Choice :   ");
a=Integer.parseInt(br.readLine());
}while(a!=2);
}
catch(MyException e)
{
System.out.println("\t\tCaught Exception "+e);
}
}
}

/* OUTPUT

prashant@prashant-OptiPlex-755:~$ clear

prashant@prashant-OptiPlex-755:~$ javac pr_6.java
prashant@prashant-OptiPlex-755:~$ java pr_6

Enter your age : 12

You are eligible to apply...

Do u want to continue
1) yes
2) no
Enter Choice :   1

Enter your age : 15

You are eligible to apply...

Do u want to continue
1) yes
2) no
Enter Choice :   1

Enter your age : 34

You r not eligeble because your age is 34 (greater)
Caught Exception MyException
prashant@prashant-OptiPlex-755:~$
*/

read more

Write a program to understand concept of packages.

, by Prashant Gunjal


/*
Aim : Write a program to understand concept of packages.
*/

first of all create folder MyPack (case sensitive ). in that folder create balance.java file with following code


package MyPack;
import java.io.*;
public class balance
{
String name,address,mob;
double bal;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
/*public balance()
{
System.out.println("\tPlease Create your account ...");
}
public balance(String n,double b)
{
name=n;
bal=b;
}*/
public void init() throws Exception
{
System.out.print("\n\tEnter name : ");
name=br.readLine();
System.out.print("\n\tEnter Address : ");
address=br.readLine();
System.out.print("\n\tEnter Mobile no : ");
mob=br.readLine();
System.out.print("\n\tEnter Ammount for initialization of account : ");
bal=Double.parseDouble(br.readLine());
}
public void show()
{
if(bal<0)
System.out.println("\t -->");
else
System.out.println("\t"+name + " :  $"+bal);
}
}

now compile above file and make sure that .class file is created in folder MyPack 

after that write following file pr_5.java outside MyPack folder

Program : 


import MyPack.*;

class pr_5
{
public static void main(String argv[]) throws Exception
{
balance test = new balance();
System.out.println("********Creation of Account************");
test.init();
System.out.println("\n\n********Display Balance ***************\n");
test.show();
}
}

/* OUTPUT
prashant@prashant-OptiPlex-755:~$ javac pr_4.java
prashant@prashant-OptiPlex-755:~$ java pr_4
********Creation of Account************

Enter name : prashant

Enter Address : Sangamner,Ahmed Nagar

Enter Mobile no : 9921074899

Enter Ammount for initialization of account : 100


********Display Balance ***************

prashant :  $100.0
prashant@prashant-OptiPlex-755:~$

*/

read more

program to understand concept of Interfaces.

, by Prashant Gunjal


/*
Aim : Write a program to understand concept of Interfaces.
*/
import java.lang.*;

interface stack
{
void push(int item);
int pop();
}

class fix implements stack
{
private int st[];
private int top;
private int max;
fix(int size)
{
st= new int[size];
top=-1;
max=size;
}

public void push(int item)
{
if(top==max-1)
System.out.print("\t!!!!!Overflow!!!!!!!!!!");
else
st[++top]=item;
}
public int pop()
{
if(top==-1)
System.out.println("\t\t!!!!!!!!!!!!Underflow!!!!!!!!!");
else
return(st[top--]);
return(-1);
}
}

class pr_4
{
public static void main(String args[])
{
int i,j;
System.out.println("\tFixing object ie stack with size 7 ");
fix obj =new fix(7);
System.out.println("\n\tInserting Elements as follow (ie. pushing 1 t0 9 sequence)....");
for(i=1;i<10;i++)
{
System.out.print("\n\t\tElement :   "+ i );
obj.push(i);
}
System.out.println("\n\tPoping out elements as follow (ie. 9 times poping)...");
for(i=1;i<10;i++)
{
j=obj.pop();
if(j!=-1)
System.out.println("\t\tpoped element : "+j);
}
}
}

/* OUTPUT
prashant@prashant-OptiPlex-755:~$ javac pr_5.java
prashant@prashant-OptiPlex-755:~$ java pr_5
Fixing object ie stack with size 7

Inserting Elements as follow (ie. pushing 1 t0 9 sequence)....

Element :   1
Element :   2
Element :   3
Element :   4
Element :   5
Element :   6
Element :   7
Element :   8 !!!!!Overflow!!!!!!!!!!
Element :   9 !!!!!Overflow!!!!!!!!!!
Poping out elements as follow (ie. 9 times poping)...
poped element : 7
poped element : 6
poped element : 5
poped element : 4
poped element : 3
poped element : 2
poped element : 1
!!!!!!!!!!!!Underflow!!!!!!!!!
!!!!!!!!!!!!Underflow!!!!!!!!!
prashant@prashant-OptiPlex-755:~$
*/

read more

demonstrate concept of array string and vector.

, by Prashant Gunjal


/*
Aim : Write a program to demonstrate concept of array string and vector.
*/
import java.lang.*;
import java.io.*;
import java.util.*;

class pr_3
{
public static void main(String argv[]) throws IOException
{
int i,j,cnt,temp;
String s1,s2,s3;
s1=argv[0];
s2=argv[1];
System.out.println("\n\n**************String Related Operations *****************");
System.out.println("\t 1) Display  ");
System.out.print("\n\t   String s1 = "+s1);
System.out.print("\n\t   String s2 = "+s2);

System.out.print("\n\n\t 2) Length ");
System.out.print("\n\t   Length of String s1 = "+s1.length());
System.out.print("\n\t   Length of String s2 = "+s2.length());

System.out.print("\n\n\t 3) Substring ");
s3=s1.substring(3,6);
System.out.print("\n\t   Substring of String s1(3,6) = "+s3);
s3=s2.substring(3);
System.out.print("\n\t   Substring of String s2(3) = "+s3);

System.out.print("\n\n\t 4) Concatination ");
System.out.print("\n\t   Concatination of String s1 & s2 is = "+s1+s2);

System.out.print("\n\n\t 5) Equality ");
System.out.println("\n\t   String s1 and s2 are equal to each other .. =>   "+s1.equals(s2));

System.out.print("\n****************One Diamentional array*************************");

int no[];
no=new int[20];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("\n\tEnter Count of numbers to insert in array : ");
cnt=Integer.parseInt(br.readLine());
for(i=0;i<cnt;i++)
{
System.out.print("\n\tEnter no of location "+ i +" =  " );
no[i]=Integer.parseInt(br.readLine());
}
System.out.print("\n\tContents of array are : ");
for(i=0;i<cnt;i++)
{
System.out.print("\t"+no[i]);
}
System.out.print("\n\tSorted array is : ");
for(i=0;i<cnt;i++)
{
for(j=0;j<cnt;j++)
{
if(no[i]>no[j])
{
temp=no[i];
no[i]=no[j];
no[j]=temp;
}
}
}
for(i=0;i<cnt;i++)
{
System.out.print("\t"+no[i]);
}

System.out.println("\n*************Multidiamentional (ie example of 2d) Array***********");
int mat1[][],mat2[][],mat3[][],r,c;
mat1 = new int [5][5];
mat2 = new int [5][5];
mat3 = new int [5][5];

System.out.print("\n\tEnter no of rows : ");
r=Integer.parseInt(br.readLine());
System.out.print("\n\tEnter no of  columns : ");
c=Integer.parseInt(br.readLine());

System.out.println("\n\tEnter Matrix 1 ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print("\t");
mat1[i][j]=Integer.parseInt(br.readLine());
}
System.out.print("\n");
}

System.out.println("\n\tEnter Matrix 2 ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print("\t");
mat2[i][j]=Integer.parseInt(br.readLine());
}
System.out.print("\n");
}
System.out.println("\n\tAddition of Matrix 1 and Matrix 2 is : ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
System.out.print("\t"+mat3[i][j]);
}
System.out.print("\n");
}


System.out.print("\n********************Vector Demonstration *****************");
Vector<Integer> v = new Vector<Integer>(20);
System.out.print("\n\t1)Status....");
System.out.print("\n\n\tInitial size = "+v.size());
System.out.print("\n\tInitial Capacity = "+v.capacity());
System.out.print("\n\t2)Inserting elements last ....");
System.out.print("\n\tEnter no of Element to insert : ");
cnt=Integer.parseInt(br.readLine());
for(i=0;i<cnt;i++)
{
System.out.print("\n\t\tEnter "+ i +" no element  : ");
j=Integer.parseInt(br.readLine());
v.add(j) ;
}
//v.addElement(2); //method 2
//v.addElement(new Integer(3)); //method 3

System.out.print("\n\tNow size = "+v.size());
System.out.print("\n\n\t3)Display content  ");
System.out.print("\n\tContent of Vector are : ");
for(i=0;i<v.size();i++)
{
System.out.print("  "+v.get(i));
}
System.out.print("\n\n\t3)Inserting Element by index ");
System.out.print("\n\t\tEnter index no  : ");
j=Integer.parseInt(br.readLine());
System.out.print("\n\t\tEnter element : ");
temp=Integer.parseInt(br.readLine());
v.add(j,temp);

System.out.print("\n\tContent of Vector are : ");
for(i=0;i<v.size();i++)
{
System.out.print("  "+v.get(i));
}

System.out.print("\n\n\t4.Finding element ");
System.out.print("\n\t\tEnter element to find  : ");
j=Integer.parseInt(br.readLine());
System.out.print("\n\t\tElement present :   "+v.contains(j));

System.out.print("\n\n\t5.Finding First element ");
System.out.print("\n\t\tFirst Element is = "+v.firstElement());

System.out.print("\n\n\t6.Finding Last  ");
System.out.print("\n\t\tLast Element is = "+v.lastElement());

System.out.print("\n\n\t7.Replace by index ");
System.out.print("\n\t\tEnter index no  : ");
j=Integer.parseInt(br.readLine());
System.out.print("\n\t\tEnter element : ");
temp=Integer.parseInt(br.readLine());
v.set(j,temp);
System.out.print("\n\tContent of Vector are : ");
for(i=0;i<v.size();i++)
{
System.out.print("  "+v.get(i));
}

System.out.print("\n\n\t8.Remove element by index");
System.out.print("\n\t\tEnter index no  : ");
j=Integer.parseInt(br.readLine());
v.remove(j);
System.out.println("\n\tContent of Vector are : ");
for(i=0;i<v.size();i++)
{
System.out.print("  "+v.get(i));
}


}
}
/* OUTPUT
prashant@prashant-OptiPlex-755:~$ javac pr_3.java
prashant@prashant-OptiPlex-755:~$ java pr_3 prashant gunjal


******************String Related Operations *********************
1) Display  

  String s1 = prashant
  String s2 = gunjal

2) Length 
  Length of String s1 = 8
  Length of String s2 = 6

3) Substring 
  Substring of String s1(3,6) = sha
  Substring of String s2(3) = jal

4) Concatination 
  Concatination of String s1 & s2 is = prashantgunjal

5) Equality 
  String s1 and s2 are equal to each other .. =>   false

****************One Diamentional array**************************
Enter Count of numbers to insert in array : 5

Enter no of location 0 =  8

Enter no of location 1 =  5

Enter no of location 2 =  6

Enter no of location 3 =  1

Enter no of location 4 =  3

Contents of array are : 8 5 6 1 3
Sorted array is : 8 6 5 3 1
*************Multidiamentional (ie example of 2d) Array******************

Enter no of rows : 3

Enter no of  columns : 3

Enter Matrix 1 
1
2
3

4
5
6

7
8
9


Enter Matrix 2 
0
1
2

3
4
5

6
7
8


Addition of Matrix 1 and Matrix 2 is : 
1 3 5
7 9 11
13 15 17

*************************Vector Demonstration *********************
1)Status....

Initial size = 0
Initial Capacity = 20
2)Inserting elements last ....
Enter no of Element to insert : 4

Enter 0 no element  : 1

Enter 1 no element  : 2

Enter 2 no element  : 3

Enter 3 no element  : 4

Now size = 4

3)Display content  
Content of Vector are :   1  2  3  4

3)Inserting Element by index 
Enter index no  : 2

Enter element : 60

Content of Vector are :   1  2  60  3  4

4.Finding element 
Enter element to find  : 60

Element present :   true

5.Finding First element 
First Element is = 1

6.Finding Last  
Last Element is = 4

7.Replace by index 
Enter index no  : 0

Enter element : 8

Content of Vector are :   8  2  60  3  4

8.Remove element by index
Enter index no  : 3

Content of Vector are :   8  2  60  4
prashant@prashant-OptiPlex-755:~$ ^C
prashant@prashant-OptiPlex-755:~$ 
*/



read more