import java.util.Scanner; public class Memory { /* Complete the public static me
ID: 3538097 • Letter: I
Question
import java.util.Scanner;
public class Memory {
/* Complete the public static method called shuffle below
Because arr is a 2D array (class type), any changes made to it, will change the argument
so the method does not need to return anything (void)
Use Math.random => gives a double x where 0 <= x < 1 to help you generate the cards positions.
The method should shuffle pairs of 2 random cards 50 times.
*/
public static void shuffle (int[][] arr){
}
/* complete the printOut method
on first line print nos " 1 2 3 4" (from 1 to the length of the first row of arr)
on the subsequent lines print out the array arr starting with a line number and then spaces
between entries.
if the value of show at that position is false, print out a * else print out
the content of arr at that position and then a space
Sample output:
1 2 3 4
1 * * 7 *
2 * * * 3
3 7 * * *
4 * * * 3
*/
public static void printOut(int[][] arr, boolean[][] show) {
}
/* complete the method available below that returns a boolean.
false if all the show 2D array is true, true otherwise
*/
public static boolean available(boolean[][] show){
}
public static void main(String args[]){
// Make a Scanner object called keys
// Make a 2D array called cards of type int of size 4x4
// Make a 2D array called show of type boolean of size 4x4
// initialize the cards (put 1 - 8 in first two rows, and then 1 - 8 again in second two rows)
// initialize all the entries in the 2D array called show to false
// shuffle the cards
// make 4 int variables called row1, col1 , row2, col2 to hold the 2 cards the user will select
// make an int variable called attempt that will keep track of the number of attempts and set it to 1
// choose an appropriate control for the while loop that uses the available method
while( ){
// print out Attempt No: xxx
System.out.println( );
// call the method printOut to display the cards
System.out.println("Pick a card ( row then column e.g. 1 4 ): ");
// read in row1 and col1 ( by using nextInt ) and print out the card at that position
System.out.println("That card is a : " );
System.out.println("Pick a card ( row then column e.g. 1 4 ): ");
// read in row2 and col2 and print out the card
System.out.println("That card is a : " );
// if the two cards are identical set their show position to true and print out
// You made a match. Press Enter to try again.
// if they are not identical print out
// Sorry the cards did not match. Press Enter to try again.
// skip two lines from the Scanner and increase the attempt variable
System.out.println(" ");
}
// print out Congratulations! You solved it in xxx attempts
System.out.println();
}
}
Explanation / Answer
import java.util.Scanner;
public class Memory {
/* Complete the public static method called shuffle below
Because arr is a 2D array (class type), any changes made to it, will change the argument
so the method does not need to return anything (void)
Use Math.random => gives a double x where 0 <= x < 1 to help you generate the cards positions.
The method should shuffle pairs of 2 random cards 50 times.
*/
public static void shuffle (int[][] arr){
int r1,c1,r2,c2,tmp,n;
n=arr[0].length;
for(int i=0;i<50;i++)
{
r1=(int)(Math.random()*n);
c1=(int)(Math.random()*n);
r2=(int)(Math.random()*n);
c2=(int)(Math.random()*n);
tmp=arr[r1][c1];
arr[r1][c1]=arr[r2][c2];
arr[r2][c2]=tmp;
}
}
/* complete the printOut method
on first line print nos " 1 2 3 4" (from 1 to the length of the first row of arr)
on the subsequent lines print out the array arr starting with a line number and then spaces
between entries.
if the value of show at that position is false, print out a * else print out
the content of arr at that position and then a space
Sample output:
1 2 3 4
1 * * 7 *
2 * * * 3
3 7 * * *
4 * * * 3
*/
public static void printOut(int[][] arr, boolean[][] show) {
int n=arr[0].length;
System.out.print(" ");
for(int i=0;i<n;i++)
{ System.out.print((i+1)+" "); }
System.out.println();
for(int i=0;i<arr.length;i++)
{
System.out.print((i+1)+" ");
for(int j=0;j<n;j++)
{
if( show[i][j]) { System.out.print(arr[i][j]+" "); }
else { System.out.print("* "); }
}
System.out.println();
}
}
/* complete the method available below that returns a boolean.
false if all the show 2D array is true, true otherwise
*/
public static boolean available(boolean[][] show){
int n=show[0].length;
for(int i=0;i<show.length;i++)
{
for(int j=0;j<n;j++)
{
if( !show[i][j]) { return true; }
}
}
return false;
}
public static void main(String args[]){
// Make a Scanner object called keys
Scanner keys=new Scanner(System.in);
// Make a 2D array called cards of type int of size 4x4
int cards[][]=new int[4][4];
// Make a 2D array called show of type boolean of size 4x4
boolean show[][]=new boolean[4][4];
// initialize the cards (put 1 - 8 in first two rows, and then 1 - 8 again in second two rows)
for(int i=0;i<8;i++)
{ cards[i/4][i%4]=(i+1); }
for(int i=8;i<16;i++)
{ cards[i/4][i%4]=(i+1-8); }
// initialize all the entries in the 2D array called show to false
for(int i=0;i<16;i++)
{ show[i/4][i%4]=false; }
// shuffle the cards
shuffle(cards);
// make 4 int variables called row1, col1 , row2, col2 to hold the 2 cards the user will select
int row1, col1 , row2, col2;
// make an int variable called attempt that will keep track of the number of attempts and set it to 1
int attempt=1;
// choose an appropriate control for the while loop that uses the available method
while(available(show)){
// print out Attempt No: xxx
System.out.println( "Attempt No: "+attempt );
// call the method printOut to display the cards
printOut(cards,show);
System.out.println("Pick a card ( row then column e.g. 1 4 ): ");
// read in row1 and col1 ( by using nextInt ) and print out the card at that position
row1=keys.nextInt();
col1=keys.nextInt();
System.out.println("That card is a : " +cards[row1-1][col1-1] );
System.out.println("Pick a card ( row then column e.g. 1 4 ): ");
// read in row2 and col2 and print out the card
row2=keys.nextInt();
col2=keys.nextInt();
System.out.println("That card is a : " +cards[row2-1][col2-1] );
// if the two cards are identical set their show position to true and print out
if( cards[row1-1][col1-1] == cards[row2-1][col2-1] )
{
show[row1-1][col1-1]=true;
show[row2-1][col2-1]=true;
// You made a match. Press Enter to try again.
System.out.println("You made a match. Press Enter to try again.");
}
else
{
// if they are not identical print out
System.out.println("Sorry the cards did not match. Press Enter to try again.");
// Sorry the cards did not match. Press Enter to try again.
}
// skip two lines from the Scanner and increase the attempt variable
keys.nextLine();
keys.nextLine();
attempt++;
System.out.println(" ");
}
// print out Congratulations! You solved it in xxx attempts
System.out.println("Congratulations! You solved it in "+attempt+" attempts");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.