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

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...
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
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...
1. Write a Java program from scratch that meets the following requirements: a. The program is...
1. Write a Java program from scratch that meets the following requirements: a. The program is in a file called Duplicates.java that defines a class called Duplicates (upper/lower case matters) b. The program includes a Java method called noDuplicates that takes an array of integers and returns true if all the integers in that array are distinct (i.e., no duplicates). Otherwise it returns false. Make sure the method is public static. example tests: noDuplicates({}) returns true noDuplicates({-1, 1}) returns true...
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...
Socket-based Java server program
Write a Socket-based Java server program that responds to client messages as follows: When it receives a message from a client, it simply converts all the letters into ASCII characters and sends back them to the client. Write both client and server programs demonstrating this.
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT