Wednesday, 10 June 2015

Create an interface coversions. Which have the following functions Inchestomillimeters(), hptowatts(),wattstohp(),poundstogram(),gra


Code for Create an interface coversions. Which have the following functions Inchestomillimeters(), hptowatts(),wattstohp(),poundstogram(),gramtopounds() in Java



 



 
interface Convert_Measures
{
   double inchestomm(double inches);
   double hptowatts(double hp);
   double wattstohp(double watts);
   double poundstogm(double pounds);
   double gmtopounds(double gm);
}


class Cnvrt_Msrs implements Convert_Measures
{
   publicdouble inchestomm(double inches)
   {
       double mm;
       mm = 2.5 * 10 * inches;
       System.out.println("There are " + mm + " mm in " + inches + " inches");
       return mm;
   }

   publicdouble hptowatts(double hp)
   {
       double watts;
       watts = 746 * hp;
       System.out.println("There are " + watts + " watts in " + hp + " hp");
       return watts;
   }

   publicdouble wattstohp(double watts)
   {
       double hp;
       hp = (1/746) * watts;
       System.out.println("There are " + hp + " hp in " + watts + " watts");
       return hp;
   }

   publicdouble poundstogm(double pounds)
   {
       double gm;
       gm = 453.59 * pounds;
       System.out.println("There are " + gm + " gm in " + pounds + " pounds");
       return gm;
   }

   publicdouble gmtopounds(double gm)
   {
       double pounds;
       pounds = (1/453.59) * gm;
       System.out.println("There are " + pounds + " pounds in " + gm + " gm");
       return pounds;
   }
}

class Conversions
{
   publicstaticvoid main(String args[])
   {
       Cnvrt_Msrs CM = new Cnvrt_Msrs();
       double mm;
       double pounds;
       double watts;
       mm = CM.inchestomm(2);
       pounds = CM.gmtopounds(4.3);
       watts = CM.hptowatts(3);
   }
}


/*
            Output

There are 50.0 mm in 2.0 inches
There are 0.00947992680614652 pounds in 4.3 gm
There are 2238.0 watts in 3.0 hp

*/



 

Program sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order


Code for Program sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order in Java



 



 
import java.util.StringTokenizer;
classstring
{
    publicstaticvoid main(String args[])
    {
        operations op = new operations();
        op.getToken();
        op.sort();
        op.diffOp();
    }
}
class operations
{
    static String in="My first name is Vidyadhar and my last name is Yagnik";
    static String arr[]=new String[30];
    staticint c=0;
    
    void getToken()
    {
        StringTokenizer st=new StringTokenizer(in," ");
        
        while(st.hasMoreTokens())
        {
            arr[c]=st.nextToken();
            c++;
        }
        System.out.println("The original string is:::"+in);
        System.out.println("-------The Tokens of the strings--------");
        for(int i=0;i<c;i++)
        {
            System.out.println(arr[i]);
        }
    }
    void sort()
    {
        for(int i=0;i<c;i++)
        {
            for(int j=i+1;j<c;j++)
            {
                if(arr[j].compareToIgnoreCase(arr[i])  <  0)
                {
                    String t=arr[i];
                    arr[i]=arr[j];
                    arr[j]=t;
                }
            }
        }
        System.out.println("--------The sorted array of strings---------");
        for(int k=0;k<c;k++)
        {
            System.out.println(arr[k]);
        }
    }
    void diffOp()
    {
        int space=0,capital=0,vowel=0;

        //Count no. of white spaces,capital letters,vowels from the stringchar temp;
        for(int i=0;i<in.length();i++)
        {
            temp=in.charAt(i);
            if(temp==' ')
                space++;
            if(temp>=65 && temp<=90)
                capital++;
            if(temp=='a' || temp=='A' || temp=='e' || temp=='E' || temp=='i' || temp=='I' || temp=='o' || temp=='O' || temp=='u' || temp=='U')
                vowel++;
        }

        System.out.println("The no. of white space is::"+space);
        System.out.println("The no. of capital letter is::"+capital);
        System.out.println("The no. of vowel is::"+vowel);
        
        //replace space with '/'
        
        String newString=new String(in);
        newString=newString.replace(' ','/');
        System.out.println("The replaced string is:::"+newString);

        //reverse string 

        StringBuffer revers=new StringBuffer(in);
        revers.reverse();
        System.out.println("The reverse string is:::"+revers);    
    }    

}
/*
            Output

java string

The original string is:::My first name is Vidyadhar and my last name is Yagnik
-------The Tokens of the strings--------
My
first
name
is
Vidyadhar
and
my
last
name
is
Yagnik
--------The sorted array of strings---------
and
first
is
is
last
my
My
name
name
Vidyadhar
Yagnik
The no. of white space is::10
The no. of capital letter is::3
The no. of vowel is::13
The replaced string is:::My/first/name/is/Vidyadhar/and/my/last/name/is/Yagnik
The reverse string is:::kingaY si eman tsal ym dna rahdaydiV si eman tsrif yM

*/

