Download presentation
Presentation is loading. Please wait.
Published byElisabeth Wilkins Modified over 9 years ago
1
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide
2
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 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(int i=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(int i=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( int i=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 (boolean exp3) {code3 } … else if (boolean expN) {codeN } else {codeN+1} The code under the first true boolean exp 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 = 1; switch (age) { case 1: System.out.println("A"); case 2: break; case 5: System.out.println("B"); default: System.out.println("C"); } 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"); } ABCBC
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(int i=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.: The output is: 01234 for(int i=0; i<10; i++) { if(i==5) break; System.out.print(i); }
19
Test 3 Study Guide – “break and continue ” continue – Function: directly go to the next iteration of a loop – E.g.: for(int i=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]"); int n = s.nextInt(); if(n 9) throw new 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.: public static class Team{ public static int number; public static int getTeamNumber() {return number;} } 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"); 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"); } Answer: Don’t forget all the “break”s.
29
Sample problems of Test 3 Sunday0 Monday1 Tuesday2 Wednesday3 Thursday4 Friday5 Saturday6 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. private static int 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; } private static int 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; } Note: only after JavaSE7, it is valid
30
Please let me know your questions. I will be here till 8:30pm
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.