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

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7: Arrays and Array Lists. To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the generalized.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
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.
COMS S1007 Object-Oriented Programming and Design in Java July 8, 2008.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
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[]
Chapter 7  Arrays and Array Lists 1 Chapter 7 Arrays and Array Lists.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
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.
ACSE th Conference The Iconic Programmer Stephen Chen.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
CS0007: Introduction to Computer Programming Introduction to Arrays.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
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 3 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.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ARRAYLIST Collections of Data. ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has.
Week 12 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Arrays and ArrayLists.
Chapter 12 Recursion, Complexity, and Searching and Sorting
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COMP 121 Week 1: Testing and Debugging. Testing Program testing can be used to show the presence of bugs, but never to show their absence! ~ Edsger Dijkstra.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
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 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
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];
Let’s not leave anything to chance. How that process to generate random numbers takes places requires some complicated statistics that are outside the.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
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.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Introduction to Computer Science and Object-Oriented Programming
Principles of Computer Science I
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 8 Arrays Objectives
Chapter Six - Iteration
Chapter 8 Arrays Objectives
Introduction to Computer Science and Object-Oriented Programming
Unit 3 Test: Friday.
CS150 Introduction to Computer Science 1
Chapter 8 Arrays Objectives
Presentation transcript:

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

Week 9 Topics Random Numbers and Simulations Using a Debugger Arrays Array Lists

9.1.1 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.1 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.1.2 Using a Debugger A debugger is a program that you can use to execute another program and analyze its run-time behavior You can make effective use of a debugger by mastering just three concepts: breakpoints, single-stepping, and inspecting variables

9.1.2 Using a Debugger Cont. When a debugger executes a program, the execution is suspended whenever a breakpoint is reached The single-step command executes the program one step at a time You should step into a method to check if carries out its job correctly, you should step over it if you know it works correctly

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 4th Edition by Cay Horstmann Random Numbers and Simulations (section 6.5 in Big Java) Using a Debugger (section 6.6 in Big Java) Arrays (section 7.1 in Big Java) Array Lists (section 7.2 in Big Java)