Jul 24, 2014

Card , Deck , Hand Class In Poker Game Using Java

This Tutorial Is Focused On Initial Development Of  A Poker Game Using Java. Initially You Need Three Java Classes.Which Need To Store Card Details, Card Deck Details and Player Hand Details.

1. Card Class

This Class Contain The Information About A Card.Such As Card Index Which Vary From 1-52(Altogether Their Are 52 Playing Cards In The Pack), Card Suit Which Vary From 1-4(Their Are Four Suits In The Card Pack) and Card Value Which Vary From 2-14.

Also This Class Contain Few Methods To Get The Card Index , Card Suit and Card Value.This Class Should Have A Method To Create A New Card.In This Example I Have Used The Parameter Constructor To Create A New Card Objects. 


public class Card{

   private int CardIndex;//Vary From 0 to 51
   private int CardSuit;//Vary From 1 to 4 ( 1=Spades, 2=Hearts, 3=Clubs, 4=Diamonds)
   private int CardValue;//Vary From 2 to 14 ( 11=Jack, 12=Queen, 13=King, 14=Ace)

   public Card(){
   }

   public Card(int CardIndex,int CardSuit,int CardValue){
      this.CardIndex = CardIndex;
      this.CardSuit  = CardSuit;
      this.CardValue = CardValue;
   }

   public int getCardIndex(){
      return CardIndex;
   }

   public int getCardSuit(){
      return CardSuit;
   }

   public int getCardValue(){
      return CardValue;
   }

}

2.Deck Class

This Class Contains Set Of Card Objects.Initially This Class Contains All The 52 Card Objects.In A Game This Class Contains Current Card Objects Available In The Deck.To Store The Card Objects This Class Has An Array List Of Card Type.

Initially When The Game Is Starting, Deck Class Should Have All The 52 Card Objects. In This Class We Are Using Default Constructor To Create The Deck With 52 Card Objects.

Also This Class Should Have Few Methods To Shuffle The Deck , Get The Deck Size , Deal A Card From Deck and Add A Card To Deck.

import java.util.ArrayList;
import java.util.Collections;

public class Deck {

   private ArrayList<Card> deck = new ArrayList<Card>();//Cards Available In The Deck

       //Creating The Card Pack
   public Deck() { 
      int iIndex = 1;
      for (int iSuit = 1; iSuit < 5; iSuit++) { // 1=Spades, 2=Hearts, 3=Clubs, 4=Diamonds
         for(int iValue = 2; iValue < 15; iValue++ ) { //11=Jack, 12=Queen, 13=King, 14=Ace
            deck.add(new Card(iIndex,iSuit,iValue));
            iIndex++;
         }
      }
   }

   //Shuffles The Deck
   public void shuffle() {
      Collections.shuffle(deck);
   }

   //Get The Deck Size
   public int getDeckSize() {
      return deck.size();
   }

   //Deals(Return & Remove) A Card From The Top Of The Deck
   public Card dealCard() {
      return (Card)deck.remove(0);
   }

   public void addCard(Card c){
      deck.add(c);
   }

}

3.Hand Class

This Class Contains Set Of Card Objects Which Player Has.Initially This Class Has No Card Objects.In A Game This Class Contains Current Card Objects Available In The Player Hand.This Class Has An Array List Of Card Type To Store The Card Objects That Are Available In The Player Hand .

Also This Class Should Have Few Methods To Get The Hand Size ,  Add A Card To Hand, Get A Card From Hand and To Return The Hand Card List.

import java.util.ArrayList;

public class Hand {

   private ArrayList<Card> hand = new ArrayList<Card>();//Cards Available In The Hand

   public Hand(){
   }

   //Used To Add A Card To Hand
   public void addCard(Card c) {
      hand.add(c);
   }

   //Used To Remove A Card From Hand
   public Card removeCard(int iCardIndex) {
      Card x = new Card();
      for (Card c : hand) {
         if (c.getCardIndex() == iCardIndex) {
            x = c;
         }
      }
      hand.remove(x);
      return x;
   }

   //Used To Get The HandSize
   public int getHandSize() {
      return hand.size();
   }

   //Used To Get The CardList
   public ArrayList<Card> getHandCard() {
      return hand;
   }

}
   



Jul 18, 2014

Break and Continue Keyword In Java

Break Keyword

"Break" Key Word Is Used For Stop The Entire Loop. The Break Keyword Must Be Used Inside A Loop Or In A Switch Statement.The Break Keyword Will Stop The Execution Of The Innermost Loop & It Will Move Outside The Loop To Execute The Rest Of The Codes.   

