Introduction to Programming

Slides:



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

While Loops. Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right.
 Control structures  Algorithm & flowchart  If statements  While statements.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
By the end of this session you should be able to...
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 11.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Week91 APCS-A: Java Problem Solving November 2, 2005.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Identify the Appropriate Method for Handling Repetition
REPETITION CONTROL STRUCTURE
Week of 12/12/16 Test Review.
Selection and Python Syntax
Lecture 7: Repeating a Known Number of Times
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 5: Control Structures II
Week 4 – Repetition Structures / Loops
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
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
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.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays An Array is an ordered collection of variables
Java Fix a program that has if Online time for Monday’s Program
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Introduction to pseudocode
Introduction to Programming
Chapter 4 Loops While loop The for loop do… while break and continue
Topic 1: Problem Solving
Coding Concepts (Basics)
Introduction to TouchDevelop
The ‘while’ loop ‘round and ‘round we go.
Java Fix a program that has if Online time for Monday’s Program
Computer Science Core Concepts
Python programming exercise
Repetition Statements (Loops) - 2
Announcements Lab 3 was due today Assignment 2 due next Wednesday
LCC 6310 Computation as an Expressive Medium
Starter Look at the hand-out.
Iteration – While Loops
Week 7 - Monday CS 121.
Presentation transcript:

Introduction to Programming Ms. Knudtzon C Period Monday Oct 4 – Thursday Oct 7 Week 5

In Class Lab Monday, Tuesday & Wednesday class periods Work on the lab assignment, getting help from Ms.K as necessary Week 5

Thursday Lab Review Week 5

What’d we learn? Task 0 and Task 1 are working for everyone (I think) But could you do it again without looking at your code? Did you understand what you did and why you did it? Let’s look in detail at what we did Week 5

Lab: Task 0 Looping until the user says to quit Draw out the algorithm (steps) for the code What does it look like in Java? Task 0: Reading, beginning to lay out the code. Need to understand that we are making a class, that we have a variable of type InputReader that is handling the user input for us (declaring that variable), making the constructor and creating the InputReader object (with the new command), making the driver method for our code – “runMenu” which presents the user with the options of what the program does Also need to understand calling another method (getTasks) from inside runMenu and how that works in Java Algorithm for runMenu: Print the menu of choices ask the user to choose one while the user hasn’t said they want to quit do the choice the user wants print the menu again ask the user to choose again when the user quits say bye My code to do this (which calls other methods): public void runMenu(){ int choice = 0; printWelcome(); printMenu(); choice = reader.getPositiveInt(); while(choice != 1){ doChoice(choice); System.out.println("Press enter to continue"); reader.getStrInput(); //get the enter before printing the menu } System.out.println("Bye."); Week 5

Task 1: Factorial Draw out the algorithm (steps) for the code What does it look like in Java? Algorithm: Get the number the user wants to compute the factorial of multiply ((1 * 2) * 3) … [number user inputted]  indicates we want a for loop starting at 1 and ending at number  we want to store the first multiplication and multiply that result times the next number in the serier (In class exercise – draw all the variable “containers” and see what they hold at each step My Java code to do this: (Note: There are multiple ways to accomplish this task!) /* * This method computes the factorial of the integer provided by a user */ private int factorial(){ int num = reader.getPositiveInt(); int fact = 1; for(int i=1; i <=num; i++){ fact *= i; } return fact; Week 5

Task 2: Additive Factorial Draw out the algorithm (steps) for the code What does it look like in Java? Same algorithm as in factorial, only this time we are adding the next number to the result My code to do this: private int addFact(){ int num = reader.getPositiveInt(); int fact = 0; for(int i=1; i <=num; i++){ fact += i; } return fact; Week 5

Task 4: Next Perfect Square Draw out the algorithm (steps) for the code What does it look like in Java? Algorithm (do as an in class exercise) Get number from the user have a count variable that increases by 1 each time store the current square value (count * count) before we increase the count variable, see if the user’s number equals or is less than the square value if it is  we’ve found the next perfect square if not  we need to increase count and keep going My code to do this: (Note: There are multiple ways to accomplish this task!) (In class, also have Austin show the way he did this code, since he used a for loop) private int nextSquare(){ int num = reader.getPositiveInt(); boolean found = false; int counter = 1; int square = 1; while(!found){ if(num == square){ //the number entered was a perfect square found = true; } else if(num < square){ //we've found the next perfect square else{ //the squares we've found are still less than the user's input counter++; square = counter*counter; return square; Week 5

Weekend Homework Task 5 – Guessing Game Task 3: Fibonacci numbers 3 iterations of guessing game Shouldn’t be too difficult Task 3: Fibonacci numbers Series: 0,1,1,2,3,5,8,13,21,34,55,89 A little tricky, do your best – if you can’t get the program to work, turn in a pseudo-code algorithm laying out what you think might work We’ll have a mini-quiz on Tuesday in class Making sure you understand the problem-solving techniques and code that you did in the lab Week 5

Test if (testScore < 90 && studentWantsToImproveGrade){ doExtraCredit(); } public void doExtraCredit(){ //See Ms. K for what to do Week 5