CS 106A, Lecture 27 Final Exam Review 1

Similar documents
CS 106A, Lecture 27 Final Exam Review 1

CS 106A, Lecture 14 Events and Instance Variables

CS 106A, Lecture 14 Events and Instance Variables

CS106A Review Session

CS 106A, Lecture 23 Interactors and GCanvas

Memory Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

CS 106A, Lecture 19 ArrayLists

CS 106A, Lecture 16 Arrays

CS 106A, Lecture 11 Graphics

CS 106A, Lecture 25 Life After CS 106A, Part 1

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

Primitives, Objects, and Heap/Stack Review 1 Chris Piech CS106A, Stanford University

CS 106A, Lecture 24 Interactors and NameSurfer

CS 106A, Lecture 5 Booleans and Control Flow

CS 106A, Lecture 20 ArrayLists and HashMaps

CS106A Final Exam Review Session. Saturday Dec. 10, 2016 Nick Troccoli

CS 106A, Lecture 9 Problem-Solving with Strings

CS 106A, Lecture 9 Problem-Solving with Strings

Introduction to Computer Science I

CS 106A, Lecture 3 Problem-solving with Karel

Homework Assignment 2: Java Console and Graphics

CS 106B, Lecture 1 Introduction to C++

CS106A Review Session. Monday Oct. 31, 2016 Nick Troccoli

CS 106A, Lecture 7 Parameters and Return

Queues and Unit Testing

CS 106A, Lecture 3 Problem-solving with Karel

Computational Expression

APCS Semester #1 Final Exam Practice Problems

Simple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada

Drawing Geometrical Objects. Graphic courtesy of Eric Roberts

Practice Midterm Examination

Building Java Programs

Test 6. moodle.yorku.ca CSE 1020

CSSE 220 Day 8. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysAndLists from SVN

Expressions, Statements, and Control Structures

Parameters and Objects

Practice Midterm Examination

CS 170 Java Programming 1. Week 7: More on Logic

CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists

CS106AJ Midterm Review Session. October 28, 2017 Kat Gregory and Ryan Eberhardt

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

COMP-202: Foundations of Programming. Lecture 26: Image Manipulation; Wrap-Up Jackie Cheung, Winter 2015

Array Based Lists. Collections

Nested Loops Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

CS 106X, Lecture 10 Recursive Backtracking

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Flowcharts [15 points]

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

Introduction to Programming Using Java (98-388)

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS 106X Lecture 26: Inheritance and Polymorphism in C++

Building Java Programs

Objects Revisited. An object is a combination of. State persistent information, and Behavior the ability to operate on that state.

Programming Lecture 4

CSE 143 Lecture 4. Preconditions

Assignment #2: Intro to Java Due: 11AM PST on Wednesday, July 12

CS 106X, Lecture 14 Classes and Pointers

Practice Midterm Examination #1

CSE 143 Lecture 4. ArrayList. Reading: slides created by Marty Stepp

CS 106X, Lecture 16 More Linked Lists

Intensive Introduction to Computer Science. Course Overview Programming in Scratch

CSSE 220. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysListPractice from SVN

CS 106X, Lecture 23 Dijkstra and A* Search

CS 170 Java Programming 1. Week 10: Loops and Arrays

Values and Variables 1 / 30

CS61B Lecture #35. [The Lecture #32 notes covered lectures #33 and #34.] Today: Enumerated types, backtracking searches, game trees.

An Interesting Article. How Revolutionary Tools Cracked a 1700s Code.

Due: Friday, October 16 th at 5:00pm. Pair programming is permitted on this assignment. See course information sheet and honor code.

Programming Lecture 4

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

Mehran Sahami Handout #7 CS 106A September 24, 2014

Due: Wednesday, August 31 st at 11:59pm. Pair programming is permitted on this assignment. See course information sheet and honor code.

Lecture 13: Two- Dimensional Arrays

CS 302: Introduction to Programming in Java. Lecture 15

Assignment #4 Hangman Due: 11am on Monday, May 14th This assignment may be done in pairs (which is optional, not required)

AP Computer Science in Java Course Syllabus

