The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
ITI 1121. Introduction to Computing II Winter 2017
JAVA QUESTIONÂ
As you know, one limitation with Java arrays is that they are of fixed size. We will soon see alternatives that can be used when the number of elements to store is not known in advance, but for this game, it turns out that the maximum number of cards in any deck is known: there are only 51 cards in this game, so we can use arrays of size 51, and we are sure that they will always be large enough to hold as many cards as we could possibly have in any one deck.
Having an array large enough ensures that we will be able to add and remove from that array without having to worry about overflows. However, there is no direct way to know how many elements are in the array currently. In order to address this problem we will have to maintain an additional counter for each of our arrays. These counters record the current number of elements in the array (from 0 to 51, thus).
To facilitate our tasks, a utility class is provided, ArrayStringsTools. It can do a few basic things on our arrays such as sorting them, shuffling them etc.
A detailed description of the classes can be found here:
Â
A1Q4.java
import java.util.Scanner;
import java.util.Random;
/**
* The class <b>A1Q4</b> is an implementation of the
* ``Old Maid'' card game, based on the Python implementation
* given in ITI1020
*
* @author gvj (gvj@eecs.uottawa.ca)
*
*/
public class A1Q4{
/**
* Array used to store the full deck of cards,
*/
private static String deck;
/**
* The current number of cards in the full deck of cards
*/
private static int sizeDeck;
/**
* Array used to store the player's deck of cards
*/
private static String playerDeck;
/**
* The current number of cards in the player's deck of cards
*/
private static int sizePlayerDeck;
/**
* Array used to store the computer's deck of cards
*/
private static String computerDeck;
/**
* The current number of cards in the computer's deck of cards
*/
private static int sizeComputerDeck;
/**
* An instance of java.util.Scanner, which will get input from the
* standard input
*/
private static Scanner sc;
/**
* An instance of java.util.Random, to generate random numbers
*/
private static Random generator;
/**
* Constructor of the class. Creates the full deck of cards
*/
public A1Q4(){ sc = new Scanner(System.in); generator = new Random();
String suits = {"\u2660", "\u2661", "\u2662", "\u2663"};
String ranks =
{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
sizeDeck = suits.length*ranks.length - 1;
deck = new String[sizeDeck];
int index = 0;
for(int i =0 ; i < ranks.length; i++){
for(int j =0 ; j < suits.length; j++){
if(!(ranks[i]=="Q" && suits[j]=="\u2663")){
deck[index++]= ranks[i] + " of " + suits[j];
}
}
}
}
/**
* Waits for user input
*/
private static void waitForUserInput(){
sc.nextLine();
}
/**
* Deals the cards, taking sizeDeck cards out of deck, and deals them
* into playerDeck and computerDeck, starting with playerDeck
*/
private static void dealCards(){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Removes all the pairs of cards from the array deckOfCards, that
currently
* contains currentSize cards. deckOfCards is unsorted. It should also not
* be sorted once the method terminates.
*
*
@param deckOfCards the array of Strings representing the deck of cards
*
@param currentSize the number of strings in the arrayOfStrings,
*
stored from arrayOfStrings[0] to arrayOfStrings[currentSize-1]
*
@return the number of cards in deckOfCards once the pair are removed
*/
private static int removePairs(String deckOfCards, int currentSize){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Get a valid index of a card to be removed from computerDeck.
*
Note: this method does not check that the input is indeed an integer
and *
will crash if something else is provided.
* @return the valid input.
*/
private static int getValidInput(){ // REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION }
/**
* The actual game, as per the Python implementation
*/
public static void playGame(){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* The main method of this program. Creates the game object
* and calls the playGame method on it.
* @param args ignored
*/
public static void main(String args){
A1Q4 game = new A1Q4();
game.playGame();
}
}
ITI 1121. Introduction to Computing II Winter 2017
JAVA QUESTIONÂ
As you know, one limitation with Java arrays is that they are of fixed size. We will soon see alternatives that can be used when the number of elements to store is not known in advance, but for this game, it turns out that the maximum number of cards in any deck is known: there are only 51 cards in this game, so we can use arrays of size 51, and we are sure that they will always be large enough to hold as many cards as we could possibly have in any one deck.
Having an array large enough ensures that we will be able to add and remove from that array without having to worry about overflows. However, there is no direct way to know how many elements are in the array currently. In order to address this problem we will have to maintain an additional counter for each of our arrays. These counters record the current number of elements in the array (from 0 to 51, thus).
To facilitate our tasks, a utility class is provided, ArrayStringsTools. It can do a few basic things on our arrays such as sorting them, shuffling them etc.
A detailed description of the classes can be found here:
A1Q4.java
import java.util.Scanner;
import java.util.Random;
/**
* The class <b>A1Q4</b> is an implementation of the
* ``Old Maid'' card game, based on the Python implementation
* given in ITI1020
*
* @author gvj (gvj@eecs.uottawa.ca)
*
*/
public class A1Q4{
/**
* Array used to store the full deck of cards,
*/
private static String deck;
/**
* The current number of cards in the full deck of cards
*/
private static int sizeDeck;
/**
* Array used to store the player's deck of cards
*/
private static String playerDeck;
/**
* The current number of cards in the player's deck of cards
*/
private static int sizePlayerDeck;
/**
* Array used to store the computer's deck of cards
*/
private static String computerDeck;
/**
* The current number of cards in the computer's deck of cards
*/
private static int sizeComputerDeck;
/**
* An instance of java.util.Scanner, which will get input from the
* standard input
*/
private static Scanner sc;
/**
* An instance of java.util.Random, to generate random numbers
*/
private static Random generator;
/**
* Constructor of the class. Creates the full deck of cards
*/
public A1Q4(){ sc = new Scanner(System.in); generator = new Random();
String suits = {"\u2660", "\u2661", "\u2662", "\u2663"};
String ranks =
{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
sizeDeck = suits.length*ranks.length - 1;
deck = new String[sizeDeck];
int index = 0;
for(int i =0 ; i < ranks.length; i++){
for(int j =0 ; j < suits.length; j++){
if(!(ranks[i]=="Q" && suits[j]=="\u2663")){
deck[index++]= ranks[i] + " of " + suits[j];
}
}
}
}
/**
* Waits for user input
*/
private static void waitForUserInput(){
sc.nextLine();
}
/**
* Deals the cards, taking sizeDeck cards out of deck, and deals them
* into playerDeck and computerDeck, starting with playerDeck
*/
private static void dealCards(){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Removes all the pairs of cards from the array deckOfCards, that
currently
* contains currentSize cards. deckOfCards is unsorted. It should also not
* be sorted once the method terminates.
*
*
@param deckOfCards the array of Strings representing the deck of cards
*
@param currentSize the number of strings in the arrayOfStrings,
*
stored from arrayOfStrings[0] to arrayOfStrings[currentSize-1]
*
@return the number of cards in deckOfCards once the pair are removed
*/
private static int removePairs(String deckOfCards, int currentSize){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Get a valid index of a card to be removed from computerDeck.
*
Note: this method does not check that the input is indeed an integer
and *
will crash if something else is provided.
* @return the valid input.
*/
private static int getValidInput(){ // REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION }
/**
* The actual game, as per the Python implementation
*/
public static void playGame(){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* The main method of this program. Creates the game object
* and calls the playGame method on it.
* @param args ignored
*/
public static void main(String args){
A1Q4 game = new A1Q4();
game.playGame();
}
}
Â
/**
* The class <b>ArrayStringsTools</b> provides a series of
* static methods that mimic some of the methods built in
* Python
*
* @author gvj (gvj@eecs.uottawa.ca)
*
*/
public class ArrayStringsTools{
/**
* Prints the strings contained in arrayOfStrings
*
*
@param arrayOfStrings the array of Strings
*
@param currentSize the number of strings in the arrayOfStrings,
*
stored from arrayOfStrings[0] to
arrayOfStrings[currentSize-1]
*/
public static void printArray(String arrayOfStrings, int currentSize){
if( arrayOfStrings == null || currentSize > arrayOfStrings.length) {
System.out.println("ArrayStringsTools.printArray: wrong
call");
return ;
}
for(int i = 0; i < currentSize-1; i++){
System.out.print(arrayOfStrings[i] + ", ");
}
System.out.println(arrayOfStrings[currentSize-1]);
}
/**
* Does a lexicographic sorting of arrayOfStrings.
* It simply relies on the sort(Object) method
* of java.util.Arrays.
* Note that it sorts in lexicographic order, so
* "10" is before "2", "A" is before "Q" etc.
* This is not important for this game, but wouldn't
* be suitable in some other cases
*
*
@param arrayOfStrings the array of Strings
*
@param currentSize the number of strings in the arrayOfStrings,
*
stored from arrayOfStrings[0] to
arrayOfStrings[currentSize-1]
*/
public static void sortArray(String arrayOfStrings, int currentSize){
if( arrayOfStrings == null || currentSize > arrayOfStrings.length) {
System.out.println("ArrayStringsTools.sortArray: wrong call");
return ;
}
java.util.Arrays.sort(arrayOfStrings, 0, currentSize );
} /**
* Randomly shuffles arrayOfStrings.
*
*
@param arrayOfStrings the array of Strings
*
@param currentSize the number of strings in the arrayOfStrings,
*
stored from arrayOfStrings[0] to
arrayOfStrings[currentSize-1]
*/ public static void shuffleArray(String arrayOfStrings, int currentSize){
if( arrayOfStrings == null || currentSize > arrayOfStrings.length) {
System.out.println("ArrayStringsTools.shuffleArray: wrong
call");
return ;
}
java.util.Random generator = new java.util.Random();
for(int i = currentSize-1 ; i > 1 ; i--){
swapItems(arrayOfStrings, i,generator.nextInt(i-1));
}
}
private static void swapItems(String arrayOfStrings, int i, int j){
String intermediate = arrayOfStrings[i];
arrayOfStrings[i]=arrayOfStrings[j];
arrayOfStrings[j]=intermediate;
}
/**
* Removes the string at arrayOfStrings[itemToRemove] and
*
*
@param arrayOfStrings the array of Strings
*
@param currentSize the number of strings in the arrayOfStrings,
*
stored from arrayOfStrings[0] to
arrayOfStrings[currentSize-1]
*
@param itemToRemove index of the item to remove from arrayOfStrings
*
@return the new size of the modified arrayOfStrings
*/
public static int removeItemByIndex(String arrayOfStrings, int
currentSize, int itemToRemove){ call"); if( arrayOfStrings == null || currentSize > arrayOfStrings.length) {
System.out.println("ArrayStringsTools.removeItemByIndex: wrong
return currentSize;
}
if( itemToRemove < 0 || itemToRemove >= currentSize ) {
System.out.println("ArrayStringsTools.removeItem: item "
+ itemToRemove + " out of bounds. Array Unchanged.");
return currentSize;
}
int i;
for( i = itemToRemove; i < currentSize-1; i++){
arrayOfStrings[i] = arrayOfStrings[i+1];
}
arrayOfStrings[i]= null;
return currentSize-1; }
/**
* Appends itemToAdd at the end of arrayOfStrings
*
*
*
@param arrayOfStrings the array of Strings
*
@param currentSize the number of strings in the arrayOfStrings,
*
stored from arrayOfStrings[0] to
arrayOfStrings[currentSize-1]
*
@param itemToAdd the String to add to arrayOfStrings
*
@return the new size of the modified arrayOfStrings */
public static int appendItem(String arrayOfStrings, int currentSize,
String itemToAdd){ call"); if( arrayOfStrings == null || currentSize > arrayOfStrings.length) {
System.out.println("ArrayStringsTools.appendItem: wrong
} return currentSize; if( currentSize == arrayOfStrings.length) {
System.out.println("ArrayStringsTools.appendItem: array full.
Array Unchanged.");
return currentSize;
}
arrayOfStrings[currentSize++]=itemToAdd;
return currentSize;
}
}