CS Problem Solving and Object Oriented Programming Spring 2019

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

CS107 Introduction to Computer Science Lecture 2.
CS0004: Introduction to Programming Repetition – Do Loops.
CS 180 Problem Solving and Object Oriented Programming Spring 2011 February 16, 2011 Aditya Mathur Department of Computer Science Purdue University West.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 5: September 19-23, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 3: September 5-9, 2011 Aditya Mathur Department of Computer Science Purdue.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
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.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 11: Oct 31-Nov 4, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 13 Conditional.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
Control Structures II Chapter 3
Chapter 3: Decisions and Loops
Repetition (While-Loop) version]
The switch Statement, and Introduction to Looping
CS161 Introduction to Computer Science
Pseudocode Upsorn Praphamontripong CS 1110 Introduction to Programming
CS1371 Introduction to Computing for Engineers
Introduction To Flowcharting
CS1010 Programming Methodology
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Topic 5 for Loops -Arthur Schopenhauer
Chapter 6: Conditional Statements and Loops
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Looping and Repetition
Control Structure Senior Lecturer
Outline Altering flow of control Boolean expressions
CS190/295 Programming in Python for Life Sciences: Lecture 6
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
Repetition Control Structure
CMPT 102 Introduction to Scientific Computer Programming
3. Decision Structures Rocky K. C. Chang 19 September 2018
Computer Science Core Concepts
Topics Introduction to Repetition Structures
Chapter 13 Conditional Repetition
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
Looping and Repetition
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

CS 18000 Problem Solving and Object Oriented Programming Spring 2019 Section LE2 Week 4: Lecture 6 January 28. 2019 Slides updated: 3:28pm, January 28, 2019 Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA ps://www.cs.purdue.edu/homes/apm/courses/CS180_Java/CS180Spring2019/

©Aditya Mathur. CS 180. Spring 2019. Week 4 Review of Week 4 Straight-line vs “wiggly” programs if (c) {S} conditions if (c){ S1 } else{ S2 if (c1){ S1 } else if(c2){ S2 else if(c3){ S3 }….. if-else if-else-if switch 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Review of Week 4: Conditional statements switch(x){ case 1: S1; break; case 2: S2; break; .. case n: Sn; break; default: SS; } if (c) {S} if (c1){ S1 } else if(c2){ S2 else if(c3){ S3 .. else{ SS if (c){ S1 } else{ S2 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Today Repetitive execution Creating loops using the while-statement 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Loops 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Problem-1: Simple Repetition Write a Java program that 1. gets an integer N from the keyboard, and 2. prints N*N. The program repeats the above steps until the user enters N<=0. 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

Algorithm (Flow diagram) Start done=false Initialization if (!done) Loop termination condition false Exit Get N true Loop body N<0 ? Yes done=true print (N*N) No 1/28/2019

Elements of a loop Initialization: Usually variables that will change as the loop executes. Condition: If true the loop body is executed else the loop terminates. Loop body: A sequence of statements that are executed repeatedly as long as the condition remains true. 1/28/2019

Loop termination A loop must terminate Check your loop and make sure the loop condition WILL become false at some point during program execution. Or: There should be a break statement inside the loop that WILL be executed at least once during program execution. Note: Some loops are written on purpose so they never terminate. But in this class you will not be writing such loops. 1/28/2019

©Aditya Mathur. CS 180. Spring 2019. Week 4 Problem-2: AutoCopV4 Write a Java program that performs the following tasks: Defines the speed limit for cars on a highway. Prompts the user to input the current speed of a car. If the speed exceeds the maximum allowable then it sets a boolean variable ticket to true else sets it to false. If a ticket is given then print “This vehicle is above the speed limit.” Repeats the above steps until the user enters a negative integer as the speed. 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Problem-3 Monitor the pulse rate of a patient every 5 seconds. If the pulse rate is between 60 and 100 then display “Normal”. If the rate is below 60, then display “Low”. If the rate is above 100 then display “High”. 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Algorithm (Graphical) Range: 60-100 Start false if (c) true Get pulse rate (pR) What should be c? Yes No pR in range pR<60 true false Display “Normal” Display “Low” Display “High” Wait for 5 seconds” ©Aditya Mathur. CS 180. Spring 2019. Week 4 End 1/28/2019

©Aditya Mathur. CS 180. Spring 2019. Week 4 Algorithm [Textual] do forever { get pulse rate; if the pulse rate is between 60 and 100 then Display “Normal”; else if the pulse rate is<60 then Display “Low”; Display “High”; Wait for 5 seconds; } 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Loops in Java Start initialization statements; Initialization false while (condition){ statements; } condition true statements statements statements; End ©Aditya Mathur. CS 180. Spring 2019. Week 4 1/28/2019

©Aditya Mathur. CS 180. Spring 2019. Week 4 Problem: Movement of a gas particle Assume that a particle is located at the origin (position (0,0)). At each time instance the particle moves randomly one step of length 1 in any one of the four directions: left, up, right, and down. Write a Java program that simulates the behavior of this particle until it has reached a distance of 100 units or more from the origin. Print the number of steps needed to reach 100 units or more. D R U L Initial condition 1 Move right After one move √ 2 Move up After two moves 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Problem: Solution Step 1 Let us first write and test a Java program that moves the particle one step in any of the four directions selected randomly. Each direction will be represented by integers 1, 2, 3, and 4 for, respectively, L, U, R, and D. If x and y denote the current position of the particle, then the distance from the origin is √(x2 + y2) √ 2 In addition to what we have learnt so far, we will use the Java if-statement to solve this problem. 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Problem: Solution Step 2 We will now modify the program written earlier to add a loop so that the particle continues to move while it is at a distance less than 100 units from the origin. write and test a Java program that moves the particle one step in any of the four directions selected randomly. If dist denotes the current distance of the particle from the origin then what condition must be true for the particle to take another step? In addition to what we have learnt so far, we will use the Java while-statement to solve this problem. 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Live demo 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

©Aditya Mathur. CS 180. Spring 2019. Week 4 Live demo 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4

Hope you enjoyed this week! Questions? Week 4: January 28, 2019 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. 1/28/2019 ©Aditya Mathur. CS 180. Spring 2019. Week 4