CS 302 - Week 3 Jim Williams.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.
Review Review Review. Concepts Comments: definition, example, different types of comments Class: definition, example Object: definition, example Data.
CS107 Introduction to Computer Science Java Basics.
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Repetition Statements while and do while loops
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Overview of Java CSCI 392 Day One. Running C code vs Java code C Source Code C Compiler Object File (machine code) Library Files Linker Executable File.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Repetition Statements
Basic concepts of C++ Presented by Prof. Satyajit De
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CSC111 Quick Revision.
Chapter 4 Repetition Statements (loops)
Yanal Alahmad Java Workshop Yanal Alahmad
Loop Structures.
Java Programming: From Problem Analysis to Program Design, 4e
TK1114 Computer Programming
Comp Sci 200 Programming I Jim Williams, PhD.
CS Programming I Jim Williams, PhD.
CS Week 2 Jim Williams, PhD.
CS 302 Week 15 Jim Williams, PhD.
CS 200 Branches Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
Comp Sci 302 Introduction to Programming
CS 200 Using Objects Jim Williams, PhD.
CS Week 6 Jim Williams, PhD.
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
CS Week 7 Jim Williams, PhD.
Outline Altering flow of control Boolean expressions
CS Week 3 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
CS 200 More Primitives, Objects, Branches and Methods
Lec 4: while loop and do-while loop
CS 200 Loops Jim Williams, PhD.
CS 200 Arrays Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
CS 200 Primitives and Expressions
CS 200 Additional Topics and Review
CS 200 Methods, Using Objects
Nate Brunelle Today: Conditional Decision Statements
Repetition Statements
CS Week 2 Jim Williams, PhD.
CS Programming I Jim Williams, PhD.
Chap 7. Advanced Control Statements in Java
CSS161: Fundamentals of Computing
CS Week 3 Jim Williams, PhD.
Ben Stanley for gAlpha gALPHA free, four-week venture-creation workshop designed to help entrepreneurially-minded students and technologists create high-growth.
CS Programming I Jim Williams, PhD.
CS302 Week 6 Jim Williams.
Presentation transcript:

CS 302 - Week 3 Jim Williams

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 https://www.ted.com/talks/carol_dweck_the_power_of_believing_that_you_can_improve

P1 Milestone 2 due Thursday 8:00am Week 3 P1 Milestone 2 due Thursday 8:00am Commenting Example Exam Confirmation and Accommodation requests due by Friday CS Learning Center In CS Building, above entrance to Union South https://cs302-www.cs.wisc.edu/wp/contact-3/#CSLearningCenter

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

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

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

Debugging print statements Java Visualizer Breakpoints in Eclipse https://en.wikipedia.org/wiki/Debugging

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

? : operator (not recommended) condition ? <true value> : <false value> What is out? String out = "don't know"; boolean sunny = false; out = sunny ? “sunny” : “cloudy”; out:don't know out:sunny out:cloudy

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

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

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

Week 3 Exam Confirmation and Accommodation Requests Due Tomorrow

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

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?

Floating Down A Circular River?! http://www.thehiltonorlando.com/discover/pools-and-lazy-river/

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

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?

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

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

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?

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

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

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