RECURSION (CONTINUED)

List ADT. Announcements. The List interface. Implementing the List ADT

Assignment 6 YEAH Hours. Ben Barnett and Avery Wang

Practice Final Examination #2

Arrays Chris Piech CS106A, Stanford University

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

Events Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Variables, Types, and Expressions

Assignment #1: and Karel the Robot Karel problems due: 3:15pm on Friday, October 4th due: 11:59pm on Sunday, October 6th

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Assignment #4 Hangman Due: 10:30am on Friday, Febuary 17th This assignment may be done in pairs (which is optional, not required)

Assignment #4 Hangman Due: 3:15pm on Monday, November 3rd Your Early Assignment Help (YEAH) hours: 4:15pm-5:15pm, Friday, October 24th in Hewlett 200

Winter 2017 Feb 13 th, 2017 CS106A Midterm. Last Name: First Name: Sunet ID (eg jdoe): Section Leader / Grader:

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Practice Final Examination

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

Eric Roberts Handout #27 CSCI 121 October 3, Project #2 Snowman. This assignment is designed to build your skills in the following areas:

Assignment #3 Breakout!

Objects and Graphics

Garbage Collection (1)

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Building Java Programs

Advanced if/else & Cumulative Sum

Transcription:

CS 106A, Lecture 27 Final Exam Review 1 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others.

Plan for today Announcements/Exam logistics Learning Goals Graphics, Animation, Events Arrays ArrayLists 2

Plan for today Announcements/Exam logistics Learning Goals Graphics, Animation, Events Arrays ArrayLists 3

Final exam Is the final exam cumulative? What will be tested on the final exam? What about all this stuff you aren t covering today? Expressions and Variables Java Control Statements Console Programs Methods, parameters, returns Randomness Strings and chars Scanners and file processing Memory Is the final exam going to be difficult/curved? How can I practice for the final? Midterm review session was the recorded section on Friday of Week 4 4

Practicing for the final Review concepts you re unsure of Review programs we wrote in lecture Do section problems Do practice final under real conditions codestepbystep.com Colin s secret test-taking strategy: Using BlueBook s timer, give yourself 3-5 minutes to read and start writing pseudocode for each problem Once you ve thought about every problem, go back to the one that seemed easiest and start coding for real This is not about finishing every problem; it is about collecting points 5

Plan for today Announcements/Exam logistics Learning Goals Graphics, Animation, Events Arrays ArrayLists 6

Learning Goals After this lecture, I want you to be able to Lectures 1-3 (Karel): Apply programmatic thinking and decomposition to logical tasks Lecture 4 (Intro to Java): Create variables of primitive types, perform console I/O, and evaluate expressions using primitive types Lecture 5 (Booleans and Control Flow): Use loops to perform repeated tasks, use conditions to decide which tasks to perform 7

Learning Goals After this lecture, I want you to be able to Lecture 6 (Scope): Identify a variable s scope Lecture 7 (Parameters and Return): Write functions that pass parameters and leverage return values to overcome the limitation of scope in program decomposition 8

Learning Goals After this lecture, I want you to be able to Lecture 8 (Characters and Strings): Use randomness to write interesting programs, recall that Java understands chars as ASCII values (ints from 0-255), create String variables, recall that Strings are immutable 9

Learning Goals After this lecture, I want you to be able to Lecture 9 (Problem-Solving with Strings): Identify situations where common String methods like length and substring are useful, solve problems that involve manipulating Strings (often through creating new Strings) Lecture 10 (File Reading): Write programs that use files as sources of input data 10

Learning Goals After this lecture, I want you to be able to Lectures 11 and 12 (Graphics): Write programs using five types of graphical objects (rectangles, ovals, lines, labels, and images), call methods on Objects Lecture 13 (Animation): Use loops and pausing to animate graphical programs 11

Learning Goals After this lecture, I want you to be able to Lectures 14 (Events): Write programs that respond to mouse events, identify when it is appropriate to use instance variables Lecture 15 (Memory): Recall that primitives are passed by value while Objects are passed by reference in Java, apply that knowledge to know which variables values change when they are modified in other methods 12

