1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Months of the year December January November October February
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
CS102 A Wide Array of Possibilities CS 102 Java’s Central Casting.
1 CS100J 13 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
1 CS100J 14 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS100J 17 March 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. The last Java feature to study in this course Quote for the Day: Computer science has its field.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
1 Collections Working with More than One Number or Data Type or Object.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Lists.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Java Unit 9: Arrays Declaring and Processing Arrays.
Subroutines in Computer Programming Svetlin Nakov Telerik Corporation
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 8: Arrays.
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 CS100J 13 October 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. Please listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.
Insertion sort Loop invariants Dynamic memory
Java Language Basics.
Correctness issues and Loop invariants
Starting JavaProgramming
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays in Java What, why and how Copyright Curt Hill.
Asymptotic complexity Searching/sorting
A Wide Array of Possibilities
Asymptotic complexity
CS100A Sections Dec Loop Invariant Review C Review and Example
Presentation transcript:

1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational complexity; mine is called computational simplicity. – Prof. Gries Most of us don’t write perfect essays in one pass, and coding is the same: writing requires revising; programming requires revising. If you are writing too much code —it gets longer and longer, with no end in sight: stop and look for a better way. If your code is getting convoluted and you have trouble understanding it: stop and look for a better way. Learn to keep things simple, to solve problems in simple ways. This sometimes requires a different way of thinking. A key point is to break a problem up into several pieces and do each piece in isolation, without thinking about the rest of them. Our methodology for developing a loop does just that. CS October 2010 Arrays (secs )

2 Zune error 2 /* day contains the number of days since ORIGINYEAR (1 Jan 1980) */ /* Set year to current year and day to current day of current year */ year = ORIGINYEAR; /* = 1980 */ while (day > 365) { if (IsLeapYear(year)) { if (day > 366) { day= day – 366; year= year + 1; } } else { day= day – 365; year= year + 1; } Zune clock code keeps time in seconds since beginning of It calculates current day and year from it. Example year day On 31 Dec 2008, the Zune stopped working. Anger! On 1 Jan 2009 it worked. Does each iteration make progress toward termination? Not if day == 366!!

3 Array: object that stores lists of things. Holds a fixed number of values of a declared type. (So a0 will always hold 4 int values.) Basic form of a declaration: ; So, here is a declaration of x: Does not create array, it only declares x. x’s initial value is null. int[] x ; Elements of array are numbered: 0, 1, 2, …, x.length–1 The type of array a0 is int[] Store its name in a variable (as always). x a0 int[] a0 length 4

4 Notes on array length We write x.length, not x.length(), because length is a field, not a method Length field is final: an array’s length (field or actual number of items) cannot be changed once the array is created. We omit this field in the rest of the pictures. x a0 int[] a0 length 4 The length is not part of the array type, which is int[]. This means that an array variable can be assigned arrays of different lengths; x could later hold the name of a seven-item int array. (But not the name of a seven-item double array).

5 int[] x ; x null int[] a0 x= new int[4]; Create array object with 4 default values, store its name in x x a0 int[] a Assign 2*x[0], i.e. -8, to x[3] Assign 6 to x[2] int k= 3; x[k]= 2* x[0]; x[k-1]= 6; x[2]= 5; x[0]= -4; a Assign 5 to array element 2; assign -4 to array element 0 x[2] is a reference to element number 2 of array x

6 Array initializers Instead of int[] c= new int[5]; c[0]= 9; c[1]= 4; c[2]= 7; c[3]= 6; c[4]= 9; a0 array initializer: gives initial values for the array items. Values must have the same type, in this case, int. Length of the array is the number of values in the list; so … … must omit expression between brackets. Sometimes, can even omit the “new []” part (see pg 274). Use an array initializer: int[] c= new int[ ] {9, 4, 7, 6, 9};

7 Use of an array initializer public class D { public static final String[] months= new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; /** = the month, given its number m. Precondition: 1 <= m <= 12 */ public static String theMonth(int m) { return months[m–1]; } Variable months is: static: no reason to have each object contain one. public: can be seen outside class D. final: its value cannot be changed ( Careful! you can still change the elements in the array whose name it permanently holds! e.g., illegal (except in the Interactions pane…) to say months= new String[] {“Lee”}, but legal to say months[0]= “Lee”) months[m–1] is returned, since months[0] = “January”, months[1] = “February”, …

8 Differences between array and Vector (“classier”, fbofw) Declaration: int[] a; Vector v; Elements of a: int values Elements of v: any Objects Array always has n elements Number of elements can change Creation: a= new int[n]; v= new Vector(); Reference element: a[e] v.get(e) Change element: a[e]= e1; v.set(e, e1); Array locations a[0], a[1], … in successive locations in memory. Access takes same time no matter which one you reference. Elements all the same declared type (a primitive type or class type) Initialization shorthand exists. Class has no methods, can’t be extended. Can’t tell how Vectors are stored in memory. Referencing and changing elements done through method calls Elements of any Object type (but not a primitive type). Casting may be necessary when an element is retrieved. No special initialization. Class has methods, can be extended.

9 a b “Procedure” for swapping variable values public class D { /** = Swap x and y */ public static void swap (int x, int y) { int temp= x; x= y; y= temp; } …. swap(a, b); 53 A call will NOT swap a and b. Parameters x and y are initialized to the values of a and b, and thereafter, there is no way to change a and b. swap: 1D x y temp 53 ? frame for call just after frame created and args assigned to parameters:

10 Procedure swap for swapping array elements a0 c public class D { /** = Swap b[h] and b[k] */ public static void swap (int[] b, int h, int k) { int temp= b[h]; b[h]= b[k]; b[k]= temp; } …. swap(c, 3, 4); This does swap b[h] and b[k], because parameter b contains name of the array. swap: 1D b h temp a03 ? frame for call just after frame is created. k 4

b c is not here c h i k 11 Linear search public class D { /** = index of first occurrence of c in b[h..] Precondition: c is guaranteed to be in b[h..] */ public static int findFirst (int c, int[] b, int h) { } Remember: h..h-1 is the empty range // Store in i the index of first c in b[h..] int i ; // b[i] = c and c is not in b[h..i-1] return i; // invariant: c is not in b[h..i-1] while ( ) { } = h b[i] != c i= i + 1; Loopy questions: 1. initialization? 2. loop condition? 3. Progress? 4. Keep invariant true? b c is not here c is in here h i k invariant

12 /** = a random int in 0..p.length-1, assuming p.length > 0. The (non-zero) prob of int i is given by p[i]. Calls: roll(new double[] {.3,.7}) roll (new double[]{33,.33,.34})*/ public static int roll(double[] p) { double r= Math.random(); // r in [0,1) } 0 p[0] p[0]+p[1] 1 /** Store in i the segment number in which r falls. */ int i ; // r is in segment i return i; // inv: r is not in segments looked at (segments 0..i-1) while ( ) { } 1. init 2. condition 3. progress 4. invariant true = 0 r not in segment i i= i + 1; // and iEnd is the end of (just after) segment i double iEnd= p[0]; r >= iEnd iEnd= iEnd + p[i+1]; Non-uniform randomness from uniform randomness: It’s a kind of linear search!