1 / 33

Week 6 CS 302

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

shelby
Download Presentation

Week 6 CS 302

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 6 CS 302 Jim Williams, PhD

  2. This Week Lab: Multi-dimensional Arrays Exam 1: Thursday Lecture: • Methods • Review

  3. Midterm Exam 1 What is the location of the exam?

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

  5. P2 - Game of Life Key Concepts • arrays • static methods • parameter passing

  6. Defining and Calling Methods

  7. mPrint - which is Call, Definition? static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); }

  8. 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); }

  9. Compile or Not Compile? static int mPrint(String str) { System.out.println( "my:" + str); }

  10. 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); }

  11. 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; }

  12. 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); }

  13. 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)); }

  14. Multiple parameters and overloading

  15. 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); }

  16. 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)"); } }

  17. 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); }

  18. 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); }

  19. Memory Areas Stack • local, parameters in Stack Frame Heap • instances (object), arrays static Code

  20. Where in memory is variable z? static void main(String []args) { int [][] z = new int[r][c]; }

  21. Can you call this method to get the new array outside the method? static void createBoard(int [][] board) { board = new int[3][3]; }

  22. Can you call this method to get the new array? static int [][] createBoard(int r, int c) { return new int[r][c]; }

  23. 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)); }

  24. 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)); }

  25. Review Questions Expression Evaluation Scanner Arrays Loops

  26. What is result? boolean flag = true; boolean result = flag = false || flag; Suggestion: draw parenthesis to show precedence and work systematically.

  27. 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 + "#");

  28. What is value of str? String str = ""; String [] words = {"a", "b", "c"}; for ( int i = 0; i < words.length; i++) { str += "." + words[i]; }

  29. 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; }

  30. What does this do? int [] list = new int[4]; for ( int i = 0; i <= list.length; i++) { list[i] = i * 2; }

  31. 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; }

  32. 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; }

  33. 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; }

More Related