Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7 Arrays and Array Lists. Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Chapter 7 – Arrays.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most.
Chapter 6  Iteration 1 Chapter 6 Iteration. Chapter 6  Iteration 2 Chapter Goals  To be able to program loops with while and for (sometimes do ) statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Java Basics (continued)
Java Programming Week 6: Array and ArrayList Chapter 7.
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Java Programming: From the Ground Up
ARRAYLIST Collections of Data. ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has.
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 6 Chapter.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Java Basics (continued) Ms. Pack AP Computer Science A.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 7 - Iteration.
Chapter 7 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Introduction to Computer Science and Object-Oriented Programming
Lecture 07 More Repetition Richard Gesick.
Selected Topics From Chapter 6 Iteration
LOOPS.
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.
Outline Altering flow of control Boolean expressions
Chapter Six - Iteration
Chapter 7 Iteration.
Repetition Statements
Presentation transcript:

Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

Week 9 Topics Nested Loops Processing Sentinel Values Random Numbers and Simulations Arrays Array Lists

9.1.1 Nested Loops Sometimes the body of a loop is again a loop. We say that the inner loop is nested inside an outer loop. As an example, assume that we want to generate the following output: [] [][] [][][] [][][][]

9.1.1 Nested Loops Cont. String r = ""; // Triangle is 4 rows high for (int i = 1; i <= 4; ++i) { // Number of blocks per row for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "\n“; } System.out.println(r);

9.1.2 Processing Sentinel Values Sometimes, the termination of a loop can only be evaluated in the middle of a loop. You can introduce a Boolean variable to control such a loop. The value that you evaluate to determine whether to terminate such a loop construct is called a sentinel value.

9.1.2 Processing Sentinel Values Cont. Example program run: Enter value, Q to quit: 1 Enter value, Q to quit: 2 Enter value, Q to quit: 3 Enter value, Q to quit: 4 Enter value, Q to quit: Q Average = 2.5

Scanner in = new Scanner(System.in); double total = 0; double i = 0; boolean done = false; while (!done) { System.out.print(“Enter value, Q to quit:”); String input = in.next(); if (input.equalsIgnoreCase(“Q”)) done = true; else { double x = Double.parseDouble(input); total = total + x; i++; } if (i > 0) System.out.println("Average = " + total/i);

9.1.3 Random Numbers and Simulations In a simulation, you repeatedly generate random numbers and use them to simulate an activity. The basic approach of a simulation is to have a loop, generate a large number of sample values, and record these values. When completed, averages or other statistics are calculated. See the Buffon needle experiment for an example.

9.1.3 Random Numbers and Simulations Cont. Here is how to simulate the cast of a die: Random generator = new Random(); int d = 1 + generator.nextInt(6); The call generator.nextInt(6) gives you a random number between 0 and 5 (inclusive). Add 1 to obtain a number between 1 and 6. import java.util.Random; nextInt(n) – a random integer between 0 (inclusive) and n (exclusive) nextDouble() – a random floating-point number between 0 (inclusive) and 1 (exclusive)

9.2.1 Arrays An array is a sequence of values of the same type. Arrays can be very useful, but suffer from the limitation that their length is fixed. double[] data = new double[10]; for (i = 0; i < data.length; ++i) data[i] = i * 10; data[0] value is 0, data[1] is 10, data[2] is 20, data[3] is 30 … data[9] is 90

9.2.1 Arrays Cont. Representation of the array data from the previous slide: Value Subscript

9.2.2 Array Lists ArrayList is a collection class. Array lists can grow and shrink as needed. The ArrayList class provides methods for many common tasks. ArrayList accounts = new ArrayList (); accounts.add(new BankAccount(1001));

9.2.2 Array Lists Cont. BankAccount anAccount = accounts.get(2); BankAccount anAccount = new BankAccount(1729); accounts.set(2, anAccount); accounts.remove(0); System.out.println(accounts.size());

9.2.2 Array Lists Cont. Note, don’t try to access an element of an array or an array list that does not exist, or you will get an out-of-bounds exception and the program will be terminated! For example, this will generate an out-of-bounds exception: BankAccount anAccount = accounts.get(accounts.size()); Error since accounts.size() – 1 is the last valid element of the array list

Reference: Big Java 2 nd Edition by Cay Horstmann Nested Loops (section 7.3 in Big Java) Processing Sentinel Values (section 7.4 in Big Java) Random Numbers and Simulations (section 7.5 in Big Java) Arrays (section 8.1 in Big Java) Array Lists (section 8.2 in Big Java)