Menu

[Solved]Java Program 1 Learning Class Create Use Class Cardjava Cardpaneljava 2 Class Card Class M Q37287845

Java Program

1.We have been learning (in class) about how to create and use aclass. Card.java and CardPanel.java. 2 Class Card This class modelsa playing card. Recall that a playing card has one of the followingfour suits: HEART, SPADE, CLUB, and DIAMOND. Each suit has 13 cardsA,2,3,4,5,6,7,8,9,10,J,Q,K. We often represent A as 1, J as 11, Qas 12, and K as 13. Read the code of the class and Card and try tounderstand the functionality of the class. Create a class namedTestCard with main method. Create Card objects to represent each ofthe following: HEART 2, SPADE 4, CLUB Q, and DIAMOND A. A Cardobject to represent HEART 2 can be done by Card c1 = newCard(“HEART”, 2); Using System.out.println print each of theobjects. For example System.out.println(c1); 3 Class Deck You jobis to create a class to represent a deck of playing cards. Recallthat a deck has 52 distinct Cards. Thus this class will have aninstance variable deck which is an array of type Card. In thisclass write a default constructor. The constructor shouldinitialize the array deck so that this array has all 52 distinctcards. Partial code for the constructor is given below publicDeck() { deck = new Card[52]; for(int i = 0; i<52; i++) { if (i/ 13 == 0) { deck[i] = new Card(“SPADE”, i % 13 +1); //WRITE RESTOF THE CODE HERE } } 1 Your class must have following methods.getCard(int i) return the ith card of deck. Note that the type ofthis method is Card. getHand() Returns a Card array of size 5,consisting of first 5 cards of deck. shuffle() The purpose of thismethod is to shuffle the deck. Shuffling can be done as follows: For iin range 0 to 51 do the following: Randomly pick an integer namedpos among between 0 and 51 (including 0 and 51). Exchange Card atindex i (of array deck) with Card at index pos (of array deck). Youcan generate a random integer as follows. First import java.util.*Random r = new Random(); int pos = r.nextInt(52); Once you finishwriting the class Deck, run the program CardPanel. What happens?Now look at the code of CardPanel. In the method createAllImage,uncomment the line example.shuffle() and run the program again.What happens? Run the program multiple times.

Expert Answer


Answer to Java Program 1.We have been learning (in class) about how to create and use a class. Card.java and CardPanel.java. 2 Cla… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *