Question

In: Computer Science

1. Create a new class called Card that contains the following attributes: [3 K] Suit (Clubs,...

1. Create a new class called Card that contains the following attributes: [3 K]

  • Suit (Clubs, Diamonds, Spades, Hearts)

  • Value (do not use Jack, Queen, King, Ace, instead, the value of your cards should be 1-13 where 11 = jack, 12 = Queen, 13 = King, and 1 = Ace)

  • Colour (Red or Black)

2. Make two constructions for your card: [2 A]

  • A default constructor for when you create an object without any arguments

  • A constructor that has 3 parameters, one for each attribute

  • Card should overwrite the corresponding print method to print the card nicely to the user with the following layout (colour does not need to be outputted): [1 K]

 
 

Value of Suit

 

ex. "5 of Hearts"

3. Card should also contain the following methods/functions:

  • isRed() – returns true or false if the card is Red [1 I]

  • isLarger(card)– takes in a card and returns the larger card according to the values. Note Ace (1) is smallest card and King (13) is largest. If the cards are the same, return the card the method is called on. [2 I]

  • copyCard() – creates and returns a new card object with all the same attributes [2 I]

In the main, feel free to test your methods in any way. The test cases will not be affected by any print statements. You must submit before lunch even if all the tests do not pass. Note that commenting is necessary and will count for 2 marks. [2 C]

[4 K, 5I, 2C, 2A]

Code:

Main.java

class Main {
public static void main(String[] args) {
  
  
  
}
}

Card.java

class Card {
  
  
}

Solutions

Expert Solution

File Name : Card.java
// Defines the possible values available for Colour Attribute Of Card
enum Colour {
        RED, BLACK
}
//Defines the possible values available for Suit Attribute Of Card
enum Suit {
        Clubs, Diamonds, Spades, Hearts

}
class Card {
        private Colour colour;
        private Suit suit;
        /*The Mapping For Value Is : 
         * Value (do not use Jack, Queen, King, Ace, instead, the value of your cards should be 1-13 where 11 = jack, 12 = Queen, 13 = King, and 1 = Ace)
         */
        private int Value;
        
        //Default Constructor
        public Card() {
                
        }
        //Parameterized Constructor
        public Card(Colour colour,Suit suit,int Value) {
                this.colour=colour;
                this.suit=suit;
                this.Value=Value;
        }
        //Checks if the card is red or black 
        public boolean isRed() {
                if(suit==Suit.Hearts || suit==suit.Diamonds) {
                        return true;
                }
                return false;
        }
        //Returns the card whichever is bigger, else current card if both are same
        public Card isLarger(Card card) {
                if(this.Value>=card.Value) {
                        return this;
                }else {
                        return card;
                }
        }
        //Creates a new card with the values copied from current card
        public Card copyCard() {
                return new Card(this.colour,this.suit,this.Value);
        }
        
        //Prints the card in a nice format
        @Override
        public String toString() {
                if(Value==1 ) {
                        return "Ace of "+suit;
                }
                else if(Value==11) {
                        return "jack of "+suit;
                }
                else if(Value==12) {
                        return "Queen of "+suit;
                }
                else if(Value==13){
                        return "King of "+suit;
                }else {
                        return Value+" of "+suit;
                }
        }
}

File Name : Main.java


public class Main {
        //Driver Program
        public static void main(String[] args) {
                Card c=new Card(Colour.BLACK, Suit.Hearts, 13);
                System.out.println("The Card : ");
                System.out.println(c);
                System.out.println("================================================");
                System.out.println("Is it A Red Card? ");
                System.out.println(c.isRed());
                System.out.println("================================================");
                System.out.println("Is the card c or a card with Colour: Black, Suit: Diamond , Value: 11 Greater?");
                System.out.println(c.isLarger(new Card(Colour.BLACK, Suit.Diamonds, 11)));
                System.out.println("================================================");
                System.out.println("Create a copy of the card c");
                Card c1=c.copyCard();
                System.out.println(c1);
                
        }

}

Code Explanation:

Define the class Card.

  1. Enum for Colour To Possible Values RED, BLACK
  2. Enum For Suit With Possible Values Clubs, Diamonds, Spades, Hearts
  3. Define the instance variables for the Colour, Suit and Value
  4. Constructor overloading implemented in the form of Default Constructor and parameterized constructor with values as Colour,Suit And Value
  5. isRed() method implemented , it checks if Suit is Hearts or Diamonds if so returns True else False
  6. isLarger() method implemented, it checks if current card value>=passed card value, if so returns current card else the passed card.
  7. copyCard() method implemented, this will create a new card object with values of current Card Object
  8. toString() method implemented, this basically is used to print the Card in a nice format i.e {Value} Of {Suit}

2. The Main class, it is the driver program.

Here, the object of Class Card is created and respective methods created above are called to try out the functionalities developed.


Related Solutions

*in Java 1.Create a card class with two attributes, suit and rank. 2.In the card class,...
*in Java 1.Create a card class with two attributes, suit and rank. 2.In the card class, create several methods: a. createDeck – input what type of deck (bridge or pinochle) is to be created and output an array of either 52 or 48 cards. b.shuffleDeck – input an unshuffled deck array and return a shuffled one. + Create a swap method to help shuffle the deck c. countBridgePoints – inputs a 13-card bridge ‘hand’-array and returns the number of high-card...
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color....
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “white”; this is called a default constructor. Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Rabbit objects. For the first Rabbit object, call the first constructor. For the second...
PYTHON A Class for a Deck of Cards We will create a class called Card whose...
PYTHON A Class for a Deck of Cards We will create a class called Card whose objects we will imagine to be representations of playing cards. Each object of the class will be a particular card such as the '3' of clubs or 'A' of spades. For the private data of the class we will need a card value ('A', '2', '3', ... 'Q', 'K') and a suit (spades, hearts, diamond, clubs). Before we design this class, let's see a...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Language C++ Student-Report Card Generator: create a class called student and class called course. student class...
Language C++ Student-Report Card Generator: create a class called student and class called course. student class this class should contain the following private data types: - name (string) - id number (string) - email (s) - phone number (string) - number of courses (int) - a dynamic array of course objects. the user will specify how many courses are there in the array. the following are public members of the student class: - default constructor (in this constructor, prompt the...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K and n o Create an overloaded constructor to initialize the variables into any positive integers such that n > k > 0o Create a method that calculates the value of binomial coefficient, C(n,k) , using the following rule: ▪ For an array of size (n+1) X (k+1) ▪ Array [n][k] = Array[n-1][ k-1]+ Array[n-1] [k]▪ Array[n][0]= Array[n][n] = 1 ▪ Hence, C(n,k) = array...
Write a class Car that contains the following attributes: The name of car The direction of...
Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S) The position of car (from imaginary zero point) The class has the following member functions: A constructor to initialize the attributes. Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.) Overload the Turn function to change the direction to any...
Create a new Class Payroll that contains our ClassEmployee.
JAVACreate a new Class Payroll that contains our ClassEmployee.we are adding a copy constructor to our Employee Classwe are adding a toString() method to our Employee Classour company has grown so we can no longer avoid deducting taxes from our workers, so we moved the PrintPayStub() method to our newPayroll Class.we will need 2 static members to our Payroll Class to help us compute State and Federal tax deductions from our Employee PaychecksDelving deeper into the concepts of Object Oriented...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT