CS 200 Branches Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Advertisements

CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Review Review Review. Concepts Comments: definition, example, different types of comments Class: definition, example Object: definition, example Data.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
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");
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
If Statement if (amount
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
 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.
CS 139 – Programming Fundamentals Lecture 08A. Relational/comparison Operators Relational Operator Meaning > is greater than < is less than >= is greater.
CS 1428 Exam I Review. Exam Format 130 Total Points – 40 Points Writing Programs – 30 Points Tracing Algorithms and determining results – 20 Points Short.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Selections Java.
Decisions Chapter 4.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
A mechanism for deciding whether an action should be taken
CS 1428 Exam I Review.
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
CSS 161: Fundamentals of Computing
Control Structures – Selection
CS Week 2 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
CS 302 Week 15 Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
Comp Sci 302 Introduction to Programming
CS Week 8 Jim Williams, PhD.
CS 200 Using Objects Jim Williams, PhD.
CS Week 6 Jim Williams, PhD.
CS Week 7 Jim Williams, PhD.
CS 1428 Exam I Review.
Selection (if-then-else)
CS Week 3 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
CS 200 Objects and ArrayList
Java so far Week 7.
CS 200 More Primitives, Objects, Branches and Methods
CS Week 3 Jim Williams.
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Lec 4: while loop and do-while loop
CS 200 Loops Jim Williams, PhD.
CS2011 Introduction to Programming I Selections (II)
CS 200 Primitives and Expressions
CS 200 Arrays Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
CS 200 Primitives and Expressions
CS 200 Methods, Using Objects
Control Structure Chapter 3.
Lecture Notes – Week 2 Lecture-2
CS 200 Objects and ArrayList
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Nate Brunelle Today: Conditional Decision Statements
CS Week 2 Jim Williams, PhD.
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 200 Objects and ArrayList
Control Structure.
CS 1428 Exam I Review.
CS302 Week 6 Jim Williams.
Presentation transcript:

CS 200 Branches Jim Williams, PhD

This Week Team Lab: Using Objects, take paper Thursday: P3 due Pre-Exam 1: bring WISC ID, #2 Pencils 6 questions, 15 minutes Consulting hours: Request Help Queue Lecture: Branches

Best statement? x = rand.nextInt(10)-2; What is the best statement to get a value between 2 and 10, inclusive of both 2 and 10? Assume: Random rand = new Random(); int x; //x = ??? x = rand.nextInt(10)-2; x = rand.nextInt(9)+1; x = rand.nextInt(8)+2; x = rand.nextInt(9)+2; Other try it.

What are values of variables? String note = " C S\n2 \n33\n end\n"; Scanner input = new Scanner( note); String str1 = input.nextLine(); int num1 = input.nextInt(); String str2 = input.next(); String str3 = input.nextLine(); num1: 2 str2: "33" str3: "" str2: " \n33" str3: "\n end" str3: " end"

Eclipse IDE Opening project, copying in files Style and Commenting Guides Strings I/O Calling methods Compiler & Runtime Errors Scanner reading a number

Relational and Equality Operators < <= > >= == != int n = 6; boolean result = n != 5; result: true result: false

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?

What is result? boolean result = flag = false || flag; Truth Table: try it.

Does !(a && b) == !a || !b Truth Table: a b a && b !(a && b) !a !b

Are these equivalent? boolean tired = true; boolean tired = true; 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 }

Floating Down a River http://www.thehiltonorlando.com/discover/pools-and-lazy-river/

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

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

Today pre-midterm 1 Lecture: continue with Branches 6 questions, 15 minutes Lecture: continue with Branches

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

Switch: What is printed out? 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

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

Comparing: == vs equals() Primitive data types use == for comparing primitive values No equals() method for primitive data types (but there is equals() method in wrapper classes) Reference data types use == for comparing references use equals() for comparing instance/object contents The meaning of equals() depends on the class it is defined in.

What is printed out? true *compiler error false int i = 6; int j = 6; System.out.println( i == j); System.out.println( i.equals( j)); Integer k = new Integer( 7); Integer m = new Integer( 7); System.out.println( k == m); System.out.println( k.equals( m)); true *compiler error false

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

String: == vs equals() String str1 = "hello"; String str2 = "hello"; String str3 = new String("hello"); String str4 = "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));

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 + "#");

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

Debugging with Print statements See what is going on. Divide and conquer.