Two-Dimensional Arrays

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
ArrayList, Multidimensional Arrays
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Review of Recursion What is a Recursive Method?
More on Arrays Review of Arrays of ints, doubles, chars
CMSC 202 ArrayList Aug 9, 2007.
Chapter VII: Arrays.
Information and Computer Sciences University of Hawaii, Manoa
Topic: Classes and Objects
The need for Programming Languages
Sixth Lecture ArrayList Abstract Class and Interface
Programming with ANSI C ++
Arrays.
Array, Strings and Vectors
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 3: Using Methods, Classes, and Objects
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
JavaScript: Functions.
Programmazione I a.a. 2017/2018.
public class StrangeObject { String name; StrangeObject other; }
Pointers and Dynamic Variables
Methods and Parameters
String Objects & its Methods
Peer Instruction 6 Java Arrays.
Chapter 6 Methods: A Deeper Look
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CS 302 Week 8 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
CS 200 Objects and ArrayList
CMSC 202 ArrayList Aug 9, 2007.
CS2013 Lecture 4 John Hurley Cal State LA.
Can store many of the same kind of data together
Dynamic Data Structures and Generics
Object Oriented Programming in java
Variables Title slide variables.
Review of Recursion What is a Recursive Method?
CMSC 202 ArrayList Aug 9, 2007.
Unit 3 Test: Friday.
slides created by Ethan Apter
ArrayLists 22-Feb-19.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
7 Arrays.
Dynamic Data Structures and Generics
Introduction to Data Structure
Data Structures & Algorithms
slides created by Alyssa Harding
CS2013 Lecture 7 John Hurley Cal State LA.
Peer Instruction 4 Control Loops.
Review: libraries and packages
Just Enough Java 17-May-19.
CS 200 Objects and ArrayList
Classes and Objects Object Creation
Arrays.
Review of Recursion What is a Recursive Method?
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Two-Dimensional Arrays Peer Instruction 7 Two-Dimensional Arrays

Declaration and allocation of two-dimensional (2D) arrays. Which statements correctly declare and allocate a 2D integer array with 2 rows and 4 columns? int iArray[2][4]; int iArray = new int[2][4]; int iArray[][] = new int[4][2]; int iArray[][] = new int[2][4]; int iArray[][] = { { 1, 2, 3, 4 }, { 4, 5, 6, 7 } }; 2) and 4) 3) and 5) 1) and 5) 4) and 5) 2) and 5) 4) and 5) are correct, 1) does not allocate, 2) is not an array, 3) is backwards, so the answer is E) 2D Array Declaration cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

Declaration and allocation of two-dimensional (2D) arrays. Which statements correctly declare and allocate a 2D integer array with 2 rows and 4 columns? int iArray[2][4]; int iArray = new int[2][4]; int iArray[][] = new int[4][2]; int iArray[][] = new int[2][4]; int iArray[][] = { { 1, 2, 3, 4 }, { 4, 5, 6, 7 } }; 2) and 4) 3) and 5) 1) and 5) 4) and 5) 2) and 5) 4) and 5) are correct, 1) does not allocate, 2) is not an array, 3) is backwards, so the answer is E) 2D Array Declaration cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

Accessing elements in a 2D array (Part 1) What is the value of cArray[2][1] after the following code has executed? ‘a’ ‘b’ ‘c’ ‘d’ ‘e’ char cArray[][] = new char[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) cArray[i][j] = (char) ('a' + j + i); The answer is cArray[2][1] = (char) (‘a’ + 1 + 2) = ‘d’, so the answer is D), show the whole array on the board. 2D Array Access cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

Accessing elements in a 2D array (Part 1) What is the value of cArray[2][1] after the following code has executed? ‘a’ ‘b’ ‘c’ ‘d’ ‘e’ char cArray[][] = new char[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) cArray[i][j] = (char) ('a' + j + i); The answer is cArray[2][1] = (char) (‘a’ + 1 + 2) = ‘d’, so the answer is D), show the whole array on the board. 2D Array Access cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

Accessing elements in a 2D array (Part 2) Which array element correctly accesses the highlighted value in the array below? 123 234 345 456 567 678 789 890 111 222 333 444 555 666 777 987 876 765 654 543 432 321 210 iArray[4][3] iArray[3][4] iArray[3][2] iArray[2][3] iArray[3][3] The answer is C), since row and column are zero-based and Java is row-major. 2D Array Access cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

Accessing elements in a 2D array (Part 2) Which array element correctly accesses the highlighted value in the array below? 123 234 345 456 567 678 789 890 111 222 333 444 555 666 777 987 876 765 654 543 432 321 210 iArray[4][3] iArray[3][4] iArray[3][2] iArray[2][3] iArray[3][3] The answer is C), since row and column are zero-based and Java is row-major. 2D Array Access cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016 On to the lecture cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

Initializing elements in a 2D array What is the value of dArray[7][4] after the following code has executed? 63.0 74.0 85.0 None of the above double dArray[][] = new double[10][10]; for (int i = 0; i < dArray.length; i++) for (int j = 0; j < dArray[i].length; j++) dArray[i][j] = (i * 10.0) + j; The answer is (7 * 10.0) + 4 = 74.0, so B). 2D Array Initialization cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

Initializing elements in a 2D array What is the value of dArray[7][4] after the following code has executed? 63.0 74.0 85.0 None of the above double dArray[][] = new double[10][10]; for (int i = 0; i < dArray.length; i++) for (int j = 0; j < dArray[i].length; j++) dArray[i][j] = (i * 10.0) + j; The answer is (7 * 10.0) + 4 = 74.0, so B). 2D Array Initialization cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

2D Arrays as Method Return Values What is the value of iArray[3][2] after the call to the make2D method below? 18 19 20 21 None of the above int[][] iArray = make2D(5, 6); public int[][] make2D(int nrows, int ncols) { int[][] nArray = new int[nrows][ncols]; for (int i = 0; i < nrows * ncols; i++) nArray[i / ncols][i % ncols] = i; return nArray; } The array is shown below, so iArray[3][2] = 20, so C). { { 0, 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10, 11 }, { 12, 13, 14, 15, 16, 17 }, { 18, 19, 20, 21, 22, 23 }, { 24, 25, 26, 27, 28, 29 } } 2D Array Return Values cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

2D Arrays as Method Return Values What is the value of iArray[3][2] after the call to the make2D method below? 18 19 20 21 None of the above int[][] iArray = make2D(5, 6); public int[][] make2D(int nrows, int ncols) { int[][] nArray = new int[nrows][ncols]; for (int i = 0; i < nrows * ncols; i++) nArray[i / ncols][i % ncols] = i; return nArray; } The array is shown below, so iArray[3][2] = 20, so C). { { 0, 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10, 11 }, { 12, 13, 14, 15, 16, 17 }, { 18, 19, 20, 21, 22, 23 }, { 24, 25, 26, 27, 28, 29 } } 2D Array Return Values cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016 Ragged 2D Arrays What are the values of iArray.length and iArray[1].length, and iArray[3].length? int iArray[][] = { { 11, 22, 33, 44 }, { 55, 66, 77 }, { 88, 99}, { 1, 2, 3, 4, 5, 6 } }; 4, 3, 6 4, 4, 5 14, 3, 6 14, 7, 14 3, 2, 5 The 2D array has 4 rows with lengths 4, 3, 2, 6, so the answer is A), explain ragged arrays. Tagged Arrays cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016 Ragged 2D Arrays What are the values of iArray.length and iArray[1].length, and iArray[3].length? int iArray[][] = { { 11, 22, 33, 44 }, { 55, 66, 77 }, { 88, 99}, { 1, 2, 3, 4, 5, 6 } }; 4, 3, 6 4, 4, 5 14, 3, 6 14, 7, 14 3, 2, 5 The 2D array has 4 rows with lengths 4, 3, 2, 6, so the answer is A), explain ragged arrays. Tagged Arrays cs163/164: Peer 7 - 2D Arrays - Fall Semester 2016

Peer Instruction 8 Classes and Objects

Which of the following statements about Java classes is incorrect? Classes are templates for creating objects. Classes can contain methods and data, but not imports. Classes cannot be instantiated from other classes. Classes are almost always defined as public. Multiple objects can be instantiated from a single class. Answer is C), classes instantiate other classes to use their code and data. Class Definition cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

