structural programming course content and data structures
play

Structural Programming Course Content and Data Structures - PowerPoint PPT Presentation

Structural Programming Course Content and Data Structures Introduction Vectors Objects Testing/Debugging Winter 2000 Methods Arrays Tracing Programs Searching CMPUT 102: Vectors and other Repetitions


  1. Structural Programming Course Content and Data Structures • Introduction • Vectors • Objects • Testing/Debugging Winter 2000 • Methods • Arrays • Tracing Programs • Searching CMPUT 102: Vectors and other Repetitions • Object State • Files I/O • Sharing resources • Sorting Dr. Osmar R. Zaïane • Selection • Inheritance • Repetition • Recursion University of Alberta  Dr. Osmar R. Zaïane, 2000 1  Dr. Osmar R. Zaïane, 2000 2 2 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Objectives of Lecture 17 Outline of Lecture 17 Vectors and For Statements Vectors and For Statements • learn about container objects that can contain an • Containers arbitrary number of other objects. • Vectors • See the Vector object as an example of a container. • The for statement • Introduce a repetition control structure called the • Adventure Version 8 for statement that allows the iteration over indexed collections. • Re-write the Adventure program using the concept of vectors .  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 3 4 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

  2. Containers Strings and Stacks • An object’s state consists of instance variables that are bound to other objects or values. • We have already seen two containers: • Sometimes it is useful for an object’s state to – String - a container for characters include an arbitrary number of other objects. – Stack - a container for Objects • An object that remembers an arbitrary number "Fred" of other objects is called a container or a "Fred" "Barney" collection . "Wilma" F Fred r Barney e … Wilma d …  Dr. Osmar R. Zaïane, 2000 5  Dr. Osmar R. Zaïane, 2000 6 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Indexed Containers Examples of Indexed Lists • Containers whose elements are indexed by List of students and their grades List of cities I visited integers are called indexed containers. 1 Jane Doe 90 1 Edmonton 2 Bob Smith 85 • The integer indexes are the object references. 2 Vancouver 3 John Flint 83 3 Denver 4 Wilma Stone 79 4 San Diego 5 Fred Ming 75 … . . . 0 1 2 3 What is the 3 city? Who is the 10 th student? "Fred" "Barney" "Wilma" "Betty" With a stack, I can only see the top element.  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 7 8 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

  3. Java - Vector 1 Outline of Lecture 17 • Java has a class called Vector, that is an ordered indexed container of an arbitrary number of Objects. • Containers • A Vector, can hold any kind of Objects, but • Vectors not values. • The for statement Vector myCities; myCities = new Vector(); • Adventure Version 8 Vector myCities.addElement(“Edmonton”); addElement myCities.addElement(“Vancouver”); myCities.addElement(“Denver”); myCities.addElement(“San Diego”);  Dr. Osmar R. Zaïane, 2000 9  Dr. Osmar R. Zaïane, 2000 10 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Java - Vector 3 Java - Vector 2 String element; “Quit” int index; aVector “Open …” • A Vector is indexed by non-negative ints so Vector aVector; 0 “Open…” 1 aVector = new Vector(); it can be accessed by position 2 aVector.addElement(“Quit”); • The first position is 0, not 1. aVector.addElement(“Open the chest”); aVector.addElement(“Open the blue door”); • A Vector knows its current size. • A Vector can be iterated by index. index = 0; While (index < aVector. size() ) { • When you access an Object in a Vector, you element = (String) aVector .elementAt(index) ; often must cast its type to use it. System.out.println(element); index = index + 1; }  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 11 12 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

  4. Java Syntax: for Statement Outline of Lecture 17 • A for statement is a special repetition control structure for indexed collections. • Containers • The syntax of a for statement in Java is: • Vectors < for statement> ::= for (<assignment>; <condition>; <increment>) <statement> • The for statement Java shorthand for: index = index + 1; • For example: • Adventure Version 8 for (index = 0; index < aVector.size(); index++) { element = (String) aVector.elementAt(index); System.out.println(element); }  Dr. Osmar R. Zaïane, 2000 13  Dr. Osmar R. Zaïane, 2000 14 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Semantics - for for Semantics - Example • The assignment is executed to initialize the index Quit variable. Vector aVector; Open the Chest int index; • The condition is evaluated and if it is true the Open the blue door String element; statement is executed. aVector = new Vector(); • The increment is performed on the index variable. aVector.addElement(“Quit”); aVector.addElement(“Open the chest”); • The condition is re-evaluated and if it is true, the aVector.addElement(“Open the blue door”); statement is executed again. for (index = 0; index < aVector.size(); index++) { • This continues until the condition is false at which element = (String) aVector.elementAt(index); time the for statement is done. System.out.println(element); }  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 15 16 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

  5. Outline of Lecture 17 Adventure 8 • Use Vectors to modify the Arithmetic • Containers Adventure game so that many rooms are • Vectors supported. • The for statement • Use Vectors to improve the implementation • Adventure Version 8 of TextMenu.  Dr. Osmar R. Zaïane, 2000 17  Dr. Osmar R. Zaïane, 2000 18 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Adventure 8 - Changes Summary Running Adventure 8 (1) • Add the class Door. • Make many changes to class Adventure. • Make many changes to class Room. • Make changes to class TextMenu. • Leave the classes: Adventurer, RandomInt, Chest and Question unchanged.  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 19 20 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

  6. Running Adventure 8 (3) Running Adventure 8 (2)  Dr. Osmar R. Zaïane, 2000 21  Dr. Osmar R. Zaïane, 2000 22 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta Running Adventure 8 (5) Running Adventure 8 (4)  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 23 24 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

  7. Program - Adventure 8.1 Program - Adventure 7.1 import java.util.*; OLD public class Adventure { NEW import java.util.*; /* Version 8 This program is an arithmetic … */ public class Adventure { /* Version 7 /* Constructors */ This program is an arithmetic adventure game … public Adventure () { */ /* Initialize an adventure by creating the appropriate objects. /* Constructors */ */ Vector rooms; public Adventure () { int i; /* Initialize an adventure by creating the appropriate rooms = new Vector(); objects. for (i = 0; i <= 4; i++) */ rooms.addElement(new Room(i + 1)); this.firstRoom = new Room(1); this.makeDoor(rooms, 1, 2, "red"); }  Dr. Osmar R. Zaïane, 2000 25  Dr. Osmar R. Zaïane, 2000 26 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta OLD NEW Program - Adventure 8.2 Program - Adventure 7.2 this.makeDoor(rooms, 1, 3, "blue"); this.makeDoor(rooms, 2, 4, "green"); /* Main program */ this.makeDoor(rooms, 2, 5, "blue"); this.firstRoom = (Room) rooms.elementAt(0); public static void main(String args[]) { } Adventure game; /* Main program */ game = new Adventure(); public static void main(String args[]) { game.play(); Adventure game; } game = new Adventure(); game.play(); /* Private Instance Variables */ } /* Private Instance Variables */ private Room firstRoom; private Room firstRoom;  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 27 28 Structural Programming and Data Structures University of Alberta Structural Programming and Data Structures University of Alberta

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend