Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.InputMismatchException; //error handling
  3. import java.util.Random; //Import this to generate pseudo-random code.
  4.  
  5. public class Question2 {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         String[] cardNumbers = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", //array of numbers
  10.                 "Queen", "King" };
  11.  
  12.         String[] suits = { "Spades", "Clubs", "Hearts", "Diamonds" };           //array of suits
  13.  
  14.         String decisionInput;                                          
  15.         Scanner keyboard = new Scanner(System.in);
  16.         boolean repeatYes = false;
  17.         Random r = new Random();                                            //Declare the random element r here.
  18.  
  19.         do {
  20.  
  21.             String[][] deck = new String[4][4];                             //2D array created
  22.  
  23.             String[] randomSuits = new String[4];                           //Two sets of 1D arrays to store  randomized sets of
  24.                                                            
  25.             String[] randomNumbers = new String[4];                             //4 suits and numbers as per the example output.
  26.                                                    
  27.  
  28.             for (int i = 0; i < 4; i++)
  29.                 randomSuits[i] = suits[r.nextInt(4)];                       //Randomized suits being assigned
  30.                                                        
  31.  
  32.             for (int i = 0; i < 4; i++)
  33.                 randomNumbers[i] = cardNumbers[r.nextInt(13)];              //Randomized card numbers being assigned
  34.                                                            
  35.  
  36.             for (int i = 0; i < 4; i++) {                                       //Loop through 2D array, and place number + of + suits
  37.                 for (int j = 0; j < 4; j++) {
  38.                     deck[i][j] = randomNumbers[j] + " of " + suits[i];
  39.                 }
  40.             }
  41.             display(deck);                                                  //call display method on deck
  42.  
  43.             boolean c1Good = true, c2Good = true, validInput = false;                  
  44.             int chosenColumn1 = 0;
  45.  
  46.             System.out.println("\nPlease pick a card and enter the column number (1-4) where it appears");                                         
  47.  
  48.             while (c1Good == true)
  49.  
  50.             {
  51.                 do
  52.  
  53.                 {
  54.                     try                                                     //try/catch to catch the exception thrown by the console if
  55.                                                                             //the user inputs anything other than an integer
  56.                     {
  57.                         chosenColumn1 = keyboard.nextInt();                 //ask for first column
  58.  
  59.                         validInput = true;                                  //no need to repeat
  60.  
  61.                         if ((chosenColumn1 >= 1) & (chosenColumn1 <= 4))        //ensure int is between 1 and 4
  62.  
  63.                             c1Good = false;                                 //need to repeat outer loop
  64.  
  65.                         else
  66.  
  67.                             System.out.println("Please enter an integer value between 1 and 4");
  68.  
  69.                     }
  70.                     catch (InputMismatchException e)                            //catches lack of integer, avoiding program error
  71.                     {
  72.                         keyboard.nextLine();                                    //ask for input again
  73.  
  74.                         System.out.println("Please enter an integer value between 1 and 4");
  75.                     }
  76.                 }
  77.                 while (!validInput);                                            //will keep repeating this till it gets valid input
  78.             }
  79.            
  80.             String[][] transposedMatrix = transposeMatrix(deck);                //transposes the 2D array
  81.             display(transposedMatrix);                                      //displays newly transposed array
  82.  
  83.             System.out.println("\nPlease Indicate which column number (1-4) it is in now");
  84.                                                                            
  85.             int chosenColumn2 = 0; 
  86.             validInput = false;                                             //back to false for new loop use
  87.             while (c2Good == true)                                          //does the same user validation as the while loop before transpose
  88.             {
  89.                 do
  90.                 {
  91.                     try                                                    
  92.                     {
  93.                         chosenColumn2 = keyboard.nextInt();
  94.                         validInput = true;
  95.                         if ((chosenColumn2 >= 1) & (chosenColumn2 <= 4))
  96.                             c2Good = false;
  97.                         else
  98.                             System.out.println("Please enter an integer value between 1 and 4");
  99.                     }
  100.                     catch (InputMismatchException e)
  101.                     {
  102.                         keyboard.nextLine();
  103.                         System.out.println("Please enter an integer value between 1 and 4");
  104.                     }
  105.                 } while (!validInput);
  106.             }
  107.  
  108.             keyboard.nextLine();                                                //clears the line for new use below
  109.            
  110.             System.out.println("Your card is: " + transposedMatrix[chosenColumn1 - 1][chosenColumn2 - 1]);
  111.             System.out.println("Do you want to try one more time? y or n");
  112.             decisionInput = keyboard.nextLine();                                //want a new iteration
  113.  
  114.             if (decisionInput.equalsIgnoreCase("n")) {
  115.                 System.out.println("Thank you for using the JAVA Magic 101 Program");
  116.  
  117.                 repeatYes = false;                                          //ends outer do-while loop and terminates program when evaluated in 'while'
  118.                                                                             //condition below
  119.             } else if (decisionInput.equalsIgnoreCase("y")) {
  120.                 repeatYes = true;                                           //repeats program from the top when evaluated in 'while' condition below
  121.             } else {
  122.                 System.out.println("\nInvalid input!");                     //not required, but just in case
  123.             }
  124.  
  125.         } while (repeatYes == true);
  126.     }
  127.  
  128.     public static String[][] transposeMatrix(String matrix[][]) {               //method for transposing the 2D
  129.         int m = matrix.length;
  130.         int n = matrix[0].length;
  131.  
  132.         String[][] transposedMatrix = new String[n][m];                     //create the space for a transposed matrix
  133.  
  134.         for (int x = 0; x < n; x++) {                                           //fill it with values based on original 2D
  135.             for (int y = 0; y < m; y++) {
  136.                 transposedMatrix[x][y] = matrix[y][x];
  137.             }
  138.         }
  139.  
  140.         return transposedMatrix;                                               
  141.     }
  142.    
  143.     public static void display(String x[][]) {                              //method to display any 2D array
  144.         for (int row = 0; row < x.length; row++) {                         
  145.             for (int column = 0; column < x[row].length; column++) {
  146.                 System.out.printf("%-25s", x[row][column]);                 //printf to format where the elements are placed when printed (25 character spaces)
  147.             }
  148.             System.out.println();
  149.         }
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement