Java

OOP GUIDE / WORK

 

CARD CLASS SOLUTIONS

 

 

TASK – PART 1 – TESTING THE FOLLOWING CARD CLASS

 

Here are the statements that I placed in the main method of the Tester class in order to test the Card class:

 


public
class Tester

{

     public static void main(String[] args)

     {

           Card c1 = new Card();

           System.out.println(c1);

          

           Card c2 = new Card();

           System.out.println(c2);

     }

}

 

Here is some sample output I got:

 

10 of diamonds

12 of hearts

 

 

TASK – PART 2 – CARD ARRAY

 

Here is my Tester class with the Card array:

 

 

public class Tester

{

     public static void main(String[] args)

     {

           Card[] hand = new Card[6];

          

           //Creating cards

           for (int i=0; i<hand.length; i++)

           {

                hand[i] = new Card();

           }

          

           //Output cards

           for (int i=0; i<hand.length; i++)

           {

                System.out.println(hand[i]);

           }

          

           //Arrays.sort(hand);     //this gives error for now        

     }

}

 

 

Here is some sample output:

 

8 of hearts

3 of diamonds

5 of hearts

10 of spades

3 of spades

1 of hearts

 

 

TASK – PART 3 – IMPLEMENTING COMPARABLE

 

Here is my Card class that is implementing Comparable (with all previous code in light gray):

 

public class Card implements Comparable

{

     public String suit;  //hearts, diamonds, spades or clubs

     public int value;

    

     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 String toString()

     {

           return value + " of " + suit;

     }

 

     @Override

     public int compareTo(Object obj)

     {

           Card other = (Card)obj;

           if (value == other.value)

           {

                return 0;   //same

           }

           else if (value < other.value)

           {

                return -1;

           }

           else //if (value > other.value)

           {

                return 1;

           }

     }

}

 

 

 

 

TASK – PART 4 – SORTING THE ARRAY

 

Here is my solution:

 

import java.util.Arrays;

 

public class Tester

{

     public static void main(String[] args)

     {

           Card[] hand = new Card[6];

 

           //Creating cards

           for (int i=0; i<hand.length; i++)

           {

                hand[i] = new Card();

           }

 

           //Output cards

           for (int i=0; i<hand.length; i++)

           {

                System.out.println(hand[i]);

           }

 

           Arrays.sort(hand);

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

          

 

           //Output cards

           for (int i=0; i<hand.length; i++)

           {

                System.out.println(hand[i]);

           }

     }

}

 

Below is some sample output.  The first six lines are the unsorted array.  The next six lines are the sorted array.

 

3 of spades

2 of hearts

2 of clubs

10 of spades

5 of clubs

8 of spades

======

2 of hearts

2 of clubs

3 of spades

5 of clubs

8 of spades

10 of spades