Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java Program: Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and
Java Program: Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and suit. Fix this source code: import java.util.Arrays; public class Card{ protected static final String[] Suits = {"spades", "clubs", "hearts", "diamonds"}; protected static final String[] Ranks = {"1","2","3","4","5","6","7","8","9","10","J","Q","K"}; private String cardRank; private String cardSuit; public Card(){ int randomRank = (int) (Math.random() * Ranks.length); int randomSuit = (int) (Math.random() * Suits.length); cardRank = Ranks[randomRank]; cardSuit = Suits[randomSuit]; } public Card(String cardRank, String cardSuit){ cardRank = cardRank.toLowerCase(); cardSuit = cardSuit.toLowerCase(); if(Arrays.asList(Ranks).contains(cardRank)) this.cardRank = cardRank; else this.cardRank = "Invalid Rank"; if(Arrays.asList(Suits).contains(cardSuit)) this.cardSuit = cardSuit; else this.cardSuit = "Invalid Suit"; } public void getCardRank(){ System.out.print(cardRank); } public void getCardSuit(){ System.out.print(cardSuit); } public void sayCard(){ getCardSuit(); System.out.print(" "); getCardRank(); System.out.println(""); } public void setCardRank(String cardRank){ if(Arrays.asList(Ranks).contains(cardRank)) this.cardRank = cardRank; } public void setCardSuit(String cardSuit){ if(Arrays.asList(Suits).contains(cardSuit)) this.cardSuit = cardSuit; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started