Program, which creates a three thread. One thread display the numbers from 1 to 5, second thread display the square root of that number etc


Code for Program, which creates a three thread. One thread display the numbers from 1 to 5, second thread display the square root of that number etc in Java



 



 
class numbers implements Runnable
{
    Thread t;
    boolean running=true;
    public numbers(String name, int p)
    {
        t=new Thread(this,name);
        t.setPriority(p);
        t.start();
    }
    publicvoid run()
    {
        System.out.println("\n"+t+ " start");
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
        }
        System.out.println(t+ " exiting");
    }
}
class squareRoot implements Runnable
{
    Thread t;
    boolean running=true;
    public squareRoot(String name,int p)
    {
        t=new Thread(this,name);
        t.setPriority(p);
        t.start();
    }
    publicvoid run()
    {
        System.out.println("\n"+t+ " start");
        for(int i=1;i<=5;i++)
        {
            System.out.println(i*i);
        }
        System.out.println(t+ " exiting");
    }
}


class ThreadPri
{
    publicstaticvoid main(String args[])
    {
        new numbers("Numbers HIGH PRIORITY",Thread.MAX_PRIORITY);
        new squareRoot("Square MIDDLE PRIORITY",Thread.NORM_PRIORITY);
        Thread t=Thread.currentThread();
        t.setPriority(Thread.MIN_PRIORITY);
        t.setName("Cube LOW PRIORITY");
        
        System.out.println("\n"+t+ " start");
        for(int i=1;i<=5;i++)
        {
            System.out.println(i*i*i);
        }
        System.out.println(t+ " exiting");
    }
}
/*
            Output

Thread[Numbers HIGH PRIORITY,10,main] start
1
2
3
4
5
Thread[Numbers HIGH PRIORITY,10,main] exiting

Thread[Square MIDDLE PRIORITY,7,main] start
1
4
9
16
25
Thread[Square MIDDLE PRIORITY,7,main] exiting

Thread[Cube LOW PRIORITY,3,main] start
1
8
27
64
125
Thread[Cube LOW PRIORITY,3,main] exiting

*/

Write a class whose objects holds a current value and have a method to add that value, printing the new value


Code for Write a class whose objects holds a current value and have a method to add that value, printing the new value in Java



 



 



 
class Arr
{
   int int_array[]=newint[5];
   synchronized void add_to_array(int int_array[], int k)
   {
       for(int i=0; i<5; i++)
       {
           int_array[i]+=k;
           System.out.println(int_array[i]);
       }
   }
}

class Caller implements Runnable
{
    Arr A;
    int array[]={1, 2, 3, 4, 5};
    int val;
    Thread t;
    public Caller(Arr ar, int k)
    {
        A=ar;
        val=k;
        t=new Thread(this);
        t.start();
    }

    publicvoid run()
    {
        System.out.println("Thread starts..");
        A.add_to_array(array, val);
        System.out.println("Thread stops..");
    }
}

class ThreadArrAdd
{
    publicstaticvoid main(String args[])
    {
        Arr A1=new Arr();

        Caller ob1=new Caller(A1, 2);
        Caller ob2=new Caller(A1, 4);
        Caller ob3=new Caller(A1, 1);
        try
        {
            ob1.t.join();
            ob2.t.join();
            ob3.t.join();
        }
        catch(InterruptedException e)
        {
            System.out.println("Interrupted...");
        }
    }
}

/* 
            OUTPUT

Thread starts..
3
4
5
6
7
Thread stops..
Thread starts..
5
6
7
8
9
Thread stops..
Thread starts..
2
3
4
5
6
Thread stops..

*/



 

Write a class whose objects holds a current value and have a method to add that value, printing the new value


Code for Write a class whose objects holds a current value and have a method to add that value, printing the new value in Java



 



 



 
class Arr
{
   int int_array[]=newint[5];
   synchronized void add_to_array(int int_array[], int k)
   {
       for(int i=0; i<5; i++)
       {
           int_array[i]+=k;
           System.out.println(int_array[i]);
       }
   }
}

class Caller implements Runnable
{
    Arr A;
    int array[]={1, 2, 3, 4, 5};
    int val;
    Thread t;
    public Caller(Arr ar, int k)
    {
        A=ar;
        val=k;
        t=new Thread(this);
        t.start();
    }