Which of the following statements about Java classes is incorrect? Classes are templates for creating objects. Classes can contain methods and data, but not imports. Classes cannot be instantiated from other classes. Classes are almost always defined as public. Multiple objects can be instantiated from a single class. Answer is C), classes instantiate other classes to use their code and data. Class Definition cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 From which of the following locations can you instantiate a Java class? From a method in another class From a method within the same class As an instance variable in another class As an instance variable in the same class All of the above, and more besides! Answer is E), pretty much anywhere, including static methods and as a static variable. Class Instantiation cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 From which of the following locations can you instantiate a Java class? From a method in another class From a method within the same class As an instance variable in another class As an instance variable in the same class All of the above, and more besides! Answer is E), pretty much anywhere, including static methods and as a static variable. Class Instantiation cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 What is printed? public class CTest { private int x = 3; public CTest () {x = 12;} public CTest(int y) {x=y; y=9;}; public static void main(String[] args) { CTest c = new CTest(); System.out.println(c.x); } } 3 9 12 Does not compile cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 What is printed? public class CTest { private int x = 3; public CTest () {x = 12;} public CTest(int y) {x=y; y=9;}; public static void main(String[] args) { CTest c = new CTest(); System.out.println(c.x); } } 3 9 12 Does not compile cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 What is printed? public class CTest { private int x = 3; public CTest () {x = 12;} public CTest(int y) {x=y; y=9;}; public static void main(String[] args) { CTest c = new CTest(0); System.out.println(c.x); } } 3 9 12 Does not compile cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 What is printed? public class CTest { private int x = 3; public CTest () {x = 12;} public CTest(int y) {x=y; y=9;}; public static void main(String[] args) { CTest c = new CTest(0); System.out.println(c.x); } } 3 9 12 Does not compile cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

Peer Instruction 8 Classes and Objects

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 What is printed? public class CTest { private int x = 6; public CTest () {x += 6;} public CTest(int y) {x=y; y=9;}; public static void main(String[] args) { CTest c = new CTest(); System.out.println(c.x); } } 3 9 12 Does not compile cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 What is printed? public class CTest { private int x = 6; public CTest () {x += 6;} public CTest(int y) {x=y; y=9;}; public static void main(String[] args) { CTest c = new CTest(); System.out.println(c.x); } } 3 9 12 Does not compile cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 What is printed? public class CTest { private int x = 3; public CTest () {x = 12;} public CTest(int y) {x=y+6; y=9;}; public static void main(String[] args) { CTest c = new CTest(0); System.out.println(c.x); } } 3 6 9 12 Does not compile cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 What is printed? public class CTest { private int x = 3; public CTest () {x = 12;} public CTest(int y) {x=y+6; y=9;}; public static void main(String[] args) { CTest c = new CTest(0); System.out.println(c.x); } } 3 6 9 12 Does not compile cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 On to the lecture cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 How can multiple methods within a Java class read and write the same variable? Allow one method to reference a local variable of the other Declare a variable of the same name in both methods Add the variable to the class as an instance variable Pass the variable as a parameter between methods None of the above false, this is what you cannot do false, you can do this but they’re different variables, not shared true, class or instance, both are accessible from all methods false, one way communication, may if you also return it false, since C) is true Class Variables cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 How can multiple methods within a Java class read and write the same variable? Allow one method to reference a local variable of the other Declare a variable of the same name in both methods Add the variable to the class as an instance variable Pass the variable as a parameter between methods None of the above false, this is what you cannot do false, you can do this but they’re different variables, not shared true, class or instance, both are accessible from all methods false, one way communication, may if you also return it false, since C) is true Class Variables cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 Which of the following statements about objects and classes are correct? In Java, code and data can only exist in a class. Instantiation does not require memory allocation. Instantiation makes a class from an object. Many objects can be made from a single class. Only a single object can be made from a class. 1) and 4) are correct, 2), 3), and 5) are incorrect, so the answer is B) 1) and 3) 2) and 3) 1) and 5) 1) and 4) 2) and 4) Objects and Classes cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 Which of the following statements about objects and classes are correct? In Java, code and data can only exist in a class. Instantiation does not require memory allocation. Instantiation makes a class from an object. Many objects can be made from a single class. Only a single object can be made from a class. 1) and 4) are correct, 2), 3), and 5) are incorrect, so the answer is B) 1) and 3) 2) and 3) 1) and 5) 1) and 4) 2) and 4) Objects and Classes cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 Which of the following statements about public versus private are correct? Public variables and methods cannot be accessed outside the class in which they are defined. Private variables can be accessed outside the class only by writing ”getter” or “setter” methods. Private methods cannot be non-static, but public methods can be, and both can be static. Private methods comprise the ‘interface’ provided to users of the class. If you instantiate a class from outside the class you can access both private and public variables. A) false, is completely backwards B) true, that’s the normal way of accessing private data C) false, no relationship between private/public and static/nonstatic D) false, public methods are the interface, private methods are hidden E) possibly, depends on where you instantiate it from (explain) Public and Private cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 Which of the following statements about public versus private are correct? Public variables and methods cannot be accessed outside the class in which they are defined. Private variables can be accessed outside the class only by writing ”getter” or “setter” methods. Private methods cannot be non-static, but public methods can be, and both can be static. Private methods comprise the ‘interface’ provided to users of the class. If you instantiate a class from outside the class you can access both private and public variables. A) false, is completely backwards B) true, that’s the normal way of accessing private data C) false, no relationship between private/public and static/nonstatic D) false, public methods are the interface, private methods are hidden E) possibly, depends on where you instantiate it from (explain) Public and Private cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 Which of the following statements about static and non-static are correct? Static data is also called instance data, and non-static data is called class data. Only one copy of instance (non-static) exists. There is a separate copy of instance data for every object that is instantiated. Accessing class data using the class name instead of the object name is not a good practice. Accessing instance data does not require use of the class name, if done from within the same class. false, is completely backward false, multiple copies (instances) exist true, however no separate copies of methods, would waste space D) false, it is a good practice, can omit class name if within class E) false, always requires the instance name! Static and Non-static cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 Which of the following statements about static and non-static are correct? Static data is also called instance data, and non-static data is called class data. Only one copy of instance (non-static) exists. There is a separate copy of instance data for every object that is instantiated. Accessing class data using the class name instead of the object name is not a good practice. Accessing instance data does not require use of the class name, if done from within the same class. false, is completely backward false, multiple copies (instances) exist true, however no separate copies of methods, would waste space D) false, it is a good practice, can omit class name if within class E) false, always requires the instance name! Static and Non-static cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

Peer Instruction Classes and Objects

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 Which of the following statements about static and non-static are correct? Static data is also called instance data, and non-static data is called class data. Only one copy of instance (non-static) exists. There is a separate copy of instance data for every object that is instantiated. Accessing class data using the class name instead of the object name is not a good practice. Accessing instance data does not require use of the class name, if done from within the same class. false, is completely backward false, multiple copies (instances) exist true, however no separate copies of methods, would waste space D) false, it is a good practice, can omit class name if within class E) false, always requires the instance name! Static and Non-static cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 Which of the following statements about static and non-static are correct? Static data is also called instance data, and non-static data is called class data. Only one copy of instance (non-static) exists. There is a separate copy of instance data for every object that is instantiated. Accessing class data using the class name instead of the object name is not a good practice. Accessing instance data does not require use of the class name, if done from within the same class. false, is completely backward false, multiple copies (instances) exist true, however no separate copies of methods, would waste space D) false, it is a good practice, can omit class name if within class E) false, always requires the instance name! Static and Non-static cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

Class and Instance Data The code below accesses class/instance variables, which line will not compile? 0 public class Class { 1 String s0 = “Instance Data”; 2 static String s1 = “Class Data”; 3 public static void main(String args[]) { 4 Class instance = new Class(); 5 System.out.println(Class.s0); 6 System.out.println(instance.s0); 7 System.out.println(Class.s1); 8 } 5 6 7 8 All will compile Line 5 is the only compile error. Extra credit: Where will the warnings be in Eclipse? Answer is Line 8. Class and Instance Data cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

Class and Instance Data The code below accesses class/instance variables, which line will not compile? 0 public class Class { 1 String s0 = “Instance Data”; 2 static String s1 = “Class Data”; 3 public static void main(String args[]) { 4 Class instance = new Class(); 5 System.out.println(Class.s0); 6 System.out.println(instance.s0); 7 System.out.println(Class.s1); 8 } 5 6 7 8 All will compile Line 5 is the only compile error. Extra credit: Where will the warnings be in Eclipse? Answer is Line 8. Class and Instance Data cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

Putting it all together with class and instance data. public class Peer { static int i = 11; int j = 22; public static void main(String args[]) { Peer p1 = new Peer(); Peer p2 = new Peer(); Peer.i = 33; p1.j = 44; Peer.i = 55; p2.j = 66; System.out.println(Peer.i+“ ”+p1.j+“ ”+Peer.i+“ ”+p2.j); 11 44 11 66 33 44 55 66 55 44 55 66 55 66 55 66 Will not compile Will compile and the answer is C). Extra credit: Where will the warnings be in Eclipse? Answer: p1.i and p2.i. Class and Instance Data cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

Putting it all together with class and instance data. public class Peer { static int i = 11; int j = 22; public static void main(String args[]) { Peer p1 = new Peer(); Peer p2 = new Peer(); Peer.i = 33; p1.j = 44; Peer.i = 55; p2.j = 66; System.out.println(Peer.i+“ ”+p1.j+“ ”+Peer.i+“ ”+p2.j); 11 44 11 66 33 44 55 66 55 44 55 66 55 66 55 66 Will not compile Will compile and the answer is C). Extra credit: Where will the warnings be in Eclipse? Answer: p1.i and p2.i. Class and Instance Data cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

Collections and ArrayLists Peer Instruction 12 Collections and ArrayLists

What are the size of the ArrayList after the following code executes? ArrayList<String> list = new ArrayList<>(10); list.add("Java"); list.add("Basic"); list.add("C++"); list.add(2, "Python"); list.add(3, "Fortran"); list.remove("Java"); list.trimToSize(); size, capacity: 4, 4 4, 10 10, 4 10, 10 Correct, added 5, removed 1, so size is 4. Initial capacity 10, but trimToSize() B), C), D) Incorrect ArrayList Example cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016

What are the size of the ArrayList after the following code executes? ArrayList<String> list = new ArrayList<>(10); list.add("Java"); list.add("Basic"); list.add("C++"); list.add(2, "Python"); list.add(3, "Fortran"); list.remove("Java"); list.trimToSize(); size, capacity: 4, 4 4, 10 10, 4 10, 10 Correct, added 5, removed 1, so size is 4. Initial capacity 10, but trimToSize() B), C), D) Incorrect ArrayList Example cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016

cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016 What is the contents of theArrayList after the following code executes? ArrayList<String> list = new ArrayList<>(); list.add("Java"); list.add("Basic"); list.add("C++"); list.add(2, "Python"); list.add(3, "Fortran"); list.remove(1); [Java, Basic, Python, Fortran, C++] [Java, Python, C++, Fortran] [Basic, Python, Fortran, C++] [Java, Python, Fortran, C++] D) Correct, as follows: [Java] [Java, Basic] [Java, Basic, C++] [Java, Basic, Python, C++] [Java, Basic, Python, Fortran, C++] [Java, Python, Fortrain, C++] ArrayList Example cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016

cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016 What is the contents of theArrayList after the following code executes? ArrayList<String> list = new ArrayList<>(); list.add("Java"); list.add("Basic"); list.add("C++"); list.add(2, "Python"); list.add(3, "Fortran"); list.remove(1); [Java, Basic, Python, Fortran, C++] [Java, Python, C++, Fortran] [Basic, Python, Fortran, C++] [Java, Python, Fortran, C++] D) Correct, as follows: [Java] [Java, Basic] [Java, Basic, C++] [Java, Basic, Python, C++] [Java, Basic, Python, Fortran, C++] [Java, Python, Fortrain, C++] ArrayList Example cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016

Select the statement that best defines an ArrayList. ArrayList is an interface that provides a set of methods to concrete classes such as LinkedList. ArrayList is a concrete class provided by Java that implements a data structure and associated methods. ArrayList is a better data structure than an array because it is more efficient in terms of memory usage. ArrayList is a data structure that represents a list of primitives or objects stored in memory. Incorect, not an interface, a concrete class, look at the hierarchy presented in class. Correct, exactly as it says! Incorrect, very convenient, but no more efficient, in fact probably less. Incorrect, cannot store primitives in a Collection, just objects (references). ArrayLIst Definition cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016

