Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 200 - Week 8 Jim Williams, PhD.

Similar presentations


Presentation on theme: "CS 200 - Week 8 Jim Williams, PhD."— Presentation transcript:

1 CS Week 8 Jim Williams, PhD

2 This Week Team Lab: Arrays and Hangman Lecture:
drawing diagrams (bring paper and pencil) Lecture: More Arrays and Methods Unit Testing

3 How To Study?

4 Question 37 / Lab 2, Exercise B, L.
String exprStr1 = " = 4 + 1"; String exprStr2 = "4 + 1 = " ; String exprStr3 = "4 + 1 = " + (4 + 1);

5 Question 6 / Lab 3, Exercise B, K:
Random numGenerator = new Random(); int num1 = numGenerator.nextInt(10); int num2 = numGenerator.nextInt(10); int num3 = numGenerator.nextInt(10); //Does the value returned from nextInt method call change each time? //What are the possible values returned?

6 Question 9 / Lab 4, Exercise B, G:
boolean flag = false; if ( flag = true) { System.out.println("flag is true"); } else { System.out.println("flag is false"); }

7 Question 11 / Lab 5, Exercise B, N:
String str2 = null; if ( str2.length() >= 1 && str2 != null) { System.out.println( str2); } else { System.out.println("null or zero length string"); }

8 Draw a picture int [] items = new int []{5,6,7};

9 What does this do? int [] list = new int[4];
set each element to i * 2 0, 2, 4, 6 compiler error runtime error int [] list = new int[4]; for ( int i = 0; i <= list.length; i++) { list[i] = i * 2; } try it.

10 Is it possible for methodC to change the contents of the array?
public static void main(String []args) { char [] items = new char[]{'c','b','a'}; methodC( items); }

11 Where in memory is variable z?
stack heap it is local Error public static void main(String []args) { int r = 3; int c = 4; int [][] z = new int[r][c]; } A and C are both correct. Local variables are those declared within methods and are stored in the stack.

12 Can you call this method to get the new array?
yes no reference lost Error public static int [] createList(int size) { return new int[size]; } public static void main(String[]args) { int [] list = createList( 10); list[3] = 10; try it.

13 Can main see the changed value within list?
yes no reference lost Error public static void changeList(int [] list) { if ( list.length > 0) { list[0] = 10; } public static void main(String []args) { int [] z = new int[5]; changeList( z); System.out.println( z[0]); try it.

14 Can you call this method to get the new array?
yes no reference lost Error public static void createList(int [] list) { list = new int[10]; } try it.

15 What prints out? num:10 list:[1, 2, 11] num:11 list:[1, 2, 3]
static void change( int num, int [] list) { num = 11; list[2] = num; } public static void main( String [] args) { int [] list = {1,2,3}; int num = 10; change( num, list); System.out.println("num:" + num + "\nlist:" + Arrays.toString( list)); num:10 list:[1, 2, 11] num:11 list:[1, 2, 3] list:[1, 2, 10] try it.

16 What prints out? num:10 list:[1, 2, 11] list:[4,5,10] list:[1, 2, 3]
static void change( int num, int [] list) { list = new int[]{4,5,6}; list[2] = num; } public static void main( String [] args) { int [] list = new int[] {1,2,3}; int num = 10; change( num, list); System.out.println("num:" + num + "\nlist:" + Arrays.toString( list)); try it.

17 Can you call this method to get the new array outside the method?
yes no sometimes Error static void createBoard(int [][] board) { board = new int[3][3]; } try it.

18 Can you call this method to get the new array?
yes no reference lost Error static int [][] createBoard(int r, int c) { return new int[r][c]; } try it.

19 Can main see the changed value within list?
yes no reference lost Error public static void changeList(int [][] list) { list[0] = new int[3]; list[0][1] = 9; } public static void main(String []args) { int [][] z = new int[5][5]; changeList( z); System.out.println( z[0][1]); try it.

20 Will this initialize the array to all 1's?
yes no don't know Error public static void main(String []args) { int [][] board = {{2,3},{4,3,2}}; initBoard( board); } static void initBoard(int[][] board) { for ( int i = 0; i < 2; i++) for ( int j = 0; j < 3; j++) board[ i ][ j ] = 1; try it.

21 Will this initialize the array to all 1's?
yes no don't know Error public static void main(String []args) { int [][] board = {{2,3},{4,3,2}}; initBoard( board); } static void initBoard(int[][] b) { for ( int i = 0; i < b[i].length; i++) for ( int j = 0; j < b[j].length; j++) b[ i ][ j ] = 1; try it.

22 Will this initialize the array to all 1's?
yes no don't know Error public static void main(String []args) { int [][] board = {{2,3},{4,3,2}}; initBoard( board); } static void initBoard(int[][] b) { for ( int i = 0; i < b.length; i++) for ( int j = 0; j < b[ i ].length; j++) b[ i ][ j ] = 1; try it.

23 Unit Testing (method) Developer Writes method to meet specification
Tester Verifies that method meets specification

24 Specification /** * This method returns the number of 9s in the table.
table a 2 dimensional array of int the number of 9s in table */ public static int num9s(int [][]table) { return -1; }


Download ppt "CS 200 - Week 8 Jim Williams, PhD."

Similar presentations


Ads by Google