Download presentation
Presentation is loading. Please wait.
1
CS Week 4 Jim Williams, PhD
2
Understand vs. Apply Steph Curry, NBA Golden State Warriors
Basketball Analogy Demonstration vs. Practice Declarative Knowledge vs Procedural Knowledge Importance of Practice for learning If you watch the same video or even 10 different videos on how to shoot a basketball do you get better at shooting a basketball? Steph Curry, NBA Golden State Warriors
3
This Week Team Lab: Using Objects Program(s) 3 (due Thurs):
Exam 1 conflicts: Scheduling rooms and alternatives we can offer. Expect alternative offering late next week. Consulting hours: Request Help Queue Lecture: Using Objects, Branches
4
What are values of variables?
name: Minsub\n age: 22\nCS major: name: Minsub\n22\CS age: name: Minsub age: 22 String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();
5
Eclipse IDE Opening project, copying in files
Style and Commenting Guides Strings I/O Calling methods Compiler & Runtime Errors Scanner reading a number
6
What are values of variables?
name: Minsub\n22\CS age: major: name: Minsub age: 22 name: Minsub major: CS String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();
7
What are values of variables?
score1: 20 score2: 25 title: scores title: score1: score2: scores String note = "20 25\nscores"; Scanner input = new Scanner( note); int score1 = input.nextInt(); int score2 = input.nextInt(); String title = input.nextLine();
8
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);
9
Relational and Equality Operators
< <= > >= == != int n = 6; boolean result = n != 5; result: true result: false
10
Logical Operators && (AND) both sides true to be true, else false
|| (OR) either side true then true, else false ! (NOT) if true then false, and vice-versa !(a && b) == !a || !b //is this true or false?
11
Does !(a && b) == !a || !b Truth Table: a b a && b !(a && b) !a !b
12
What is result? boolean result = flag = false || flag; flag
true false try it.
13
== vs String equals() String str1 = "hello"; String str2 = "hello";
String str3 = new String("hello"); System.out.println( str1 == "hello" ); System.out.println( str1 == str2); System.out.println( str1.equals("hello")); System.out.println( str3 == str1); System.out.println( str3.equals( str2));
14
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 }
15
Floating Down a River
16
Side Trip, maybe multiple side trips
boolean tired = true; if ( tired) { System.out.println(“take break”); }
17
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”);
18
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
19
What is the value of msg? boolean flag = true;
String msg = "before if"; if ( flag = false); { msg = "" + flag; } before if true false
20
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
21
Scanner Scanner input = new Scanner("1 \ntwo \n 2\n\n");
int a = input.nextInt(); if ( input.hasNextInt()) { int b = input.nextInt(); } else { input.nextLine(); } String line = input.nextLine(); System.out.println("#" + line + "#");
22
Scanner num: 0 str4: 3 str4: line note. num: 3 str4:
String note = "Hmm\na \na\n3\n\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();
23
Debugging with Print statements
See what is going on. Divide and conquer.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.