# Lab 8 Mini-Assignment :::danger **Due Date:** Before your section (Wednesday-Thursday November 6-7) ::: ## Helpful Resources - Lecture slides: [Arrays](https://cs.brown.edu/courses/cs015/lecture/pdf/CS15.Lecture_13_Arrays_and_ArrayLists.10.17.24.pdf) - [Arrays pre-lab video](https://www.youtube.com/watch?v=3Ty0akxaJMM) - [Class & Interface/Inheritance Diagram Guide](https://docs.google.com/document/d/1LZV1M75gozJuU7jouTmusjw2B4fjLPOCSh9feQ3sDXM/edit#heading=h.n64vzvb7650b) - [Tetris Help Slides](https://docs.google.com/presentation/d/12IWPbhLZo3qtgmvpPfmlpns1y4L9ybpfAlsphi4HJsM/edit?usp=sharing) - [Tetris Handout](https://hackmd.io/@Fall2024-CS15/rJWRrH0i0) ### Problem 1: DoodleJump Reflection 1. Find a method in your DoodleJump project that could be cleaner. Look for unnecessary variables, repeated lines of code (where you could’ve used a helper method), or messy code. Copy the code for that method and add a few bullet points of why this method could be cleaner. 2. Find a method in your DoodleJump project that you think has good style. Look for helper methods, clear variable names, or clear commenting. Copy the code for that method and add a few bullet points of why this method has good style. ### Problem 2: Tetris Design Practice 1. Create a simple class diagram for your program. All classes used in your proposed design should be modeled in this diagram, **including JavaFX classes**. Think about the purpose of each class as you design your program and be prepared to discuss it. 2. What data structure will you use to represent the board? What type of object will this data structure hold? 3. What data structure will you use to represent the piece? What type of object will this data structure hold? ### Problem 3: Incremental Coding for Tetris Write out a plan for coding Tetris incrementally. Tetris is a large project, and should be coded in small, manageable steps. See earlier handouts ([FruitNinja](https://hackmd.io/@Fall2024-CS15/rycLVBCjR), [Cartoon](https://hackmd.io/@Fall2024-CS15/S1otNrRoA), [DoodleJump](https://hackmd.io/@Fall2024-CS15/SkNDBHAs0)) for examples of a plan for coding incrementally. ### Problem 4: Big-O Runtime Consider the following snippets of code and analyze what their Big-O runtimes are. **1.** Consider the following code that finds the maximum number in an array of integers. Given that the <span style="font-family: 'Consolas', monospace;">**`nums`**</span> array has a length of <span style="font-family: 'Consolas', monospace;">**`N`**</span>, and that <span style="font-family: 'Consolas', monospace;">**`N >= 1`**</span>, what would be the Big-O runtime of the <span style="font-family: 'Consolas', monospace;">**`findMax`**</span> method? <span style="font-family: 'Consolas', monospace;">public int findMax(int[] nums) { &nbsp;&nbsp;&nbsp;int max = nums[0]; &nbsp;&nbsp;&nbsp;for (int num : nums) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (num > max) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max = num; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;return max }</span> **2.** Consider the following code that finds the maximum of the first ten numbers in an array of integers. Given that the <span style="font-family: 'Consolas', monospace;">**`nums`**</span> array has a length of **`N`** and that <span style="font-family: 'Consolas', monospace;">**`N >= 10`**</span>, what would be the Big-O runtime of the <span style="font-family: 'Consolas', monospace;">**`maxOfFirstTen`**</span> method? <span style="font-family: 'Consolas', monospace;">public int maxOfFirstTen(int[] nums) { &nbsp;&nbsp;&nbsp;int max = nums[0]; &nbsp;&nbsp;&nbsp;for (int i = 0; i < 10; i ++) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (nums[i] > max) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max = num; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;return max }</span> **3.** Consider the following code that determines if there is any duplicate number in an array of integers. Given that the <span style="font-family: 'Consolas', monospace;">**`nums`**</span> array has a length of **`N`**, what would be the Big-O runtime of the naive <span style="font-family: 'Consolas', monospace;">**`hasDuplicate`**</span> method? <span style="font-family: 'Consolas', monospace;">public boolean hasDuplicate(int [] nums) { &nbsp;&nbsp;&nbsp;for (int i = 0; i < nums.length; i++) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 0; j < 10; j ++) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (i != j && nums[i] == nums[j]) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;return false; }</span> **4.** Consider the following code that determines if there is any duplicate number between two arrays of integers. Given that the <span style="font-family: 'Consolas', monospace;">**`numsA`**</span> array has a length of <span style="font-family: 'Consolas', monospace;">**`N`**</span> and the <span style="font-family: 'Consolas', monospace;">**numsB**</span> array has a length of <span style="font-family: 'Consolas', monospace;">**`M`**</span>, what would be the Big-O runtime of this naive <span style="font-family: 'Consolas', monospace;">**`hasDuplicate`**</span> method? <span style="font-family: 'Consolas', monospace;">public boolean hasDuplicate(int [] numsA, int[] numsB) { &nbsp;&nbsp;&nbsp;for (int numA : numsA) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int numB : numsB) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (numA == numB) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;return false; }</span> ### Problem 5: Tetris Algorithms 1. An important logical step in this program is clearing completed lines off the board. Write pseudocode for how you will check for and clear completed rows. This involves both “graphical clearing” i.e. hiding the cleared squares from the screen and moving the rows above down as well as “logical clearing” i.e updating the board to know about the new arrangement of squares. The steps you write should clearly outline all cases you would cover to properly clear lines. 2. Another important part of this project is checking move validity. As the current piece is moving/falling - you will want to make sure it does not fall off the edge of the board or onto an already fallen piece. Think about how you would keep track of and check for move validity? *(It will be useful to consider the tradeoffs of whether the board, the piece, or the individual components of the piece should keep track of this.)* ## To Handin: Submit your answers to [this form](https://docs.google.com/forms/d/e/1FAIpQLScGq4t3qP2LgEFYsb7UX0qN5IjJ4L79ZQXQxAouNAJo1sRkBA/viewform) before your due date. Any handins after the due date will not be accepted.