Tuesday, 20 May 2014

Standard Deviation Of A Sentence

Write a program to input a piece of text consisting of sentences terminated by either ‘.’ or ‘!’ or ‘?’. The program should output the mean and standard deviation of the lengths of sentences (measured in words). Each word is separated by a blank space.
Mean =>                     x’         = n
                                                     å x i
                                                         i= 1
                                                ---------------
                               n  

where x i is the length of the ith sentence and n is the total number of sentences.

Standard Deviation
          n
s = Ö å( xi – x’)2
            i=1
      -----------------
            n
  ( Assume a maximum of 10 sentences ) . Test your program for the following input :
HELLO!HOW ARE YOU?HOPE EVERYTHING IS FINE.BEST OF LUCK.

Answer 2.

class Q2_1998
{
 //Data members
 private String para;
 private int len;
 private String []sentences;
 private int nos;
 private int []lensent;
 //Constructor
 public Q2_1998(String par)
 {
     para=par+" ";
     len = para.length();
     sentences=new String[10];
     nos=0;
     lensent=new int[10];
     int i;
     //Initilizing array for storing length of sentences
     for(i=0;i<10;i++)
       lensent[i]=0;
 }
 //method to extract sentences
 public void Extract()
 {
        para=para.toUpperCase();
        String word="",sent="";
        int i;
        char letr;
        for(i=0;i<len;i++)
        {
            letr=para.charAt(i);
            if(letr!=' ')
                word+=letr;
           
            if(letr==' ')
            {
                sent+=word;
                sent+=" ";
                word="";
            }
          
            if(letr=='.'||letr=='!'||letr=='?')
            {
                sent+=word;
                sent+=" ";
                i++;//so as not to pick up the blank after the ., ? or !
                sentences[nos]=sent;
                sent="";
                nos++;
                word="";
            }
        }
    }
  
   //method to find length of sentences
   public void findlength()
   {
       int i,j;
       char letter;
       Extract();
       for(i=0;i<nos;i++)
       {
           for(j=0;j<sentences[i].length();j++)
           {
               letter=sentences[i].charAt(j);
               if(letter==' ')
                 lensent[i]++;
            }
      }
    }
    //method to find standard deviation
    public void standarddeviation()
    {
        int i,sum=0;
        double mean,std=0;
        findlength();
      //Finding total length of all sentences
      for(i=0;i<nos;i++)
        sum+=lensent[i];
      mean=sum/nos;
      //Finding standard deviation
      for(i=0;i<nos;i++)
      {
          std=Math.sqrt((Math.pow((lensent[i]-std),2))/nos);
          System.out.println(sentences[i]+"\t\t\t deviates from mean by \t\t"+ std);
      }
    }
}
       
       
 import java.io.*;
class Q2_1998main
{
 public static void main(String args[])
 throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String para;
        System.out.println("Enter a paragraph ");
        para=br.readLine();
        Q2_1998 obj= new Q2_1998(para);
        obj.standarddeviation();
    }
}          


              

Output


  Enter a paragraph
HELLO! HOW ARE YOU? HOPE EVERYTHING IS FINE. BEST OF LUCK.

HELLO!                                               deviates from mean by                      0.5
HOW ARE YOU?                                deviates from mean by                      1.25
HOPE EVERYTHING IS FINE.          deviates from mean by                      1.375
BEST OF LUCK.                                 deviates from mean by                      0.8125

Tuesday, 29 October 2013

Catalan Numbers

/*Catalan Numbers are the numbers of the form:
(2n)!/((n!)^2*(n+1))*/
import java.util.Scanner;
class Catalan
{
public static void main(String[ ] args)
{
    Scanner in=new Scanner (System.in);
    System.out.println("Enter value of n(1...10):");
    int n=in.nextInt();
    long n1,n2,a;
    long CatNumber;
    int cnt=1;
    if(n>=1 && n<=10)
    {
        for(a=1;a<=n;a++)
        {
            long factofa=1,factof2a=1;
            for(long i=1;i<=a;i++)
            factofa*=i;
            for(long i=1;i<=2*a;i++)
            factof2a*=i;
            CatNumber=factof2a/((factofa*factofa)*(a+1));
            System.out.println(" "+cnt++ +". "+CatNumber);
        }
    }
}
}
        

