Question

In: Computer Science

In java based on the classes provided finish the program per the requirements: ----------------------------------------------Class1 package cardgame;...

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.

  
}
}

Solutions

Expert Solution

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:


Related Solutions

Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a...
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a linked list-based queue ADT that implements the provided QueueInterface.java You may not make any changes to StackInterface.java or QueueInterface.java The classes must be generic The attached generic singly linked list node class, LLNode.java, must be used for the queue implementation; you may not make any modifications to this class Your implementations cannot throw any exceptions Each ADT must include a toString( ) method: The...
Write a java program. The program requirements are: the program will let the user input a...
Write a java program. The program requirements are: the program will let the user input a starting number, such as 2.  It will generate ten multiplication problems ranging from 2x1 to 2x10.  For each problem, the user will be prompted to enter the correct answer. The program should check the answer and should not let the user advance to the next question until the correct answer is given to the question being asked currently. After testing a total of 10 multiplication problems,...
Write a C Program to finish the following requirements: Convert a double number to its hexadecimal...
Write a C Program to finish the following requirements: Convert a double number to its hexadecimal form 1. The double number x is located between 0 and 1000(0<=x<=1000),e.g.678.345 2.The double number with the hexadecimal form contains 6 significant digits. e.g." 5D.32FA45". 3.The double number with the hexadecimal form is represented by a string(or a character array), e.g."5D.32FA45". Please write as simple as possible because I am a freshman in C! Thanks!
Write a C Program to finish the following requirements: Convert a double number to its hexadecimal...
Write a C Program to finish the following requirements: Convert a double number to its hexadecimal form 1. The double number x is located between 0 and 1000(0<=x<=1000),e.g.678.345 2.The double number with the hexadecimal form contains 6 significant digits. e.g." 5D.32FA45". 3.The double number with the hexadecimal form is represented by a string(or a character array), e.g."5D.32FA45". Please write as simple as possible because I am a freshman in C! And please give me some clues! Thanks!
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
Create a moderately complex java program that utilises 2 or more classes. Within these classes: -...
Create a moderately complex java program that utilises 2 or more classes. Within these classes: - have one that defines an exception - have that exception throw(n) in one method and handled in another -has the program continue even if the user inputs incorrect data -be creative/unique in some way
Background: introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming...
Background: introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming Language that gives us a way to utilize native datatypes as Objects with attributes and methods. A Wrapper class in Java is the type of class which contains or the primitive data types. When a wrapper class is created a new field is created and in that field, we store the primitive data types. It also provides the mechanism to covert primitive into object...
XML and JAVA Write a Java program that meets these requirements. It is important you follow...
XML and JAVA Write a Java program that meets these requirements. It is important you follow these requirements closely. • Create a NetBeans project named LastnameAssign1. Change Lastname to your last name. For example, my project would be named NicholsonAssign1. • In the Java file, print a welcome message that includes your full name. • The program should prompt for an XML filename to write to o The filename entered must end with .xml and have at least one letter...
I need a Java application with a GUI that includes the following requirements: Three classes minimum...
I need a Java application with a GUI that includes the following requirements: Three classes minimum At least one class must use inheritance At least one class must be abstract Utilization of “get set” method. (get; set; =  is related to how variables are passed between different classes. The get method is used to obtain or retrieve a particular variable value from a class. A set value is used to store the variables. The whole point of the get and set...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT