Outline. CIS 110: Intro to Computer Programming. What Do Our Programs Look Like? The Scanner Object. CIS 110 (11fa) - University of Pennsylvania 1

Similar documents
Outline. CIS 110: Introduction to Computer Programming. Review: Interactive Sum. More Cumulative Algorithms. Interactive Sum Trace (2)

News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

Programming Assignment Six. Semester Calendar. 1D Excel Worksheet Arrays. Review VBA Arrays from Excel. Programming Assignment Six May 2, 2017

Terminal Window. 11. Section 7 Exercises Program Memory Exercise 7-1 Swap Values in an Array Working memory Global Memory. 2 nd call 3 rd call

Midterms Save the Dates!

A Taste of Java and Object-Oriented Programming

CMPS 10 Introduction to Computer Science Lecture Notes

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

Programming in Fortran 90 : 2017/2018

9. BASIC programming: Control and Repetition

Brave New World Pseudocode Reference

Midterms Save the Dates!

CIS 110: Introduction to Computer Programming

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

Assembler. Building a Modern Computer From First Principles.

Oracle Database: SQL and PL/SQL Fundamentals Certification Course

Pass by Reference vs. Pass by Value

Agenda & Reading. Simple If. Decision-Making Statements. COMPSCI 280 S1C Applications Programming. Programming Fundamentals

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

Kent State University CS 4/ Design and Analysis of Algorithms. Dept. of Math & Computer Science LECT-16. Dynamic Programming

Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

Lecture 3: Computer Arithmetic: Multiplication and Division

Overview. CSC 2400: Computer Systems. Pointers in C. Pointers - Variables that hold memory addresses - Using pointers to do call-by-reference in C

Notes on Organizing Java Code: Packages, Visibility, and Scope

Esc101 Lecture 1 st April, 2008 Generating Permutation

Computer models of motion: Iterative calculations

OPL: a modelling language

Solving two-person zero-sum game by Matlab

The Codesign Challenge

ELEC 377 Operating Systems. Week 6 Class 3

LLVM passes and Intro to Loop Transformation Frameworks

Problem Set 3 Solutions

The example below contains two doors and no floor level obstacles. Your panel calculator should now look something like this: 2,400

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

Outline. Midterm Review. Declaring Variables. Main Variable Data Types. Symbolic Constants. Arithmetic Operators. Midterm Review March 24, 2014

CE 221 Data Structures and Algorithms

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Dynamic Programming. Example - multi-stage graph. sink. source. Data Structures &Algorithms II

Outline. Third Programming Project Two-Dimensional Arrays. Files You Can Download. Exercise 8 Linear Regression. General Regression

Final Exam. CSE Section M, Winter p. 1 of 16. Family Name: Given Name(s): Student Number:

CS1100 Introduction to Programming

Loop Transformations, Dependences, and Parallelization

Introduction. Leslie Lamports Time, Clocks & the Ordering of Events in a Distributed System. Overview. Introduction Concepts: Time

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

Model Clipping Triangle Strips and Quad Meshes.

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

such that is accepted of states in , where Finite Automata Lecture 2-1: Regular Languages be an FA. A string is the transition function,

MATHEMATICS FORM ONE SCHEME OF WORK 2004

High level vs Low Level. What is a Computer Program? What does gcc do for you? Program = Instructions + Data. Basic Computer Organization

Intro. Iterators. 1. Access

Parallelism for Nested Loops with Non-uniform and Flow Dependences

Parallel Numerics. 1 Preconditioning & Iterative Solvers (From 2016)

an assocated logc allows the proof of safety and lveness propertes. The Unty model nvolves on the one hand a programmng language and, on the other han

6.854 Advanced Algorithms Petar Maymounkov Problem Set 11 (November 23, 2005) With: Benjamin Rossman, Oren Weimann, and Pouya Kheradpour

CSE 326: Data Structures Quicksort Comparison Sorting Bound

From zero to Matlab in six weeks

Improving Low Density Parity Check Codes Over the Erasure Channel. The Nelder Mead Downhill Simplex Method. Scott Stransky

On Some Entertaining Applications of the Concept of Set in Computer Science Course

Lecture #15 Lecture Notes

Conditional Speculative Decimal Addition*

Concurrent models of computation for embedded software

2D Raster Graphics. Integer grid Sequential (left-right, top-down) scan. Computer Graphics

An Optimal Algorithm for Prufer Codes *

Private Information Retrieval (PIR)

Ontology Generator from Relational Database Based on Jena

Introduction to Geometrical Optics - a 2D ray tracing Excel model for spherical mirrors - Part 2

Dijkstra s Single Source Algorithm. All-Pairs Shortest Paths. Dynamic Programming Solution. Performance. Decision Sequence.

