Saturday, 20 February 2016

Producer-consumer problem

The producer-consumer problem is one of the most frequently encountered problems when we attempt multi threaded programming.First of all, let us see the characteristics of the producer-consumer problem:
  •     Producer produce items.
  •     Consumer consume the items produced by the producer.
  •     Producer finish production and let the consumers know that they are done.

package rakesh;

public class Producer_Consumer 
{
public static void main(String args[])
{
Methods m1=new Methods();
Producer p1=new Producer(m1,1);
Consumer c1=new Consumer(m1,1);
p1.start();
c1.start();
}

}
class Methods
{
private int content;
boolean available=false;
public  synchronized int get()
{
while(available==false)
{
try
{
wait();
}
catch(Exception e)
{
System.out.println(e);
}
   }
available=false;
notifyAll();
return content;
}
public synchronized void put(int value)
{
while(available==true)
{
try
{
wait();
}
catch(Exception e)
{
System.out.println(e);
}
   }
available=true;
content=value;
notifyAll();
}
}
class Producer extends Thread
{
Methods method;
int number;
Producer(Methods method,int num)
{
this.method=method;
number=num;
}
public void run()
{
for(int i=0;i<10;i++)
{
method.put(i);
System.out.println("Producer #"+this.number+":"+i);
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}

class Consumer extends Thread
{
Methods method;
int number;
Consumer(Methods method,int num)
{
this.method=method;
number=num;
}
public void run()
{
for(int i=0;i<10;i++)
{
int value=method.get();
System.out.println("Consumer #"+this.number+":"+value);
}
}
}



Friday, 19 February 2016

Convert Berlin time to Digital time

package rakesh;

import java.util.Scanner;
public class Berlin
{
public static void main(String args[])
{
System.out.println("CONVERT BERLIN TIME TO DIGITAL TIME");
Scanner sc=new Scanner(System.in);
int red1[]=new int[4];
int red2[]=new int[4];
int yellowred[]=new int[11];
int yellowlight[]=new int[4];
System.out.println("Enter the array value in first array");
for(int i=0;i<red1.length;i++)
red1[i]=sc.nextInt();
System.out.println("Enter the array value in second array");
for(int i=0;i<red2.length;i++)
red2[i]=sc.nextInt();
System.out.println("Enter the array value in third array");
for(int i=0;i<yellowred.length;i++)
yellowred[i]=sc.nextInt();
System.out.println("Enter the array value in fourth array");
for(int i=0;i<yellowlight.length;i++)
yellowlight[i]=sc.nextInt();
int temp1=0;
int temp2=0;
for(int i=0;i<red1.length;i++)
{
if(red1[i]==1)
temp1=temp1+5*1;
}
for(int i=0;i<red2.length;i++)
{
if(red2[i]==1)
temp1=temp1+1*1;
}
String hours=temp1+"";
for(int i=0;i<yellowred.length;i++)
{
if(yellowred[i]==1)
temp2=temp2+5*1;
}
for(int i=0;i<yellowlight.length;i++)
{
if(yellowlight[i]==1)
temp2=temp2+1*1;
}
String minute=temp2+"";
System.out.println("Time is "+hours+":"+minute);
}
}


Convert Digital time to Berlin time

The Berlin clock is a clock that tells the time using a series of illuminated coloured blocks.
  • the top lamp blinks to show seconds-it is illuminated on even seconds and off on odd seconds.
  • The next two rows represent hours. The upper row represents 5 hour blocks and is made up of 4 red lamps. The lower row represents 1 hour blocks and is also made up of 4 red lamps.
  • The final two rows represent the minutes. The upper row represents 5 minute blocks, and is made up of 11 lamps- every third lamp is red, the rest are yellow. The bottom row represents 1 minute blocks, and is made up of 4 yellow lamps.

package rakesh;

import java.util.Scanner;

public class Digital
{
public static void main(String args[])
{
System.out.println("CONVERT DIGITAL TIME TO BERLIN TIME");
System.out.println("Enter time in hh:mm:ss Format");
Scanner sc=new Scanner(System.in);
sc.nextLine();
String Time=sc.nextLine();
String arr[]=Time.split(":");
int red1=Integer.parseInt(arr[0])/5;
int red2=Integer.parseInt(arr[0])%5;
int yellowred=Integer.parseInt(arr[1])/5;
int yellowlight=Integer.parseInt(arr[1])%5;
   if(Integer.parseInt(arr[2])%2==0)
    System.out.println("Y");
   else
    System.out.println("O");
for(int i=0;i<red1;i++)
System.out.print("R");
for(int i=0;i<(4-red1);i++)
System.out.print("O");
System.out.println();
for(int i=0;i<red2;i++)
System.out.print("R");
for(int i=0;i<(4-red2);i++)
System.out.print("O");
System.out.println();
for(int i=1;i<=yellowred;i++)
{
if(i%3==0)
System.out.print("R");
else
       System.out.print("Y");
}
for(int i=0;i<(11-yellowred);i++)
System.out.print("O");
System.out.println();
for(int i=0;i<yellowlight;i++)
System.out.print("Y");
for(int i=0;i<4-yellowlight;i++)
System.out.print("O");
}

}


Thursday, 18 February 2016

Print all permutations of a string

package rakesh;
import java.util.Scanner;
public class Permutations
{
static void swap(char array[],int a,int  b)
{
char temp=array[a];
array[a]=array[b];
array[b]=temp;
}

static void permutate(char array[],int start,int end)
{
if(start==end)
{
System.out.println();
for(int i=0;i<=end;i++)
System.out.print(array[i]);
}
else
{
for(int i=start;i<=end;i++)
{
swap(array,start,i);
permutate(array,start+1,end);
swap(array,start,i);
}
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
char array[]=input.toCharArray();
permutate(array,0,input.length()-1);
}
}

Wednesday, 10 February 2016

Displaying source code of a web page

//URLConnectionExample.java

import java.io.*;
import java.net.*;
public class URLConnectionExample
{
public static void main(String[] args)
{
try
{
URL url=new URL("http://javaforocpjp.blogspot.in/index.html");
URLConnection urlcon=url.openConnection();
InputStream stream=urlcon.getInputStream();
int i;
while((i=stream.read())!=-1)
{
System.out.print((char)i);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}  

downlaod file from Server

//MyServer.java

import java.io.*;
import java.util.*;
import java.net.*;
class MyServer
{
public static void main(String args[])
{
try
{
ServerSocket s1=new ServerSocket(2233);
Socket s=s1.accept();
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
FileInputStream file=new FileInputStream("input.txt");
String msg="";
char ch=0;
int i=0;
while((i=file.read())!=-1)
{
char c=(char)i;
msg+=c;
}
dout.writeUTF(msg);
dout.flush();
s.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

//MyClient.java

import java.io.*;
import java.util.*;
import java.net.*;
class MyClient
{
public static void main(String args[])
{
try
{
Socket s=new Socket("localhost",2233);
DataInputStream dis=new DataInputStream(s.getInputStream());
FileOutputStream file=new FileOutputStream("output.txt");
String msg=(String)dis.readUTF();
byte brr[]=msg.getBytes();
file.write(brr);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Chat Application in Java

//MyServer.java
import java.net.*;
import java.io.*;
import java.util.*;
class MyServer
{
public static void main(String args[])
{
try
{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
Scanner sc=new Scanner(System.in);
String str="",msg="";
while(!str.equals("stop"))
{
str=(String)dis.readUTF();
System.out.println("Client :"+str);
System.out.print("Server :");
msg=sc.nextLine();
dout.writeUTF(msg);
dout.flush();
}
dis.close();
s.close();
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
//MyClient.java
import java.net.*;
import java.io.*;
import java.util.*;
class MyClient
{
public static void main(String args[])
{
try
{
Socket s=new Socket("localhost",3333);
DataInputStream dis=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
Scanner sc=new Scanner(System.in);
String str="",msg="";
while(!str.equals("stop"))
{
System.out.print("Client :");
msg=sc.nextLine();
dout.writeUTF(msg);
dout.flush();
str=(String)dis.readUTF();
System.out.println("Server :"+str);
}
dis.close();
s.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}