Search

Saturday, April 27, 2013

Prime Number Verification : Java program code along with first 100 prime numbers

Prime Number is one of the fundamental concepts of mathematics .Prime number can be defined as :
The number which is divisible only by itself and 1 . That is other than two numbers (itself and 1) no other number can give remainder 0 .

 But still when comes to coding prime number coding in Graphical user interface representation , its not that easy .


for e.g

23 , divisible by 1 and 23 only , thus its a prime number
20, divisible by 1,2,4,5,10.20 , thus it is not a prime number

Read Also :  Armstrong Number Java swing code with Example


Best efficient method to compute the prime number is to square root the number .  That is , if the number is prime then there will be no multiple of the number between 1 and the square root of the number .
Let us understand above statement with an example

Suppose we want to check whether 7687  is a prime number or not
So applying above method ,
Observing the number , the square root should lie between 80 (square of 80 is 6400) and 90 (square of 90 is 8100) . So we conclude that the square root of 7687 is smaller than 90 .
Now we just need to divide the given number 7687 from 2 to 90 , if any number is a multiple of the number
,than , the number is not prime , else the number is prime .
After you do calculation for the above given number 7687 , you will find that the number is prime.

Pseudo code :

1. Find out the square root of  the given number
2. Now consider square root value as the upper limit or the value upto which we need to decide whether the
    given number is prime or not .
3. Use hit and trial method , and divide the given number (for eg. 7687)  from 2 to the square root of
    value(90)  .
4. If there is any value which gives 0 remainder or a multiple of given number then the number is not prime
    and exit .    
5. If value reaches equal to the square root value i.e upper limit and there is no value whose remainder is 0
    or multiple or factor of the given value ,then number is  prime and exit .

List of Prime numbers upto 100 :

2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97


Read Also  :   Fibonacci series Recursion  program in java : Code with example 



prime number gui java code output image




Demo :

















so , when you run the program in the command prompt , then a gui pops up . Enter the number which you need to check , whether its a prime number or not , and then click ok . The text field with label Prime will automatically show the result by rendering true or false in the text field.

 
Code 



import java.awt.*;
import java.awt.event.*;


public class prime implements ActionListener,KeyListener
{
Frame f;
Label one,two;
TextField three,four;
Button five;
int i,t,j;
String s1="";
public prime()
{
f=new Frame("Prime Number");
one=new Label("Enter the number");
two=new Label("Prime ");
three=new TextField(5);
four=new TextField(5);
five=new Button("OK");
f.setSize(400,400);
f.setVisible(true);
f.add(one);
f.add(two);
f.add(three);
f.add(four);
f.add(five);
f.setLayout(null);
one.setBounds(20,20,140,40);
two.setBounds(20,80,140,40);
three.setBounds(180,30,140,40);
four.setBounds(180,85,140,40);
five.setBounds(240,240,40,40);
three.addKeyListener(this);
five.addActionListener(this);
}
public void keyPressed(KeyEvent k)
{
System.out.print("");
}
public void keyTyped(KeyEvent k)
{
s1+= k.getKeyChar();
}
public void keyReleased(KeyEvent k)
{
System.out.print("");
}
public void actionPerformed(ActionEvent ae)
{
t=Integer.parseInt(s1);
for(i=1;i<t;i++)
{
if((i*i)>t)
break;
}
for(j=2;j<i;j++)
{
if((t%j)==0)
{
four.setText("false");
break;
}
if(j==(i-1))
four.setText("true");
}
}

public static void main(String s[])
{
new prime();
}
}

Friday, April 26, 2013

Tic Tac Toe game : Simple Gui

Tic Tac Toe is a fun game to play . This game has many variants , but the 3x3 variant is most popular .
This game is played between two players . The program here  is written for applet .The game has simple 3 rows and 3 columns , so make it a 3x3 grid

Rules of the Tic Tac Toe Game

The player whose turn is first has the choice to mark  X or  O   to one of the nine squares , Here we choose X for first player .
Second player then mark its symbol  O in the remaining 8 squares
The thumb of the rule is to make the three successive squaare grids of the same sign , it does not matter whether they are horizontal , vertical or diagonal , they just need to be three consecutive squares filled with same symbol X or O.

First player who is able to make three consecutive squares with same symbol  WINS .

You can find the Best gui Tic tac toe  game java code along with clear and  exit options

http://javahungry.blogspot.com/2013/05/tic-tac-toe-game-best-graphics-perfect.html

Console based Tic tac toe java code is here :This is a console based tic tac toe game

