Friday 30 November 2012

Word Frequency Count :: Java

Word Frequency Count
This program prints the words in order of their frequencies. Higher frequency word will appear on top and words with same frequency will be sorted alphabetically.

import java.util.HashMap;
import java.util.Arrays;

public class WordFrequencyCounter{
   
    private class Word implements Comparable<Word>{
        String word;
        int count;
        public Word(String word, int count){
            this.word=word;
            this.count=count;
        }
        public int compareTo(Word otherWord){
            if(this.count == otherWord.count){
                return this.word.compareTo(otherWord.word);
            }
            return (otherWord.count - this.count);
        }
    }
   
    private Word[] getFrequentWords(String words[]){
        HashMap<String, Word> map = new HashMap<String, Word>();
        for(String s:words){
            Word w = map.get(s);
            if(w == null){
                w = new Word(s, 1);
            }else{
                w.count++;
            }
            map.put(s, w);
        }
        Word[] list = map.values().toArray(new Word[]{});
        Arrays.sort(list);
        return list;
    }
   
    public static void main(String... args){
        String[] words = {"hello", "hi", "how", "are", "you", "what's up", "hi", "hello"};
        Word[] frequency = new WordFrequencyCounter().getFrequentWords(words);
        for(Word w:frequency){
            System.out.println(w.word+" "+w.count);
        }
    }
} 


profile for Mohammad Faisal at Stack Overflow, Q&A for professional and enthusiast programmers

Tuesday 12 June 2012

Java Keywords Categorized

I'd recently looking at the java keywords and thought of categorizing them to make it easier to memorize.

I'd worked on them and categorized on my best but as everything is never perfect so I need your suggestions and support to make them more effective.

Here I'd categorized them as:

  1. Data Types:
    1. byte
    2. short
    3. int
    4. long
    5. float
    6. double
    7. char
    8. boolean
    9. strictfp
  2. Control Statements:
    1. if
    2. else
    3. for
    4. while
    5. do
    6. switch
    7. case
    8. default
    9. break
    10. continue
  3. Access Specifier / Modifier:
    1. public
    2. protected
    3. private
    4. static
  4. Class / Object / Method:
    1. class
    2. interface
    3. enum
    4. abstract
    5. super
    6. this
    7. new
    8. extends
    9. implements
    10. instanceof
    11. void
    12. return
    13. native
    14. final
  5. Exception Handling:
    1. try
    2. catch
    3. finally
    4. throw
    5. throws
  6. Package:
    1. package
    2. import
  7. Threading / IO:
    1. synchronized
    2. volatile
    3. transient
  8. Debugging:
    1. assert
  9. Reserved but not used:
    1. goto
    2. const
  10. Reserved for literals:
    1. true
    2. false
    3. null

Thursday 10 November 2011

Java program to multiply two numbers upto 100000 places

Here's I'd created a Java program by which you can be able to multiply any two integer numbers upto 100000 places.
e.g., if you had to multiply
    99999999999999999999999999999999999999999999999
X  99999999999999999999999999999999999999999999999
--------------------------------------------------------------------------------------------------
*******What will be the output of your program??????*******
--------------------------------------------------------------------------------------------------



Here's my program:


Friday 28 October 2011

Execute DOS command :: JAVA

In the following program I'd tried to open a Notepad using DOS command.
In the same way you can execute other various commands like shutdown your computer or compile a java program.
Here it go:

public class ExecuteDOSCommand{
    public static void main(String... s){
        try{
            Thread.sleep(5000);//the main thread sleep for 5sec. then it execute
            Process p=Runtime.getRuntime().exec("Notepad");//command to open notepad
        }catch(Exception e){
                System.out.println("Sorry! Something went wrong.");
        }
    }
}


Here are other codes you can try:

Process p=Runtime.getRuntime().exec("shutdown -s -t 10");//shutdown your computer after 10sec.