    publicvoid run()
    {
        System.out.println("Thread starts..");
        A.add_to_array(array, val);
        System.out.println("Thread stops..");
    }
}

class ThreadArrAdd
{
    publicstaticvoid main(String args[])
    {
        Arr A1=new Arr();

        Caller ob1=new Caller(A1, 2);
        Caller ob2=new Caller(A1, 4);
        Caller ob3=new Caller(A1, 1);
        try
        {
            ob1.t.join();
            ob2.t.join();
            ob3.t.join();
        }
        catch(InterruptedException e)
        {
            System.out.println("Interrupted...");
        }
    }
}

/* 
            OUTPUT

Thread starts..
3
4
5
6
7
Thread stops..
Thread starts..
5
6
7
8
9
Thread stops..
Thread starts..
2
3
4
5
6
Thread stops..

*/

Program for Thread Priority


Code for Program for Thread Priority in Java



 



 



 
class thrun implements Runnable
{
      Thread t;
      boolean runn = true;

      thrun(String st,int p) 
      {
    t = new Thread(this,st);
    t.setPriority(p);
    t.start();
      }
    
      publicvoid run()
      {
    System.out.println("Thread name : " + t.getName());
    System.out.println("Thread Priority : " + t.getPriority());
      }     
    
}

class priority
{
      publicstaticvoid main(String args[])
      {
     Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
     thrun t1 = new thrun("Thread1",Thread.NORM_PRIORITY + 2);
     thrun t2 = new thrun("Thread2",Thread.NORM_PRIORITY - 2);
     System.out.println("Main Thread : " + Thread.currentThread());
     System.out.println("Main Thread Priority : " + Thread.currentThread().getPriority());
      }
}

/*
            Output

Thread name : Thread1
Main Thread : Thread[main,10,main]
Thread name : Thread2
Thread Priority : 7
Main Thread Priority : 10
Thread Priority : 3

*/

Write a program for restaurant


Code for Write a program for restaurant in Java



 



 



 
import java.io.*;

class customerOrder
{
    boolean valueset=false;
    String str[]=new String[3];

    synchronized void d_takeOrder(Thread t)
    {
        if(valueset)
        {
            try
            {
                wait();        
            }catch(InterruptedException e)
            {
                System.out.println(e);
            }
        }
        System.out.println("\n"+t);
        try
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            for(int i=0;i<3;i++)
            {
                System.out.print("\n Take an Order "+(i+1)+" :: ");
                str[i]=br.readLine();
            }
        }catch(IOException e)
        {
            System.out.println(e);
        }

        valueset=true;
        notify();
    }
    synchronized void d_dispOrder(Thread t)
    {
        if(!valueset)
        {
            try
            {
                wait();        
            }catch(InterruptedException e)
            {
                System.out.println(e);
            }
        }
        System.out.println("\n"+t);
        for(int i=0;i<3;i++)
        {
            System.out.print("\n Place an Order "+(i+1)+" :: "+str[i]);
        }
        valueset=false;
        notify();
    }
    
}
class takeOrder implements Runnable
{
    customerOrder d;
    Thread t;
    takeOrder(customerOrder d)
    {
        this.d=d;
        t=new Thread(this,"Manager take an order");
        t.start();
    }
    publicvoid run()
    {
        for(int i=0;i<2;i++)
        {
            d.d_takeOrder(t);
        }
    }
}
class dispOrder implements Runnable
{
    customerOrder d;
    Thread t;
    dispOrder(customerOrder d)
    {
        this.d=d;
        t=new Thread(this,"Manager place an order");
        t.start();
    }
    publicvoid run()
    {
        for(int i=0;i<2;i++)
        {
            d.d_dispOrder(t);
        }
    }
}
class Restaurant
{
    publicstaticvoid main(String args[])
    {
        customerOrder d=new customerOrder();
        new takeOrder(d);
        new dispOrder(d);
    }
}
/*
            Output

Thread[Manager take an order,5,main]

 Take an Order 1 :: 2 Roti

 Take an Order 2 :: 1 plat Veg.Jaipuri Sabji

 Take an Order 3 :: 1 plat Pulav

Thread[Manager place an order,5,main]

 Place an Order 1 :: 2 Roti
 Place an Order 2 :: 1 plat Veg.Jaipuri Sabji
 Place an Order 3 :: 1 plat Pulav

Thread[Manager take an order,5,main]

 Take an Order 1 :: 3 Roti

 Take an Order 2 :: 1 plat Paneerkadai Sabji

 Take an Order 3 :: 1 plat Biriyani

Thread[Manager place an order,5,main]

 Place an Order 1 :: 3 Roti
 Place an Order 2 :: 1 plat Paneerkadai Sabji
 Place an Order 3 :: 1 plat Biriyani

*/