Java

OOP GUIDE / WORK

 

CARD CLASS 3 SOLUTIONS

 

 

TASK – PART 1 – CARD CLASS SETUP

No solution required.

 

 

TASK – PART 2 – RED OR BLACK CARD (CARD PROGRAM 01)

 

Here is my solution:

 

public class CardProgram01

{

     public static void main(String[] args)

     {

           Card c1 = new Card();

           System.out.println(c1.toString());

          

           if (c1.isHeart() || c1.isDiamond())

           {

                System.out.println("Red card");

           }

           else

           {

                System.out.println("Black card");

           }

     }

}

 

 

TASK – PART 3 – CARD HAND & TOTAL (CARD PROGRAM 02)

 

Here is my solution:

 

public class CardProgram02

{

     public static void main(String[] args)

     {

           Card c1 = new Card();

           Card c2 = new Card();

           Card c3 = new Card();

           Card c4 = new Card();

           Card c5 = new Card();

           System.out.println(c1 + "," + c2 + "," + c3 + "," + c4 + "," + c5);

           int total = c1.value + c2.value + c3.value + c4.value + c5.value;

           System.out.println("Hand total: " + total);

     }

}

 

 

TASK – PART 4 – WAR (CARD PROGRAM 03)

 

Here is my solution:

 

 

public class CardProgram03

{

     public static void main(String[] args)

     {

           Card p1Card = new Card();

           Card p2Card = new Card();

          

           System.out.println("Player 1 got " + p1Card);

           System.out.println("Player 2 got " + p2Card);

          

           if (p1Card.value > p2Card.value)

           {

                System.out.println("Player 1 wins.");

           }

           else if (p2Card.value > p1Card.value)

           {

                System.out.println("Player 2 wins");

           }

           else

           {

                System.out.println("Tie!");

           }

     }

}

 

 

 

TASK – PART 5 – BLACKJACK (CARD PROGRAM 04)

 

Below is my solution.  Note that Aces are always worth 1 in this program.  Also, the total score for each player is shown but the winner isn’t explicitly outputted.  This would be an easy addition to make.

 

The following method was added to the Card class:

 

     public int blackJackValue()

     {

        if (isFaceCard())

        {

              return 10;

        }

        else

        {

              return value;

        }

     }

 

import java.util.Scanner;

 

public class CardProgram04

{

     public static void main(String[] args)

     {

           int total = 0;

          

           Card c1 = new Card();

           System.out.println(c1);

           Card c2 = new Card();

           System.out.println(c2);

           total = c1.blackJackValue() + c2.blackJackValue();

           System.out.println("Your current total is: " + total);

          

           boolean done = false;

           while(!done)

           {

                System.out.println("Want another card?");

                Scanner scr = new Scanner(System.in);

                String ans = scr.nextLine();

               

                if (ans.equals("y"))

                {

                      Card c3 = new Card();

                      System.out.println(c3);

                      total = total + c3.blackJackValue();

                      System.out.println("Your current total is " + total);

                     

                      if (total > 21)

                      {

                           System.out.println("You busted!");

                           done = true;

                      }

                }

                else

                {

                      done = true;

                }

               

           }

           System.out.println("You finished with " + total);

          

           System.out.println("======");

           System.out.println("DEALER'S TURN");

           System.out.println("======");

          

           Card dc1 = new Card();

           System.out.println(dc1);

           Card dc2 = new Card();

           System.out.println(dc2);

           int dtotal = dc1.blackJackValue() + dc2.blackJackValue();

          

           while (dtotal<16)

           {

                Card dc3 = new Card();

                System.out.println(dc3);

                dtotal = dtotal + dc3.blackJackValue();

           }

          

           System.out.println("The dealer finished with " + dtotal);

     }

 

}

 

 

Here is sample output from the above program:

 

4 of hearts

Q of diamonds

Your current total is: 14

Want another card?

y

A of hearts

Your current total is 15

Want another card?

y

3 of hearts

Your current total is 18

Want another card?

n

You finished with 18

======

DEALER'S TURN

======

2 of spades

2 of diamonds

A of spades

6 of hearts

7 of spades

The dealer finished with 18