Syntax Is Simply "break;" Inside The Loop.

Following Example Shows How To Use Break Keyword Inside A Loop.

class EasyCodeStuff {
   public static void main(String args[]){

      int [] MyNumbers = {5,3,8,2,6,1,11};

      for(int MyCount : MyNumbers){
         if(MyCount == 6){
            break;
         }
         System.out.print("My Count Is : ");
         System.out.println(MyCount); 
      }
   }
} 

OutPut

My Count Is : 5
My Count Is : 3
My Count Is : 8
My Count Is : 2  
  
In The Above Example Inside The For Loop We Have If Condition To Check "MyCount" Variable Value. If "MyCount" Variable Value Is 6, It Will Exit The Loop.



Continue Keyword

The "Continue" Keyword Can Be Used In Any Of The Loop Control Structures. It Causes The Loop To Immediately Jump To The Next Iteration Of The Loop.The Continue Statement Is Used To Jump To The Next Iteration Of A For Or While Loop, By Skipping The Rest Of The Code Lines In The Current Iteration.

The Different Between Continue and Break Statement Is If We Use Break Inside A For Loop It Will Exit From The For Loop.But If We Used A Continue Statement Inside For Loop It Will Go To The Update Statement Of The For Loop. 

If We Used  "continue;" Keyword Inside A While Loop Or Do While Loop It Move To The Boolean Expression Of The Loop.

Syntax Is Simply "continue;" Inside The Loop.
Following Example Shows How To Use Continue Keyword Inside A Loop.

class EasyCodeStuff {
   public static void main(String args[]){

      int [] MyNumbers = {5,3,8,2,6,1,11};

      for(int MyCount : MyNumbers){
         if(MyCount == 6){
            continue;
         }
         System.out.print("My Count Is : ");
         System.out.println(MyCount); 
      }
   }
} 

OutPut

My Count Is : 5
My Count Is : 3
My Count Is : 8
My Count Is : 2My Count Is : 1
My Count Is : 11



For Loop and Enhanced For Loop In Java

For Loop

For Loop Allows Code To Be Repeatedly Executed. A For Loop Is Classified As An Iteration Statement. A For Loop Can Be Use, When We Know How Many Times A Code Is To Be Repeated. For Loop Syntax Looks Like Below.

for(initialization; Boolean_expression; update)
{
   //Code We Want To Run Repeatedly 
}


Inside The Brackets Of The For
 Loop You Have To Define A Variable & Initialized It To A Value.Then Put A Semi Colon & Write The Boolean Condition To Run The For Loop. After That Put Another Semi Colon & Write The Update Statement For The For Condition Variable.

Following Example Shows How To Use While Loop In Java.

class EasyCodeStuff {
   public static void main(String args[]){

      for(int MyCount = 1; MyCount < 5; MyCount++){
         System.out.print("My Count Is : ");
         System.out.println(MyCount);
      }
   }
} 

OutPut

My Count Is : 1
My Count Is : 2
My Count Is : 3
My Count Is : 4 
 

Enhanced For Loop 

Enhanced For Loops Also Like For Loop. Enhanced For Loops Mainly Used With Arrays. Enhanced For Loop Syntax Looks Like Below.


for(declaration : expression)
{
   //Code We Want To Run Repeatedly  
}

In Enhanced For Loops Declared Variable Type Must Be Compatible With The Array That We Are Going To Use In The Enhanced For Loop.For Example If We Have A Integer Array We Have To Use The Declaration Variable As Integer Too. In The Expression You Have To Give The Array. 
Following Example Shows How To Use While Loop In Java.

class EasyCodeStuff {
   public static void main(String args[]){

               int [] MyNumbers = {1,2,3,4,5};
      for(int MyCount : MyNumbers){
         System.out.print("My Count Is : ");
         System.out.println(MyCount); 
      }
   }
} 

OutPut

My Count Is : 1
My Count Is : 2
My Count Is : 3
My Count Is : 4  
My Count Is : 5  

While and Do While Loop In Java

While Loop

A While Loop Is A Control Flow Statement That Allows Code To Be Executed Repeatedly Based On A Given Boolean Condition. The While Loop Can Be Thought Of As A Repeating If Statement.While Loop Syntax Looks Like Below.
 
while ( condition ) {
     //Codes You Want To Execute After Condition Is True

}

We Have To Write "while" In Lowercase.The Condition We Want To Write Goes Inside The Round Brackets. We Can Write The Code That We Want To Execute Inside The Curly Brackets.Make Sure You Have Change The Condition Varible Value In Side The Curly Brackets.Otherwise You Will End Up With A Infinite Loop.

