In: Computer Science
In java based on the classes provided finish the program per the requirements:
----------------------------------------------Class1
package cardgame;
{
private String suit;// spades,diamonds,clubs,hearts
private int value; // 1 to 13
public static final String[] SUITS
={"clubs","hearts","diamonds","spades"};
/**
* @return the suit
*/
public String getSuit() {
return suit;
}
/**
* @param suit the suit to set
*/
public void setSuit(String suit) {
this.suit = suit;
}
/**
* @return the value
*/
public int getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}
//you have to write two methods for random generation of suit and
value
public int ranSuit()
{
int value= (int)(Math.random()*4)+0;
return value;
}
public int ranValue()
{
int value= (int)(Math.random()*13)+1;
return value;
}
}
--------------------------------------------Class2
public class CardTrick {
public static void main(String[] args)
{
CardGame[] magicHand = new CardGame[7];//array of objects
for(int i=0;i<magicHand.length;i++)
{
CardGame c1 = new CardGame();//object
c1.setValue(c1.ranValue());//random number 1 to 13
c1.setSuit(CardGame.SUITS[c1.ranSuit()]);
magicHand[i] =c1;
}
for(int i=0;i<magicHand.length;i++)
{
System.out.println(magicHand[i].getSuit() +" "+
magicHand[i].getValue());
}
// take input suit and value from user. compare with
array.if same card is
//in the array print your card is found.
}
}
CardGame class has two attributes - type of suit and the card number. The setters and getters function are provided by the class. The task is to take an input from user with the suit type and card number and compare each object of magicHand array with the user's input in a loop and output if the user's card is found or not.
The added code is in bold.
Code:
package cardgame;
import java.lang.*;
import java.util.*;
// ----------------------------------------------Class1
class CardGame
{
private String suit;//
spades,diamonds,clubs,hearts
private int value; // 1 to 13
public static final String[] SUITS
={"clubs","hearts","diamonds","spades"};
/**
* @return the suit
*/
public String getSuit() {
return suit;
}
/**
* @param suit the suit to set
*/
public void setSuit(String suit) {
this.suit = suit;
}
/**
* @return the value
*/
public int getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}
//you have to write two methods for random generation
of suit and value
public int ranSuit()
{
int value=
(int)(Math.random()*4)+0;
return value;
}
public int ranValue()
{
int value=
(int)(Math.random()*13)+1;
return value;
}
}
// --------------------------------------------Class2
public class CardTrick {
public static void main(String[] args)
{
CardGame[] magicHand = new
CardGame[7];//array of objects
for(int
i=0;i<magicHand.length;i++)
{
CardGame c1 =
new CardGame();//object
c1.setValue(c1.ranValue());//random number 1 to 13
c1.setSuit(CardGame.SUITS[c1.ranSuit()]);
magicHand[i]
=c1;
}
for(int
i=0;i<magicHand.length;i++)
{
System.out.println(magicHand[i].getSuit() +" "+
magicHand[i].getValue());
}
// take input suit and value from
user. compare with array.if same card is
//in the array print your card is
found.
Scanner sc = new
Scanner(System.in); // input stream
// user inputs
int suitNumber,
user_card_number;
String user_suit;
// taking user input
System.out.println("Enter suit
type: 0 for clubs, 1 for hearts, 2 for diamonds, 3 for
spades");
suitNumber = sc.nextInt();
System.out.println("Enter card
number from 1 to 13");
user_card_number =
sc.nextInt();
// invalid card choice
if((suitNumber<0 ||
suitNumber>3) || (user_card_number<1 ||
user_card_number>13)) {
System.out.println("Invalid inputs");
} else {
// get the suit
type in string
user_suit =
CardGame.SUITS[suitNumber];
int i;
// loop for all
the cards in magicHand
for(i=0;i<magicHand.length;i++)
{
// match found, if both attributes are
equal
if(user_suit.equals(magicHand[i].getSuit())
&& user_card_number == magicHand[i].getValue())
{
System.out.println("Your card
is found");
break;
}
}
// reached at
the end of loop but no match found
if(i==magicHand.length) {
System.out.println("Your card is not
found");
}
}
}
}
Sample output:
Code snippet screenshot: