Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two-Dimensional Arrays

Similar presentations


Presentation on theme: "Two-Dimensional Arrays"— Presentation transcript:

1 Two-Dimensional Arrays
Peer Instruction 7 Two-Dimensional Arrays

2 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

3 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

4 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’ ) = ‘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

5 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’ ) = ‘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

6 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

7 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

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

9 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

10 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

11 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

12 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

13 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

14 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

15 Peer Instruction 8 Classes and Objects

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 Peer Instruction 8 Classes and Objects

25 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

26 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

27 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

28 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

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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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

38 Peer Instruction Classes and Objects

39 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

40 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

41 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

42 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

43 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); 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

44 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); 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

45 Collections and ArrayLists
Peer Instruction 12 Collections and ArrayLists

46 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

47 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

48 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

49 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

50 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

51 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

52 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

53 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

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

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

56 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.

57 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

58 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

59 Peer Instruction 11 Recursion

60 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

61 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

62 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

63 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

64 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), = 30, = stack overflow. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

65 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), = 30, = stack overflow. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

66 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), = 30, = stack overflow. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

67 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), = 30, = stack overflow. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

68 Peer Instruction 11 Recursion

69 cs163/164: Peer 11 - Recursion - Fall Semester 2016
What does the recursive code below print when called with d = 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 Answer is E), method adds , 6 terms. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

70 cs163/164: Peer 11 - Recursion - Fall Semester 2016
What does the recursive code below print when called with d = 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 Answer is E), method adds , 6 terms. Recursion Example cs163/164: Peer 11 - Recursion - Fall Semester 2016

71 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

72 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

73 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); } 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

74 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); } 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

75 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

76 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

77 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

78 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

79 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 None of the above cs163/164: Peer 11 - Recursion - Fall Semester 2016

80 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 None of the above Stack overflow cs163/164: Peer 11 - Recursion - Fall Semester 2016

81 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 None of the above cs163/164: Peer 11 - Recursion - Fall Semester 2016

82 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 None of the above cs163/164: Peer 11 - Recursion - Fall Semester 2016

83 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

84 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

85 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

86 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


Download ppt "Two-Dimensional Arrays"

Similar presentations


Ads by Google