Java

OOP GUIDE / WORK

 

CARD CLASS 2 SOLUTIONS

 

 

TASK – PART 1 – CARD CLASS SETUP


No solution required.

 

 

TASK – PART 2 – CARD CLASS SETUP

 

Here is my solution:

 

 

public class Card

{

     public int value;

     public String suit;

 

     public Card (int v, String s)

     {

           value = v;

           suit = s;

     }

 

     public Card()

     {

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

           int rn = (int)(Math.random() * 4);

           if (rn == 0)

                suit = "hearts";

           else if (rn == 1)

                suit = "diamonds";

           else if (rn == 2)

                suit = "spades";

           else

                suit = "clubs";

     }

    

     public boolean isHeart()

     {

     if (suit.equals("hearts"))

            return true;

     else

            return false;

     }

    

     public boolean isDiamond()

     {

     if (suit.equals("diamonds"))

            return true;

     else

            return false;

     }

    

     public boolean isSpade()

     {

     if (suit.equals("spades"))

            return true;

     else

            return false;

     }

    

     public boolean isClub()

     {

     if (suit.equals("clubs"))

            return true;

     else

            return false;

     }

    

     public boolean isFaceCard()

     {

     if (value == 11 || value == 12 || value == 13)

            return true;

     else

            return false;

     }

    

     public String toString()

     {

     if (value == 1)

            return "A of " + suit;

     else if (value == 11)

            return "J of " + suit;

     else if (value == 12)

            return "Q of " + suit;

     else if (value == 13)

            return "K of " + suit;

     else

            return value + " of " + suit;

     }

}

 

Note: My solution’s toString() method is slightly different from what the question asks for.  Mine outputs the entire suit’s name while the question asks for the first letter.

 

To conform with the question’s requirement, you can simply take the first letter of the suit by using:

            return "A of " + suit.substring(0, 1);

 

 

TASK – PART 3 – TESTING THE CARD CLASS


Here is my solution:

 

public class CardTester

{

     public static void main(String[] args)

     {

           Card c1 = new Card();

          

           if (c1.isHeart())

                System.out.println("It's a heart!");

           else if (c1.isDiamond())

                System.out.println("It's a diamond!");

           else if (c1.isClub())

                System.out.println("It's a club!");

           else if (c1.isSpade())

                System.out.println("It's a spade!");

          

           if (c1.isFaceCard())

                System.out.println("It's a face card!");

           else

                System.out.println("It's not a face card!");

               

           System.out.println(c1);

     }

}