haris dic.

import java.util.ArrayList;

import java.util.Iterator;

import java.util.*;

/**

* class SpellChecker for a text application (for example, a word processor, or something similar).

*

* @author (Waqar Ahmad 116-bscs-2010,Haris zeeshan 105-bscs-2010,Ammar Hassan 60-bscs-2012,Ammar zaheer 69-bscs-2009)

* @version (1.0)

*/

public class SpellChecker

{

DictReader dr;

private ArrayList<String> dic;

private ArrayList<String> listofWords;

/**

* Constructor for objects of class SpellChecker

*/

public SpellChecker()

{

dr=new DictReader(“words.txt”);

dic=dr.getDictionary();

listofWords=new ArrayList<String>();

}

//########## Base Task Level 1 impelements by Ammar hassan and Ammar zaheer

/**

* This method returns the number of words in the dictionary.

*/

public int numberOfWords()

{

return dic.size();

}

/**

* This method returns true, if (and only if) the given word is found in the dictionary.

*/

public boolean isKnownWord(String word)

{

if(dic.contains(word))

return true;

else

return false;

}

/**

* This method returns true if (and only if) all words in the given list are found in the dictionary.

*/

public boolean allKnown(ArrayList<String> words)

{

for(int i=0;i<words.size();i++)

{

if(!isKnownWord(words.get(i).toLowerCase()))

return false;

}

return true;

}

//######### Base Task Level 2 impelements by Waqar Ahmad & Haris zeeshan

/**

* This method returns a list of all words from the dictionary that start with the given prefix.

*/

public ArrayList<String> wordsStartingWith(String prefix)

 

{

Iterator <String>it=dic.iterator();

while(it.hasNext())

{

String ch=it.next();

if(ch.startsWith(prefix.toLowerCase()) || ch.startsWith(prefix.toUpperCase()))

{

listofWords.add(ch);

}

}

return listofWords;

}

/**

* This method returns a list of all words from the dictionary that include the given substring.

*/

public ArrayList<String> wordsContaining(String text)

{

String s1;

ArrayList<String> wordsList=new ArrayList();

for(int i=0;i<dic.size();i++)

{

s1=dic.get(i);

if (s1.toLowerCase().contains(text.toLowerCase()) || s1.toUpperCase().contains(text.toUpperCase()))

{

wordsList.add(s1);

}

}

return wordsList;

}

//######### Base tasks (level 3) impelements by Waqar Ahmad

/**

* Insert the given word into the dictionary if it does not exist in it.

* The word should only be inserted if it does not already exist in the dictionary.

*/

public void insert(String newWord)

{

boolean isExist=isKnownWord(newWord);

if(isExist==false)

{

dic.add(newWord);

}

Collections.sort(dic);

}

/**

* Remove the given word from the dictionary.

* If the word was successfully removed, return true.

* If not (for example it did not exist) return false.

*/

public boolean remove(String newWord)

{

if(dic.contains(newWord))

{

int a=dic.indexOf(newWord);

dic.remove(a);

return true;

}

else

return false;

}

/**

* Method use to write ArrayList on a text File

*/

public void save()

{

dr.save(dic);

}

//#########   Challenge tasks  co-effort of group on these Methods

/**

* Return true if (and only if) the given word is a palindrome

*/

public boolean isPalindrome(String word)

{

int a=word.length()-1;

for(int i=0;i<=a/2;i++)

{

if(word.toLowerCase().charAt(i)==word.toLowerCase().charAt(a))

{    a–;

}

else

{

return false;

}

}

if(dic.contains(word))

{

System.out.println(“Dictionary not Contains Given Word !!”);

}

else

{

System.out.println(“word dose not exist in dic”);

}

return true;

}

/**

* Return a list of all words that are anagrams of the given word.

* Anagrams are treated as case-insensitive.

*/

public ArrayList<String> anagrams(String word)

{

// trying to complete ,but Cannot achive this goal

return dic  ;

}

/**

* . Return a list of words that are in one of the dictionaries, but not in the other.

*   pass another dictionary as a parameter

*/

public ArrayList<String> difference(ArrayList<String> dictionary)

{

ArrayList <String> difWords=new <String> ArrayList();

for(int index=0;index<=dictionary.size();index++)

{   String word=dictionary.get(index);

if(!dic.contains(word))

{

difWords.add(word);

}

}

return difWords;

}

}

Spell Checker

import java.util.*;

/**

* Its a class which takes Dictionary from the class DictReader and Checks

* different methods upon it.

*

* @author (Farhan ullah(033-BSCS-2010), Zaeem Nazir(117-BSCS-2010), Mir Qasim(111-BSCS-2010),

*          Ahmad Butt(066-BSCS-2010) )

* @version (SpellCheck ver.1.0)

*/

 

public class SpellChecker

{

private ArrayList<String> words;

private ArrayList<String> anagrams= new ArrayList<String>();

private DictReader readers;

/**

* Constructor for objects of class SpellChecker The Constructor of the

* Class Spell Checker loads the whole dictionary in the text file to the

* memory and save it to the arraylist words

*/

public  SpellChecker()

{

// words= new ArrayList<String>();

readers=new DictReader(“words.txt”);

words=readers.getDictionary();

}

// ———-LEVEL 1———-

/**

* The Method is Implemented by Ahmad Butt. The Function runs an iterator

* and shows the size of the arraylist The Words in the dictionary are

* seprated by Space so each line is an index and no of index equal to the

* total number of word.

*

*/

 

public int numberOfWords()

{

int size;

size=words.size();

return size;

 

}

 

/**

* The Method is Implemented by Ahmad Butt.The case insensitive method is

* made by Zaeem Nazir. The method isKnownWord takes an input from the user

* and it checks it weather the word is in the included dictionary or not.

* The Word is first changed to the lowercase and the enhanced for loop is

* used over the whole arraylist and the word is searched

*/

public boolean isKnownWord(String word)

{

boolean number=false;

if(words.contains(word))

{

number=true;

}

return number;

}

/**

* This method is a bit same to the previous one. This is also implemeted by Ahamd

* Butt. This method takes an arraylist and compare it to existing array

* list words

*/

public boolean allKnown(ArrayList<String>  inputList)

{

 

for(String group: inputList)

{

boolean allWords=false;

for(String list: words)

{

if(group.equals(list))

{

allWords=true;

}}

 

if(allWords!=true)

{

return false;

}

}

return true;

}

// ————— level 2 —————-

/**

* This method is implemented by Zaeem Nazir. This method takes a prefix in

* format string from user and then compare it to the whole dictionary and

* all the words which start with the given prefix are shown This method is

* case insensitive. The word “Glib” and “glib” are same.

*/

 

public ArrayList<String> wordsStartingWith(String prefix)

{

ArrayList<String> availableList=new ArrayList<String>();//making of a arry list

String caseChanger=prefix.toUpperCase();

String caseChanger2;

for(String storeList: words)//variable name storelist type string…save words index

{

caseChanger2=storeList.toUpperCase();

if(caseChanger2.startsWith(caseChanger))//function starwith

{

availableList.add(storeList);

}

 

}

return availableList;

 

}

/**

* This method is implemented by Zaeem Nazir. This method takes a word in

* format string from user and then compare it to the whole dictionary and

* all the words which start with the given text are shown This method is

* case insensitive. The word “play” and “Play” are same.

*/

public ArrayList<String> wordsContaining(String text)

{

ArrayList<String> newList= new ArrayList<String> ();

String converter= text.toUpperCase();

String converter2;

for(String checker: words)

{

converter2=checker.toUpperCase();

if(converter2.contains(converter))

{

newList.add(checker);

}

}

return newList;

}

 

// ————— level 3 —————-

/**

* This method is implemented by Mir Qasim. This is a simple method

* which takes a input and checks weather the given word is already in

* dictionary or not if word is not present then it is added to the

* arraylist

*/

public boolean insert(String newWord)

{

boolean checker=false;

if(words.contains(newWord))

{

return checker=false;

}

else

{

words.add(newWord);

checker=true;

Collections.sort(words);

}

return checker;

} /**

* This method is implemented by Mir Qasim. This is a simple method

* which checks a word is present in dictionary or not if it is present then

* it removes it from the dictionary

*

*/

public boolean remove(String newWord)

{

boolean remover=false;

remover=isKnownWord(newWord);

if(remover=true)

{

words.remove(newWord);

}

return remover;

}

/**

* This method is implemented by Mir Qasim. This method simply saves

* the arraylist file to the disk in text format.

*/

public void save()

{

DictReader dictreader=new DictReader(“words.txt”);//class name..object name…new costruter

dictreader.save(words);

}

// ———————- Challenge Tasks—————

/**

* This method is implemented by Farhan Ullah using the existing

* function isKnown by Ahmad Butt and the Caseinsensitive logic by Zaeem

* Nazir. This method gets an input from the user and then reverses it then

* it compares it to the dictionary if the word is found then it is a

* Palindrome.

*/

public boolean isPalindrome(String word) {

boolean result = false;

String revWord = word;

StringBuffer reader = new StringBuffer(revWord);

revWord = reader.reverse().toString();

result = isKnownWord(revWord); // isKnownWord Already Made Function

// above.

return result;

 

}

/**

* This method is implemented by Farhan Ullah. This mehod takes a word in perimeter and returns a list of words that are anagrams of that word.

*/

public ArrayList<String> anagrams(String word) {

word=word.toLowerCase();

ArrayList<String> anag=new ArrayList<String>();

int size=word.length();

Iterator it=words.iterator();

while(it.hasNext())

{

int flag=0;

String check=(String)it.next();

 

if(check.length()==size)

{

char x[] = new char[size];

x=(check.toLowerCase()).toCharArray();

if(!check.equals(word))

{

for(int i=0;i<size;i++)

{

for(int j=0;j<size;j++)

{

if(word.charAt(i)==x[j])

{

x[j]=’0′;

flag++;

break;

}

}

}

}

}

if(flag==size)

anag.add(check);

 

}

return anag;

}

 

/**

* This method is implemented by Farhan Ullah. This can be called a

* blueprint of the exisiting method it takes two dictionaries and comapre

* them to each other and check the word which are in only one dictionary

* and prints them.

*/

public ArrayList<String> difference(ArrayList<String> dictionary) {

ArrayList<String> difference = new ArrayList<String>();

for (String input : dictionary) {

boolean result = false;

for (String word : words) {

if (input.equals(word)) {

result = true;

}

}

if (result != true) {

difference.add(input);

}

}

return difference;

}

 

 

}

 

 

 

 

 

 

 

 

 

POLYMORPHISM IN JAVA

Polymorphism:

The word Polymorphism means of many forms.In programming this word is meant to reuse the single code multiple times. In object oriented programming its a big question that why the Polymorphism is done, what is the purpose of it in our code?

There are lots of people who don’t even know the purpose and usage of Polymorphism.Polymorphism is the 3rd main pillar of OOP without it the object oriented programming is incomplete. Lets go in the depth of Polymorphism.

Why Polymorphism is done in OOP?

Its obvious that when we do inheritance between two classes, all the methods and properties of the first class are derived to the other class so that this becomes the child class which  adobes all the functionality of base class.It can also possesses its own separate methods.

But there is a big problem in inheriting the second class to the first class as it adobes all the methods same as the base class has,which  means that after inheritance both(base class& child class) have the methods of same name and same body as shown in this example:~
Base class:
public class fish
{
  public void eat()
   {
        console.writeline("fish eat");
    }
  public void swim()
   {
          console.writeline("fish swim");
    }
  }
Derieved Class:
class Dolphen:fish
{

  public void eat()
   {
        console.writeline("fish eat");
    }
  public void swim()
   {
          console.writeline("fish swim");
    }
}
In this example it is clear that when we Inherit two classes, all the methods with the same name and same body are adobed by the derived class as shown above.Both methods have the same name but if we change the body of the second method then it makes Compiler disturb whether to compile base class method or derived classes.
To remove this problem and to tell the compiler that which method is to be executed we need to use Polymorphism.

How the Polymorphism is done?

Polymorphism is used to remove the problem which is shown in the above code.It is done not by changing the name of the methods of both base & derived class.Infect it is done by adding the “virtual” keyword before the base class method, and the “override” keyword before the derived class method.As shown in this exmple:
Base Class:
public class fish
        {
            public virtual void eat()
            {
                Console.WriteLine("fish eat");
            }
            public virtual void swim()
            {
                Console.WriteLine("fish swim");
            }
        } 
 Derived Class:

class Dolphin : fish
        {

            public override void eat()
            {
                Console.WriteLine("Dolphin can eat");
            }
            public override void swim()
            {
                Console.WriteLine("Dolphin can swim");
            }
        }
This is actually the Polymorphism in which we write virtual keyword with the base class method and we write override keyword with the derived class method as we did. It helps the compiler to select the method to be executed.
Here is the complete code in which Polymorphism has been applied.
class Program
    {
        public class fish
        {
            public virtual void eat()
            {  }
            public virtual void swim()
            { }
            public virtual void dive()
            {}

        }
       public class Dolphin : fish
         {
            public override void eat()
            { Console.WriteLine("Dolphin eats Plants"); }
            public override  void swim()
            { Console.WriteLine("Dolphin swims quickly"); }
            public override  void dive()
            { Console.WriteLine("Dolphin dive deeply "); }
           public void dance()
            { Console.WriteLine("Dolphin can Dance"); }

        }
        public class Shark : fish
        {
            public override  void eat()
            { Console.WriteLine("Shark eats dead animal"); }
            public override  void swim()
            { Console.WriteLine("Sharks swim fastest than Dolphin"); }
            public override  void dive()
            { Console.WriteLine("Sharks Dive deeper than Dolphin"); }

            public void kill()
            { Console.WriteLine("Shark kills Others"); }

        }

        static void Main(string[] args)
        {
            Dolphin D = new Dolphin();
            D.dance();
            D.dive();
            D.eat();
            D.swim();
            Shark S = new Shark();
            S.kill();
            S.dive();
            S.eat();
            S.swim();
            Console.ReadLine();
        }
    }
Types of Polymorphism:
In general there are three types of polymorphism:
Overloading polymorphism:

Overloading polymorphism is where functions of the same name exist, with similar functionality, in classes which are completely independent of each other (these do not have to be children of the object class). For example, the complex class, the image class and the link class may each have the function “display”. This means that we do not need to worry about what type of object we are dealing with if all we want to do is display it on the screen.

Parametric polymorphism:

Parametric polymorphism is the ability to define several functions using the same name, but using different parameters (name and/or type). Parametric polymorphism automatically selects the correct method to be adopted according to the type of data passed in the parameter.

We can therefore define several addition() methods using the same name which calculates a sum of values.

  • The int addition(int,int) method would return the sum of two integers.
  • The float addition(float, float) would return the sum of two floats.
  • The char addition(char, char) would result as the sum of two characters as defined by the author.
  • etc.

signature is the name and type (static) given to the arguments of a function. Therefore it is a method’s signature which determines what is called on.

Inclusion polymorphism:

The ability to redefine a method in classes that are inherited from a base class is called specialization. One can therefore call on an object’s method without having to know its intrinsic type: this is inclusion polymorphism. This makes it possible to disregard the specialized class details of an object family, by masking them with a common interface (this being the basic class).

Imagine a game of chess, with the objects kingqueenbishopknightrook and pawn, each inheriting thepiece object.
The movement method could, using inclusion polymorphism, make the corresponding move according to the object class that is called on. This therefore allows the program to perform piece.movement without having to be concerned with each piece’s class.