Showing posts with label JAVA PROGRAMMING. Show all posts
Showing posts with label JAVA PROGRAMMING. Show all posts

Decoding Encoded Message

Decoding Encoded Message

 /*The Computer Department of the Agency of International Espionage is trying to decode intercepted messages. The agency’s spies have determined that the enemy encodes messages by first converting all characters to their ASCII values and then reversing the string.
For example consider A_z ( the underscore is just to highlight the space. The ASCII values of A, <space>, z are 65, 32 and 122 respectively. Concatenate them to get 6532122, then reverse this to get 2212356 as the coded message.
Write a program which reads a coded message and decodes it. The coded message will not exceed 200 characters. It will contain only alphabets (A..Z and a..z) and spaces. ASCII values of A…Z are 65 to 90 and those of a..z are 97 to 122.
Sample Output
Enter the encoded message
2312179862310199501872379231018117927

The decoded message is ::  Have A Nice Day*/

import java.io.*;
class Q2_2004
{
    //Data Members
    public String msg;
    public String ans;
    public String rev;
    public int len;
    public String newans;
   
    //Methods
    //Default Constructor
    Q2_2004()
    {
        msg="";
        ans="";
        rev="";
        len=0;
        newans="";
    }

    //Function to input the encoded message
    public void Input()
    throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   
        int i,len1;
        System.out.println("Enter the encoded message");
        msg=br.readLine();
        len1=msg.length();
        for(i=(len1-1);i>=0;i--)
           rev+=msg.charAt(i);
        len=rev.length();
    }

    //Function to extract the digits from the reverse number and decode it
    public void Decode()
    throws IOException
    {
        Input();
        int lwrnum[]={97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122};
        char lwrcas[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        int uprnum[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};
        char uprcas[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
   
        int i,j;
        int digit;
        int number=0;
        for(i=0;i<len;i++)
        {
            digit=Integer.parseInt(rev.substring(i,i+1));
            number=number*10+digit;
       
            if(number>=65 && number <=90)
            {
               for(j=0;j<26;j++)
               {
                   if(number==uprnum[j])
                     ans+=uprcas[j];
                }//j
                number=0;
            }//if
            else
            if(number>=97 && number <=122)
            {
               for(j=0;j<26;j++)
               {
                   if(number==lwrnum[j])
                     ans+=lwrcas[j];
                }//j
                number=0;
            }//if
            else
            if(number==32)
            {
              ans+=" ";
              number=0;
            }
         }//i
         ans=" "+ans;
         ans+=" ";
         len=ans.length();
    
         char ltr;
         for(i=0;i<len;i++)
         {
             ltr=ans.charAt(i);
             if(ltr!=' ')
               newans+=ltr;
             else
             if(ltr==' ')
             {
                 newans+=ltr;
                 ltr=ans.charAt(i+1);
                 for(j=0;j<26;j++)
                 {
                     if(ltr==lwrcas[j])
                        ltr=uprcas[j];
                 }
                 newans+=ltr;
                 i++;
             }//if
         }
    }

    //Function to Display the string
    public void Display()
    throws IOException
    {
        Decode();
        System.out.print("The decoded message is :: "+newans);
    }
}
                   
class Q2_2004main
{
    public void main()
    throws IOException
    {
        Q2_2004 SM=new Q2_2004();
        SM.Display();
    }
}

Search From a list of telephone Numbers

Search From a list of telephone Numbers

/* A file contains a list of names and telephone numbers in the following form:-
AJIT                                        281050
ANIL                                       462890
RISHIKESH                            524352

The names contain only one word and the names and telephone numbers are separated by
white spaces. Write a interactive program to   :-

    Create the above file
    Display the contents of two columns
    Search a telephone number for a given name( Assure no duplication of name)
    Exit the program.


The program must create the program for the first time.
Test your program for the following data:-

AJIT                                        281050
ANIL                                       462890
RISHIKESH                            524352
JAVAGAL                              345678
MONGIA                                234561
RAHUL                                   765433
ROBIN                                    543221
SACHIN                                  765433
SAURAV                                8765908
VENKATESH                         7654322

Answer 3*/
import java.io.*;
class Names_Telephones
{
    public static void main(String args[])
    throws IOException
    {
        String filename="Directory.Txt";
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
        int i,n;
        String search;
     
        System.out.println("How many students");
        n=Integer.parseInt(br.readLine());
        String name,phone;
     
        FileWriter fw=new FileWriter(filename);
        BufferedWriter bw=new BufferedWriter(fw);
        PrintWriter outfile=new PrintWriter(bw);
     
        System.out.println("Enter "+n+" records one by one");
     
        //Writing Records into file
        for(i=1;i<=n;i++)
        {
            System.out.println("Name::");
            name=br.readLine();
            outfile.println(name);
         
            System.out.println("Phone Number::");
            phone=br.readLine();
            outfile.println(phone);
        }
        outfile.close();
     
        //Reading Records into file
        FileReader readfile=new FileReader(filename);
        BufferedReader infile=new BufferedReader(readfile);
     
        System.out.println("Enter name to search");
        search=br.readLine();
     
        while((name=infile.readLine())!=null)
        {
            phone=infile.readLine();
            if(name.compareToIgnoreCase(search)==0)
            {
                System.out.println("Name ::"+name+ " Phone number :: "+phone);
            }
        }
      infile.close();
    }

}

Diamond String

Diamond String

/* Write a program to print a rectangle with a diamond shape gap exactly at the center of the
Rectangle, using an input string with odd number of characters, not exceeding 20.
For Example:-
INPUT                         :-         HAPPY-HAPPY
OUTPUT:
Enter a word of odd number of characters
HAPPY-HAPPY

HAPPY-HAPPY
HAPPY HAPPY
HAPP       APPY
HAP            PPY
HA                PY
H                     Y
HA                PY
HAP            PPY
HAPP       APPY
HAPPY HAPPY
HAPPY-HAPPY

Answer 1.*/
import java.io.*;
class Diamond_String
{
    //Data Member
    public String word;
    public int len;
    public int spc;
 
    //Methods
    //Default Constructor
    Diamond_String()
    {
        word="";
        len=0;
        spc=0;
    }
     
        //Function to Input String
        public void Input()
        throws IOException
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            do
            {
                System.out.println("Enter a word of odd number of characters");
                word=br.readLine();
                word=word.toUpperCase();
                len=word.length();
                if(len>=20)
                System.out.println("Word is more than 20 characters.....Try again");
                if(len%2==0)
                System.out.println("Word is of even characters.....Try again");
            }
            while((len%2)==0);
        }
       
         //Function to Display first half of String
         public void Display1()
         {
            int x,i,j,k;
            spc=1;
            x=(len/2)-1;
            k=0;
            System.out.println(word);
            for(i=1;i<=(len/2);i++)
            {
                //Printing left hand side
                for(j=0;j<=x;j++)
                    System.out.print(word.charAt(j));
                x--;
         
                //Printing spaces
                for(j=1;j<=spc;j++)
                    System.out.print(" ");
                spc+=2;
         
                //Printing right hand side
                for(j=k;j<(len/2);j++)
                    System.out.print(word.charAt(j));
                k++;
             
                System.out.println();
            }
        }
     
        //Function to Display second half of String
        public void Display2()
        {
            int i,j,k,x;
            spc-=4;
            k=(len/2)-2;
            x=1;
         
            for(i=1;i<=((len/2)-1);i++)
            {
                //Printing left hand side
                for(j=0;j<=x;j++)
                    System.out.print(word.charAt(j));
                x++;
                  
                //Printing spaces
                for(j=spc;j>=1;j--)
                    System.out.print(" ");
                spc-=2;
         
                //Printing right hand side
                for(j=k;j<(len/2);j++)
                    System.out.print(word.charAt(j));
                k--;
                System.out.println();
            }
         
            System.out.println(word); 
             
         
        }
    }
     

