JAVA Write a class to create a card (made up of 4 suits ( Hearts, Diamonds, Club
ID: 3665223 • Letter: J
Question
JAVA
Write a class to create a card (made up of 4 suits ( Hearts, Diamonds, Clubs and Spades) and cards from 2 to 10 and Jacks(11), Queens(12), King(13), and Ace(1). Two getters and Two setters.
A print method that will print a card in the form of for example ----2H( or 2 of Hearts)
Here is the update -- they must also write two constructors -
1. One default that sets a card to 13 of Spades
(which is equivliant to Ace of Spades espically when they need to print the card) and
2. One that accepts two parameters - one int for rank of the card and one string for the suit of Card.
Explanation / Answer
I took short characters for suits in class for simplicity. Here is my code:
class Card
{
char suit;// H for hearts D for diamonds C for clubs and S for spades
int rank;
Card()
{
suit = 'S';
rank = 13;
}
Card(char s, int r)
{
suit = s;
rank = r;
}
char getsuit()
{
return suit;
}
int getrank()
{
return rank;
}
void setsuit(char s)
{
suit = s;
}
void setrank(int r)
{
rank = r;
}
void print()
{
System.out.print(rank);
System.out.print(suit);
}
}
public class Ass1
{
public static void main(String args[]) throws Exception
{
Card c1 = new Card('H', 2);
c1.print();
}
}
If you have any doubt than please ask and don't forget to give me the feedback
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.