COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Size: px
Start display at page:

Download "COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand"

Transcription

1 COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

2 COSC 236 Web Site You will always find the course material at: or or From this site you can click on the COSC-236 tab to download the PowerPoint lectures, the Quiz solutions and the Laboratory assignments. 2

3 3

4 Review of Quiz 23 Use Lecture 22 Slide 42 as your starting point 4

5 Review of Quiz 23 Use Lecture 22 Slide 42 as your starting point Replace the System.out.println() statement with: System.out.println("Deal five cards:"); System.out.println( Deal five cards: ); Add a loop that executes five times with a single printf statement to print the output as shown: 5

6 Review of Quiz 23 Use Lecture 22 Slide 42 as your starting point Replace the System.out.println() statement with: System.out.println("Deal five cards:"); System.out.println( Deal five cards: ); for (int i = 1; i <= 5; i++) { System.out.printf(" Card %d = %-17s value of card = %d.\n", i, deck.get(i-1), value(deck.get(i-1))); Add a loop that executes five times with a single printf statement to print the output as shown: Let s look at this printf statement in detail 6

7 Review of Quiz 23 import java.util.*; public class Quiz23 { public static void main(string[] args) { String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"; List<String> deck = new ArrayList<String>(); for (String rank : ranks) { // build sorted deck for (String suit : suits) { System.out.println( Deal five cards: ); deck.add(rank + " of " + suit); for (int i = 1; i <= 5; i++) { Collections.shuffle(deck); System.out.printf(" Card %d = %-17s value of card = %d.\n", System.out.println("Deal five cards:"); i, deck.get(i-1), value(deck.get(i-1))); for (int i = 1; i <= 5; i++) { System.out.printf(" Card %d = %-17s value of card = %d.\n", i, deck.get(i-1), value(deck.get(i-1))); Let s look at this printf statement in detail 7

8 Review of Quiz 23 import java.util.*; public class Quiz23 { public static void main(string[] args) { String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"; List<String> deck = new ArrayList<String>(); for (String rank : ranks) { // build sorted deck for (String suit : suits) { System.out.println( Deal five cards: ); deck.add(rank + " of " + suit); for (int i = 1; i <= 5; i++) { Collections.shuffle(deck); System.out.printf(" Card %d = %-17s value of card = %d.\n", System.out.println("Deal five cards:"); i, deck.get(i-1), value(deck.get(i-1))); for (int i = 1; i <= 5; i++) { System.out.printf(" Card %d = %-17s value of card = %d.\n", i, deck.get(i-1), value(deck.get(i-1))); The %d formats an integer card number The %-17s left justifies (-) the String representing the card drawn in 17 character spaces. Queen of Diamonds is largest and takes 17 characters 8

9 Review of Quiz 23 import java.util.*; public class Quiz23 { public static void main(string[] args) { String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"; List<String> deck = new ArrayList<String>(); for (String rank : ranks) { // build sorted deck for (String suit : suits) { System.out.println( Deal five cards: ); deck.add(rank + " of " + suit); for (int i = 1; i <= 5; i++) { Collections.shuffle(deck); System.out.printf(" Card %d = %-17s value of card = %d.\n", System.out.println("Deal five cards:"); i, deck.get(i-1), value(deck.get(i-1))); for (int i = 1; i <= 5; i++) { System.out.printf(" Card %d = %-17s value of card = %d.\n", i, deck.get(i-1), value(deck.get(i-1))); Finally, we print out the value of the card in integer (%d) format This uses the method value that we must write to return the value of the card 9

10 Review of Quiz 23 import java.util.*; public class Quiz23 { public static void main(string[] args) { String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"; List<String> deck = new ArrayList<String>(); for (String rank : ranks) { // build sorted deck for (String suit : suits) { System.out.println( Deal five cards: ); deck.add(rank + " of " + suit); for (int i = 1; i <= 5; i++) { Collections.shuffle(deck); System.out.printf(" Card %d = %-17s value of card = %d.\n", System.out.println("Deal five cards:"); i, deck.get(i-1), value(deck.get(i-1))); for (int i = 1; i <= 5; i++) { System.out.printf(" Card %d = %-17s value of card = %d.\n", i, deck.get(i-1), value(deck.get(i-1))); Finally, we print out the value of the card in integer (%d) format This uses the method value that we must write to return the value of the card 10

11 Review of Quiz 23 public static int value(string card) { char number = card.charat(0); import java.util.*; public class Quiz23 { public static void main(string[] args) { String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"; if (number == '1') {return 10; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"; List<String> if (number deck = new == ArrayList<String>(); 'J') {return 10; for (String rank : ranks) { // build sorted deck if for (number (String suit : == suits) 'Q') { {return 10; System.out.println( Deal five cards: ); deck.add(rank + " of " + suit); for (int i = 1; i <= 5; i++) { Collections.shuffle(deck); if (number == 'K') {return System.out.printf(" 10; Card %d = %-17s value of card = %d.\n", System.out.println("Deal five cards:"); i, deck.get(i-1), value(deck.get(i-1))); for if (int (number i = 1; i <= == 5; i++) 'A') { {return 11; System.out.printf(" Card %d = %-17s value of card = %d.\n", i, deck.get(i-1), value(deck.get(i-1))); return Character.getNumericValue(number); Grab the first character of the card description The first character is a 1, J, Q, or K for 10, Jack, Queen or King each of which has value 10. The Ace has a value of 11 11

12 Review of Quiz 23 public static int value(string card) { char number = card.charat(0); import java.util.*; public class Quiz23 { public static void main(string[] args) { String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"; if (number == '1') {return 10; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"; List<String> if (number deck = new == ArrayList<String>(); 'J') {return 10; for (String rank : ranks) { // build sorted deck if for (number (String suit : == suits) 'Q') { {return 10; System.out.println( Deal five cards: ); deck.add(rank + " of " + suit); for (int i = 1; i <= 5; i++) { Collections.shuffle(deck); if (number == 'K') {return System.out.printf(" 10; Card %d = %-17s value of card = %d.\n", System.out.println("Deal five cards:"); i, deck.get(i-1), value(deck.get(i-1))); for if (int (number i = 1; i <= == 5; i++) 'A') { {return 11; System.out.printf(" Card %d = %-17s value of card = %d.\n", i, deck.get(i-1), value(deck.get(i-1))); return Character.getNumericValue(number); All other cards start with the value of the card Use the method Character.getNumericValue() to convert the character to its integer value equivalent 12

13 import java.util.*; Review of Quiz 23 public static int value(string card) { char number = card.charat(0); public class Quiz23 { { public static void void main(string[] args) args) { { = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"; if String[] (number ranks = {"2", == '1') "3", "4", {return "5", "6", "7", 10; "8", "9", "10", "Jack", "Queen", "King", "Ace"; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"; List<String> if String[] (number suits deck = new {"Clubs", == ArrayList<String>(); 'J') "Diamonds", {return "Hearts", 10; "Spades"; for List<String> (String rank deck : ranks) = new { // ArrayList<String>(); build sorted deck if for for (String (number suit rank :: == suits) ranks) 'Q') { { // {return build sorted 10; deck System.out.println( Deal five cards: ); for deck.add(rank (String suit + " of : suits) " + suit); { for (int i = 1; i <= 5; i++) { Collections.shuffle(deck); if deck.add(rank (number == + " of 'K') " + suit); {return System.out.printf(" 10; Card %d = %-17s value of card = %d.\n", System.out.println("Deal five cards:"); i, deck.get(i-1), value(deck.get(i-1))); Collections.shuffle(deck); for if System.out.println("Deal (int (number i = 1; i <= == 5; i++) 'A') { five cards:"); {return 11; System.out.printf(" Card %d = %-17s value of card = %d.\n", i, deck.get(i-1), value(deck.get(i-1))); return for (int i = Character.getNumericValue(number); 1; i <= 5; i++) { System.out.printf(" Card %d = %-17s value of card = %d.\n", i, deck.get(i-1), value(deck.get(i-1))); Here public static is int the value(string card) { char number = card.charat(0); if (number == '1') {return 10; if (number == 'J') {return 10; entire if (number == program: 'Q') {return 10; if (number == 'K') {return 10; if (number == 'A') {return 11; return Character.getNumericValue(number); 13

14 12.1 Thinking Recursively Introduction (pp ) A Nonprogramming Example (pp ) An Iterative Solution Converted to Recursion (pp ) Structure of Recursive Solutions (pp ) 12.2 A Better Example of Recursion Introduction (pp ) Mechanics of Recursion (pp ) 14

15 12.1 Thinking Recursively Introduction (pp ) This chapter focuses on a programming technique known as recursion that allows us to solve certain complex problems in a highly elegant manner. The chapter begins by comparing recursion with the problemsolving techniques you already know. Then it discusses the low-level mechanics that make recursion work in Java. Finally, we examine a number of problems that are easily expressed using this technique. 15

16 12.1 Thinking Recursively Introduction (pp ) Recursion turns out to have a surprising range of useful applications including Recursion instead of iteration (ie: replaces loops) Recursive graphics that are known as fractals But programming recursively also requires some special techniques that we ll have to explore. Recursive programming also requires a different mind-set in general So the chapter explores a large set of example problems to reinforce this new way of thinking. 16

17 12.1 Thinking Recursively Introduction (pp ) The problem-solving techniques we have employed so far fall under the heading of: Classical iteration, also known as The iterative approach Iteration (Iterative) A programming technique in which you describe actions to be repeated using a loop. This is what we have done so far in COSC-236 In this chapter we will explore a new technique known as recursion. Recursion (Recursive) A programming technique in which you describe actions to be repeated using a method that calls itself. 17

18 Recursion recursion: The definition of an operation in terms of itself. Solving a problem using recursion depends on solving smaller occurrences of the same problem. recursive programming: Writing methods that call themselves to solve problems recursively. An equally powerful substitute for iteration (loops) Particularly well-suited to solving certain types of problems 18

19 Why learn recursion? "cultural experience" - A different way of thinking of problems Can solve some kinds of problems better than iteration Elegant - simplistic, short code (when used well) Many programming languages ("functional" languages such as Scheme, ML, and Haskell) use recursion exclusively (no loops) A key component of COSC-237 (CS-2) which follows COSC

20 12.1 Thinking Recursively Introduction (pp ) A Nonprogramming Example (pp ) An Iterative Solution Converted to Recursion (pp ) Structure of Recursive Solutions (pp ) 12.2 A Better Example of Recursion Introduction (pp ) Mechanics of Recursion (pp ) 20

21 12.1 Thinking Recursively Introduction (pp ) A Nonprogramming Example (pp ) 21

22 12.1 Thinking Recursively A Nonprogramming Example (pp ) If you re standing in line, you might wonder what position you re in. Are you number 10 in the line? Number 20? How would you find out? 22

23 12.1 Thinking Recursively A Nonprogramming Example (pp ) Most people would solve this problem iteratively: by counting the people in the line: one, two, three, and so on. This approach is like a while loop that continues while there are more people left to count. 23

24 12.1 Thinking Recursively A Nonprogramming Example (pp ) The iterative approach is a natural one, but it has some limitations. For example, what if the person in front of you is taller than you? Will you be able to see past that person to count all the people in front of him? And what if the line goes around the block and you can t see around the corner to count the people there? 24

25 12.1 Thinking Recursively A Nonprogramming Example (pp ) Can you think of another way to determine your position in the line? To think about the problem recursively, you have to imagine that all the people standing in line work together to solve the problem: Instead of having one person do all of the counting, each person is responsible for a little piece. 25

26 12.1 Thinking Recursively A Nonprogramming Example (pp ) One cooperative approach would be to ask the person in front of you what your place in line is. That person would ask the person in front of them, who then would ask the person in front of them. Eventually we get to the person in the front of the line who knows their position! 26

27 12.1 Thinking Recursively A Nonprogramming Example (pp ) The person in the front of the line then starts the feedback in the opposite direction The one behind him is second in line, The one behind him is third in line, The one behind him is forth in line, etc Eventually we get to you -- and you then know your position in line 27

28 Exercise (pp ) (To a student in the front row) How many students total are directly behind you in your "column" of the classroom? You have poor vision, so you can see only the people right next to you. So you can't just look back and count. But you are allowed to ask questions of the person next to you. How can we solve this problem? (recursively ) 28

29 The idea (pp ) Recursion is all about breaking a big problem into smaller occurrences of that same problem. Each person can solve a small part of the problem. What is a small version of the problem that would be easy to answer? What information from a neighbor might help me? 29

30 Recursive algorithm (pp ) Number of people behind me: If there is someone behind me, ask him/her how many people are behind him/her. When they respond with a value N, then I will answer N + 1. If there is nobody behind me, I will answer 0. 30

31 Recursion and cases (pp ) Every recursive algorithm involves at least 2 cases: base case: A simple occurrence that can be answered directly. recursive case: A more complex occurrence of the problem that cannot be directly answered, but can instead be described in terms of smaller occurrences of the same problem. Some recursive algorithms have more than one base or recursive case, but all have at least one of each. A crucial part of recursive programming is identifying these cases. 31

32 12.1 Thinking Recursively Introduction (pp ) A Nonprogramming Example (pp ) An Iterative Solution Converted to Recursion (pp ) Structure of Recursive Solutions (pp ) 12.2 A Better Example of Recursion Introduction (pp ) Mechanics of Recursion (pp ) 32

33 12.1 Thinking Recursively Introduction (pp ) A Nonprogramming Example (pp ) An Iterative Solution Converted to Recursion (pp ) 33

34 12.1 Thinking Recursively An Iterative Solution Converted to Recursion (pp ) As a first example, we will explore a problem that has a simple iterative solution. It won t be a very impressive use of recursion because the problem is easily solved with iteration. But exploring a problem that has a straightforward iterative solution allows us to compare the two solutions. 34

35 Recursion in Java (pp ) Consider the following method to print a line of * characters: // Prints a line containing the given number of stars. // Precondition: n >= 0 public static void printstars(int n) { for (int i = 0; i < n; i++) { System.out.print("*"); System.out.println(); // end the line of output Write a recursive version of this method (that calls itself). Solve the problem without using any loops. Hint: Your solution should print just one star at a time. 35

36 A basic case (pp ) What are the cases to consider? What is a very easy number of stars to print without a loop? public static void printstars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); else {... 36

37 Handling more cases (pp ) Handling additional cases, with no loops (in a bad way): public static void printstars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); else if (n == 2) { System.out.print("*"); System.out.println("*"); else if (n == 3) { System.out.print("*"); System.out.print("*"); System.out.println("*"); else if (n == 4) { System.out.print("*"); System.out.print("*"); System.out.print("*"); System.out.println("*"); else... 37

38 Handling more cases 2 (pp ) Taking advantage of the repeated pattern (somewhat better): public static void printstars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); else if (n == 2) { System.out.print("*"); printstars(1); // prints "*" else if (n == 3) { System.out.print("*"); printstars(2); // prints "**" else if (n == 4) { System.out.print("*"); printstars(3); // prints "***" else... 38

39 Using recursion properly (pp ) Condensing the recursive cases into a single case: public static void printstars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); else { // recursive case; print one more star System.out.print("*"); printstars(n - 1); 39

40 "Recursion Zen (pp ) The real, even simpler, base case is an n of 0, not 1: public static void printstars(int n) { if (n == 0) { // base case; just end the line of output System.out.println(); else { // recursive case; print one more star System.out.print("*"); printstars(n - 1); Recursion Zen: The art of properly identifying the best set of cases for a recursive algorithm and expressing them elegantly. (A COSC 236 informal term) 40

41 12.1 Thinking Recursively Introduction (pp ) A Nonprogramming Example (pp ) An Iterative Solution Converted to Recursion (pp ) Structure of Recursive Solutions (pp ) 12.2 A Better Example of Recursion Introduction (pp ) Mechanics of Recursion (pp ) 41

42 12.1 Thinking Recursively Introduction (pp ) A Nonprogramming Example (pp ) An Iterative Solution Converted to Recursion (pp ) Structure of Recursive Solutions (pp ) 42

43 12.1 Thinking Recursively Structure of Recursive Solutions (pp ) Every recursive solution that you write will have two key ingredients: a base case a recursive case. Base Case A case within a recursive solution that is so simple that it can be solved directly without a recursive call. Recursive Case A case within a recursive solution that involves reducing the overall problem to a simpler problem of the same kind that can be solved by a recursive call. 43

44 12.1 Thinking Recursively Structure of Recursive Solutions (pp ) Let s look at a simple recursive program: a base case (print out Bottom) a recursive case (pre and post print) Base Case This occurs in the middle of the recursive algorithm Recursive Cases Pre-print occurs on the way to the bottom Post-print occurs on the way back up 44

45 import java.util.*; 12.1 Thinking Recursively public class Lecture24S32 { public static void main(string[] args) { Structure of Recursive Solutions (pp ) Scanner console = new Scanner(System.in); Let s look at a simple recursive program: System.out.print("Enter number of recursions: "); int n = console.nextint(); recursive(n); System.out.println("\n Base public static Case void recursive(int n) { if (n <= 0) {System.out.printf("\n Bottom. n = %d",n); else { Recursive System.out.printf("\n Cases Preprint. n = %d", n); recursive(n-1); System.out.printf("\nPostprint. n = %d", n); a base case (print out Bottom) a recursive case End. n = (pre " + n); and post print) This occurs in the middle of the recursive algorithm Pre-print occurs on the way to the bottom Post-print occurs on the way back up 45

46 import java.util.*; 12.1 Thinking Recursively public class Lecture24S32 { Structure of Recursive Solutions (pp ) public static void main(string[] args) { Main program calls Scanner console = new Scanner(System.in); Let s System.out.print("Enter look at a number simple of recursions: recursive "); program: recursive method a base case (print out Bottom) a recursive case End. n = (pre " + n); and post print) int n = console.nextint(); recursive(n); System.out.println("\n Base public static Case void recursive(int n) { if (n <= 0) {System.out.printf("\n Bottom. n = %d",n); else { Recursive System.out.printf("\n Cases Preprint. n = %d", n); recursive(n-1); System.out.printf("\nPostprint. n = %d", n); This occurs in the middle of the recursive algorithm Pre-print occurs on the way to the bottom Post-print occurs on the way back up Base prints out Bottom Pre-print before recursive call Post-print after recursive call 46

47 import java.util.*; 12.1 Thinking Recursively public class Lecture24S32 { Structure of Recursive Solutions (pp ) public static void main(string[] args) { Main program calls Scanner console = new Scanner(System.in); Let s System.out.print("Enter look at a number simple of recursions: recursive "); program: recursive method a base case (print out Bottom) a recursive case End. n = (pre " + n); and post print) int n = console.nextint(); recursive(n); System.out.println("\n Base public static Case void recursive(int n) { if (n <= 0) {System.out.printf("\n Bottom. n = %d",n); else { Recursive System.out.printf("\n Cases Preprint. n = %d", n); recursive(n-1); System.out.printf("\nPostprint. n = %d", n); This occurs in the middle of the recursive algorithm Pre-print occurs on the way to the bottom Post-print occurs on the way back up Base prints out Bottom Pre-print before recursive call Post-print after recursive call 47

48 12.1 Thinking Recursively Introduction (pp ) A Nonprogramming Example (pp ) An Iterative Solution Converted to Recursion (pp ) Structure of Recursive Solutions (pp ) 12.2 A Better Example of Recursion Introduction (pp ) Mechanics of Recursion (pp ) 48

49 12.1 Thinking Recursively Introduction (pp ) A Nonprogramming Example (pp ) An Iterative Solution Converted to Recursion (pp ) Structure of Recursive Solutions (pp ) 12.2 A Better Example of Recursion 49

50 12.2 A Better Example of Recursion (pp ) Consider the following recursive method: public static int mystery(int n) { if (n < 10) { return n; else { int a = n / 10; int b = n % 10; return mystery(a + b); What is the result of the following call? mystery(648) Recursive tracing 50

51 Recursive tracing (pp ) Consider the following recursive method: public static int mystery(int n) { if (n < 10) { return n; else { int a = n / 10; int b = n % 10; return mystery(a + b); What is the result of the following call? mystery(648) 51

52 A recursive trace mystery(648): int a = 648 / 10; // 64 int b = 648 % 10; // 8 return mystery(a + b); // mystery(72) mystery(72): int a = 72 / 10; // 7 int b = 72 % 10; // 2 return mystery(a + b); // mystery(9) mystery(9): return 9; 52

53 Recursive tracing 2 Consider the following recursive method: public static int mystery(int n) { if (n < 10) { return (10 * n) + n; else { int a = mystery(n / 10); int b = mystery(n % 10); return (100 * a) + b; What is the result of the following call? mystery(348) 53

54 A recursive trace 2 mystery(348) int a = mystery(34); int a = mystery(3); return (10 * 3) + 3; // 33 int b = mystery(4); return (10 * 4) + 4; // 44 return (100 * 33) + 44; // 3344 int b = mystery(8); return (10 * 8) + 8; // 88 return (100 * 3344) + 88; // What is this method really doing? 54

55 Assignments for this week 1. Laboratory for Chapter 13 due today Monday 11/24 IMPORTANT: When you me your laboratory Word Document, be sure it is all in one file 2. Laboratory for Chapter 12 due Monday 12/1 IMPORTANT: When you me your laboratory Word Document, be sure it is all in one file 3. Read pp (Chapter 12) for Monday 12/1 4. Be sure to complete Quiz 24 before leaving class tonight This is another program to write You must demonstrate the program to me before you leave lab 55

CSE 143 Lecture 10. Recursion

CSE 143 Lecture 10. Recursion CSE 143 Lecture 10 Recursion slides created by Marty Stepp and Alyssa Harding http://www.cs.washington.edu/143/ Recursion Iteration: a programming technique in which you describe actions to be repeated

More information

Lecture 19: Recursion

Lecture 19: Recursion Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Recursion recursion: The definition of an operation

More information

Recursion. Garfield AP Computer Science. As usual, significant borrowings from Stuart Reges and Marty Stepp at UW -- thanks!!

Recursion. Garfield AP Computer Science. As usual, significant borrowings from Stuart Reges and Marty Stepp at UW -- thanks!! Recursion Garfield AP Computer Science As usual, significant borrowings from Stuart Reges and Marty Stepp at UW -- thanks!! Definitions recursion: The definition of an operation in terms of itself. Solving

More information

CSE 143. Lecture 9: introduction to recursion reading: 12.1

CSE 143. Lecture 9: introduction to recursion reading: 12.1 CSE 143 Lecture 9: introduction to recursion reading: 12.1 Recursion recursion: The definition of an operation in terms of itself. Solving a problem using recursion depends on solving smaller occurrences

More information

Recursion. Readings: RS Chapter 12. Recursion. Recursion: The definition of an operation in terms of itself.

Recursion. Readings: RS Chapter 12. Recursion. Recursion: The definition of an operation in terms of itself. Recursion Readings: RS Chapter 12 CSC216: Programming Concepts Java NC State CSC216 Faculty Slides are modified from those provided by Marty Stepp www.buildingjavaprograms.com Some materials from Joseph

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 2: Recursion & Performance analysis 2018-2019 Fall System Life Cycle Programs pass through a period called system life cycle, which is defined by the following steps: 1.

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Jalil Boudjadar, Tommy Färnqvist. IDA, Linköping University

Jalil Boudjadar, Tommy Färnqvist. IDA, Linköping University Lecture 15 Recursion TDDD86: DALP Print version of the lecture Data structures, algorithms and programming paradigms 1 november 2016 Jalil Boudjadar, Tommy Färnqvist. IDA, Linköping University 15.1 Content

More information

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion. Notes - Recursion So far we have only learned how to solve problems iteratively using loops. We will now learn how to solve problems recursively by having a method call itself. A geeky definition of recursion

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

CSE11 Lecture 20 Fall 2013 Recursion

CSE11 Lecture 20 Fall 2013 Recursion CSE11 Lecture 20 Fall 2013 Recursion Recursion recursion: The definition of an operation in terms of itself. Solving a problem using recursion depends on solving smaller or simpler occurrences of the same

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

Recursion. Java recursive programming: Writing methods that call themselves to solve problems recursively.

Recursion. Java recursive programming: Writing methods that call themselves to solve problems recursively. Recursion recursion: The definition of an operation in terms of itself. Solving a problem using recursion depends on solving smaller occurrences of the same problem. Java recursive programming: Writing

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site I have decided to keep this site for the whole semester I still hope to have blackboard up and running, but you

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING INPUT AND OUTPUT Input devices Keyboard Mouse Hard drive Network Digital camera Microphone Output devices.

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-4: do/while loops, assertions reading: 5.1, 5.5 1 The do/while loop do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Generating random numbers Obtain a random double value between 0.0 and 1.0, excluding 1.0 Math.random() Random

More information

Chapter 13: Arrays of Objects

Chapter 13: Arrays of Objects Chapter 13: Arrays of Objects Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey As in Chapter 11, we will be following a more standard approach than the one taken by Downey in

More information

AP Programming - Chapter 17 Lecture page 1 of 5

AP Programming - Chapter 17 Lecture page 1 of 5 page 1 of 5 Recursion I. Thinking Recursively In this chapter we look at another method of repetition, recursion. A recursive computation solves a problem by calling itself to solve a smaller piece of

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-4: do/while loops, assertions reading: 5.1, 5.5 1 The do/while loop do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's

More information

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam Seat Number Name CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam This is a closed book exam. Answer all of the questions on the question paper in the space provided. If

More information

CIS 110: Introduction to Computer Programming

CIS 110: Introduction to Computer Programming CIS 110: Introduction to Computer Programming Lecture 8 Hey (Objects), Listen! ( 3.2-3.3) 10/2/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline Review: what is a library? The Math class The String

More information

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea Announcements Recursion and why study it Tutoring schedule updated Do you find the sessions helpful? Midterm exam 1: Tuesday, April 11, in class Scope: will cover up to recursion Closed book but one sheet,

More information

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003 1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 7, 2003 Name: Email Address: TA: Section: You have 90 minutes to complete this exam. For coding questions, you do not need to

More information

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

More information

CS141 Programming Assignment #6

CS141 Programming Assignment #6 CS141 Programming Assignment #6 Due Sunday, Nov 18th. 1) Write a class with methods to do the following output: a) 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 b) 1 2 3 4 5 4 3 2 1 1 2 3 4 * 4 3 2 1 1 2 3 * * * 3 2 1

More information

Wednesday January 31, 2:30-4pm ENGMC 103

Wednesday January 31, 2:30-4pm ENGMC 103 Academic Forum Wednesday January 31, 2:30-4pm ENGMC 103 The academic forum is an opportunity for students to provide feedback to the department on all aspects of the undergraduate academic experience,

More information

This exam is open book. Each question is worth 3 points.

This exam is open book. Each question is worth 3 points. This exam is open book. Each question is worth 3 points. Page 1 / 15 Page 2 / 15 Page 3 / 12 Page 4 / 18 Page 5 / 15 Page 6 / 9 Page 7 / 12 Page 8 / 6 Total / 100 (maximum is 102) 1. Are you in CS101 or

More information

Midterm Examination (MTA)

Midterm Examination (MTA) M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and

More information

CSE143 Notes for Monday, 4/25/11

CSE143 Notes for Monday, 4/25/11 CSE143 Notes for Monday, 4/25/11 I began a new topic: recursion. We have seen how to write code using loops, which a technique called iteration. Recursion an alternative to iteration that equally powerful.

More information

Debugging [continued]

Debugging [continued] 1 Debugging [continued] Admiral Grace Murray Hopper http://www.history.navy.mil/photos/images/h96000/h96566kc.htm 2 Debugging Your Program Debugging Your Program. [summary] 1. Edit the program (type in

More information

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz The in-class quiz is intended to give you a taste of the midterm, give you some early feedback about

More information

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money

More information

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs 1 Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs Michael Albert michael.albert@cs.otago.ac.nz Keywords: abstract data type, collection, generic class type, stack 2 Collections

More information

Lesson 24: Recursive Algorithms #1 (W07D3)

Lesson 24: Recursive Algorithms #1 (W07D3) Lesson 24: Recursive Algorithms #1 (W07D3) Balboa High School Michael Ferraro October 2, 2015 1 / 52 Do Now public static int mysteryfcn(int n) { //precondition: n > 0 int result = 1; while ( n >= 1 )

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

More information

! 52 playing cards in a deck. ! 5 thousand undergrads at Princeton. ! 1 million characters in a book. ! 10 million audio samples in an MP3 file.

! 52 playing cards in a deck. ! 5 thousand undergrads at Princeton. ! 1 million characters in a book. ! 10 million audio samples in an MP3 file. Arrays 1.4 Arrays This lecture. Store and manipulate huge quantities of data. Array. Indexed sequence of values of the same type. Examples.! 52 playing cards in a deck.! 5 thousand undergrads at Princeton.!

More information

CS201 - Assignment 1, Part 2 Due: Wednesday February 19, at the beginning of class

CS201 - Assignment 1, Part 2 Due: Wednesday February 19, at the beginning of class CS201 - Assignment 1, Part 2 Due: Wednesday February 19, at the beginning of class 1 Java Practice To give you some practice writing functions/methods in Java, for the first part of this assignment, you

More information

1. An operation in which an overall value is computed incrementally, often using a loop.

1. An operation in which an overall value is computed incrementally, often using a loop. Practice Exam 2 Part I: Vocabulary (10 points) Write the terms defined by the statements below. 1. An operation in which an overall value is computed incrementally, often using a loop. 2. The < (less than)

More information

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014 M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014 Question One: Choose the correct answer and write it on the external answer booklet. 1. Java is. a. case

More information

1 Short Answer (5 Points Each)

1 Short Answer (5 Points Each) COSC 117 Exam #1 Solutions Fall 015 1 Short Answer (5 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. A compiler will take a program written in

More information

7. Arrays, More Java Looping

7. Arrays, More Java Looping 7-1 7. Arrays, More Java Looping Review and Preview In the last class, we introduced the idea of looping repeating code blocks. In this class Java lesson, we look at another way to loop (the Java for loop)

More information

Example Program. public class ComputeArea {

Example Program. public class ComputeArea { COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for

More information

Example: Monte Carlo Simulation 1

Example: Monte Carlo Simulation 1 Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149

More information

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I AL GHURAIR UNIVERSITY College of Computing CSC 209 JAVA I week 2- Arithmetic and Decision Making: Equality and Relational Operators Objectives: To use arithmetic operators. The precedence of arithmetic

More information

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

More information

APCS-AB: Java. Recursion in Java December 12, week14 1

APCS-AB: Java. Recursion in Java December 12, week14 1 APCS-AB: Java Recursion in Java December 12, 2005 week14 1 Check point Double Linked List - extra project grade Must turn in today MBCS - Chapter 1 Installation Exercises Analysis Questions week14 2 Scheme

More information

CS 1083 Introduction to Programming I for Computer Scientists Sample Final Questions

CS 1083 Introduction to Programming I for Computer Scientists Sample Final Questions CS 1083 Introduction to Programming I for Computer Scientists Sample Final Questions 1. What is printed by the following program? Show all your work for partial credit. public class SampleFinalq1 { public

More information

Last Class. While loops Infinite loops Loop counters Iterations

Last Class. While loops Infinite loops Loop counters Iterations Last Class While loops Infinite loops Loop counters Iterations public class January31{ public static void main(string[] args) { while (true) { forloops(); if (checkclassunderstands() ) { break; } teacharrays();

More information

Recitation: Loop Jul 7, 2008

Recitation: Loop Jul 7, 2008 Nested Loop Recitation: Loop Jul 7, 2008 1. What is the output of the following program? Use pen and paper only. The output is: ****** ***** **** *** ** * 2. Test this program in your computer 3. Use "for

More information

Full file at

Full file at Chapter 2 Console Input and Output Multiple Choice 1) Valid arguments to the System.out object s println method include: (a) Anything with double quotes (b) String variables (c) Variables of type int (d)

More information

Top-Down Program Development

Top-Down Program Development Top-Down Program Development Top-down development is a way of thinking when you try to solve a programming problem It involves starting with the entire problem, and breaking it down into more manageable

More information

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1 DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1 March 11, 2018 Question 1: Identify the common elements of two sorted

More information

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Computer Programming, I. Laboratory Manual. Experiment #6. Loops Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #6

More information

Topic 12 more if/else, cumulative algorithms, printf

Topic 12 more if/else, cumulative algorithms, printf Topic 12 more if/else, cumulative algorithms, printf "We flew down weekly to meet with IBM, but they thought the way to measure software was the amount of code we wrote, when really the better the software,

More information

Recursion. Chapter Simple Recursion. Goals Trace recursive algorithms Implement recursive algorithms

Recursion. Chapter Simple Recursion. Goals Trace recursive algorithms Implement recursive algorithms Chapter 19 Recursion Goals Trace recursive algorithms Implement recursive algorithms 19.1 Simple Recursion One day, an instructor was having difficulties with a classroom s multimedia equipment. The bell

More information

Claremont McKenna College Computer Science

Claremont McKenna College Computer Science Claremont McKenna College Computer Science CS 51 Handout 4: Problem Set 4 February 10, 2011 This problem set is due 11:50pm on Wednesday, February 16. As usual, you may hand in yours until I make my solutions

More information

Array Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18

Array Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18 Array Lists CSE 1310 Introduction to Computers and Programming University of Texas at Arlington Last modified: 4/17/18 1 DEPARTAMENTAL FINAL EXAM Monday, DEC 10, 5:30pm-8pm rooms will be determined 2 Fixed

More information

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions: CS 170 Exam 2 Version: A Fall 2015 Name (as in OPUS) (print): Section: Seat Assignment: Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do

More information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

Nested Loops. A loop can be nested inside another loop.

Nested Loops. A loop can be nested inside another loop. Nested Loops A loop can be nested inside another loop. Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started

More information

Manipulating One-dimensional Arrays

Manipulating One-dimensional Arrays Manipulating One-dimensional Arrays Mitsu Ogihara Department of Computer Science University of Miami 1 / 30 Table of Contents 1 For each 2 Exchanging Values 3 Reversing 2 / 30 For-each iteration For enumerating

More information

1.4 Arrays. A Foundation for Programming. Arrays. Many Variables of the Same Type. This lecture. Store and manipulate huge quantities of data.

1.4 Arrays. A Foundation for Programming. Arrays. Many Variables of the Same Type. This lecture. Store and manipulate huge quantities of data. A Foundation for Programming 1.4 Arrays any program you might want to write objects functions and modules graphics, sound, and image I/O arrays store and manipulate huge quantities of data conditionals

More information

Classes Classes 2 / 35

Classes Classes 2 / 35 Classes 1 / 35 Classes Classes 2 / 35 Anatomy of a Class By the end of next lecture, you ll understand everything in this class definition. package edu. gatech. cs1331. card ; import java. util. Arrays

More information

Recursive Definitions

Recursive Definitions Recursion Objectives Explain the underlying concepts of recursion Examine recursive methods and unravel their processing steps Explain when recursion should and should not be used Demonstrate the use of

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination Tuesday, November 4, 2008 Examiners: Mathieu Petitpas [Section 1] 18:30

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

1.4 Arrays. A Foundation for Programming. Many Variables of the Same Type. Arrays. !!! any program you might want to write

1.4 Arrays. A Foundation for Programming. Many Variables of the Same Type. Arrays. !!! any program you might want to write A Foundation for Programming 1.4 Arrays any program you might want to write objects functions and modules graphics, sound, and image I/O arrays store and manipulate huge quantities of data conditionals

More information

Module 7: Arrays (Single Dimensional)

Module 7: Arrays (Single Dimensional) Module 7: Arrays (Single Dimensional) Objectives To describe why arrays are necessary in programming ( 7.1). To declare array reference variables and create arrays ( 7.2.1 7.2.2). To obtain array size

More information

Flow of Control: Loops. Chapter 4

Flow of Control: Loops. Chapter 4 Flow of Control: Loops Chapter 4 Java Loop Statements: Outline The while statement The do-while statement The for Statement Java Loop Statements A portion of a program that repeats a statement or a group

More information

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008) Student Name: Student Number: Section: Faculty of Science Midterm COMP-202B - Introduction to Computing I (Winter 2008) Friday, March 7, 2008 Examiners: Prof. Jörg Kienzle 18:15 20:15 Mathieu Petitpas

More information

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011 CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011 What is your name?: This test has the following sections: I. True/False... 60 points; (30 questions, 2 points each) II.

More information

CITS1001 week 4 Grouping objects

CITS1001 week 4 Grouping objects CITS1001 week 4 Grouping objects Arran Stewart March 20, 2018 1 / 31 Overview In this lecture, we look at how can group objects together into collections. Main concepts: The ArrayList collection Processing

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Summary of Methods; User Input using Scanner Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables

More information

Overview. Lab 5 Methods and Parameters

Overview. Lab 5 Methods and Parameters Lab 5 Methods and Parameters Overview At this point in the course, you should have a set of skills which allow you to create functionality at the level of using control structures like if statements and

More information

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit. Name CS 110 Practice Final Exam originally from Winter, 2003 Instructions: closed books, closed notes, open minds, 3 hour time limit. There are 4 sections for a total of 49 points. Part I: Basic Concepts,

More information

Adam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods

Adam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods Adam Blank Lecture 2 Winter 2019 CS 2 Introduction to Programming Methods CS 2: Introduction to Programming Methods File I/O, Object Oriented Programming, and Lists Questions From Last Time 1 What are

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1 self-check: Ch. 4 #2; Ch. 5 # 1-10 exercises: Ch. 4 #2, 4, 5, 8; Ch. 5 # 1-2 Copyright 2009

More information

Debugging [continued]

Debugging [continued] Debugging [continued] Admiral Grace Murray Hopper http://www.history.navy.mil/photos/images/h96000/h96566kc.htm 1 2 Debugging Your Program Debugging: Where we left off Debugging Your Program. [summary]

More information

Darrell Bethea May 25, 2011

Darrell Bethea May 25, 2011 Darrell Bethea May 25, 2011 Yesterdays slides updated Midterm on tomorrow in SN014 Closed books, no notes, no computer Program 3 due Tuesday 2 3 A whirlwind tour of almost everything we have covered so

More information

Recursion. Recursion [Bono] 1

Recursion. Recursion [Bono] 1 Recursion Idea A few examples wishful thinking method Recursion in classes Ex: palindromes Helper functions Computational complexity of recursive functions Recursive functions with multiple calls Recursion

More information

COMP110 Jump Around. Go ahead and get today s code in Eclipse as shown on next few slides. Kris Jordan

COMP110 Jump Around. Go ahead and get today s code in Eclipse as shown on next few slides. Kris Jordan Go ahead and get today s code in Eclipse as shown on next few slides COMP110 Jump Around Fall 2015 Sections 2 & 3 Sitterson 014 November 19th, 2015 Kris Jordan kris@cs.unc.edu Sitterson 238 Classroom Materials

More information

Introduction to Software Development (ISD) Week 3

Introduction to Software Development (ISD) Week 3 Introduction to Software Development (ISD) Week 3 Autumn term 2012 Aims of Week 3 To learn about while, for, and do loops To understand and use nested loops To implement programs that read and process

More information

ITERATION WEEK 4: EXMAPLES IN CLASS

ITERATION WEEK 4: EXMAPLES IN CLASS Monday Section 2 import java.util.scanner; public class W4MSection2 { ITERATION WEEK 4: EXMAPLES IN CLASS public static void main(string[] args) { Scanner input1 = new Scanner (System.in); int CircleCenterX

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises: #7 videos: Ch. 4 #2-4 The if/else statement Executes one block if a test is true,

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming (Spring 2012) Lecture #9: Arrays Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112 Acknowledgements: some slides

More information

Object Oriented Programming. Java-Lecture 1

Object Oriented Programming. Java-Lecture 1 Object Oriented Programming Java-Lecture 1 Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen

More information