330 likes | 538 Views
Week 6 CS 302. Jim Williams, PhD. This Week. Lab: Multi-dimensional Arrays Exam 1: Thursday Lecture: Methods Review. Midterm Exam 1. What is the location of the exam?. Midterm Exam - Thursday. Bring ID and #2 pencil Exam seating directly in front/behind, 1 empty seat to each side
E N D
Week 6 CS 302 Jim Williams, PhD
This Week Lab: Multi-dimensional Arrays Exam 1: Thursday Lecture: • Methods • Review
Midterm Exam 1 What is the location of the exam?
Midterm Exam - Thursday • Bring ID and #2 pencil • Exam seating • directly in front/behind, 1 empty seat to each side • Multiple choice - focus on reading Java • Write on your exam any assumptions • Review Questions posted on Piazza
P2 - Game of Life Key Concepts • arrays • static methods • parameter passing
mPrint - which is Call, Definition? static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); }
Is count: Argument or Parameter? public static void main(String []args) { int num = 10; printCount( 23); printCount( num+3); } static void printCount(int count) { System.out.println( count); }
Compile or Not Compile? static int mPrint(String str) { System.out.println( "my:" + str); }
What prints out? static void calc(int num) { num = 3; } public static void main(String []args) { int n = 5; calc( n); System.out.println( n); }
What prints out? public static void main(String []args) { int n = 5; calc( n); System.out.println( n); } static int calc(int num) { return 3; }
What prints out? static int calc(int num) { return 3; } public static void main(String []args) { int n = 5; n = calc( n); System.out.println( n); }
Which is called first: calc or println? static int calc(int num) { num -= 33; return num; } public static void main(String []args) { int n = 55; System.out.println( calc( n)); }
What prints out? static void print( double value) { System.out.println( "print(double)"); } static int print( int value) { System.out.print( "print(int)"); return 1; } public static void main( String [] args) { print( 10); }
What prints out? static void print( double val1, double val2) { System.out.println( "print(double)"); } public static void main( String [] args) { double value = 10.0; print( value, 20); } static void print( int val1, int val2) { System.out.println( "print(int)"); } }
What prints out? static void print( double val1, double val2) { System.out.println( "void print"); } static int print( double num1, double num2) { System.out.print( "int print"); return 1; } public static void main( String [] args) { print( 10.0, 20.0); }
What prints out? static void print( double val1, double val2) { System.out.println( "print(double)"); } static int something( int val1, int val2) { System.out.print( "something(int)"); return 1; } public static void main( String [] args) { print( 10, 20); }
Memory Areas Stack • local, parameters in Stack Frame Heap • instances (object), arrays static Code
Where in memory is variable z? static void main(String []args) { int [][] z = new int[r][c]; }
Can you call this method to get the new array outside the method? static void createBoard(int [][] board) { board = new int[3][3]; }
Can you call this method to get the new array? static int [][] createBoard(int r, int c) { return new int[r][c]; }
What prints out? 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)); }
What prints out? 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)); }
Review Questions Expression Evaluation Scanner Arrays Loops
What is result? boolean flag = true; boolean result = flag = false || flag; Suggestion: draw parenthesis to show precedence and work systematically.
Scanner Scanner input = new Scanner("1 \ntwo \n 2\n\n"); int a = input.nextInt(); if ( input.hasNextInt()) { int b = input.nextInt(); } else { input.nextLine(); } String line = input.nextLine(); System.out.println("#" + line + "#");
What is value of str? String str = ""; String [] words = {"a", "b", "c"}; for ( int i = 0; i < words.length; i++) { str += "." + words[i]; }
What is the value of sum? double sum = 0.0; double []nums = {3.1, 4.5, 2.3}; for ( int i = 1; i <= nums.length; i++) { sum += nums[i-1] + 1; }
What does this do? int [] list = new int[4]; for ( int i = 0; i <= list.length; i++) { list[i] = i * 2; }
Will this initialize the array to all 1's? 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; }
Will this initialize the array to all 1's? 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; }
Will this initialize the array to all 1's? 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; }