Midterm Review 01 SP17 ICS 111 Ed Meyer
Exam Details On Laulima > Quizzes, Tests and Surveys Due Thursday 2/23 by 11:55pm Password: toast 1 attempt; 1 hr 15 minutes Reserve uninterrupted time for yourself I do NOT recommend starting at 10:40pm Covers all material so far Weeks 01-06 2
Exam Format 3 Sections 1. Multiple Choice & True/False 2. Short Answer 3. Programming 3
Exam Format 1. Multiple Choice & True/False 10 Questions Given some code and ask what will print/happen Conceptual questions 4
Exam Format 2. Short Answer 2 Questions Respond in complete sentences directly in the exam Given larger coding snippets and trace through them Ask to explain the difference between code 5
Exam Format 3. Programming 2 Questions Given a scenario or specifications and code from scratch Code only what you would put in the main method Any imports will be assumed Majority will come from approach, but syntax is still part of your grade 6
Topics Covered By Week Week 01: Program Skeleton, Reserved Words, Printing Week 02: Data Types, Arithmetic, Variables Week 03: Input/Output (Scanner) Week 04: Conversions and Objects Week 05: Error Handling / IF-statements Week 06: Comparisons / RNG 7
Week 01 Naming Conventions Classes: Camel case notation Variables: lower camel case Constants: all uppercase Reserved Words Program Skeleton Printing: print vs println 8
Week 02 Complex Messages Printing characters that you normally cannot print Escape characters with \ Concatenation using + Concept of a variable Primitive Data Types Constants Naming convention final keyword: final double PI = 3.14; 9
Week 02 (Cont.) Using String variables Arithmetic Expressions Order of operations (PEMDAS) What does % do? Comparisons Comparison operators: <, >, <=, >=, ==,!= Evaluate to true or false Declaring vs. Initializing Concept of an assignment statement 10
Arithmetic Expressions int num1 = 7; int num2 = 3; double result = num1 / num2; // 2.0 System.out.print(result); Integer Division When dividing by integers, only the quotient is used. The decimal is discarded. 7 / 3 = 2 remainder 1 11
Arithmetic Expressions int num1 = 200091; int num2 = 2; double result = num1 % num2; // 1.0 System.out.print(result); 12
Week 03 Input/Output Import the Scanner class Why? What don't we need to import? Where can you go to check if you're not sure? Create a Scanner object Use System.in as an argument Use various methods to read input from the user What are some methods? 13
Week 04 Levels of Precedence Highest: Unary + and Lowest: Assignment statement = Data Conversion Changing the data type from more/less to less/more memory space Widening vs Narrowing Conversion Casting truncates values 14
Data Conversion double num1 = 11.0; double num2 = 2.0; // 5.5 System.out.println(num1 / num2); int result = (int) (num1 / num2); // 5 System.out.println(result); 15
Week 04 (cont.) String methods String anatomy (positions/indexes) touppercase() and tolowercase() length() substring(beginindex) substring(beginindex, endindex) trim() charat(int index) replace(oldchar, newchar) 16
Working with Strings String name = "Huzzah!"; // 7 System.out.println(name.length()); // Huz System.out.println(name.substring(0,3)); // zah! System.out.println(name.substring(3)); // zah! System.out.println(name.substring(name.length() / 2)); System.out.println(name.substring(name.length())); 17
Working with Strings String name = "Huzzah!"; // 7 System.out.println(name.length()); // Huz System.out.println(name.substring(0,3)); // zah! System.out.println(name.substring(3)); // zah! System.out.println(name.substring(name.length() / 2)); System.out.println(name.substring(name.length())); 18
Week 05 Error Handling try/catch try { // Risky code } catch (exception) { // Error message } 19
Week 05 (cont.) Exceptions Are caught in the catch block InputMismatchException StringIndexOutOfBoundsException ArithmeticException 20
Week 05 if statement Execute code based on a condition The condition evaluates to true/false if (condition) { // Execute when true } else { // Execute when false } 21
Week 05 (cont.) if statement What if you wanted to do additional tests? if (condition1) { // When condition1 is true } else { if (condition2) { // When condition1 is false and // condition2 is true } else { // When condition1 is false and // condition2 is false } } 22
Trace Through int x = 0; int a = 6; int b = 5; if (a > 0) { if (b < 0) { x = x + 2; } else { if (a > 5) { x = x + 8; } else { x = x + 1; } } } else { x = x + 3; } What is the value of x? 8 23
Week 05 (cont.) Switch case When you have discrete possibilities Code will "fall through" if there is no break switch (value) { case 0: break; default: break; } 24
Week 05 (cont.) Short circuit evaluations && and && has a higher precedence than 25
Week 06 More on comparisons with equality Equality for PDT use == Equality for objects use.equals Two objects that are.equals may not be == If two objects are ==, they are.equals. Why? Objects point to a place in memory that has the value PDT is a box that stores the value Object is a box that points to another box that stores the value 27
Week 06 (cont.) Random Number Generation (RNG) Need to import java.util.random; Methods.nextDouble().nextInt().nextInt(int n) 28
Good Luck! 29