CS211 Spring 2002 Lecture 3: OOP Overview 1/28/2003 ------------------------------------------------------------------------------- 0) Announcements: + Just added? See http://www.cs.cornell.edu/courses/cs211/2003sp/ Read syllabus! Do everything in "What To Do First"!!! + Office hours and consulting have been happening (1st line of defense!) + A1 due Thurs Objectives/Topics of Lecture 3 + Basic ("foundational") data structures + Pros and Cons of each ------------------------------------------------------------------------------- 1) Variables +----+ Java Syntax | | <==== 1 - naming rules +----+ - scope x - assignment, operators - default values Pros and Cons? ------------------------------------------------------------------------------- 2) Strings + collections of characters + objects in Java + string literal: "stuff" (saves hassle of calling a constructor) + constructors? + so, what is a string literal? + immutable -- once created, cannot change! + Resemblence of Strings to arrays: + java.lang.String in API Pros and Cons ------------------------------------------------------------------------------- 3) Arrays + fixed collection of objects - type[][].... var = new type[size1][size2].... - array of arrays in Java + indexing + initializer lists and anonymous arrays Pros and Cons ------------------------------------------------------------------------------- 4) Lists + data structure that can grow + limits to size? + the gist: field(s) is/are the same (or "related") type as class! + think of a field as a LINK public class Person { public String name; style??? public Person friend; public Person(String name) {this.name=name;} public String toString() {return name;} } + how to build? // Create 3 people: Person p1 = new Person("A"); Person p2 = new Person("B"); Person p3 = new Person("C"); // Link people together, with "A" as first person: ------------------------------------------------------------------------------- + how to operate on list? (perform action(s) on some/all items) Count people in our list: picture of list: algorithm and pseudocode for iteration: start ___________________ set _____________________ if ________________ isn't null ___________ count set _____________________________ repeat output count code: ------------------------------------------------------------------------------- 5) Vectors and ArrayLists + Arrays that shrink and grow + ArrayList: must import java.util.ArrayList http://java.sun.com/j2se/1.4/docs/api/java/util/ArrayList.html + Vector: must import java.util.Vector http://java.sun.com/j2se/1.4/docs/api/java/util/Vector.html // ArrayList example: import java.util.ArrayList; public class ArrayList1 { public static void main(String[] args) { ArrayList vals = new ArrayList(4); vals._____(new Person("A")); vals._____(new Person("B")); vals._____(new Person("C")); for (int i=0; i < vals.size() ; i++) System.out.println(vals._____(i)); } } // order of printing? ------------------------------------------------------------------------------- 6) Trees + Links to multiple things + example: Build tree of people: Person has bio-Mom and bio-Dad Start with Person "0" and fill in ancestors will get "upside down" tree 0,1,2,3,4,5,6 becomes: called _________ tree Java classes? class Person { public String name; public Person(String name) {this.name=name;} public String toString() {return name;} } public class Tree1 { public static void main(String[] args) { int height = 3; int nodes = (int) ___________________________ Person[] ps = new Person[nodes]; for (int i = 0; i < nodes-1; i++) ps[i]=new Person("p"+i); ps[ ].mother = ps[ ]; ps[ ].mother = ps[ ]; ps[ ].mother = ps[ ]; ps[ ].mother = ps[ ]; ps[ ].mother = ps[ ]; ps[ ].mother = ps[ ]; // count people....? } } -------------------------------------------------------------------------------