Select the statement that best defines an ArrayList. ArrayList is an interface that provides a set of methods to concrete classes such as LinkedList. ArrayList is a concrete class provided by Java that implements a data structure and associated methods. ArrayList is a better data structure than an array because it is more efficient in terms of memory usage. ArrayList is a data structure that represents a list of primitives or objects stored in memory. Incorect, not an interface, a concrete class, look at the hierarchy presented in class. Correct, exactly as it says! Incorrect, very convenient, but no more efficient, in fact probably less. Incorrect, cannot store primitives in a Collection, just objects (references). ArrayLIst Definition cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016

cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016 Select the statement that best describes the attributes of an ArrayList. An ArrayList can grow or shrink dynamically iff the programmer explicitly modifies the capacity. An ArrayList can grow or shrink dynamically without any limitations or special action by the programmer. An ArrayList can grow or shrink dynamically, but an element can only be added at the beginning or end. An ArrayList cannot grow or shrink dynamically, it must be defined by the programmer. Incorrect, don’t have to worry about the capacity. Correct, that’s what is nice. Incorrect, can insert in the middle, i.e. List.add(int index, Element e); Incorrect, ArrayLists can grow and shrink dynamically ArrayLIst Attributes cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016

cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016 Select the statement that best describes the attributes of an ArrayList. An ArrayList can grow or shrink dynamically iff the programmer explicitly modifies the capacity. An ArrayList can grow or shrink dynamically without any limitations or special action by the programmer. An ArrayList can grow or shrink dynamically, but an element can only be added at the beginning or end. An ArrayList cannot grow or shrink dynamically, it must be defined by the programmer. Incorrect, don’t have to worry about the capacity. Correct, that’s what is nice. Incorrect, can insert in the middle, i.e. List.add(int index, Element e); Incorrect, ArrayLists can grow and shrink dynamically ArrayLIst Attributes cs163/164: Peer 12 - Collections: ArrayList - Fall Semester 2016

cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016 On to the lecture cs163/164: Peer 8 - Classes and Objects - Fall Semester 2016

Interfaces Writing an interface requires you to specify the methods a class implementing the interface does not need to address. True False

Interfaces It is possible to write an interface to specify the methods a class implementing the interface does not need to address. True False – any signatures not implemented cause a compile error.

Interfaces We can create an array of an interface type, and store the reference to an object implementing that interface as: Its type Its index An element in the array A reference to the array

Interfaces We can create an array of an interface type, and store the reference to an object implementing that interface as: Its type Its index An element in the array A reference to the array

Peer Instruction 11 Recursion

Select the correct definition of recursion. Java code that invokes a method in the same class in which the calling method resides. Java code that invokes a method in another class, which then calls back to the original class. Java code that invokes the method in which the calling code itself resides. Java code that causes a stack overflow by calling itself in an infinite loop. Incorrect, you call other methods in your class (such a helper methods) all the time! Incorrect, thats just a callback, often used to report status or progress. Correct, call the method that you’re in is recursion. Incorrect, this is recursion, but not the kind you want! Recursion Definition cs163/164: Peer 11 - Recursion - Fall Semester 2016

Select the correct definition of recursion. Java code that invokes a method in the same class in which the calling method resides. Java code that invokes a method in another class, which then calls back to the original class. Java code that invokes the method in which the calling code itself resides. Java code that causes a stack overflow by calling itself in an infinite loop. Incorrect, you call other methods in your class (such a helper methods) all the time! Incorrect, thats just a callback, often used to report status or progress. Correct, call the method that you’re in is recursion. Incorrect, this is recursion, but not the kind you want! Recursion Definition cs163/164: Peer 11 - Recursion - Fall Semester 2016

