Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with java. Thank you Firstly, for this week Lab let them build a Card

ID: 3782520 • Letter: N

Question

Need help with java.
Thank you Firstly, for this week Lab let them build a Card class that is made up of two variables - a rank (int) and a suit (string). This class must have at least two constructors - one default that will create a card that is Ace (represented by 14) of Spades and the other constructor must accept an int and a String as parameters. They must also include getters and setters. They must also have a print method that will print a card for example if a card is 11 of hearts it must print Jack of Hearts.

Explanation / Answer

public class Card
{
   private int rank, suit;
   // declaring cards
   private static String[] SUITS = { "hearts", "spades", "diamonds", "clubs" };
   private static String[] RANKS = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
  
   // constructor
   public Card(int suit, int rank)
   {
       this.rank=rank-1;
       this.suit=suit-1;
   }
  
   public Card()
   {
       this.rank=0;
       this.suit=0;
   }
  
   // Getter setter methods
   public int getRank() {
       return rank;
   }

   public int getSuit() {
       return suit;
   }
  
   //tostring method
   public @Override String toString()
   {
       return RANKS[rank] + " of " + SUITS[suit];
   }
  
   // //main
   public static void main(String args[]){
       // creating card instances
       Card card1 = new Card(1,11);
       Card card2 = new Card();
       // printing cards
       System.out.println(card1);
       System.out.println(card2);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote