AP Computer Science Homework Set 5 2D Arrays

Similar documents
J a v a Quiz (Unit 3, Test 0 Practice)

CS170 Lab 11 Abstract Data Types & Objects

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

AP Computer Science Java Mr. Clausen Program 9A, 9B

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

The resulting tile cannot merge with another tile again in the same move. When a 2048 tile is created, the player wins.

Conditionals (with solutions)

Iteration CHAPTER 6. Topic Summary

Welcome to the HVAC Sales Academy Learning Management System (LMS) Walkthrough:

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Java CPD (I) Frans Coenen Department of Computer Science

AP Computer Science Java Subset

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Lowercase Letters Capital Letters Picture Cards

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Session 7 Fractions and Decimals

LOOPS CHAPTER CHAPTER GOALS

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

WHAT S NEW IN MS EXCEL 2013

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Java Program Coding Standards Programming for Information Technology

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Visual Logic Instructions and Assignments

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

Introduction to Python

Accounts Payable Expense Distribution Tables

Math 55: Discrete Mathematics

Colored Hats and Logic Puzzles

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

Arrays in Java. data in bulk

Building Java Programs

Building Java Programs

Performance Improvement In Java Application

Chapter 2 Introduction to Java programming

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

TeachingEnglish Lesson plans

20 CODE CHALLENGES. GCSE (9 1) Computer Science GCSE REFORM. February 2015

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

In this Chapter you ll learn:

PROBABILITY. SIMPLE PROBABILITY is the likelihood that a specific event will occur, represented by a number between 0 and 1.

Third Grade Math Games

Programming Languages CIS 443

Pseudo code Tutorial and Exercises Teacher s Version

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Using Files as Input/Output in Java 5.0 Applications

Python. KS3 Programming Workbook. Name. ICT Teacher Form. Do you speak Parseltongue?

Understand numbers, ways of representing numbers, relationships among numbers, and number systems

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

CmpSci 187: Programming with Data Structures Spring 2015

Term Project: Roulette

Building Java Programs

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Five daily lessons. Page 8 Page 8. Page 12. Year 2

Statoil esourcing portal. Supplier user guide February 2015

Install Java Development Kit (JDK) 1.8

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix:

Topic 11 Scanner object, conditional execution

Year 2 Summer Term Oral and Mental Starter Activity Bank

Project 2: Bejeweled

Question: What is the probability that a five-card poker hand contains a flush, that is, five cards of the same suit?

Homework/Program #5 Solutions

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Moving from CS 61A Scheme to CS 61B Java

There are a number of superb online resources as well that provide excellent blackjack information as well. We recommend the following web sites:

Minnesota Virtual Academy Online Syllabus for AP Computer Science A

Microsoft Excel Introduction to Microsoft Excel 2007

Part 3: GridWorld Classes and Interfaces

3 length + 23 size = 2

COMPUTER SCIENCE. Paper 1 (THEORY)

GENEVA COLLEGE INFORMATION TECHNOLOGY SERVICES. Password POLICY

Java Interview Questions and Answers

Bus Rider Activities

Q&As: Microsoft Excel 2013: Chapter 2

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Definition and Calculus of Probability

Building Java Programs

SEC External Guide for Using the Encryption Solution

Contents. 9-1 Copyright (c) N. Afshartous

Web Intelligence User Guide

Cyber Security Workshop Encryption Reference Manual

AP Computer Science Static Methods, Strings, User Input

New York University Computer Science Department Courant Institute of Mathematical Sciences

How to test and debug an ASP.NET application

An Australian Microsoft Partners in Learning (PiL) Project

Cambridge English: First (FCE) Writing Part 1

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

What's New in ADP Reporting?

Programming Fundamentals I CS 110, Central Washington University. November 2015

CS 106 Introduction to Computer Science I

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

10 Java API, Exceptions, and Collections

Java Web Services SDK

Chapter 8 Implementing FSP Models in Java

Gaddesden Row JMI School Year 1 January-February Look at what we are doing!

Sample CSE8A midterm Multiple Choice (circle one)

Transcription:

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