cs163/164: Peer 11 - Recursion - Fall Semester 2016 What does the recursive code below print when called with i = 1234, j = 5? System.out.println(compute(1234, 5)); public int compute(int i, int j) { // base case if (i < j) return i; // recursion call return compute(i - j, j); } 1 2 3 4 5 Answer is D), method does repeated subtraction to produce the module of the number. It’s going to stop when I < j which would be 4 – no need to go through a bunch of calculations…. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

cs163/164: Peer 11 - Recursion - Fall Semester 2016 What does the recursive code below print when called with i = 1234, j = 5? System.out.println(compute(1234, 5); public int compute(int i, int j) { // base case if (i < j) return i; // recursion call return compute(i - j, j); } 1 2 3 4 5 Answer is D), method does repeated subtraction to produce the module of the number. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the recursive code below print when called with value = 10 ? System.out.println(compute(10)); public static int compute(int value) { if (value == 0) return 0; int term = value ; return term + compute(value - 2); } 28 20 30 stack overflow Answer is C), 10 + 8 + 6 + 4 + 2 = 30, 9 + 7 + 5 + 3 + 1 + -1 + -3 ... = stack overflow. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the recursive code below print when called with value = 10 ? System.out.println(compute(10)); public static int compute(int value) { if (value == 0) return 0; int term = value ; return term + compute(value - 2); } 28 20 30 stack overflow Answer is C), 10 + 8 + 6 + 4 + 2 = 30, 9 + 7 + 5 + 3 + 1 + -1 + -3 ... = stack overflow. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the recursive code below print when called with value = 9 ? System.out.println(compute(9)); public static int compute(int value) { if (value == 0) return 0; int term = value ; return term + compute(value - 2); } 28 20 30 stack overflow Answer is C), 10 + 8 + 6 + 4 + 2 = 30, 9 + 7 + 5 + 3 + 1 + -1 + -3 ... = stack overflow. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the recursive code below print when called with value = 9 ? System.out.println(compute(9)); public static int compute(int value) { if (value == 0) return 0; int term = value ; return term + compute(value - 2); } 28 20 30 stack overflow Answer is C), 10 + 8 + 6 + 4 + 2 = 30, 9 + 7 + 5 + 3 + 1 + -1 + -3 ... = stack overflow. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

Peer Instruction 11 Recursion

cs163/164: Peer 11 - Recursion - Fall Semester 2016 What does the recursive code below print when called with d = 100.0 and n = 6? public static double compute(double d, int n) { // base case if (n == 0) return 0.0; // compute term double term = d; // recursion call return term + compute(d / 10.0, n - 1); } 0.0 100.0 101.01 111.11 111.111 Answer is E), method adds 100.0 + 10.0 + 1.0 + 0.1 + 0.01 + 0.001, 6 terms. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

cs163/164: Peer 11 - Recursion - Fall Semester 2016 What does the recursive code below print when called with d = 100.0 and n = 6? public static double compute(double d, int n) { // base case if (n == 0) return 0.0; // compute term double term = d; // recursion call return term + compute(d / 10.0, n - 1); } 0.0 100.0 101.01 111.11 111.111 Answer is E), method adds 100.0 + 10.0 + 1.0 + 0.1 + 0.01 + 0.001, 6 terms. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

cs163/164: Peer 11 - Recursion - Fall Semester 2016 What does the recursive code below print when called with s = ”aabbccddeeff”? public static String munge(String s) { if (s == null || s.length() <= 1) // base case return s; else if (s.charAt(0) == s.charAt(1)) return munge(s.substring(1, s.length())); else return s.charAt(0) + munge(s.substring(1, s.length())); } aabbccddeeff abcdefabcdef ababcdcdefef abcdef empty string Answer is D), method removes duplicates and keeps unique letters. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

cs163/164: Peer 11 - Recursion - Fall Semester 2016 What does the recursive code below print when called with s = ”aabbccddeeff”? public static String munge(String s) { if (s == null || s.length() <= 1) // base case return s; else if (s.charAt(0) == s.charAt(1)) return munge(s.substring(1, s.length())); else return s.charAt(0) + munge(s.substring(1, s.length())); } aabbccddeeff abcdefabcdef ababcdcdefef abcdef empty string Answer is D), method removes duplicates and keeps unique letters. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

cs163/164: Peer 11 - Recursion - Fall Semester 2016 What does the recursive code below print when called with number = 13 and base = 2? public static void convert(int number, int base) { int remainder = number % base; int quotient = number / base; if (quotient > 0) convert(quotient, base); System.out.print(remainder); } 1111011 11011 1101 1011 110 Answer is C), performs conversion to binary number. What is the base case? Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

cs163/164: Peer 11 - Recursion - Fall Semester 2016 What does the recursive code below print when called with number = 13 and base = 2? public static void convert(int number, int base) { int remainder = number % base; int quotient = number / base; if (quotient > 0) convert(quotient, base); System.out.print(remainder); } 1111011 11011 1101 1011 110 Answer is C), performs conversion to binary number. What is the base case? Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the following code do: public int array11(int[] nums, int index) { if (index == nums.length) return 0; if (nums[index] == 11) return 1 + array11(nums, index+1); else return array11(nums, index+1);} Replaces the values of 11 in the array with 1 Removes the values of 11 from the array Duplicates the values of 11 in the array Counts the occurrences of 11 in the array cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the following code do: public int array11(int[] nums, int index) { if (index == nums.length) return 0; if (nums[index] == 11) return 1 + array11(nums, index+1); else return array11(nums, index+1);} Replaces the values of 11 in the array with 1 Removes the values of 11 from the array Duplicates the values of 11 in the array Counts the occurrences of 11 in the array cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the following code do: public boolean array220(int[] nums, int index) { if (index < nums.length-1) { if (nums[index]*10 == nums[index+1]) return true; else return array220(nums, index+1); } return false; Returns true if any number is preceeded by 10*itself Returns true if there are duplicate values in the array Returns true if any number is preceeded by itself/10 Returns true if any number is preceeded by 10 % itself cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the following code do: public boolean array220(int[] nums, int index) { if (index < nums.length-1) { if (nums[index]*10 == nums[index+1]) return true; else return array220(nums, index+1); } return false; Returns true if any number is preceeded by 10*itself Returns true if there are duplicate values in the array Returns true if any number is preceeded by itself/10 Returns true if any number is preceeded by 10 % itself cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the following code print when i = 1? public static void rec1 (int i) { if (i==0) System.out.print(i + " "); for (int j=0; j<2; j++) { rec1(i-1); } 1 0 0 0 0 1 0 0 0 0 None of the above cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the following code print when i = 1? public static void rec1 (int i) { if (i==0) System.out.print(i + " "); for (int j=0; j<2; j++) { rec1(i-1); } 1 0 0 0 0 1 0 0 0 0 None of the above Stack overflow cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the following code print when i = 1? public static void rec2 (int i) { if (i==0) { System.out.print(i + " "); } else { for (int j=0; j<2; j++) { rec2 (i-1); } 1 0 0 0 0 1 0 0 0 0 None of the above cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the following code print when i = 1? public static void rec2 (int i) { if (i==0) { System.out.print(i + " "); } else { for (int j=0; j<2; j++) { rec2 (i-1); } 1 0 0 0 0 1 0 0 0 0 None of the above cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the following code print when list = {1,3,5,7,9} public int rec(int [] list){ return rec3(list, 0); } public int rec3(int [] list, int start){ if (start == list.length - 1) { return list[start]; } else { return Math.max(list[start], rec3(list, start + 1)); 1 3 5 7 9 cs163/164: Peer 11 - Recursion - Fall Semester 2016

What does the following code print when list = {1,3,5,7,9} public int rec(int [] list){ return rec3(list, 0); } public int rec3(int [] list, int start){ if (start == list.length - 1) { return list[start]; } else { return Math.max(list[start], rec3(list, start + 1)); 1 3 5 7 9 cs163/164: Peer 11 - Recursion - Fall Semester 2016

cs163/164: Peer 11 - Recursion - Fall Semester 2016 Solve this Given a string and a non-empty substring sub, compute recursively the number of times that sub appears in the string, without the sub strings overlapping. public int strCount(String str, String sub) { } cs163/164: Peer 11 - Recursion - Fall Semester 2016

cs163/164: Peer 11 - Recursion - Fall Semester 2016 Solution public int strCount(String str, String sub) { if (sub.length() > str.length()) return 0; if (str.substring(0,sub.length()).equals(sub)) return 1 + strCount(str.substring(sub.length()), sub); else return strCount(str.substring(1), sub); } cs163/164: Peer 11 - Recursion - Fall Semester 2016