Following Example Shows How To Use While Loop In Java.

class EasyCodeStuff {
   public static void main(String args[]){

      int MyCount = 1;
       
      while(MyCount < 5){
         System.out.print("My Count Is : ");
         System.out.println(MyCount);
         MyCount++; 
      }
   }
} 

OutPut

My Count Is : 1
My Count Is : 2
My Count Is : 3
My Count Is : 4  


Do While Loop

Do While Loop Is Related To While Loop.The Main Difference Between While Loop and Do While Is, In Do While Loop, Loop Will Run At Least Once, Even Without Condition Is False.In Do While Loop, While Is In Bottom and Do Is At The Top.We Have To Write The Codes That We Want To Execute Inside The Curly Brackets That Are With The Do Loop.Do While Loop Syntax Looks Like Below.


do{
     //Codes You Want To Execute After Condition Is True

}
 while ( condition ); 


Following Example Shows How To Use Do While Loop In Java.

class EasyCodeStuff {
   public static void main(String args[]){

      int MyCount = 1;
      
      do{
         System.out.print("My Count Is : ");
         System.out.println(MyCount);
         MyCount++; 
      }
      while(MyCount < 5)
   }
} 

OutPut

My Count Is : 1
My Count Is : 2
My Count Is : 3
My Count Is : 4


Jul 17, 2014

Switch Statement in Java

We Can Use Switch Statement for Do Many Different Things Depending On One Variable. For Example You May Have Variable Called Month To Store The Month From 1 to 12. Depending On The Month You can Do Multiple Things.

Following Example Demonstrate How To Use Switch Statement.

class EasyCodeStuff {
   public static void main(String args[]){
      
      int MyMonth = 2;

      switch(MyMonth){

      case 1:
         System.out.print("January");
         break;
      case 2:
         System.out.print("February");
         break;
      case 3:
         System.out.print("March");
         break;
               case 4:
         System.out.print("April");
         break;
      case 5:
         System.out.print("May");
         break;
      case 6:
         System.out.print("June");
         break;
      case 7:
         System.out.print("July");
         break;
      case 8:
         System.out.print("August");
         break;
      case 9:
         System.out.print("September");
         break;
      case 10:
         System.out.print("October");
         break;
      case 11:
         System.out.print("November");
         break;
      case 12:
         System.out.print("December");
         break;
      default:
         System.out.print("Invalid Month");
         break;
      }
   }
}

Above Example Will Give The Out Put As "February".Because The Value Of The "MyMonth" Variable Is 2.

"case 1" Mean , Value Of "MyMonth" Variable Is 1. After The Colon(:) You Can Write Your Codes, What You Want To Do After "MyMonth" Variable Have Value 1.But Make Sure You Have Put A "break;" Statement In Last Line.

In The Following Example You Can See Their Statement Called "default;" After The Last Case Statement. This Section Execute If Non Of The Case Are Not True. 


Java Logical Operators ( And && Or || )

If You Have To Check Two Variable Values To Given  Two Values, You May Some Time Write Many If Else Statements To Check For Many Variable Values.Without Writing Many If Else Statement You Can Check Many Values That Mean Many If Else Statements In A Single Statement By Using AND or OR Logical Operators.  

You Can Use AND(&&) Logical Operator If You Want To Do Something After Every Operator Gets True.

You Can Use OR(||) Logical Operator If You Want To Do Something After Any Of The Operator Gets True.

Following Example Demonstrate How AND , OR Operators Work.

class EasyCodeStuff {
   public static void main(String args[]){
     
      int No1 = 10;
      int No2 = 20;

      //AND Logical Opearator

      if (No1 == 10 && No1 == 20){
         System.out.println("No1 is Equal To 10 AND No2 is Equal To 20");
      }else{
         System.out.print("No1 is Not Equal To 10 OR No2 is Not Equal To 20");
      }
      
      //OR Logical Operator
      if (No1 == 10 || No1 == 20){
         System.out.println("No1 is Equal To 10 OR No2 is Equal To 20");
      }else{
         System.out.print("No1 is Not Equal To 10 AND No2 is Not Equal To 20");
      }
   }
}    



If Else In Java With ( == , != , < , > , <= , >= Opearators)

In This Example I'm Trying To Explain How If Else Statements Work In Java.
You can Check many Logical Operators With If Else Statements.
There Are Mainly 6 Types.

1. Equal                                       ==
2. Not Equal                                != 
3. Less Than                                <
4. Greater Than                          > 
5. Less Than Or Equal               <=
6. Greater Than Or Equal         >=

