Question

In: Computer Science

(a) Create a Card class that represents a playing card. It should have an int instance...

(a) Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Include the following methods:

  • A constructor with two arguments for initializing the two instance variables.
  • A copy constructor.
  • A method equals — with one argument — which compares the calling object with another Card and returns true if and only if the corresponding ranks and suits are equal. Make sure your method will not generate null pointer exception.
  • A method toString which returns the String value that represents the state of a Card object. For example, if rank is 2 and suit is ‘d’, The toString method should return (diamonds, 2).
  • A static method read which reads rank and suit from the user, and returns an object of type Card.

(b) Write a driver program that creates four Card objects, where c1 is initialized to 2 and ‘d’, c2 is a copy of c1, c3 is initialized with user’s input, and c4 is set to null. Demonstrate all the capabilities and methods of the Card class and generate the following output.

~/cs1400/2020fall/exam $ java CardTest

card1 is (diamonds, 2)

card2 is (diamonds, 2)

creating card3...

enter rank(1-13): 8

enter suit(d=diamonds, h=hearts, s=spades, c=clubs): c

card3 is (clubs, 8)

set card4 to null

card1==card2? true

card1==card3? false

card1==card4? false

Solutions

Expert Solution

Program Code Screenshot (Card.java)

Program Code Screenshot (CardTest.java)

Program Sample Input/Output Screenshot

Program Code to Copy (Card.java)

import java.util.Scanner;

public class Card {
    // instance variables
    private int rank;
    private char suit;

    // getters and setters
    public int getRank() {
        return this.rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public char getSuit() {
        return this.suit;
    }

    public void setSuit(char suit) {
        this.suit = suit;
    }

    // constructor
    public Card(int r, char s) {
        rank = r;
        suit = s;
    }

    // copy constructor
    public Card(Card other) {
        rank = other.getRank();
        suit = other.getSuit();
    }

    // equals method
    public boolean equals(Card other) {
        if(other==null)
            return false;
        return rank == other.rank && suit == other.suit;
    }

    // toString method
    public String toString() {
        switch (suit) {
            case 'd':
                return "(diamonds, " + rank + ")";

            case 'h':
                return "(hearts, " + rank + ")";

            case 's':
                return "(spades, " + rank + ")";

            case 'c':
                return "(clubs, " + rank + ")";

        }
        return "";
    }

    // A static method read which reads rank and suit from the user, and returns an
    // object of type Card.
    public static Card create() {
        int rank;
        char suit;
        Scanner sc = new Scanner(System.in);

        System.out.print("enter rank(1-13): ");
        rank = sc.nextInt();

        System.out.print("enter suit(d=diamonds, h=hearts, s=spades, c=clubs): ");
        suit = sc.next().charAt(0);

        // create object and return
        return new Card(rank, suit);
    }
}

Program Code to Copy (CardTest.java)

public class CardTest {
    // Write a driver program that creates four Card objects
    public static void main(String[] args) throws Exception {
        // c1 is initialized to 2 and ‘d’
        Card c1 = new Card(2, 'd');

        // Print card 1
        System.out.println("card1 is "+c1);

        // c2 is a copy of c1
        Card c2 = new Card(c1);

        // Print card 2
        System.out.println("card2 is "+c2);

        // c3 is initialized with user’s input
        Card c3 = Card.create();

        // Print card 3
        System.out.println("card3 is "+c3);
        
        // c4 is set to null. 
        System.out.println("set card4 to null");
        Card c4 = null;


        // Demonstrate all the capabilities and methods of the Card class 
        System.out.println("card1==card2? "+c1.equals(c2));

        System.out.println("card1==card3? "+c1.equals(c3));

        System.out.println("card1==card4? "+c1.equals(c4));
        
    }
}

Related Solutions

(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have a constructor Sentence(int size), where size determines the length of the sentence field of a given Sentence. The class should have an instance method public boolean isValid() that determines the validity of a sentence according to the rules detailed below. Also create a public nested class Word, with instance variables String value and Type type. Within this class, you must create: A public enum...
C++ Question Create a class for a card in a deck of playing cards. The object...
C++ Question Create a class for a card in a deck of playing cards. The object must contain methods for setting and retrieving the suit and the type of card (type of card meaning 2,3,4,5,6,7,8,9,10,J,Q,K,A). Separate the declaration of the class from its implementation by using the declaration in a header file and the implementation in a .cpp file. Also create a driver program and a makefile which complies the code and makes it able to run. This is more...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make sure the variable names match those #words. size will be a character, either "S", "M", or "L". #extra_shots will be an integer. # #FrapOrder should have a constructor with two required #parameters, one for each of those attributes (size and #extra_shots, in that order). # #FrapOrder should also have a method called get_total. #get_total should calculate the total cost of the order. #If size...
For this Lab you have to implement a class Builder. Your Builder class should have instance...
For this Lab you have to implement a class Builder. Your Builder class should have instance variable name. , Supply a constructor method for your Builder class and the following methods: getName(), makeRow(int n, String s), printPyramid(int n, String s). Examining the problem, we need to create a Builder class, declare Builder class as follows public class Builder { } Inside the Builder class, declare a String variable called name. Step 3: Defining the constructors: Remember that the purpose of...
Create a class called FibGenerator with 3 methods: public int nthFib(int n). This method should call...
Create a class called FibGenerator with 3 methods: public int nthFib(int n). This method should call computeFibRecurse(n). private int computeFibRecurse(int n), which should recurse (that is, call itself) unless n is 1 or 2. If n is 1 or 2, the method should return 1. A main method that prints “STARTING”, then constructs a FibGenerator generator and then calls nthFib(), passing in interesting values. To look into this problem, you’re going to use software to analyze software. Add an instance...
(In C++) Write a class that will represent a card in a standard deck of playing...
(In C++) Write a class that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) of each card. Write methods to • Initialize the deck of cards • Perform a perfect shuffle In a perfect shuffle, the deck is broken exactly in half and rearranged so that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT