1 / 30

CSc2310 tutoring session, week 8 Fall, 2012

CSc2310 tutoring session, week 8 Fall, 2012. Test 3 Study Guide. Haidong Xue. 5:30pm—8:30pm 11/06/2012 and 11/07/2012 . CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/ There are 2 sections: 1. Test 3 Study Guide

ankti
Télécharger la présentation

CSc2310 tutoring session, week 8 Fall, 2012

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. CSc2310 tutoring session, week 8Fall, 2012 • Test 3 Study Guide HaidongXue 5:30pm—8:30pm 11/06/2012 and 11/07/2012

  2. CSc2310 Tutoring • Time: 5:30pm-8:30pm • Tutor: HaidongXue • Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/ There are 2 sections: 1. Test 3 Study Guide 2. Q&A • Answer your questions about java programming

  3. Test 3 Study Guide • Array: declare, initialize, access, resize (add/delete), create array of objects • If-else statement • Switch statement • Loops: while, for ,do: boundary, nested loops • Break and continue operators: when and how to use, for example, search an item in an array • Try/catch block: how to get the error messages, use exceptions to gain proper user input • Graphics basic: concept of pixels, coordinates • Class Variables and Methods • Be sure to review the assignments and quizzes.

  4. Test 3 Study Guide - Array • Declare Type[] name; e.g.: int[] ages; • Initialize • Using “{}” e.g.: int[] ages = {12, 42, 45}; • Initialize each element e.g.: int[] ages = new int[3]; for(inti=0; i<3; i++) ages[i]=20; • Access Using index e.g.: ages[0] = 10; int b = ages[2];

  5. Test 3 Study Guide - Array • resize (add/delete) In fact, no way to resize an array. What you can do is to create a new one, and copy all the values to them. e.g.: int[] ages1 = {23, 23, 45}; // the length of ages1 is 3 //”resize” to 10 int[] temp = ages1; ages1 = new int[10]; for(inti=0; i<temp.length; i++) ages1[i] = temp[i];

  6. Test 3 Study Guide - Array • Create array of objects Use the class name as the type e.g. // assuming there exists a class “Team” Team[] teams = new Team[3]; for( inti=0; i<3; i++) teams[i] = new Team();

  7. Test 3 Study Guide • Array: declare, initialize, access, resize (add/delete), create array of objects • If-else statement • Switch statement • Loops: while, for ,do: boundary, nested loops • Break and continue operators: when and how to use, for example, search an item in an array • Try/catch block: how to get the error messages, use exceptions to gain proper user input • Graphics basic: concept of pixels, coordinates • Class Variables and Methods • Be sure to review the assignments and quizzes.

  8. Test 3 Study Guide – “if-else” • Grammar if(boolean exp1) { code1 } else if (boolean exp2) {code2 } else if (booleanexp3) {code3 } … else if (booleanexpN) {codeN } else {codeN+1} • The code under the first true booleanexp will be executed • If they are all false, the code in “else” will be executed

  9. Test 3 Study Guide • Array: declare, initialize, access, resize (add/delete), create array of objects • If-else statement • Switch statement • Loops: while, for ,do: boundary, nested loops • Break and continue operators: when and how to use, for example, search an item in an array • Try/catch block: how to get the error messages, use exceptions to gain proper user input • Graphics basic: concept of pixels, coordinates • Class Variables and Methods • Be sure to review the assignments and quizzes.

  10. Test 3 Study Guide – “switch” • Grammar switch(variable){ case value1: code1 case value2: code2 …. case valueN: codeN default: codeN+1 } All the code after the case with the value of the given variable will be executed. If there is no case having the value of the given variable, all the code after the default will be executed. When executing a code segment, if there is a “break;”, it will jump out of the switch structure

  11. Test 3 Study Guide – “switch” int age = 5; switch (age) { case 1: System.out.println("A"); case 2: break; case 5: System.out.println("B"); default: System.out.println("C"); } int age = 1; switch (age) { case 1: System.out.println("A"); case 2: break; case 5: System.out.println("B"); default: System.out.println("C"); } A B C

  12. Test 3 Study Guide • Array: declare, initialize, access, resize (add/delete), create array of objects • If-else statement • Switch statement • Loops: while, for ,do: boundary, nested loops • Break and continue operators: when and how to use, for example, search an item in an array • Try/catch block: how to get the error messages, use exceptions to gain proper user input • Graphics basic: concept of pixels, coordinates • Class Variables and Methods • Be sure to review the assignments and quizzes.

  13. Test 3 Study Guide – loops • Grammar do {code block} while(boolean expression); • Function: 1. Execute the code block once 2. Repeat that: if the boolean expression is true, execute the code block once

  14. Test 3 Study Guide – loops • Grammar while(boolean expression) {code block} • Function: Repeat that: if the boolean expression is true, execute the code block once

  15. Test 3 Study Guide – loops • Grammar for(expression 1; boolean expression; expression 2 ) { code block } • Function: expression 1; while(boolean expression){ code block; expression 2; }

  16. Test 3 Study Guide – loops • Nested loop When put a loop into another loop’s body, we have a nested loop, e.g.: for(inti=0; i<3; i++){ for(int j=0; j<5; j++){ System.out.print(A); } } In the output, there will be 15 “A”s

  17. Test 3 Study Guide • Array: declare, initialize, access, resize (add/delete), create array of objects • If-else statement • Switch statement • Loops: while, for ,do: boundary, nested loops • Break and continue operators: when and how to use, for example, search an item in an array • Try/catch block: how to get the error messages, use exceptions to gain proper user input • Graphics basic: concept of pixels, coordinates • Class Variables and Methods • Be sure to review the assignments and quizzes.

  18. Test 3 Study Guide – “break and continue ” • break • Function: directly jump out of a structure(loop or switch) • E.g.: for(inti=0; i<10; i++) { if(i==5) break; System.out.print(i); } The output is: 01234

  19. Test 3 Study Guide – “break and continue ” • continue • Function: directly go to the next iteration of a loop • E.g.: for(inti=0; i<10; i++) { if(i==5) continue; System.out.println(i); } The output is: 012346789

  20. Test 3 Study Guide • Array: declare, initialize, access, resize (add/delete), create array of objects • If-else statement • Switch statement • Loops: while, for ,do: boundary, nested loops • Break and continue operators: when and how to use, for example, search an item in an array • Try/catch block: how to get the error messages, use exceptions to gain proper user input • Graphics basic: concept of pixels, coordinates • Class Variables and Methods • Be sure to review the assignments and quizzes.

  21. Test 3 Study Guide – “try-catch” • Get the message of a exception • Using the method “getMessage()” • Use exceptions to gain proper user input Scanner s = new Scanner(System.in); while(true){ try{ System.out.println("Please input a int in [0, 9]"); intn = s.nextInt(); if(n<0 || n>9) thrownew Exception("Wrong number!"); else{ System.out.println("The number obtained is: " + n); break; } } catch(Exception e){ System.out.println(e.getMessage()); } }

  22. Test 3 Study Guide • Array: declare, initialize, access, resize (add/delete), create array of objects • If-else statement • Switch statement • Loops: while, for ,do: boundary, nested loops • Break and continue operators: when and how to use, for example, search an item in an array • Try/catch block: how to get the error messages, use exceptions to gain proper user input • Graphics basic: concept of pixels, coordinates • Class Variables and Methods • Be sure to review the assignments and quizzes.

  23. Test 3 Study Guide – pixels,coordinates • Pixel the most basic element of an image; it displays a single color • Coordinates of a pixel A pair of coordinates tell the position of a pixel in certain graphical context. Note: the upper left corner is (0, 0)

  24. Test 3 Study Guide • Array: declare, initialize, access, resize (add/delete), create array of objects • If-else statement • Switch statement • Loops: while, for ,do: boundary, nested loops • Break and continue operators: when and how to use, for example, search an item in an array • Try/catch block: how to get the error messages, use exceptions to gain proper user input • Graphics basic: concept of pixels, coordinates • Class Variables and Methods • Be sure to review the assignments and quizzes.

  25. Test 3 Study Guide – class variables and methods • Class variables and methods, are the variables and methods with a static modifier. • They are used by the class name, but not an object variable • E.g.: publicstaticclassTeam{ publicstaticintnumber; publicstaticintgetTeamNumber() {returnnumber;} } To use “number” or “getTeamNumber()”, we use the class name “Team”: int n = Team.number; int n = Team.getTeamNumber(); Note: not an object of Team

  26. Test 3 Study Guide • Array: declare, initialize, access, resize (add/delete), create array of objects • If-else statement • Switch statement • Loops: while, for ,do: boundary, nested loops • Break and continue operators: when and how to use, for example, search an item in an array • Try/catch block: how to get the error messages, use exceptions to gain proper user input • Graphics basic: concept of pixels, coordinates • Class Variables and Methods • Be sure to review the assignments and quizzes. Do it by yourself

  27. Sample problems of Test 3 Show the values of the variables i and j after each series of statements has been executed. i = 7; j = i-- * 2 + 1; Answer: i is 6; j is 15. (“--” is applied in the last, so after the calculation of j)

  28. Sample problems of Test 3 Convert the following if...else if... structure to a switch structure. That is, write a switch statement that will accomplish the equivalent of the code below. (You may assume choice is an integer variable that already has been assigned some value.) if (choice == 1) System.out.println("Choice 1"); else if (choice == 2) System.out.println("Choice 2"); else if (choice == 3) System.out.println("Choice 3"); else System.out.println("Bad choice"); Answer: switch (choice){ case 1: System.out.println("Choice 1"); break; case 2: System.out.println("Choice 2"); break; case 3: System.out.println("Choice 3"); break; default: System.out.println("Bad choice"); } Don’t forget all the “break”s.

  29. Sample problems of Test 3 • Write two methods using different approaches to convert each element in the first column to the corresponding element in the second column. The method will take one of the elements in the first column as the parameter and return the corresponding element in the second column to the caller. Note: only after JavaSE7, it is valid privatestaticint convert2(String s){ if(s.equals("Sunday")) return 0; if(s.equals("Monday")) return 1; if(s.equals("Tuesday")) return 2; if(s.equals("Wednesday")) return 3; if(s.equals("Thursday") )return 4; if(s.equals("Friday") )return 5; if(s.equals("Saturday")) return 6; return-1; } privatestaticint convert1(String s){ switch(s){ case"Sunday": return 0; case"Monday": return 1; case"Tuesday": return 2; case"Wednesday": return 3; case"Thursday": return 4; case"Friday": return 5; case"Saturday": return 6; } return-1; }

  30. Please let me know your questions. I will be here till 8:30pm

More Related