AP Computer Science Homework Set 5 2D Arrays

Similar documents
AP Computer Science Homework Set 5 2D Arrays

AP Computer Science Homework Set 3 Class Methods

AP Computer Science Homework Set 2 Class Design

AP Computer Science Homework Set 1 Fundamentals

Homework Set 1- Fundamentals

AP Computer Science Homework Set 2 Class Design

AP Computer Science Homework Set 1 Fundamentals

Homework Set 2- Class Design

AP Computer Science Homework Set 3 Class Methods

RANDOM NUMBER GAME PROJECT

CS 113 MIDTERM EXAM 2 SPRING 2013

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CMPSCI 187 / Spring 2015 Hangman

Set<Integer> s = new TreeSet<Integer>(); s.add( 7 ); s.add( 7 ); System.out.println( s.size() );

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

CSCI 1301: Introduction to Computing and Programming Spring 2018 Project 3: Hangman 2.0 (A Word Guessing Game)

St. Edmund Preparatory High School Brooklyn, NY

Evil Hangman Project

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

In this lab, you will learn more about selection statements. You will get familiar to

Instance Method Development Demo

Lab 4: Strings/Loops Due Apr 22 at midnight

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Registration and Login

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Object Oriented Programming 2013/14. Final Exam June 20, 2014

CS 351 Design of Large Programs Coding Standards

AP Computer Science Final Project - Indexing and the Word-Location Trick Indexing Algorithms

CSIS 10B Lab 2 Bags and Stacks

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS 251 Intermediate Programming Coding Standards

Programming - 2. Common Errors

Model Solutions. COMP 102: Test 1. 6 April, 2016

CS-140 Fall 2018 Test 2 Version A Nov. 12, Name:

Lecture 5: Methods CS2301

Chapter 1 Introduction to Java

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

More on variables and methods

Array. Prepared By - Rifat Shahriyar

Midterm 1 Study Guide

APCS Semester #1 Final Exam Practice Problems

CS100M November 30, 2000

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE

Computer Science II CSci 1200 Test 2 Overview and Practice

Web Client Instructions Simplifying the Process

CS2 Practical 1 CS2A 22/09/2004

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

CO Java SE 8: Fundamentals

Guessing Game with Objects

CS3901 Intermediate Programming and Data Structures Winter 2008 (Q2 AY 08) Assignment #5. Due: noon Tuesday, 3 March 2008

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

static String usersname; public static int numberofplayers; private static double velocity, time;

MCS Workstation Software Installation Guide

TeenCoder : Java Programming (ISBN )

Basics of Information Worksheet

FORGOT PASSWORD INSTRUCTIONS

Comp Assignment 2: Object-Oriented Scanning for Numbers, Words, and Quoted Strings

EXPERIMENTS IN COMPUTER SCIENCE

This project is worth 600 points Due on April 17 Pick one from the following project to create complete running program with documentation

Lesson 10: Quiz #1 and Getting User Input (W03D2)

CONCORDIA UNIVERSITY Summer 2005 Comp 248 /1 Section AA Introduction to Programming Final Examination/A

Math for Liberal Studies

CSE 114 Midterm 1. Please leave one seat between yourself and each of your neighbors.

CMPSCI 187: Programming With Data Structures. Review for First Midterm 9 October 2011

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

CS 134 Programming Exercise 2:

Programming Assignment Unit 7

Back public class HelloWorld { public static void main ( String arg[] ) { Front Basic Setup. Java Quick Sheet. ~ 35 Flashcards. 1 AP CS - Rodriguez

Discrete Structures Lecture The Basics of Counting

Assignment 2.4: Loops

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

CS 113 PRACTICE FINAL

AP Computer Science Unit 3. Programs

Introduction to Java Applications

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

Programming Problems 15th Annual Computer Science Programming Contest

Instructions. Objectives:

Fundamentals of Programming Session 4

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

AP Computer Science in Java Course Syllabus

Topics. Chapter 5. Equality Operators

Practice Midterm 1. Problem Points Score TOTAL 50

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

Subject: Computer Science

LAB 13: ARRAYS (ONE DIMINSION)

CSE 143 Au03 Final Exam Page 1 of 15

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CS121/IS223. Object Reference Variables. Dr Olly Gotel

The little book of programming challenges

ECE15: Homework 10. void wordstats(file *fp, char string[]) { void printfrequencies(file *fp) {

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. Use a nested for loop to populate the 2D array 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. ** Note: the strategic use of tabs and new lines 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: **HINT: You will need 3 sets of loops to fill your 2-D array. -One set of loops is to fill row 0. -One set of loops is to fill column 0. -The third set of loops is to fill the rest. 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 that will have one instance variable, which is a 2D array of MySong objects. Before you can write Jukebox, you will need to write a MySong class in the same project. The MySong class should have 1) 2 instance variables for the song s title and rating 2) 2- argument constructor for the song s title and the rating 3) A getter method that returns the rating of the MySong object 4) A tostring() that prints out MySong objects in a user-friendly format Then, write your Jukebox class: a. Write an instance variable that is a 3x4 2D array of MySong objects called songlist. b. 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 ); c. 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. d. 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. e. 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

EXTRA CREDIT PROGRAMS (10 points each) P5F. Time to upgrade your PasswordCreator program (P3I). 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 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

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