Time Conversion

import java.util.Scanner;
    public class Conversion
    {
        public static void main(String[]args)
        {
            Scanner in=new Scanner (System.in);
            System.out.println("Enter time in milliseconds");
            double time=in.nextDouble();
            String unit="ms";
            if(time>=365*24*60*60*1000L)
            {
                unit="years";
                time/=365*24*60*60*1000L;
            }
            else if(time>=24*60*60*1000)
            {
                unit="days";
                time/=24*60*60*1000;
            }
            else if(time>=60*60*1000)
            {
                unit="hours";
                time/=60*60*1000;
            }
            else if(time>=60*1000)
            {
            unit="minutes";
            time/=60*1000;
        }
        else if(time>=1000)
        {
            unit="seconds";
            time/=1000;
        }
        System.out.println("Most appropriate unit for this time is "+unit+" ");
        System.out.println("This time is nearly equal to "+time+" "+unit);
    }
}

Mersenne Numbers

/*Check if a number is mersenne number or double mersenne number
Mersenne Numbers are the Numbers which are one less than a power of 2 i.e., (2^1-1),(2^2-1),(2^3-1).........
Some Mersenne Numbers are 1,3,7,15,31,63,127.......

Double Mersenne Numbers is a number of the form 2^(2^n-1)-1
Some Double Mersenne Numbers are 1,7,127,32767,2147483647.........*/
import java.util.Scanner;
public class Numbers
{
long number;
Numbers(long n)
{
number=n;
}
boolean isMersenne(){
long n=0;
for(int i=1;i<=20;i++)
{
n=(long)(Math.pow(2,i)-1);
if(n==number){
return true;
}
}
return false;
}
boolean isDoubleMersenne(){
long n=0;long pwr=0;
for(int i=1;i<=20;i++)
{
pwr=(long)Math.pow(2,i)-1;
n=(long)(Math.pow(2,pwr)-1);
if(n==number)
{
    return true;
}
}
return false;
}
public void genMersenneNos(){
    long num=0;
    System.out.println("Generated Mersenne numbers are :");
    for(int i=1;i<=10;i++)
    {
        num=(long)(Math.pow(2,i)-1);
        System.out.print(num+" ");
    }
    System.out.println();
}
public void genDoubleMersenneNos()
{
    long num=0;long pwr=0;
    System.out.println("Generated double Mersenne Numbers are:");
    for(int i=1;i<=6;i++)
    {
     pwr=(long)Math.pow(2,i)-1;
     num=(long)(Math.pow(2,pwr)-1);
     System.out.print(num+" ");
    }
    System.out.println();
}
public static void main(String[]args){
    Scanner in=new Scanner(System.in);
    long n;
    System.out.println("Enter a number");
    n=in.nextLong();
    Numbers numObject=new Numbers(n);
    if(numObject.isDoubleMersenne()==true)
    {
        System.out.println(n+"is a double Mersenne Number");
        numObject.genDoubleMersenneNos();
    }
    else if(numObject.isMersenne()==true)
    {
      System.out.println(n+"is a Mersenne Number");
      numObject.genMersenneNos();
    }
    else
    System.out.println(n+"is neither a Mersenne Number nor a Double Mersenne Number");
}
}
    


   

Monday, 28 October 2013

Printing a sentence in reverse order of words

 /*The input in this problem will consist of a number of lines of English text consisting of the letters of the English alphabet, the punctuation marks (‘) apostrophe, (.) full stop, (,) comma, (;) semicolon, (:) colon and white space characters (blank, newline). Your task is to print the words of the text in reverse order without any punctuation marks other than blanks.
For example consider the following input text :
This is a simple piece of text to illustrate this problem.

If you are smart you will solve this right.

The corresponding output would read as:

right this solve will you smart are you If problem this illustrate to text of piece sample a is This

That is, the lines are printed in reverse order.
Note:- Individual words are not reversed.

Input format

The first line of input contains a single integer N (<=20), indicating the number of lines in the input. This is followed by N lines of input text. Each line should accept a maximum of 80 characters.

Output format

Output the text containing the input lines in reverse order without punctuations except blanks as illustrated above.

Test your program for the following data and some random data.

SAMPLE DATA

INPUT :

2
Emotions, controlled and directed to work, is character. By Swami Vivekananda.

OUTPUT :
Vivekananda Swami By character is work to directed and controlled Emotions


INPUT :

1
Do not judge a book by its cover.

OUTPUT :
cover by its book a judge not Do


Answer 2.*/
import java.io.*;
class Q2_2007
{
  //Data members
  private String para;
  private String newpara;
  int len;
  int n;
  //Constructor
  public Q2_2007()
  {
      para="";
      newpara="";
      len=0;
  }
  //Function to input string
  public void Input()
  throws IOException
  {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.print("Enter number of lines ::");
     n=Integer.parseInt(br.readLine());
     System.out.println("Enter a paragraph of "+ n + " lines ");
     para=br.readLine();
     para=" "+para;
     len=para.length();
  }
  //Function to reverse para
  public void Reversepara()
  {
      int i;
      String word="";
      char letter;
      for(i=(len-1);i>=0;i--)
      {
          letter = para.charAt(i);
          if(letter!='\'' && letter!='.'&&letter!=','&&letter!=';'&&letter!=':'&&letter!=' ')
            word=letter+word;
          else
          {
              newpara+=word;
              newpara+=" ";
              word="";
            }
        }
        System.out.println("\n new paragraph ::\n"+newpara);
    }
}


class Q2_2007main
{
   public static void main(String args[])
   throws IOException
    {
        Q2_2007 obj = new Q2_2007();
        obj.Input();
        obj.Reversepara();
    }
}

Sunday, 27 October 2013

Java - Whats It?


Java is a computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another. Java applications are typically compiled to bytecode (class file) that can run on any Java virtual machine (JVM) regardless of computer architecture. Java is, as of 2012, one of the most popular programming languages in use, particularly for client-server web applications, with a reported 9 million developers. Java was originally developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.
The original and reference implementation Java compilers, virtual machines, and class libraries were developed by Sun from 1991 and first released in 1995. As of May 2007, in compliance with the specifications of the Java Community Process, Sun relicensed most of its Java technologies under the GNU General Public License. Others have also developed alternative implementations of these Sun technologies, such as the GNU Compiler for Java (bytecode compiler), GNU Classpath (standard libraries), and IcedTea-Web (browser plugin for applets). 




There were five primary goals in the creation of the Java language:
  1. It should be "simple, object-oriented and familiar"
  2. It should be "robust and secure"
  3. It should be "architecture-neutral and portable"
  4. It should execute with "high performance"
It should be "interpreted, threaded, and dynamic"


Syntax
Main article: Java syntax
The syntax of Java is largely derived from C++. Unlike C++, which combines the syntax for structured, generic, and object-oriented programming, Java was built almost exclusively as an object-oriented language. All code is written inside a class, and everything is an object, with the exception of the primitive data types (e.g. integers, floating-point numbers, boolean values, and characters), which are not classes for performance reasons.
Unlike C++, Java does not support operator overloading or multiple inheritance for classes. This simplifies the language and aids in preventing potential errors and anti-pattern design.
Java uses similar commenting methods to C++. There are three different styles of comments: a single line style marked with two slashes (//), a multiple line style opened with /* and closed with */, and the Javadoc commenting style opened with /** and closed with */. The Javadoc style of commenting allows the user to run the Javadoc executable to compile documentation for the program.