AP Computer Science Homework Set 5 2D Arrays Note: all programs described below should work if the size of the 2D array is changed. P5A. Create a 3x4 2D array of integers and fill it with random numbers between 0 and 9. Use a nested for loop to print the array in a rectangular format. Finally, print the row, column pair of coordinates of all instances of the number 5. Use nested for loops to populate the 2D array and print out its contents. The strategic use of if statements and tabs will allow you to print the array in a rectangular format (i.e. 3 rows deep and 4 columns wide ) P5B. Create a 3x5 2D array of integers and fill it with numbers 1-15 in row-major (left to right, top to bottom) order using a nested for loop. Print the 2D array in rectangular format using nested for-each loop. Then complete the following computations: a. Calculate and print the sum total of all 15 elements of the array. b. Calculate and print the sum total of each row in the array. c. Calculate and print the sum total of each column in the array. Be sure to preface each printout with a clear statement of what quantity is being printed. Page 1
P5C. Got Multiplication Tables? Write a program that will create and print a multiplication table using a 2D array. The program should be able to accept any number of rows and/or columns and correctly generate the multiplication table. Row and column numbers should be displayed along the top and left side of the multiplication table. See the example below: Page 2
P5D. Write a program that will fill a 2D array with the letters of the alphabet a-z in row major order (i.e. left-right, top-down as you would read a book.) Once the letter z is reached, the cycle begins again with an a until all elements of the 2D array are filled. Print your array after using nested for loops to test your result. The program should work for any size 2D array. An optional challenge Add a space between every letter that is printed AND the ability to choose from lowercase letters (a-z), capital letters (A-Z), or digits 0-9. Prompt the user for the number of rows, columns, and types of characters to print (lowercase, uppercase, or digits. Example output is shown below: Page 3
P5E. Create a class called Jukebox. A Jukebox will consist of a 2D array of MySong objects called songlist. Write a program to perform the following tasks: a. Write a zero-argument constructor to fill the jukebox with the following MySong objects and ratings or fill with your own songs. You can cut and paste the following code to quickly fill up your jukebox free of charge songlist[0][0] = new MySong( "Jet Airliner", 5 ); songlist[0][1] = new MySong( "Slide", 4 ); songlist[0][2] = new MySong( "Tom Sawyer", 3 ); songlist[0][3] = new MySong( "Purple Rain", 2 ); songlist[1][0] = new MySong( "Sing a Song", 1 ); songlist[1][1] = new MySong( "Baba O'Riley", 5 ); songlist[1][2] = new MySong( "Jumper", 4 ); songlist[1][3] = new MySong( "Car Wash", 3 ); songlist[2][0] = new MySong( "Kung Fu Fighting", 2 ); songlist[2][1] = new MySong( "Right as Rain", 4 ); songlist[2][2] = new MySong( "Beat It", 5 ); songlist[2][3] = new MySong( "Bust a Move", 4 ); b. Write a tostring() method that will traverse the 2D array songlist and print all songs in the Jukebox using nested for-each loops. Design your tostring() method to print out the songs in the Jukebox in a userfriendly format. c. Write a method randomsong() that randomly picks a song to play. This can be done by using Math.random() to pick random numbers for a row and a column in the Jukebox and prints the name of the song at that location. Make sure that your code picks row/column combinations that are within the bounds of the 2D array. d. Finally, write a method playsongofrating( int rating ) that takes an integer argument and prints only those songs in the Jukebox whose rating is equal to the parameter rating. Page 4
P5F. Time to upgrade your PasswordCreator program and stay one step ahead of the Black Hats. This version should: a. use JOptionPanes to separately ask for the user s last name and proposed password. b. prevent the user from creating a password that contains his/her last name. This will be in addition to the alphanumeric requirement in the first version of your PasswordCreator program. c. continually ask the user to enter a valid proposed password (but not last name) until a valid password is entered. For example, if the user s last name is Smith, then any form of Smith in the proposed password will render the password invalid. For instance, 2smith, SMITH123, smith*321, etc., should all render the password invalid. The password Sm123!axwith should be accepted since the last name Smith has been broken up into two segments, and therefore does constitute the last name Smith in its entirety. Hint: convert both the last name and proposed password to lowercase and perform the necessary comparisons. See the code below: String lastname = new String( "SMIth5" ); String lastnamelowercase = lastname.tolowercase(); System.out.println( lastname ); System.out.println( lastnamelowercase ); // prints SMIth5 // prints smith5 If a successful password has been entered, the program should end (i.e. the program should not ask the user for another password or remain in an endless loop). Below is a summary of the String methods from the AP Computer Science quick reference that might be of help. Page 5
P5G. 2D Guessing Game (optional) Write a simple 2D Guessing Game program that will allow the user to guess the row and column that the number 0 is in. If the row and column guess is correct, the computer will display a You Won! message. If the row and column guess is incorrect, the computer will tell the user how many rows and columns the guess is off by. For example if the number to be guessed is in (0,0) and the user guesses (3, 4), the computer will tell the user he/she is off by 3 rows and 4 columns. If you wish, you may limit the user to a certain number of guesses to increase the difficulty level. Complete the following tasks for your guessing game (feel free to add your own features that maintain the spirit of the game) a. Create a 2D array called gameboard of type int. Use dialog boxes to ask the user how many rows and columns he/she wants for the game. b. Use a for loop to fill the 2D array with random numbers between 1 and 100. c. Use Math.random() to pick a random row and random column to place the number 0 in (this will be the number that the user must attempt to find). Hint: call Math.random() twice to generate two random numbers one for the row and one for the column. Be sure not to pick rows and/or columns that are out of bounds! d. Print the 2D array in rectangular format using an X to represent each number in the 2D array, effectively hiding the numbers in the 2D array. e. Use a dialog box to ask the user for a row and column to use as their guess to find the number 0. f. If the guess is correct, reply with a message confirming the win! (e.g. BAM! You found the number 0!) g. If the guess is incorrect, replay with a message telling the user how many rows and how many columns he/she is off by (see the example above). h. Reprint the array, placing a N for No Bam! in place of the X to show that this location has already been picked. i. Repeat steps (e) through (h) until the number 0 is found or until all locations have been checked (unless you choose the limit the number of guesses). Below are some features that might be useful for this program public static void main( String[] args ) throws InterruptedException { Thread.sleep( 1000 ); System.out.println( \f ); } // end method main // delays program // need throws InterruptedException // after public static void main // clears the screen Page 6
P5H. 2D Image Processing (coming soon) P5I. QR Codes (coming soon) By the end of the lesson students should be able to: a. Write the Java code to create and populate a 2D Array. b. Write nested for and for-each loops to populate and process 2D Arrays. Page 7