http://javahungry.blogspot.in/2013/06/tic-tac-toe-version-3-console-based.html



Pseudo code for Tic Tac Toe Game :

1. Board is drawn first
2. Player X and Player O click the mouse on the empty grid of the board
3. Now Check at each click on the board
            3.1 Is there any three vertical grids with same Mark (i.e X or O)
                   if true
                             Player with that mark Wins and Stop
           3.2  Is there any three horizontal grids with same Mark (i.e X or O)
                   if true
                             Player with that mark Wins and Stop
          3.3   All grids filled  
                  if true
                             Declare result is stalemate
4.   Repeat step 2      



Demo :

tic tac toe game java code output image















Tic Tac Toe  Game Java Code 




import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;


public class TicTacToe extends Applet implements MouseListener
{
Frame f;
int flag=2,n,m,i=0;
char ch[]=new char[9];



public TicTacToe()
{
f=new Frame("Tic Tac Toe");
f.setLayout(null);
f.setVisible(true);
f.setSize(600,600);
f.addMouseListener(this);
for(i=0;i<9;i++)
ch[i]='B';
}
public void mouseClicked(MouseEvent e)
{
Graphics g=f.getGraphics();
g.drawLine(200,0,200,600);
g.drawLine(400,0,400,600);
g.drawLine(0,200,600,200);
g.drawLine(0,400,600,400);
flag--;
int x=e.getX();
int y=e.getY();
if(flag==1)
{
if(x<200&&y<200){m=0;n=0;ch[0]='R';}
if((x>200&&x<400)&&(y<200)){m=200;n=0;ch[1]='R';}
if((x>400&&x<600)&&(y<200)){m=400;n=0;ch[2]='R';}
if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='R';}
if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='R';}
if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='R';}
if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='R';}
if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='R';}
if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='R';}
g.setColor(Color.red);
g.drawLine(m,n,m+199,n+199);
g.drawLine(m+199,n,m,n+199);
}

if(flag==0)
{

if(x<200&&y<200){m=0;n=20;ch[0]='P';}
if((x>200&&x<400)&&(y<200)){m=200;n=20;ch[1]='P';}
if((x>400&&x<600)&&(y<200)){m=400;n=20;ch[2]='P';}
if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='P';}
if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='P';}
if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='P';}
if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='P';}
if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='P';}
if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='P';}
g.setColor(Color.green);
g.drawOval(m+10,n+10,169,169);
// g.drawLine(m,n,m+189,n+189);
// g.drawLine(m+199,n,m,n+199);
flag=flag+2;
}

for(i=0;i<9;i++) // for draw
{
if(ch[i]!='B')
{
if(i==8)
draw();
}
else
break;
}

for(i=0;i<3;i++) //for vertical
{
// System.out.print(ch[i]);
if(ch[i]!='B')
{
if((ch[i+3]==ch[i])&&(ch[i+6]==ch[i]))
win();
}
}
for(i=0;i<7;i++) //for horizontal
{

if(ch[i]!='B')
{
if((ch[i]==ch[i+1])&&(ch[i]==ch[i+2]))
win();
i=i+2;
}
else
i=i+2;
}

if(ch[4]!='B') //for diagonals
{
if(((ch[0]==ch[4])&&(ch[4]==ch[8]))||((ch[2]==ch[4])&&(ch[4]==ch[6])))
win();
}


}
public Frame win()
{

Frame m=new Frame("Result");
Label l=new Label("you win");
m.setLayout(null);
m.add(l);
l.setBounds(20,20,60,60);
m.setVisible(true);
m.setSize(100,100);
return m;
}

public Frame draw()
{
Frame m=new Frame("Result");
Label l1=new Label("Stalemate");
m.setLayout(null);
m.add(l1);
l1.setBounds(20,20,60,60);
m.setVisible(true);
m.setSize(100,100);
return m;

}
public void mouseReleased(MouseEvent e)
{
System.out.print("");
}




public void mouseEntered(MouseEvent e)

{
System.out.print("");
}
public void mouseExited(MouseEvent e)
{
System.out.print("");
}
public void mousePressed(MouseEvent e)
{
System.out.print("");
}

public static void main(String args [])
{
new TicTacToe();
}
}

Thursday, April 25, 2013

Quadratic Probing and Linear Probing : Java program source code

Quadratic Probing and Linear Probing are the techniques to avoid collision in the hash tables . Suppose the hash value generated  is already occupied in the hash table , then quadratic probing or linear probing helps to find a lace in the hash table .
Quadratic Probing takes arbitrary quadratic polynomial and add it to the original hash index . The arbitrary quadratic polynomial is added till the hash value generated is not occupied in the hash table .

