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? 3650 Humanities 125 Ag Hall 272 Bascom Other (due to conflicts) 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
Defining and Calling Methods
mPrint - which is Call, Definition? mPrint call then mPrint definition static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); B is the correct answer
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); argument parameter actual parameter formal parameter count is a parameter (zyBooks) or formal parameter the number 23, for example is an argument (also called an actual parameter).
Compile or Not Compile? static int mPrint(String str) { compiles as part of a class will not compile as part of a class missing a statement invalid data type static int mPrint(String str) { System.out.println( "my:" + str); } C is the best answer. it is true that mPrint method must be part of a class but that is not enough to make it compile since mPrint has int as the return type, there must be a return statement that returns a value of type int. One could argue that the mistake is that changing int to void would be a better solution as it is not clear what int value should be returned.
What prints out? static void calc(int num) { num = 3; } 5 35 error static void calc(int num) { num = 3; } public static void main(String []args) { int n = 5; calc( n); System.out.println( n); try it.
What prints out? public static void main(String []args) { int n = 5; 3 5 35 error public static void main(String []args) { int n = 5; calc( n); System.out.println( n); } static int calc(int num) { return 3; try it.
What prints out? static int calc(int num) { return 3; } 5 35 error static int calc(int num) { return 3; } public static void main(String []args) { int n = 5; n = calc( n); System.out.println( n); try it
Which is called first: calc or println? error static int calc(int num) { num -= 33; return num; } public static void main(String []args) { int n = 55; System.out.println( calc( n)); put a print statement within the calc method to see if it is called before the println method.
Multiple parameters and overloading
What prints out? print(double) 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); print(double) print(int) invalid overloading error try it.
What prints out? print(double) 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)"); print(double) print(int) invalid overloading error try it.
What prints out? void print int print invalid overloading other error 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); void print int print invalid overloading other error try it.
What prints out? print(double) 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); print(double) something(int) invalid overloading error try it.
Memory Areas Stack local, parameters in Stack Frame Heap instances (object), arrays static Code review 1 passing primitive to method changing parameter in method 2 passing array to method changing contents of array in method 3 passing array to method changing parameter to another array in method 4 passing a string to method calling string method in method public class ClassNameHere { static void method1(int num) { num = 5; } static void method2(int []listA) { listA = new int[3]; listA[1] = 10; static void method3(StringBuffer name) { name //name.toUpperCase(); public static void main(String[] args) { String name = "Helen"; method3( name); System.out.println( name); // int [] listB = {4, 6, 8}; // method2( listB); // System.out.println( listB[1]); // int num = 4; // method1( num); // System.out.println("num=" + num);
Where in memory is variable z? stack heap it is local Error static void main(String []args) { 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.
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.
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.
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.
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.
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. try it.
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 = ""; "a.b.c." ".a.b.c" error String str = ""; String [] words = {"a", "b", "c"}; for ( int i = 0; i < words.length; i++) { str += "." + words[i]; } try it
What is the value of sum? double sum = 0.0; 6.8 9.9 12.9 runtime error 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; } try it.
What does this do? int [] list = new int[4]; set each element to i * 2 0, 2, 4, 8 compiler error runtime error int [] list = new int[4]; for ( int i = 0; i <= list.length; i++) { list[i] = i * 2; } try it.
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.
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.
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.