CompSci 4 Chap 10 November 4, 2010 Prof. Susan Rodger.

Slides:



Advertisements
Similar presentations
More Recursion: Permutations and Towers of Hanoi
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Visual Lists By Chris Brown under Prof. Susan Rodger Duke University July 2012.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Programming with Microsoft Visual Basic th Edition
How Tall Are You? Introducing Functions By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made June 2014 by.
Using Functions in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University July 2008.
Princess & Dragon Part 2: Teaching a Dragon to Fly—Methods & Properties By Elizabeth Liang under the direction of Professor Susan Rodger Duke University.
Visual C++ Programming: Concepts and Projects
CompSci 18S Recursion Dec 4, 2006 Prof. Susan Rodger.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Using Functions in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University July 2008.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Arrays and Array Visualization
An Introduction to Sorting Chapter 11 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Methods Tutorial: Part One By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June 9, 2008.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Line up By Melissa Dalis Professor Susan Rodger Duke University June 2011.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Arrays and Array Visualization Alice. What is an array? An array is a collection of objects or information that is organized in a specific order. The.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
Changing Camera Views! Part 2: Simple Scene Change & Lighting Fixes By Bella Onwumbiko under the direction of Professor Susan Rodger Duke University July.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
CompSci 4 Chap 7 Sec 2 Apr 7, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Changing Color, Using Text Objects, and Random Selection in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2008.
CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Methods Tutorial: Part Two By Deborah Nelson Duke University Professor Susan Rodger June 16, 2008.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Animating Objects in Groups: Using Arrays and Lists By Ruthie Tucker under the direction of Professor Susan Rodger Summer 2008.
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 6: The Repetition Structure
CompSci 4 Introduction to Programming Animation and Virtual Worlds Prof. Susan Rodger.
Variables and Inheritance Part 1
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
September 26, 2011 Sorting and Searching Lists. Agenda Review quiz #3 Team assignment #1 due tonight  One per team Arcade game Searching  Linear  Binary.
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops Susan Rodger Duke University July 2011.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Can I get your number? By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Nonvisual Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
CompSci 4 Starting Alice Jan 15, 2009 Prof. Susan Rodger Alice is named in honor of Lewis Carroll’s Alice in Wonderland.
CompSci 4 Starting Alice Sep 2, 2010 Prof. Susan Rodger Alice is named in honor of Lewis Carroll’s Alice in Wonderland.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.
CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.
Using Placeholders to Simplify your Methods: Learning Methods, Part 2 By Deborah Nelson Duke University Professor Susan Rodger June 16, 2008.
CompSci 101 Introduction to Computer Science November 11, 2014 Prof. Rodger CompSci 101 Fall Review for exam.
CompSci 4 Chap 10 Nov 22, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Variables and Inheritance Part 1 Alice. Review: Properties A class defines properties for its own kind of object. When an object is created (instantiated),
Arrays and Array Visualization
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops
Design and Analysis of Algorithms
Memory Matching Challenge
Variables and Inheritance Part 1
CIS16 Application Development and Programming using Visual Basic.net
Introduction to Computer Science
Presentation transcript:

CompSci 4 Chap 10 November 4, 2010 Prof. Susan Rodger

Announcements Assignment 6 is due tonight! Today –Chap 10 – variables and arrays Next Time: Chapter 8, Sec 1 –Recursion –Reading quiz –Also, more practice with Arrays

Review: Properties Where is the class? Where is the object? A class defines properties When an object is created it receives is own set of properties

State and Changing State State of object – each property stores info about the object –Example: vehicle isShowing State change

Class-level Variables New variables can be added to the properties of an object –class-level The value of the variable can be changed –The variable is mutable. –Can be used to track state changes.

Inheritance If an object (and its new variable) are saved out and given a new name, a new class is created. –This is inheritance! –The new class inherits all properties and methods of original class. Did this before by adding new methods to a class and saving it out.

What is an array? An array is a collection of objects or information organized in a specific order The individual components (elements) are of the same type (all object or all number, etc.) Analogy – Music on your CD or Ipod –Collection of songs listed in order –CD allows you to Play songs in order Play songs by specifying its number Play songs in random order

Arrays in Alice In Alice, array is a data structure to organize objects or information An array is not visible, it is a way of organizing But…. –Alice has a 3D model to help you “see” the array

Example – Create a visualization of an array of people Add 5 people to the world Add an array visualization Not an array yet, must add people to the array Positions in array numbered starting with 0

Initialize array - Add Alice to Array in position 0 Alice automatically moves to the 0 position!

Add Soldier to the Array Soldier moves automatically to position 1 (which is the 2cd position)!

Add RandomGuy, Skater and Rockette The array initialization is complete! Set isVisible for arrayVisualization to false –Array not seen

Accessing elements in an array Can specify an element at a particular location in the array

Repeat for all items in the array – in order Use “loop” – complicated version Note: index in loop is used in body What does this do?

Array vs. List Array –Elements are ordered –Can access a particular element – 3 –Use “Loop” - loop over elements – one at a time, OR every second element, etc List –Elements are not ordered –Use “For all in order”, “For all together” – does something to each element in the list – just don’t know the order this occurs

Array Visualization vs Array without Visualization Array Visualization moves the objects and puts them on the numbers in the array Array without visualization is also possible, create a new world variable and select array (same way create a list). The array is not shown and the objects don’t move and stand in a line. This is harder to use since you cannot see which object is in which position in the array.

Swapping two elements in the array Swap the objects at positions 0 (fanDancer) and 3 (duckPrince) Add in an ObjectVisualization, this is like a variable for an object. (same folder where ArrayVisualization is)

Swapping objects at 0 and 3 (cont) Only one element at a time can be in a slot in the array. To swap two elements, you have to move one of them out temporarily. Move object at index 0 to objectVisualization (this frees up slot 0)

Swapping objects at 0 and 3 (cont) Now you can move the item in slot 3 over to slot 0 (note the duckPrince moved over) Now slot 3 is empty

Swapping objects at 0 and 3 (cont) Now move the object that was originally in slot 0 and was saved temporarily in the ObjectVisualization, over to slot 3

Don’t Swap with Do Together! This does not always work because the timing may not be exact. You may end up with two elements on the same square. Don’t Do This Way!

Setting elements in array Objects in an array are called elements Use “let” to set a position in an array Using “let”: Ex., the object in slot 3 moves to slot 0. Don’t do this if there is already something in position 0! Move the item in position 0 away first!

Swapping objects at 0 and 3 (code) Here is the code that corresponds to the swapping of the items in slots 0 and 3. This moves the item in slot 0 to a temp location, freeing up slot 0. The item in slot 3 moves to slot 0. Then the item in the temp location moves to slot 3.

Shuffle the array For each item in the array, swap it randomly with another object Make sure you use the swap on the previous page to swap two objects.

SelectionSort the array Find the position of the shortest object –Swap that object with the object in position 0 Find the position of the next shortest object –(Note: don’t consider slot 0, it is already in sorted order. Only consider slots from 1 on) –Swap that object with the object in position 1 –Now slots 0 and 1 contain items where they belong in sorted order Etc…. Until the array is sorted.

Classwork Today Shuffle Array Sort Array by heights