It works on the following formula

For a original hash index H , H+1*1 , H+2*2 ,H+3*3 ,H+4*4......


Read Also :   How hash map works internally in java    


Linear Probing is somewhat easy  , it search sequentially in the hash table for a given hash value . It does not involve any arbitrary polynomial value .

Linear probing including two values one is starting value and other is interval value . If the hash value is occupied at starting point then the next hash value is calculated by adding the interval value to the starting value . If , it is also occupied in the hash table , then again we add interval value to the hash value generated .
Then again we keep looking for the hash value for which the hash table has no entry .

It works on the following formula

For a starting vaue H and interval value 1   :   H, H+1,H+2 , H+3,H+4.....

Pseudo code is given below :

Quadratic Probing algorithm  to insert key in the hash table

1. Retrieve key k
2. Compute hash function h[k] = k % size of the table
3. If hash table is empty at the computed hash value place
          then insert key at h[k]
    else
     we need to find another empty place in the hash table to insert the key in the hash table
        3.1    Use quadratic probing to compute the hash value of the key again  
        3.2    If   hash table place is empty then insert key at h[k] and exit
                 else
                 Repeat 3.1 step again  


Read Also :   Internal working of Hashset or How set ensures uniqueness in java


Linear Probing algorithm to insert key in the hash table

1. Retrieve key k
2. Compute hash function h[k]= k %size of the table
3. If hash table is empty at the computed hash value place
          then insert key at h[k]
    else
        we need to find another empty place in the hash table to insert the key in the table
         3.1    Use linear probing to compute the hash value of the key again , in linear probing we generally
                  keep adding some constant value to the computed hash value .
         3.2    If hash table place is empty then insert key at  h[k] and exit
                  else
                  Repeat  3.1 step again .


Read Also :   Difference between Iterator and Enumerator

Demo :

quadratic probing and linear probing java program code output image














Code :