Learning Goals After this lecture, I want you to be able to Lectures 16 (Arrays): Describe the purpose of data structures in programming, know how to store data in and retrieve data from arrays Lecture 17 (2D Arrays): Recognize 2D arrays as grids or arrays of arrays, apply nested for loops to working with 2D arrays Lecture 18 (More Arrays): Identify uses for arrays in writing complex programs 13

Learning Goals After this lecture, I want you to be able to Lectures 19 (ArrayLists): Know how to store data in and retrieve data from ArrayLists Lecture 20 (ArrayLists and HashMaps): Know how to store data in and retrieve data from HashMaps, identify the most appropriate data structure between arrays, ArrayLists, and HashMaps for different storage needs 14

Learning Goals After this lecture, I want you to be able to Lectures 23 (Interactors and GCanvas): Know how to create graphical user interfaces (GUIs) with Java s interactive components Lecture 24 (GCanvas): Write richer graphical programs leveraging multiple classes Lectures 24-26 (BiasBars, Life After CS106A): Identify real-world challenges where 106Alevel programming knowledge can help 15

Learning Goals Assignments gave you practice synthesizing lots of different topics from lecture Exams assess the extent to which you are able to recall and synthesize learning goals Because exams are high-pressure, timed situations, you don t need to score spectacularly for me to believe that you understand the course s material 16

Plan for today Announcements/Exam logistics Learning Goals Graphics, Animation, Events Arrays ArrayLists 17

Graphics Look at lecture slides for lists of different GObject types and their methods Remember: the x and y of GRect, GOval, etc. is their upper-left corner 18

Animation Standard format for animation code: while (condition) { update graphics pause(pause_time); } 19

Events Two ways for Java to run your code: from run() and from event handlers (mouseclicked, mousemoved, actionperformed, etc.) Event handlers must have exactly the specified signature; otherwise they won t work! e.g., public void mouseclicked(mouseevent e) If you need access to a variable in an event handler that you use elsewhere in your code, it should be an instance variable (e.g., paddle in Breakout) 20

Plan for today Announcements/Exam logistics Learning Goals Graphics, Animation, Events Arrays ArrayLists 21

1D Arrays An array is a fixed-length list of a single type of thing. An array can store primitives and Objects. You cannot call methods on arrays, e.g., no myarray.contains() Get the length by saying myarray.length. (No parentheses!) Print array with Arrays.toString(myArray), not println(myarray)! [2, 4, 6, 8] [I@4ddced80 22

1D Array Practice Write the method int longestsortedsequence(int[] array) e.g. int[] array = {3, 8, 10, 1, 9, 14, -3, 0, 14, 207, 56, 98, 12} 3 3 4 2 1 Sorted in this case means nondecreasing, so a sequence could contain duplicates: e.g. int[] array = {17, 42, 3, 5, 5, 5, 8, 2, 4, 6, 1, 19} 2 5 3 2 Link: http://www.codestepbystep.com/problem/view/java/arrays/longestsortedsequence 23

2D Arrays = Arrays of Arrays! int[][] a = new int[3][4]; Outer array a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] 24

Chess Knight: moves in an L -shape (two steps in one direction, one step in a perpendicular direction) 25

knightcanmove() boolean knightcanmove(string[][] board, int startrow, int startcol, int endrow, int endcol) (startrow, startcol) must contain a knight (endrow, endcol) must be empty (endrow, endcol) must be reachable from (startrow, startcol) in a single move Assume that (startrow, startcol) and (endrow, endcol) are within bounds of array 26

knightcanmove() 0 1 2 3 4 5 6 7 0 king 1 knight 2 3 rook 4 5 6 7 27

knightcanmove() knightcanmove(board, 2, 2, 3, 4) returns false 0 1 2 3 4 5 6 7 0 king 1 knight 2 3 rook No knight at (2, 2) 4 5 6 7 28

knightcanmove() knightcanmove(board, 1, 2, 0, 4) returns false 0 1 2 3 4 5 6 7 0 king 1 knight Space occupied 2 3 rook 4 5 6 7 29

knightcanmove() knightcanmove(board, 1, 2, 3, 2) returns false 0 1 2 3 4 5 6 7 0 king 1 knight 2 (1, 2) to (3, 2) is not a valid move 3 rook 4 5 6 7 30

knightcanmove() knightcanmove(board, 1, 2, 3, 3) returns true 0 1 2 3 4 5 6 7 0 king 1 knight 2 3 rook 4 Knight is at (1, 2) and (3, 3) is empty and (1, 2) -> (3, 3) is a valid move 5 6 7 31

knightcanmove() // This method returns true if the starting square contains a knight, // the end square is empty, and the knight can legally move from the // start square to the end square. private boolean knightcanmove(string[][] board, int startrow, int startcol, int endrow, int endcol) { } 32

knightcanmove() // This method returns true if the starting square contains a knight, // the end square is empty, and the knight can legally move from the // start square to the end square. private boolean knightcanmove(string[][] board, int startrow, int startcol, int endrow, int endcol) { if (board[startrow][startcol].equals("knight")) { } } 33

knightcanmove() // This method returns true if the starting square contains a knight, // the end square is empty, and the knight can legally move from the // start square to the end square. private boolean knightcanmove(string[][] board, int startrow, int startcol, int endrow, int endcol) { if (board[startrow][startcol].equals("knight")) { if (board[endrow][endcol].equals("")) { } } } 34

knightcanmove() // This method returns true if the starting square contains a knight, // the end square is empty, and the knight can legally move from the // start square to the end square. private boolean knightcanmove(string[][] board, int startrow, int startcol, int endrow, int endcol) { if (board[startrow][startcol].equals("knight")) { if (board[endrow][endcol].equals("")) { int rowdifference = Math.abs(startRow - endrow); int coldifference = Math.abs(startCol - endcol); if ((rowdifference == 1 && coldifference == 2) (rowdifference == 2 && coldifference == 1)) { return true; } } } } 35

knightcanmove() // This method returns true if the starting square contains a knight, // the end square is empty, and the knight can legally move from the // start square to the end square. private boolean knightcanmove(string[][] board, int startrow, int startcol, int endrow, int endcol) { if (board[startrow][startcol].equals("knight")) { if (board[endrow][endcol].equals("")) { int rowdifference = Math.abs(startRow - endrow); int coldifference = Math.abs(startCol - endcol); if ((rowdifference == 1 && coldifference == 2) (rowdifference == 2 && coldifference == 1)) { return true; } } } return false; } 36

Plan for today Announcements/Exam logistics Learning Goals Graphics, Animation, Events Arrays ArrayLists 37

ArrayList An ArrayList is a flexible-length list of a single type of thing. An ArrayList can only store Objects. For primitives, use ArrayList<Integer> instead of ArrayList<int>. (Integer is a wrapper class for int) Other wrapper classes: Double instead of double, Character instead of char, Boolean instead of boolean. An ArrayList has a variety of methods you can use like.contains,.get,.add,.remove,.size, etc. 38

Array vs ArrayList Array Fixed size Efficient (not a concern in this class) No methods, can only use myarray.length (no parentheses!) Can store any object or primitive ArrayList Expandable Less efficient than Array (not a concern in this class) Convenient methods like.add(),.remove(),.contains() Cannot store primitives, so use their wrapper classes instead 39

deleteduplicates() private void deleteduplicates(arraylist<string> list) Guaranteed that list is in sorted order {"be", "be", "is", "not", "or", "question", "that", "the", "to", "to"} becomes { be, is, not, or, question, that, the, to } Solution strategy: Loop through ArrayList Compare pairs of elements If element.equals(nextelement), remove element from the list 40

deleteduplicates Loop through ArrayList Compare pairs of elements If element.equals(nextelement), remove element from the list 41

deleteduplicatesreverse Loop through ArrayList in reverse Compare pairs of elements If element.equals(previouselement), remove element from the list 42

Recap Announcements/Exam logistics Learning Goals Graphics, Animation, Events Arrays ArrayLists Next time: Final Exam Review 2 43