Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 302 - Week 3 Jim Williams, PhD.

Similar presentations


Presentation on theme: "CS 302 - Week 3 Jim Williams, PhD."— Presentation transcript:

1 CS Week 3 Jim Williams, PhD

2 Learning and Effort Influence Intelligence
"...effort and difficulty, that's when their neurons are making new connections, stronger connections. That's when they're getting smarter." - Carol Dweck

3 What are you doing (mentally)?
Images:

4 Exam Confirmation and Accommodation requests due by Friday
Week 3 Exam Confirmation and Accommodation requests due by Friday P1 Milestone 1 due Thursday 8:00am

5 Are these equivalent? boolean tired = true; if ( tired) {
Same result in all cases Different result sometimes Error boolean tired = true; if ( tired) { //take break tired = false; } if ( !tired) { //keep working boolean tired = true; if ( tired) { //take break tired = false; } else { //keep working }

6 Floating Down a River

7 Side Trip, maybe multiple side trips
boolean tired = true; if ( tired) { System.out.println(“take break”); }

8 One side of the Island or the other
boolean sunny = false; if ( sunny) { System.out.print(“sunny”); } else { System.out.print(“not sunny”); } System.out.println( “ and back together”);

9 Equivalent? Yes No char chr = //any valid char out = 'W';
if ( chr == 'A') { out = 'X'; } else if ( chr == 'B') { out = 'Y'; } else { out = 'Z'; } char chr = //any valid char out = 'W'; if ( chr == 'A') { out = 'X'; } if ( chr == 'B') { out = 'Y'; } if ( chr != 'A' || chr != 'B') { out = 'Z'; } Yes No

10 Dangling Else - What is out?
int value = 0; String out = "abc"; if ( value >= 1 ) if ( value <= 10) out = "1 to 10"; else out = "else"; out: abc out: 1 to 10 out: else out: abcelse

11 Values of a1 and a2? String str1 = "Hello";
a1: true a2: true a1: false a2: false String str1 = "Hello"; String str2 = new String( "Hello"); boolean a1 = str1 == str2; boolean a2 = str1.equals(str2);

12 Values of a1 and a2? char ch1 = 'H'; String str = "Hello";
a1: true a2: true a1: false a2: false char ch1 = 'H'; String str = "Hello"; char ch2 = str.charAt(0); boolean a1 = ch1 == ch2; boolean a2 = ch1.equals(ch2);

13 java.util.Random Random randGen; //Declare reference variable
randGen = new Random(); //create instance // randGen.setSeed( 123); //set state int valueA = randGen.nextInt( 5); //get a value int valueB = randGen.nextInt( 5); int valueC = randGen.nextInt( 5);

14 Eclipse IDE (Integrated Development Environment)
Interpreter Translator Users Editor (Virtual) Machine Compiler Hello.java Hello.class Computer Files Programmer

15 Programming Errors Syntax/Compile time Runtime & Logic Files
Editor (Virtual) Machine Compiler Hello.java Hello.class Computer Files Programmer

16 Programming Errors Syntax compiler error Logic
program runs but provides wrong values Runtime program crashes or throws exception

17 Debugging print statements Java Visualizer Breakpoints in Eclipse

18 What is print out? a char choice = 'a'; switch (choice) {
case 'a': System.out.print("a"); case 'b': System.out.print("b"); break; default: System.out.print("other"); } a b ab other

19 char - Which will compile?
1, 2, 3 1, 3 1 2, 3 int intValue = 0; char aChar = 'B'; intValue = aChar; // 1 aChar = intValue + 1; // 2 aChar = '\u0041'; // 3

20 Increment && Decrement
i = i + 1; i += 1; i++; ++i; i = i -1; i -= 2; --i; i--;

21 Week 3 Exam Confirmation and Accommodation Requests Due Tomorrow

22 Puzzle int i = 5, j = 2, k; j += 3; k = i++ + --j;
After this executes, what are the values in i, j and k?

23 Puzzle int i = 5, j = 2, k; j = i + 3; k = i++ + --j - 1;
After this executes, what are the values in i, j and k?

24 Floating Down A Circular River?!

25 How many times will this execute?
1 until num > 0 until num <= 0 Scanner input = new Scanner( System.in); int num; int sum = 0; do { num = input.nextInt(); sum += num; } while ( num > 0);

26 Body of While will execute...
boolean finished = false; while ( !finished) { String line = input.nextLine(); if ( line.equals("done")) { finished = true; } else { System.out.println("echo:" + line); } until "done" is input never forever other?

27 How many "hello"? for ( int a = 0; a <= 5; a++) {
System.out.print("hello "); } 4 5 6 other?

28 How many "hello"? for ( int a = 5; a >= 1; a++) {
System.out.print("hello "); } 4 5 6 other?

29 How many "bye"? for ( int i = 1; i <= 5; i++) {
for ( int j = 2; j < 6; j++) { System.out.print("bye "); } 5 9 20 other?

30 What does this print? for ( int i = 1; i <= 5; i++) {
1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3 4,1 4,2 4,3 5,1 5,2 5,3 1,1 2,1 3,1 1,2 2,2 3,2 1,3 2,3 3,3 1,4 2,4 3,4 1,5 2,5 3,5 for ( int i = 1; i <= 5; i++) { for ( int j = 1; j <= 3; j++) { System.out.print(i+","+j+" "); } System.out.println();

31 What is the value of count?
3 9 12 other int count = 0; for ( int i = 1; i < 4; i++) { for ( int j = 6; j > 3; j--) { count += 1; } System.out.println("count=" + count);

32 What is the value of sum? 3 9 12 other int sum= 0;
boolean done = false; for ( int i = 1; i < 4 && !done; i++) { for ( int j = 6; j > 3; j--) { if ( j > 4) continue; sum += 1; } if ( i == 3) break; System.out.println("sum=" + sum); 3 9 12 other

33 Scanner num: 0 str4: line str4: line note. num: 3 str4:
String note = "Hmm\na 3\nline note."; Scanner input = new Scanner( note); int num = 0; String str1 = input.nextLine(); String str2 = input.next(); if ( input.hasNextInt()) { num = input.nextInt(); } String str4 = input.nextLine();

34 What is the value of msg? boolean flag = true;
String msg = "before if"; if ( flag = false); { msg = "" + flag; } before if true false


Download ppt "CS 302 - Week 3 Jim Williams, PhD."

Similar presentations


Ads by Google