/**
* A class that implements the ADT dictionary by using hashing
* and linear probing to resolve collisions.
* The dictionary is not sorted and has distinct search keys.
* Notes: Uses probe for add, but locate for remove and getValue.
* Uses linear probing, but includes code for quadratic probing.
* Has a display method for illustration and testing.
*
* @author Subham
* @version 2.0
*/
public class HashedDictionary<K, V>
{
public TableEntry<K, V>[] hashTable; // dictionary entries
private int numberOfEntries;
private int locationsUsed; // number of table locations not null
private static final int DEFAULT_SIZE = 101; // must be prime
private static final double MAX_LOAD_FACTOR = 0.5; // fraction of hash table that can be filled

public HashedDictionary()
{
this(DEFAULT_SIZE); // call next constructor

} // end default constructor

public HashedDictionary(int tableSize)
{
// ensure that table is prime size at least as big as user wants;
// if tableSize is prime, do not change it
int primeSize = getNextPrime(tableSize);

hashTable = new TableEntry[primeSize];
numberOfEntries = 0;
locationsUsed = 0;
} // end constructor

// -------------------------
// We've added this method to display the hash table for illustration and testing
// -------------------------
public void display()
{
for (int index = 0; index < hashTable.length; index++)
{
if (hashTable[index] == null)
System.out.println("null ");
else if (hashTable[index].isRemoved())
System.out.println("notIn ");
else
System.out.println(hashTable[index].getKey() + " " + hashTable[index].getValue());
} // end for
System.out.println();
} // end display
// -------------------------

// 20.14
public V add(K key, V value)
{
V oldValue;
if (isHashTableTooFull())
rehash();
int index = getHashIndex(key);
index = quadraticProbe(index, key);
assert (index >= 0) && (index < hashTable.length);
if ( (hashTable[index] == null) || hashTable[index].isRemoved())
{ // key not found, so insert new entry
hashTable[index] = new TableEntry<K, V>(key, value);
numberOfEntries++;
locationsUsed++;
oldValue = null;
}
else
{ // key found; get old value for return and then replace it
oldValue = hashTable[index].getValue();
hashTable[index].setValue(value);
} // end if

return oldValue;
} // end add


// 20.12
public V remove(K key)
{
V removedValue = null;

int index = getHashIndex(key);
index = locate(index, key);

if (index != -1)
{
// key found; flag entry as removed and return its value
removedValue = hashTable[index].getValue();
hashTable[index].setToRemoved();
numberOfEntries--;
} // end if
// else not found; result is null

return removedValue;
} // end remove

// 20.11
public V getValue(K key)
{
V result = null;

int index = getHashIndex(key);
index = locate(index, key);

if (index != -1)
result = hashTable[index].getValue(); // key found; get value

// else not found; result is null

return result;
} // end getValue

// 20.15
private int quadraticProbe(int index, K key)
{
boolean found = false;
int removedStateIndex = -1; // index of first location in
// removed state
int i=0;
while ( !found && (hashTable[index] != null) && i< hashTable.length)
{
if (hashTable[index].isIn())
{
if (key.equals(hashTable[index].getKey()))
found = true; // key found
else // follow probe sequence
{
index = (index + i*i) % hashTable.length; // linear probing
i++;
}
}
else // skip entries that were removed
{
// save index of first location in removed state
if (removedStateIndex == -1)
removedStateIndex = index;

index = (index + i*i) % hashTable.length; // linear probing
i++ ;
} // end if
} // end while
// Assertion: either key or null is found at hashTable[index]

if (found || (removedStateIndex == -1) )
return index; // index of either key or null
else
return removedStateIndex; // index of an available location
} // end probe

// 20.13
public int locate(int index, K key)
{
boolean found = false;
int i=0;
while ( !found && (hashTable[index] != null) && i<hashTable.length)
{
if ( hashTable[index].isIn() && key.equals(hashTable[index].getKey()) )
found = true; // key found
else // follow probe sequence
{
index = (index + i*i) % hashTable.length; // linear probing
i++;
}
} // end while
// Assertion: either key or null is found at hashTable[index]

int result = -1;
if (found)
result = index;

return result;
} // end locate

public boolean contains(K key)
{
return getValue(key) != null;
} // end contains


public boolean isEmpty()
{
return numberOfEntries == 0;
} // end isEmpty


public boolean isFull()
{
return false;
} // end isFull


public int getSize()
{
return numberOfEntries;
} // end getSize


public final void clear()
{
for (int index = 0; index < hashTable.length; index++)
hashTable[index] = null;

numberOfEntries = 0;
locationsUsed = 0;
} // end clear

public int getHashIndex(K key)
{

int hashIndex = key.hashCode() % hashTable.length;

if (hashIndex < 0)
{
hashIndex = hashIndex + hashTable.length;
} // end if

return hashIndex;
} // end getHashIndex

/** Task: Increases the size of the hash table to a prime > twice its old size. */
private void rehash()
{
TableEntry<K, V>[] oldTable = hashTable;
int oldSize = hashTable.length;
int newSize = getNextPrime(oldSize + oldSize);
hashTable = new TableEntry[newSize]; // increase size of array
numberOfEntries = 0; // reset number of dictionary entries, since
// it will be incremented by add during rehash
locationsUsed = 0;

// rehash dictionary entries from old array to the new and bigger
// array; skip both null locations and removed entries
for (int index = 0; index < oldSize; index++)
{
if ( (oldTable[index] != null) && oldTable[index].isIn() )
add(oldTable[index].getKey(), oldTable[index].getValue());
} // end for
} // end rehash

/** @return true if lambda > MAX_LOAD_FACTOR for hash table;
* otherwise returns false. */
private boolean isHashTableTooFull()
{
return locationsUsed > MAX_LOAD_FACTOR * hashTable.length;
} // end isHashTableTooFull

private int getNextPrime(int integer)
{
// if even, add 1 to make odd
if (integer % 2 == 0)
{
integer++;
} // end if

// test odd integers
while(!isPrime(integer))
{
integer = integer + 2;
} // end while

return integer;
} // end getNextPrime

private boolean isPrime(int integer)
{
boolean result;
boolean done = false;

// 1 and even numbers are not prime
if ( (integer == 1) || (integer % 2 == 0) )
{
result = false;
}

// 2 and 3 are prime
else if ( (integer == 2) || (integer == 3) )
{
result = true;
}

else // integer is odd and >= 5
{
assert (integer % 2 != 0) && (integer >= 5);

// a prime is odd and not divisible by every odd integer up to its square root
result = true; // assume prime
for (int divisor = 3; !done && (divisor * divisor <= integer); divisor = divisor + 2)
{
if (integer % divisor == 0)
{
result = false; // divisible; not prime
done = true;
} // end if
} // end for
} // end if

return result;
} // end isPrime


// 20.09
class TableEntry<S, T>
{
private S key;
private T value;
private boolean inTable; // true if entry is in table

private TableEntry(S searchKey, T dataValue)
{
key = searchKey;
value = dataValue;
inTable = true;
} // end constructor

public S getKey()
{
return key;
} // end getKey

private T getValue()
{
return value;
} // end getValue

private void setValue(T newValue)
{
value = newValue;
} // end setValue

private boolean isIn()
{
return inTable;
} // end isIn

private boolean isRemoved() // opposite of isIn
{
return !inTable;
} // end isRemoved

// state = true means entry in use; false means entry not in use, ie deleted
private void setToRemoved()
{
key = null;
value = null;
inTable = false;
} // end setToRemoved

private void setToIn() // not used
{
inTable = true;
} // end setToIn
} // end TableEntry
} // end HashedDictionary




HashedDictionaryTest.java



import java.io.*;
import java.util.*;
public class HashedDictionaryTest
{
String method;
String key;
String value;
public HashedDictionaryTest(String method, String key, String value){
this.method = method;
this.key = key;
this.value = value;
}
public static void main(String[] args)
{
// declaring nameList as HashedDictionary instead of DictionaryInterface enables display method in HashedDictionary
HashedDictionary<Name, String> nameList = new HashedDictionary<Name, String>(11);
String line[]= new String[100];
String subline[]=new String[100];
String[] tokens=new String[100];
String subtoken[] =new String[100];
int i=0,j=0,k=0;
try{
Scanner scanner = new Scanner(new File("userfile.txt")).useDelimiter(" ");
while (scanner.hasNext()) {
line[i] = scanner.nextLine();
Scanner scanner1 = new Scanner(line[i]).useDelimiter(" ");
while (scanner1.hasNext()) {
subline[j] = scanner1.nextLine();
j++;
} i++;
}
String strLine;
while ((strLine = subline[k]) != null) {
tokens = strLine.split(" ");
for(int m=0; m<tokens.length;m++){
subtoken[m]=tokens[m];
}
if(tokens.length==1)
{
subtoken[1]=null;
subtoken[2]=null;
}
else if (tokens.length==2)
subtoken[2]=null;
HashedDictionaryTest record = new HashedDictionaryTest(subtoken[0],subtoken[1],subtoken[2]);//process record , etc
chooseMethod(subtoken[0],subtoken[1],subtoken[2],nameList);
k++;
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}


public static void chooseMethod(String s , String s1 , String s2 ,HashedDictionary<Name, String> nameList )
{
if (s.equals("add"))
nameList.add(new Name(s1),s2);
else if (s.equals("print"))
nameList.display();
else if(s.equals("locate"))
nameList.locate( nameList.getHashIndex(new Name(s1)),new Name(s1));
else if (s.equals("remove"))
{
int index =nameList.getHashIndex(new Name(s1));
index=nameList.hashTable.length-index;
Name m= (Name)nameList.hashTable[index-1].getKey();
nameList.remove(m);
}
}
}

// end HashedDictionaryTest




Name.java



/** This class is like the one in the folder Comparable,
* but adds a constructor and hashCode for testing purposes. */
public class Name
{
private String first; // first name
private String last; // last name

public Name()
{
this("","");
} // end default constructor

public Name(String firstName) // for testing ***************
{
this(firstName, "");
} // end constructor

public Name(String firstName, String lastName)
{
first = firstName;
last = lastName;
} // end constructor

@Override
public int hashCode() // for testing ***************
{
// this hash code causes collisions
int h = 0;

for (int i = 0; i < first.length(); i++)
{
h = h + first.charAt(i);
}
return h;
// a reasonable hash code follows:
// return first.hashCode() + last.hashCode();
} // end hashCode

public void setName(String firstName, String lastName)
{
setFirst(firstName);
setLast(lastName);
} // end setName

public String getName()
{
return toString();
} // end getName

public void setFirst(String firstName)
{
first = firstName;
} // end setFirst

public String getFirst()
{
return first;
} // end getFirst

public void setLast(String lastName)
{
last = lastName;
} // end setLast

public String getLast()
{
return last;
} // end getLast

public void giveLastNameTo(Name aName)
{
aName.setLast(last);
} // end giveLastNameTo

@Override
public String toString()
{
return first + " " + last;
} // end toString


@Override
public boolean equals(Object other)
{
boolean result;

if ((other == null) || (getClass() != other.getClass()))
result = false;
else
{
Name otherName = (Name)other;
result = first.equals(otherName.first) &&
last.equals(otherName.last);
} // end if

return result;
} // end equals

} // end Name




userFile.txt


add 555-1234 Tabatha
add 555-1235 Toni
print
locate Toni
remove Tabatha
add 555-1236 Tobbie
print
locate Tabatha