Saturday, 22 March 2014

10) Write an application to generate following threads I. Count vowels from accepted string from user. II. Calculate sum of digits of number accepted from user.

import java.io.*;
class VowelCount implements Runnable
{
    public void run()
    {
        int cnt=0;
           try
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter the string");
            String data=br.readLine();
            int len=data.length();
            for(int i=0;i<len;i++)
            {
                if(data.charAt(i)=='a'|| data.charAt(i)=='e'|| data.charAt(i)=='i'|| data.charAt(i)=='o'|| data.charAt(i)=='u' || data.charAt(i)=='A'|| data.charAt(i)=='E'|| data.charAt(i)=='I'|| data.charAt(i)=='O'|| data.charAt(i)=='U')
                cnt++;
            }
            System.out.println("Total number of vowels in entered string are "+cnt);
        }
        catch(Exception e){}
    }
}

class SumOfDigit implements Runnable
{
    public void run()
    {
        int sod=0;
        try
        {
            Thread.sleep(10000);
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter the number ");
            int no=Integer.parseInt(br.readLine());
            while(no>0)
            {
                sod=sod+no%10;
                no=no/10;
            }
            System.out.println("Sum of digit of entered number is "+sod);
        }
        catch(Exception ex){}
    }
}


class VowelSod
{
    public static void main(String args[])
    {
        try
        {
            VowelCount vc=new VowelCount();
            Thread t1=new Thread(vc);
            SumOfDigit sd=new SumOfDigit();
            Thread t2=new Thread(sd);
            t1.start();
            t2.start();
        }
        catch(Exception e1){}
    }
}

No comments:

Post a Comment