1. Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. Design a Card class that contains a character data field to hold a suit (s for spades, h for hearts, d for diamonds, or c for clubs) and an integer data field for a value from 1 to 13. (When you learn more about string handling in the chapter “Characters, Strings, and the StringBuilder,” you can modify the class to hold words for the suits, such as spades or hearts, as well as words for some of the values—for example, ace or king.) Include get and set methods for each field. Save the class as Card.java. Write an application that randomly selects two playing cards and displays their values. Simply assign a suit to each of the cards, but generate a random number for each card’s value. Appendix D contains information about generating random numbers. To fully understand the process, you must learn more about Java classes and methods. However, for now, you can copy the following statements to generate a random number between 1 and 13 and assign it to a variable: final int CARDS_IN_SUIT = 13; myValue = ((int)(Math.random() * 100) % CARDS_IN_SUIT + 1); After reading the chapter “Making Decisions,” you will be able to have the game determine the higher card. For now, just observe how the card values change as you execute the program multiple times. Save the application as PickTwoCards.java. You use the Math.random() function to generate a random number. The function call uses only a class and method name—no object—so you know the random() method must be a static method.
    public class Card { private char suit; private int value; public char getSuit() { return suit; } public int getValue() { return value; } public void setSuit(char s) { suit = s; } public void setValue(int v) { value = v; } }
    public class PickTwoCards { public static void main(String[] args) { final int CARDS_IN_SUIT = 13; int myValue; int yourValue; Card myCard = new Card(); Card yourCard = new Card(); myValue = ((int)(Math.random() * 100) % CARDS_IN_SUIT + 1); yourValue = ((int)(Math.random() * 100) % CARDS_IN_SUIT + 1); myCard.setValue(myValue); yourCard.setValue(yourValue); myCard.setSuit('s'); yourCard.setSuit('h'); System.out.println("My card is the " + myCard.getValue() + " of " + myCard.getSuit()); System.out.println("Your card is the " + yourCard.getValue() + " of " + yourCard.getSuit()); } }
  2. Computer games often contain different characters or creatures. For example, you might design a game in which alien beings possess specific characteristics such as color, number of eyes, or number of lives. Design a character for a game, creating a class to hold at least three attributes for the character. Include methods to get and set each of the character’s attributes. Save the file as MyCharacter. java. Then write an application in which you create at least two characters. In turn, pass each character to a display method that displays the character’s attributes. Save the application as TwoCharacters.java.

    public class MyCharacter { private int age; private int numberOfLegs; private int numberOfEyes; public int getAge() { return age; } public int getLegs() { return numberOfLegs; } public int getEyes() { return numberOfEyes; } public void setAge(int a) { age = a; } public void setLegs(int l) { numberOfLegs = l; } public void setEyes(int e) { numberOfEyes = e; } }
    public class TwoCharacters { public static void main(String[] args) { int age; int legs; int eyes; int yourValue; MyCharacter char1 = new MyCharacter(); MyCharacter char2 = new MyCharacter(); age = ((int)(Math.random() * 100) + 1); legs = ((int)(Math.random() * 100) + 1); eyes = ((int)(Math.random() * 100) + 1); char1.setAge(age); char1.setLegs(legs); char1.setEyes(eyes); age = ((int)(Math.random() * 100) + 1); legs = ((int)(Math.random() * 100) + 1); eyes = ((int)(Math.random() * 100) + 1); char2.setAge(age); char2.setLegs(legs); char2.setEyes(eyes); display(char1); display(char2); } public static void display(MyCharacter ch) { System.out.println("The character is " + ch.getAge() + " years old, has " + ch.getLegs() + " legs and " + ch.getEyes() + " eyes"); } }