CS 200 - Week 6 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
Advertisements

©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 7:
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Review Review Review. Concepts Comments: definition, example, different types of comments Class: definition, example Object: definition, example Data.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Chapter 4: Control Structures II
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter 2 Basic Computation
Intro to ETEC Java.
The switch Statement, and Introduction to Looping
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 5: Control Structures II
Primitive Data, Variables, Loops (Maybe)
Java Programming: From Problem Analysis to Program Design, 4e
CS Week 2 Jim Williams, PhD.
CS 200 Branches Jim Williams, PhD.
CS 200 Using Objects Jim Williams, PhD.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
CS Week 7 Jim Williams, PhD.
CS Week 3 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Chapter 2: Basic Elements of Java
Week 6 CS 302 Jim Williams, PhD.
CS 200 Objects and ArrayList
CS 200 More Primitives, Objects, Branches and Methods
CS Week 3 Jim Williams.
Control Statements Loops.
CS 200 Loops Jim Williams, PhD.
Chapter 6 Methods.
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
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Control Statements Loops.
Repetition Statements
CS Week 2 Jim Williams, PhD.
Additional control structures
Week 7 CS 302 Jim Williams.
Ben Stanley for gAlpha gALPHA free, four-week venture-creation workshop designed to help entrepreneurially-minded students and technologists create high-growth.
CS302 Week 6 Jim Williams.
Presentation transcript:

CS 200 - Week 6 Jim Williams, PhD

This Week Exam 1 - Thursday Team Lab: Branches & Maze Lecture: Loops

Midterm Exam 1 What is the location of your exam? 3650 Humanities 6210 Social Science 105 Psychology Other (due to conflicts) notified via email What is the location of your exam?

Midterm Exam - Thursday Bring ID and #2 pencil Exam seating directly in front/behind, 1 empty seat to each side Multiple choice - focus on tracing/explaining Java Review Questions posted on Piazza

Number Systems Decimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary 0, 1

Decimal 100 10 1 3 0 2 = 302

Binary 8 4 2 1 1 1 1 0 = 14

Binary 8 4 2 1 0 1 0 1 = 5

Convert from Binary to Decimal 40 41 42 other 101010

Convert from Decimal to Binary 11001 11101 10011 other 29

Names for Numbers of Bits 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 bit nibble (4 bits) byte (8 bits) 2 bytes (16)

Hexadecimal (group bits by nibble) 0000 = 0 0001 = 1 0010 = 2 0011 = 3 0100 = 4 0101 = 5 0110 = 6 0111 = 7 1000 = 8 1001 = 9 1010 = A 1011 = B 1100 = C 1101 = D 1110 = E 1111 = F

What character is this? 0000 0000 0100 0001 A B Unicode: @ Unicode: 0x003E 62 > 0x003F 63 ? 0x0040 64 @ 0x0041 65 A 0x0042 66 B 0x0043 67 C http://www.ssec.wisc.edu/~tomw/java/unicode.html

Color 183, 1, 1 B70101 Red, Green, Blue (RGB) 183, 1, 1 B70101 Red, Green, Blue (RGB) https://umark.wisc.edu/brand/web/colors.php

Incrementing m int m = 2; m = m + 1; //add one to m store in m m += 1; //compound operator m++; //increment operator (post) ++m; //increment operator (pre)

Value of i after this executes? int i = 5; i = i++; 5 6 other

Value of m after this executes? int i = 5; int k = 6; int m = i++ + ++k; 11 12 other

Difference between if and while boolean done = true; if ( done) { System.out.println( done); } while ( done) {

Which loop is the better choice? int a = 1; while ( a < 5) { System.out.println("hello"); a++; } while for other for ( int a = 1; a < 5; a++) { System.out.println("hell o"); }

3 loops - Different Purposes do { //indefinite - body executes at least once } while ( !finished); while ( !finished) { //indefinite - body may never execute } for ( int i=1; i < 5; i++) { //definite

Race Track https://www.wikihow.com/Race-Your-Car

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?

What is the value of i after this? int i = 4; int j = 3; while ( i <= 15) { i = 2 * -i + j; System.out.println( "i:" + i + " j:" + j); } 5 13 -23 49

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? int count = 0; for ( int i = 1; i < 4; i++) { for ( int j = 6; j > 3; j--) { count += 1; } System.out.println("count=" + count); 3 9 12 other

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

What does this do? for ( char ch = 'A'; ch < 'Z'; ch += 3) { System.out.print( ch); }

What do a and b possibly have in common? public static void main(String []args) { mA(); mB(); } static void mA() { int a = 10; static void mB() { int b; System.out.println( b); may be the exact same memory location looking at b will reveal a (unintentially) nothing Error A is the best answer. This example is typically discussed with a memory diagram intended to illustrate how local variables are allocated on the stack. Local variables are a part of the stack frame that are allocated when a method is called and discarded when the method ends. Since mA() has 1 int variable allocated (pushed on stack) and then discarded (popped off stack) then mB() may reuse that same memory area. However, this depends on how the Java virtual machine implements stack frames. Thinking through this possibility is intended to help develop a deep understanding of local variables.

Is result true or false? true Integer m = 5; Integer n = 5; false boolean result = m == n; true false error A is correct. The fine print at the provided link says for Integer instances based on int literals between -128 and 127 will have the same reference for the same int literal value. The intention of this example is to emphasize experimenting, discovering short illustrative examples and an introduction to the Java Language Specification. Part of relevant details: If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b. Ideally, boxing a primitive value would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rule above is a pragmatic compromise, requiring that certain common values always be boxed into indistinguishable objects. Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html