An Entropy-Based Approach to Integrated Information Needs Assessment

LECTURE 7. Lex and Intro to Parsing

The PCAT Programming Language Reference Manual

This chapter discusses aspects of heat conduction. The equilibrium heat conduction on a rod. In this chapter, Arrays will be discussed.

An Approach to Real-Time Recognition of Chinese Handwritten Sentences

An Approach in Coloring Semi-Regular Tilings on the Hyperbolic Plane

Exercises (Part 4) Introduction to R UCLA/CCPR. John Fox, February 2005

Compiling Process Networks to Interaction Nets

Lecture 5: Multilayer Perceptrons

AMath 483/583 Lecture 21 May 13, Notes: Notes: Jacobi iteration. Notes: Jacobi with OpenMP coarse grain

Chapter 2 Introduction to Query Optimization

Dijkstra s Single Source Algorithm. All-Pairs Shortest Paths. Dynamic Programming Solution. Performance

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Transaction-Consistent Global Checkpoints in a Distributed Database System

Introduction to Programming. Lecture 13: Container data structures. Container data structures. Topics for this lecture. A basic issue with containers

AADL : about scheduling analysis

T3 (IP) Classic connected to Integral 5

Biostatistics 615/815

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

Checking Multiple Conditions

Quicksort. Part 1: Understanding Quicksort

Priority queues and heaps Professors Clark F. Olson and Carol Zander

Internet Traffic Managers

ON SOME ENTERTAINING APPLICATIONS OF THE CONCEPT OF SET IN COMPUTER SCIENCE COURSE

Wightman. Mobility. Quick Reference Guide THIS SPACE INTENTIONALLY LEFT BLANK

Composition of UML Described Refactoring Rules *

Math Homotopy Theory Additional notes

Related-Mode Attacks on CTR Encryption Mode

Transcription:

Outlne CIS 110: Intro to Computer Programmng The Scanner Object Introducng Condtonal Statements Cumulatve Algorthms Lecture 10 Interacton and Condtonals ( 3.3, 4.1-4.2) 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 1 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 2 The Scanner Object What Do Our Programs Look Lke? 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 3 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 4 Ths program replcates a drawng of the snkng Ttanc and two survvors n the water. **** ***** ****** ******* ******** ********* ********** *********** *********** \o_ \o/ ~^~^~^~^~^~^~^~^~^~^~^~^~^~^ ~^~^~^~^~^~^~^~^~^~^~^~^~^~^ ~^~^~^~^~^~^~^~^~^~^~^~^~^~^ ~^~^~^~^~^~^~^~^~^~^~^~^~^~^ 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 5 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 6 CIS 110 (11fa) - Unversty of Pennsylvana 1

10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 7 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 8 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 9 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 10 / \ / \ \./ \./ ------------- \/ (o) (o) \/ \/ <> \/ ^ ^ ^ ************ ^ ^ * /\ /\ * ^ ^ * /\ /\ /\ * ^ ^ * /\ /\ /\ * ^ ^ * * ^ ^ * * ^ ^ * * ^ ------------- 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 11 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 12 CIS 110 (11fa) - Unversty of Pennsylvana 2

10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 13 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 14 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 15 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 16 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 17 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 18 CIS 110 (11fa) - Unversty of Pennsylvana 3

Real World Programs Out There Real programs are INTERACTIVE! 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 19 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 20 Introducng the Scanner Class Object that lets you read nput from the user. Scanner s n the "java.utl" package. mport java.utl.*; Creates a new Scanner that reads from some source. Scanner n = new Scanner(System.n); System.out.prntln( "Echo: " + n.nextlne()); "The keyboard" Grabs the next lne of nput from the Scanner. 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 21 Readng Input From The User // Reads n a double double d = n.nextdouble(); // Reads n an nteger nt n = n.nextint(); // Reads n an entre lne Strng lne = n.nextlne(); // Reads a token Strng token = n.next(); 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 22 Tokens Tokens are "chunks" of an nput separated by a delmter (here, whtespace). Ths s a strng "some tokens" n wth t! Tokens: ths, s, a, strng, wth, "some, tokens", n, t! Includes punctuaton (e.g., quotes and bangs). Asde: Packages and mport Classes are bundled nto sets called packages. The mport declaraton says that you wsh to use classes found n a partcular package. // Make avalable all classes n java.utl mport java.utl.*; // Make avalable just the Scanner class mport java.utl.scanner; 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 23 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 24 CIS 110 (11fa) - Unversty of Pennsylvana 4

Problem: Makng Decsons Based on User Input Condtonal Statements Scanner n = new Scanner(System.n); double savngs = n.nextdouble(); // If amount s greater than 100, // prnt a congratulatons msg! 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 25 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 26 Introducng Condtonal Statements Syntax of Condtonal Statements "Execute ths block only f savngs s greater than 100." Scanner n = new Scanner(System.n); double savngs = n.nextdouble(); f (savngs > 100) { System.out.prntln("Congratulatons!"); The test or guard f (<test>) { <statement> <statement> <statement> The statements to execute, the body or block 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 27 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 28 Semantcs of Condtonals Else Branches Is the guard true? TRUE Execute the Block Execute statements after condtonal double savngs = n.nextdouble(); f (savngs > 100) { System.out.prntln("Congratuatons!"); else { System.out.prntln("You need more money!"); FALSE "Execute ths block f we don't go nto the frst block" (.e., when savngs s less than or equal to 0). 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 29 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 30 CIS 110 (11fa) - Unversty of Pennsylvana 5

Else-f Branches Relatonal Operators double savngs = n.nextdouble(); f (savngs > 100) { System.out.prntln("Congratuatons!"); else f (savngs > 50) { System.out.prntln("That's decent."); else { System.out.prntln("Need more!"); "Else" = f the prevous guard fals, try ths one! 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 31 > /* greater than */ < /* less than */ >= /* greater than or equals */ <= /* less than or equals */ == /* equals */!= /* not-equals*/ // Syntax: <expr> <op> <expr>, e.g., 1!= 2 Only works on prmtve data. We'll dscuss what to do for objects, e.g., Strngs, later. 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 32 Operator Precedence ++, --, +, - // Unary operators *, /, % // Multplcaton operators +, - // Addton operators <, >, <=, >= // Relatonal operators ==,!= // Equalty operators =, +=, -=, *=, /=, %= // Assgnment operators V Lower Precedence 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 33 Mutually exclusve branches f (savngs < 100) { f (savngs < 100) { f (savngs >= 50) { f (savngs == 75) { else f (savngs >= 50) { else f (savngs == 75) { double savngs = 75; else f gves you true mutually exclusve branches. 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 34 Object Equalty Scanner n = new Scanner(System.n); Strng s = n.nextlne(); // Wll never be true. == only works for prmtve types. f (s == "yes") { System.out.prntln("s1 s yes!"); // Need to use the equals method to check equalty for objects. f (s.equals("yes")) { System.out.prntln("s1 s really yes!"); Multple Condtons Scanner n = new Scanner(System.n); Strng name = n.nextlne(); double amount = n.nextdouble(); // Logcal AND: true f both condtons are true f (name.equals("mcscrooge") && amount > 1000) { System.out.prntln("Y U SO RICH!?"); // Logcal OR: true f one of the condtons s true f (name.equals("peter") amount < 10) { System.out.prntln("Y U SO POOR!?"); 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 35 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 36 CIS 110 (11fa) - Unversty of Pennsylvana 6

Problem: Interactve Sum Cumulatve Algorthms Can you wrte a program that computes the sum of numbers from 1 to the user's nput? Scanner n = new Scanner(System.n); System.out.prnt("n? "); nt n = n.nextint(); System.out.prntln(); nt sum = 0; for (nt = 0; < n; ++) { sum += ; System.out.prntln( "Sum of 1 to " + 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 37 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 38 Interactve Sum Trace (1) Interactve Sum Trace (2) n 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 39 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 40 Interactve Sum Trace (3) Interactve Sum Trace (4) n n n 5 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 41 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 42 CIS 110 (11fa) - Unversty of Pennsylvana 7

Interactve Sum Trace (5) Interactve Sum Trace (6) n n 5 n n 5 sum 0 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 43 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 44 Interactve Sum Trace (7) Interactve Sum Trace (8) 0 0 n n 5 sum 0 n n 5 sum 0 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 45 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 46 Interactve Sum Trace (9) Interactve Sum Trace (10) 1 1 n n 5 sum 0 n n 5 sum 0 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 47 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 48 CIS 110 (11fa) - Unversty of Pennsylvana 8

Interactve Sum Trace (11) Interactve Sum Trace (12) 1 2 n n 5 sum 1 n n 5 sum 1 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 49 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 50 Interactve Sum Trace (13) Interactve Sum Trace (13) 2 3 n n 5 sum 3 n n 5 sum 3 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 51 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 52 Interactve Sum Trace (14) Interactve Sum Trace (15) 3 4 n n 5 sum 6 n n 5 sum 6 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 53 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 54 CIS 110 (11fa) - Unversty of Pennsylvana 9

Interactve Sum Trace (16) Interactve Sum Trace (17) 4 5 n n 5 sum 10 n n 5 sum 10 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 55 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 56 Interactve Sum Trace (18) n n 5 sum 10 10/15/2011 CIS 110 (11fa) - Unversty of Pennsylvana 57 CIS 110 (11fa) - Unversty of Pennsylvana 10