Java

OOP GUIDE / WORK

 

HAND CLASS SOLUTIONS

 

 

TASK – PART 1 – SETUP – CARD CLASS

 

No solution required.


 

TASK – PART 2 – HAND CLASS

 

I have added some functionality to the Card class.  Here is my solution for the Card class and the Hand class as well as the Tester class:

public class Card

{

     public int value;

     public String suit;

 

     public Card(int tvalue, String tsuit)

     {

           value = tvalue;

           suit = tsuit;

     }

 

     public Card()

     {

           value = (int)(Math.random() * 13 + 1);

           double rn = Math.random();

           if (rn <= 0.25)

           {

                suit = "Hearts";

           }

           else if (rn <= 0.50)

           {

                suit = "Diamonds";

           }

           else if (rn <= 0.75)

           {

                suit = "Spades";

           }

           else

           {

                suit = "Clubs";

           }

     }

 

     public Card(String tsuit)

     {

         suit = tsuit;

         value = (int)(Math.random() * 13 + 1);

     }

 

     public String toString()

     {

         return value + " of " + suit;

     }

    

     public boolean isRed()

     {

     if (suit.equals("Hearts") || suit.equals("Diamonds"))

     {

            return true;

     }

     else

      {

            return false;

     }

     }

    

     public boolean isBlack()

     {

     if (suit.equals("Hearts") || suit.equals("Diamonds"))

     {

            return false;

     }

     else

     {

            return true;

     }

   }    

}

 

public class Hand

{

     //INSTANCE VARIABLES (OBJECTS)

     public Card c1;

     public Card c2;

     public Card c3;

     public Card c4;

     public Card c5;

    

     //CONSTRUCTORS

     public Hand(Card tc1, Card tc2, Card tc3, Card tc4, Card tc5)

     {

           c1 = tc1;

           c2 = tc2;

           c3 = tc3;

           c4 = tc4;

           c5 = tc5;

     }

    

     public Hand()

     {

           c1 = new Card();

           c2 = new Card();

           c3 = new Card();

           c4 = new Card();

           c5 = new Card();

     }

    

     //INSTANCE METHODS

     public Card getC1()

     {

           return c1;

     }

     public Card getC2()

     {

           return c2;

     }

     public Card getC3()

     {

           return c3;

     }

    

     public Card getC4()

     {

           return c4;

     }

    

     public Card getC5()

     {

           return c5;

     }

    

     public boolean isAllRed()

     {

         if(c1.isRed() && c2.isRed() && c3.isRed() && c4.isRed() && c5.isRed())    

         {

         return true;

         }

         else

         {

         return false;

         }

     }

    

     public boolean isAllBlack()

     {

         if(c1.isBlack() && c2.isBlack() && c3.isBlack() && c4.isBlack() && c5.isBlack())

         {

         return true;

         }

         else

         {

         return false;

         }

     }

    

     public boolean containsPair()

     {

           if (c1.value == c2.value || c1.value == c3.value || c1.value == c4.value || c1.value == c5.value ||

                c2.value == c3.value || c2.value == c4.value || c2.value == c5.value ||

                c3.value == c4.value || c3.value == c5.value ||

                c4.value == c5.value)

           {

                return true;

           }

           else

           {

                return false;

           }

     }

    

     public String toString()

     {

           return c1 + ", " + c2 + ", " + c3 + ", " + c4 + ", " + c5;

           //above is calling the toString for each card object

     }

}

 

public class Question02

{

     public static void main(String[] args)

     {

           Hand h1 = new Hand();

           System.out.println(h1);  //calling toString on h1

           System.out.println("Pair? " + h1.containsPair());

          

           Card cardA = new Card("Clubs");

           Card cardB = new Card("Clubs");

           Card cardC = new Card("Spades");

           Card cardD = new Card("Spades");

           Card cardE = new Card("Clubs");

           Hand h2 = new Hand(cardA, cardB, cardC, cardD, cardE);

           System.out.println(h2);

           System.out.println("All Red? " + h2.isAllRed());

           System.out.println("All Black? " + h2.isAllBlack());

     }

}