12.07.2015 Views

Homework (.pdf) - thecubscientist.com

Homework (.pdf) - thecubscientist.com

Homework (.pdf) - thecubscientist.com

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

AP Computer Science<strong>Homework</strong> Set 52D ArraysNote: all programs described below should work if the size of the 2D array ischanged.P5A. Create a 3x4 2D array of integers and fill it with random numbers between 0and 9. Use a nested for loop to print the array in a “rectangular” format. Finally, printthe row, column pair of coordinates of all instances of the number “5”. Use nested forloops to “populate the 2D array” and print out its contents. The strategic use of ifstatements 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 <strong>com</strong>plete the following<strong>com</strong>putations: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 beingprinted.Page 1AP Computer Science – HW Set 5 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


P5C. Got Multiplication Tables? Write a program that will create and print amultiplication table using a 2D array. The program should be able to accept anynumber of rows and/or columns and correctly generate the multiplication table. Rowand column numbers should be displayed along the top and left side of themultiplication table. See the example below:Page 2AP Computer Science – HW Set 5 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


P5D. Write a program that will fill a 2D array with the letters of the alphabet “a-z” inrow 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 arrayare filled. Print your array after using nested for loops to test your result. Theprogram should work for any size 2D array.An optional challenge…Add a “space” between every letter that is printed AND the ability to choose fromlowercase letters (a-z), capital letters (A-Z), or digits 0-9. Prompt the user for thenumber of rows, columns, and types of characters to print (lowercase, uppercase, ordigits.Example output is shown below:Page 3AP Computer Science – HW Set 5 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


P5E. Create a class called “Jukebox”. A “Jukebox” will consist of a 2D array ofMySong objects called songList. Write a program to perform the following tasks:a. Write a zero-argument constructor to fill the jukebox with the followingMySong objects and ratings or fill with your own songs. You can cut and pastethe 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 andprint all songs in the “Jukebox” using nested for-each loops. Design yourtoString() method to print out the songs in the “Jukebox” in a userfriendlyformat.c. Write a method randomSong() that randomly picks a song to play. This canbe done by using Math.random() to pick random numbers for a row and acolumn in the “Jukebox” and prints the name of the song at that location.Make sure that your code picks row/column <strong>com</strong>binations that are within thebounds of the 2D array.d. Finally, write a method playSongofRating( int rating ) that takesan integer argument and prints only those songs in the “Jukebox” whoserating is equal to the parameter rating.Page 4AP Computer Science – HW Set 5 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


P5F. Time to upgrade your “PasswordCreator” program and stay one stepahead of the “Black Hats.” This version should:a. use JOptionPanes to separately ask for the user’s last name and proposedpassword.b. prevent the user from creating a password that contains his/her last name. Thiswill 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 theproposed 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 thenecessary <strong>com</strong>parisons. 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 programshould 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 quickreference that might be of help.Page 5AP Computer Science – HW Set 5 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


P5G. 2D Guessing Game (optional)Write a simple 2D Guessing Game program that will allow the user to guess the rowand column that the number “0” is in. If the row and column guess is correct, the<strong>com</strong>puter will display a “You Won!” message. If the row and column guess isincorrect, the <strong>com</strong>puter will tell the user how many rows and columns the guess is offby. For example if the number to be guessed is in (0,0) and the user guesses (3, 4), the<strong>com</strong>puter will tell the user he/she is off by 3 rows and 4 columns. If you wish, youmay 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 ownfeatures that maintain the spirit of the game)a. Create a 2D array called gameBoard of type int. Use dialog boxes to askthe 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 and100.c. Use Math.random() to pick a random row and random column to placethe number “0” in (this will be the number that the user must attempt tofind). Hint: call Math.random() twice to generate two randomnumbers…one for the row and one for the column. Be sure not to pick rowsand/or columns that are out of bounds!d. Print the 2D array in rectangular format using an “X” to represent eachnumber 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 guessto 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 manyrows 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 showthat this location has already been picked.i. Repeat steps (e) through (h) until the number “0” is found or until alllocations have been checked (unless you choose the limit the number ofguesses).Below are some features that might be useful for this programpublic 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 screenPage 6AP Computer Science – HW Set 5 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


P5H. 2D Image Processing (<strong>com</strong>ing soon)P5I. QR Codes (<strong>com</strong>ing 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 7AP Computer Science – HW Set 5 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!