Following Example Shows Use Of Operators With if Else Statements

class EasyCodeStuff {
   public static void main(String args[]){
     
      int No1 = 10;

      //Equal Opeartion
      if (No1 == 10){
         System.out.println("No1 is Equal To 10");
      }else{
         System.out.print("No1 is Not Equal To 10");
      }
      
      //Not Equal Opeartion
      if (No1 != 10){
         System.out.println("No1 is Not Equal To 10");
      }else{
         System.out.println("No1 is Equal To 10");
      }
       
      //Less Than Opeartion
      if (No1 < 10){
         System.out.println("No1 is Less Than 10");
      }else{
         System.out.println("No1 is Not Less Than 10");
      }

      //Greater Than Opeartion
      if (No1 > 10){
         System.out.println("No1 is Greater Than 10");
      }else{
         System.out.println("No1 is Not Greater Than 10");
      }

      //Less Than Or Equal Opeartion
      if (No1 <= 10){
         System.out.println("No1 is Less Than Or Equal To 10");
      }else{
         System.out.println("No1 is Not Less Than 10");
      }

      //Greater Than Or Equal Opeartion
      if (No1 >= 10){
         System.out.println("No1 is Greater Than Or Equal To 10");
      }else{
         System.out.println("No1 is Not Greater Than 10");
      }
   }
}   



Java Increments, Decrements and Operators

In This Example I'm Trying To Explain Pre-Increment , Post-Increment , Operators and How They Behave.

Following "JavaOpeartors.java" File Shows The Outputs.

class JavaOpeartors {
   public static void main(String args[]){
     
      int No1 = 10;
      System.out.print("Initial No1 : ");
      System.out.println(No1);
       
      ++No1;
      System.out.print("Pre Increment ++No1 : ");
      System.out.println(No1);

      System.out.print("Post Increment No1++ : ");

      System.out.println(No1++);

               System.out.print("After Post Increment No1 : ");

      System.out.println(No1);

      No1 = No1 + 3;

      System.out.print("No1 + 3 : ");
      System.out.println(No1);

      No1 += 10;

      System.out.print("No1 += 10 : ");
      System.out.println(No1);

      No1 -= 5;

      System.out.print("No1 -= 5 : ");
      System.out.println(No1);

      No1 *= 2;

      System.out.print("No1 *= 2 : ");
      System.out.println(No1);
   }
}

OutPut
Initial No1 : 10
Pre Increment ++No1 : 11
Post Increment No1++ : 11 
After Post Increment No1 : 12
No1 + 3 : 15
No1 += 10 : 25 
No1 -= 5 : 20 
No1 *= 2 : 40




Jul 6, 2014

Java Simple Command Line Calculator With Math Operators

In This Example We Are Trying To Create A Simple Calculator That Works With User Inputs In Command Line.Open Text File & Save As "CmdCalculator".Create The Class and Main Method Inside It.Import The Scanner Class.

Create A Scanner Object To Get The User Inputs.
Create Two Double Variables To Save Two Inputs.
Create Another Variable To Save The Answer.

import java.util.Scanner;

class CmdCalculator {

   public static void main(String args[]){
      Scanner MyScanner = new Scanner(System.in);
      double No1,No2,Answer;
     
   }
}

Now We Need Is Get The User Input and Do The Calculation and Print The Outputs.In The Command Prompt Use Scanner.nextDouble Method To Get The User Inputs as double Variable Rather Than Text..Complete The Coding.

import java.util.Scanner;

class CmdCalculator {

   public static void main(String args[]){
      Scanner MyScanner = new Scanner(System.in);
      double No1,No2,Answer;
     
      System.out.print("Enter The No1 : ");
      No1 = MyScanner.nextDouble();

      System.out.print("Enter The No2 : ");

      No2 = MyScanner.nextDouble();

      Answer = No1 + No2;

      System.out.println("Addition Is : ");
      System.out.print(Answer);

      Answer = No1 - No2;

      System.out.println("Subtraction Is : ");
      System.out.print(Answer);

      Answer = No1 / No2;

      System.out.println("Division Is : ");
      System.out.print(Answer);

      Answer = No1 * No2;

      System.out.println("Multiplication Is : ");
      System.out.print(Answer);
   }
}

Now Run The Program.It Will First Ask Two User Input As No1 and No2.You Will Get Following Outputs At The End.

Enter The No1 : 12
Enter The No2 : 4
Addition Is : 16
Subtraction Is : 8 
Division Is : 3 
Multiplication Is : 48


JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...