csc2310 tutoring session, week 8 fall, 2012

Post on 24-Feb-2016

24 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

CSc2310 tutoring session, week 8Fall, 2012

Haidong Xue

5:30pm—8:30pm11/06/2012 and 11/07/2012

- Test 3 Study Guide

• 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

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.

Test 3 Study Guide - Array• Declare

Type[] name;e.g.: int[] ages;

• Initialize– Using “{}”e.g.: int[] ages = {12, 42, 45}; – Initialize each elemente.g.: int[] ages = new int[3];for(int i=0; i<3; i++) ages[i]=20;

• AccessUsing indexe.g.: ages[0] = 10;int b = ages[2];

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 10int[] temp = ages1;ages1 = new int[10];for(int i=0; i<temp.length; i++) ages1[i] = temp[i];

Test 3 Study Guide - Array

• Create array of objectsUse the class name as the typee.g.// assuming there exists a class “Team”Team[] teams = new Team[3];for( int i=0; i<3; i++) teams[i] = new Team();

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.

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

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.

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

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

A BC

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.

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

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

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

Test 3 Study Guide – loops• Nested loopWhen 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

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.

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

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

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.

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<0 || 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()); }}

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.

Test 3 Study Guide – pixels,coordinates

• Pixelthe most basic element of an image; it displays a single color• Coordinates of a pixelA pair of coordinates tell the position of a pixel in certain graphical context. Note: the upper left corner is (0, 0)

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.

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

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

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)

Sample problems of Test 3Convert 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.

Sample problems of Test 3

Sunday 0Monday 1Tuesday 2Wednesday 3Thursday 4Friday 5Saturday 6

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

Please let me know your questions.

I will be here till 8:30pm

top related