In: Computer Science
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 {
}
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.
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.