class Diamond_Example
{
    public void main()
    throws IOException
    {
        Diamond_String DS=new Diamond_String();
        DS.Input();
        DS.Display1();
        DS.Display2();
    }

}

Login and Logout time

Login and Logout time

/* Login and Logout time
The manager of a company wants to analyse the machine usage from the records to find the utilization of the machine. He wants to know how long the each user used the machine. When the user wants to use the machine he must login to the machine and after finishing the work he must log off the machine.
Each log record consists of:

User Identification number
Login time and date
Logout time and date

Time consists of
Hours
Minutes

Date consists of
Day
Month

You may assume all logins and logouts are in the same year and there are 100 users at most. The time format is 24 hour machine hours and minutes.
Design a program to find:
• To find the duration for which each user has logged.
• Output all records along with the duration. You may assume no user will login for more than 48 hours.

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

Sample data:
INPUT :
Number of users : 3
USER LOGIN LOGOUT
IDENTIFICATION TIME & DATE TIME & DATE

149         20:10 20-12 2:50        21-12
173        12:30   20-12 12:30 21-12
142       16:20  20-12 16:30 20-12

OUTPUT :

USER LOGIN LOGOUT DURATION
IDENTIFICATION TIME & DATE TIME & DATE HOURS:MINS
149 20:10 20-12 2:50 21-12 6:40
173 12:30 20-12 12:30 21-12 24:00
142 16:20 20-12 16:30 20-12 00:0

The user who logged in for the longest duration:-
173 12:30 20-12 12:30 21-12 24:00

Answer 3.*/
import java.io.*;
class Date1
{
 //Data members
 public int day;
 public int month;
 //Constructor
 public Date1()
 {
     day=0;
     month=0;
 }
 //Function to input date
 public void Input()
 throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter day ::");
       day=Integer.parseInt(br.readLine());
       System.out.println("Enter month ::");
       month=Integer.parseInt(br.readLine());
    }
  //function to display date
  public void display()
  {
      System.out.print(day+"-"+month);
   }
}
class Time
{
 //Data members
 public int hour;
 public int minute;
 //Constructor
 public Time()
 {
     hour=0;
     minute=0;
 }
 //Function to input time
 public void Input()
 throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter hour ::");
       hour=Integer.parseInt(br.readLine());
       System.out.println("Enter minute ::");
       minute=Integer.parseInt(br.readLine());
    }
  //function to display time
  public void display()
  {
      System.out.print(hour+":"+minute);
   }
}
class Q3_2004
{
 //Data members
 public Date1 logindt;
 public Date1 logoutdt;
 public Time logintm;
 public Time logouttm;
 private int userid;
 //Constructor
 Q3_2004()
 {
     userid=0;
     logindt=new Date1();
     logoutdt=new Date1();
     logintm=new Time();
     logouttm=new Time();
  }
  //Function to input data
  public void Input()
  throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter user identification number ::");
       userid=Integer.parseInt(br.readLine());
       System.out.println("Enter login time::");
       logintm.Input();
       System.out.println("Enter login date::");
       logindt.Input();
       System.out.println("Enter logout time::");
       logouttm.Input();
       System.out.println("Enter logout date::");
       logoutdt.Input();     
    }
   //Function to find duration
   public Time Duration()
   {
       Time duration=new Time();
       if(logintm.minute > logouttm.minute)
         duration.minute=(60-logintm.minute)+logouttm.minute;
       else
         duration.minute = logouttm.minute-logintm.minute;
       duration.hour=(24-logintm.hour)+logouttm.hour;
       return duration;
    }
  //Function to display
  public void Display()
  {
      System.out.print(userid+"\t\t"+logintm.hour+":"+logintm.minute+"\t"+logindt.day+"-"+logindt.month+"\t\t"+logouttm.hour+":"+logouttm.minute+"\t"+logoutdt.day+"-"+logoutdt.month);
  }
  
  //Function to find duration
  public void findduration()
  throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       int n,i,longhour=0;
     
       Q3_2004 longobj=new Q3_2004();
          
       System.out.println("Number of users :;");
       n=Integer.parseInt(br.readLine());
       Time longdur =new Time();
       for(i=1;i<=n;i++)
       {
           System.out.println("\nEnter details for user "+i);
           Q3_2004 obj=new Q3_2004();
           obj.Input();
           obj.Display();
           Time dur=new Time();
           dur=obj.Duration();
           System.out.print("\t\t");
           dur.display();
           System.out.println("\n");
         
           if(dur.hour>longhour)
           {
               longhour=dur.hour;
               longobj=obj;
               longdur=dur;
            }
        }
     
        System.out.println("\nThe user logged in for the longest duration ::");
        longobj.Display();
        System.out.print("\t\t");
        longdur.display();
    }
}
class Q3_2004main
{
  public static void main(String args[])
  throws IOException
  {
      Q3_2004 obj=new Q3_2004();
      obj.findduration();
    }
}

Printing a sentence in reverse order of words

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();
    }
}

Mersenne Numbers

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");
}
}

Time Conversion

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);
    }
}

Catalan Numbers

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);
        }
    }
}
}

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).

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 = Ã– Ã¥( x– 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

Two strings are anagram if they are written using same exact letters,ignoring space, punctuation and capitalization. Each letter should have the same count in both strings.

PROGRAM: ANAGRAM STRINGS

 Two strings are anagram if they are written using same exact letters,ignoring space, punctuation and capitalization. Each letter should have the same count in both strings.

 For Example:Mary and Army are anagram of each other.

 SYNTAX:
import java.io.*;
class anagram
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Two Strings to check whether they are anagram or not");
        String s1=br.readLine();
        String s2=br.readLine();
        String nstr="",ntr="";int count=0;
        for(int i=0;i less than s1 length;i++)
        {
            if(Character.isLetter(s1.charAt(i)))
            {
                nstr+=s1.charAt(i);
            }
        }
        for(int i=0;i less than s2 length;i++)
        {
            if(Character.isLetter(s2.charAt(i)))
            {
                ntr+=s2.charAt(i);
            }
        }
        nstr=nstr.toLowerCase();
        ntr=ntr.toLowerCase();
        for(int i=0;i less than nstr length;i++)
        {
            for(int j=0;j less than ntr length;j++)
            {
                if(nstr.charAt(i)==ntr.charAt(j))
                {
                    count++;
                    break;
                }
            }
        }
        if(count==nstr.length())
        {
            System.out.println("Strings are anagram");
        }
        else
        System.out.println("Strings are not anagram");
    }
}

MENU DRIVEN PROGRAM IN JAVA SIMPLE CALCULATOR

MENU DRIVEN PROGRAM IN JAVA SIMPLE CALCULATOR

import java.io.*;
public class Menu
{
    private int a;  //Instance Variable
    private int b;
    private int c;
    private static InputStreamReader in=new InputStreamReader(System.in);
    private static BufferedReader br=new BufferedReader(in);
    void menu()
    {
        //System.out.println("\f");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.println("5. Exit");
        System.out.println("Press your choice.........>");
    }

    void sum(int a,int b)
    {
        this.a=a;
        this.b=b;
        this.c=this.a+this.b;
        System.out.println("Sum="+this.c);
        // this.a=Integer.parseInt(br.readLine());
    }

    void sub(int x,int y)
    {
        a=x;
        b=y;
        c=a-b;
        System.out.println("Subtraction="+c);
    }

    void mult(int x,int y)
    {
        a=x;
        b=y;
        c=a*b;
        System.out.println("Multplication="+c);
    }

    void div(int x,int y)
    {
        a=x;
        b=y;
        c=a/b;
        System.out.println("Division="+c);
    }

    public static void main(String args[])throws IOException
    {
        Menu ob=new Menu();

        int ch,a,b;
        ch=0;
        do
        {
            ob.menu();
            ch=Integer.parseInt(br.readLine());
            switch(ch)
            {
                case 1:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.sum(a,b);
                break;
                case 2:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.sub(a,b);
                break;
                case 3:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.mult(a,b);
                break;
                case 4:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.div(a,b);
                break;
                case 5:
                System.exit(0);
                default:
                System.out.println("Wrong choice:");
            }
        }while(ch!=5);
    }       
}

Sample Output
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Press your choice.........>
1
Enter two number
2
5
Sum=7
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Press your choice.........>

5