1 / 178

cs5 black alums ~ reunion!

cs5 black alums ~ reunion!. Today!. Tues., Mar. 4 and Tues. Mar. 11, we'll have a reunion of CS5 black: your CS60 will be in Shan B442 on Mar. 4 . . Your host!. We 'll be there!. CS 60 today…. L ooks like next up is Java !. c. Hw #6, due on p day!. in Java.

zia
Download Presentation

cs5 black alums ~ reunion!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. cs5 black alums ~ reunion! Today! Tues., Mar. 4 and Tues. Mar. 11, we'll have a reunion of CS5 black: your CS60 will be in Shan B442 on Mar. 4. Your host! We'll be there!

  2. CS 60 today… Looks like next up is Java! c Hw #6, due on p day! in Java Takehomemidterm this week Thursday's class is set aside for the midterm (though any time before Sat. 11:59pm is OK). Java: the language for data lovers Finally! All data structures are equal, but some are more equal than others. - George big-O(rwell)

  3. cs60 midterm reminders… NO lecture on Thursday - can study/take the midterm (or arrange as you see fit) study guide online + today Pick up from Olin B163 starting Wed. 3/5 Takein a single two-hour block by Sat. 3/8 Written or typed (may use editor … but don't run things) 2-sided page of notes (strategy: create notes; review hwks) Individually done (worth one assignment) No other CS60 deadlines until Friday, 3/14 (hw6)

  4. M.T. Review (1) For up to two points! of extra credit on the midterm… Pair up with s/o you don't know (or don't know well) and find something unusual you have in common… Names: • Big-O analysis of algorithms • What does the big-O running time of an algorithm measure? • What does big-O runtime ignore?What's N in O(N) or O(2**N)? • Name an algorithm, perhaps one that you've implemented, that runs in each of these asymptotic runtimes: • O(1), O(log(N)), O(N), O(N**2), O(2**N), O(N!) • What is the difference among best-case, balanced-case, and worst-case running times? (Balanced-case applies to binary search trees.) • Why is the best-case run-time for determining the number of nodes in a binary search tree NOT O(1)? What is it? • What are the big-O running times for the list-, tree-, and graph-algorithms you've written this term? In particular, you should feel comfortable reasoning about the big-O running time of algorithms similar to those and writing a sentence of justification as to why. • Practice: What is the big-O run time of append and reverse, written in Racket? Of find, reach, and taut that you wrote in Prolog? • Logic programming with Prolog • Prolog "programs" are a collection of facts and rules. Make sure that you understand the examples from class and on homework. • Why can prolog produce multiple identical answers to a query? • What is the fundamental procedure that prolog uses in seeking bindings that satisfy predicates? [Search – in fact, depth-first search] • What is "unification"? [Answer: it's instantiating a variable (in whole or in part) by binding a value or structure to it.] What are the differences among prolog's =, ==, and is operators? Similarly, what are the differences between \+,\==, and \= ? • Why does the order in which prolog clauses are placed sometimes matter to its inference of variable/value bindings? • What type of problems is Prolog well-suited for? • You should feel comfortable composing a Prolog solution to a structural-recursion problem on lists or trees or graphs • What does it mean for a logical statement (that may include variables) to be satisfiable or unsatisfiable or a tautology? • What does fail. do? Why does Prolog not have epicfail. ? Thing in common: Then, read over these & circle those you find least familiar… • Functional Programming in Racket/Scheme • Make sure that you understand and feel comfortable with Scheme's syntax and basic functions like first, rest, etc. • You should be comfortable about the differences in the assumptions that append, list, and cons make about their inputs. • What does assoc do and what is an association list? • Make sure that you can write basic Scheme functions like those developed in lecture and in the assignments. Nothing so large as Unicalc, to be sure, but a small function that might use one helper function would make a reasonable exam question. In particular, recursion is the secret to all happiness in functional programming -- you may want to review decomposing problems recursively. • What are anonymous functions and where are they useful? How is lambda used in Scheme? • What is tail recursion and how can it improve performance? • You should be familiar with the Scheme higher-order functions such as map, foldr, filter, and sort. You certainly don't need to memorize them, but you should feel like you could implement them and other higher-order functions from a definition using recursion. You should also feel comfortable using them in order to implement higher-level tasks. • How can objects such as graphs and trees (for example, binary search trees) be encoded as lists? Make sure that you feel comfortable writing Scheme functions to manipulate these information structures. • You should feel comfortable with the use-it-or-lose-it approach to problem decomposition and solving. How do make-change, reach?, min-dist, and most-multiples work? • How does the merge technique work to combine two sorted lists into a single, overall sorted list? What is its big-O running time? • How many miles per fortnight is one furlongs per hour? We will do this as the final piece of class today…

  5. Programming language space JFLAP computation Prolog Racket Python Prolog Matlab abstraction axis Java C Java JFLAP Hmmm HWare task-independent task-specific specificity axis Racket You'll use at least four programming languages... the language formerly known as Scheme

  6. All corners of computational space? Say hello. MoO moO MoO mOo MOO OOM MMM moO moO MMM mOo mOo moO MMM mOo MMM moO moO MOO MOo mOo MoO moO moo mOo mOo moo Cow Fibonacci Whitespace Hello world 0 lI'moH A cher 1 lI'moH B cher A cha' B cha' 18 { A B boq latlh cha' B "A" cher "B" cher } vangqa' Piet Var’aq Fibonacci Fibonacci Malbolge (=<`$9]7<5YXz7wT.3,+O/o'K%$H"'~D|#z@b=`{^Lx8%$Xmrkpohm_Ni;gsedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543s+O<oLm Hello world Who are you calling fringe? Fortunately, CS 60 does NOT goto the fringe of the programming-language universe !

  7. Introducing… Java … a data-structuring language compiled statically typed object-oriented

  8. Objects 42 42 "Oh yeah!" y x Data reigns a Point object… a String object… Java ~ an object-oriented programming language Classes input Methods Functions Types output

  9. Objects 42 42 "Oh yeah!" y x Data reigns a Point object… a String object… In Java, the fundamental activity is designing and creating DATATYPES and DATA (rather than functions) Classes input Methods Functions Types output

  10. Objects 42 42 "Oh yeah!" y x Data reigns a Point object… a String object… In Java, the fundamental activity is designing and creating DATATYPES and DATA (rather than functions) data are objects datatypes are classes Classes input Methods Functions Types output

  11. Objects 42 42 "Oh yeah!" y x Data reigns a Point object… Let's dive in! a String object… In Java, the fundamental activity is designing and creating DATATYPES and DATA (rather than functions) data are objects This seems a bit disorganized to be about data organization! datatypes are classes Classes input Methods Functions Types output

  12. class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public static void main(String[] args) { Point p1 = new Point(4, 2); Point p2 = new Point(0, 42); System.out.println("Oh yeah!"); } } Point p1 Point p2

  13. class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public static void main(String[] args) { Point p1 = new Point(4, 2); Point p2 = new Point(0, 42); System.out.println("Oh yeah!"); } } 4 2 0 42 y x y x Point p1 Point p2 "Oh yeah!"

  14. Draw what happens here! class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public static void main(String[] args) { Point p1 = new Point(7, 8); Point p2 = new Point(p1.x, p1.y); p2.x = 42.0; System.out.println("p1.x is " + p1.x); } other artistic interpretations of objects are welcome Point p1 Point p2 What gets printed?

  15. Draw what happens here! class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public static void main(String[] args) { Point p1 = new Point(7, 8); Point p2 = p1; p2.x = 42.0; System.out.println("p1.x is " + p1.x); } other artistic interpretations of objects are welcome Point p1 Point p2 What gets printed?

  16. The assignment operator, =, always does the same thing… ... it copies the VALUEon the right to the LOCATIONon the left. p2.x = 42 LHS is a LOCATION RHS is a VALUE that value might be a reference! numbers 42 p2.x p2 = p1 objects 7 8 p1 p2 y x

  17. In Java… The assignment operator, =, always does the same thing… ... it copies the CONTENTSon the right to the LOCATIONon the left. p2.x = 42 LHS is a LOCATION RHS is a VALUE that value might be a reference! … the programmer has the ability (and responsibility) to keep track of all of the DATAin their computations… numbers 42 p2.x p2 = p1 objects 7 8 p1 p2 y x

  18. class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public static void main(String[] args) { Point p1 = new Point(4, 2); Point p2 = new Point(0, 42); System.out.println("Oh yeah!"); } } 4 2 0 42 y x y x Anyone recognize these cool mascots? Point p1 Point p2 "Oh yeah!"

  19. Oh, Yeah! http://www.youtube.com/watch?v=nBeUGqeYsQg

  20. objects in C++ just aren't Kool... Well, at he very least, they're not Kool-aid! Prof O'Neill's handiwork... In C++, data objects are cows. Hah! But cows don't come in 11 delicious flavors! 4 2 y x

  21. Eclipse I'DE like to know what an IDE is! Integrated Development Environment The Eclipse IDE: "no feature left behind"! discover features as needed/desired…

  22. Java is a compiledlanguage writing your program HelloWorld.java source code compiling your program javac HelloWorld.java HelloWorld.class byte code running your program java HelloWorld Run! I knew this HelloWorld program had ".class" !

  23. Code observations… ? in a file named HelloWorld.java class HelloWorld { public static void main(String[] args) { System.out.println("Adios, Prolog."); System.out.println("Hello, Java!"); } } This looks alien, though Prolog might appreciate it!

  24. Overhead ~ baggage args? Arg! The one and only class of this program class HelloWorld { public static void main(String[] args) { System.out.println("Adios, Prolog."); System.out.println("Hello, Java!"); } } Makes this method accessible from any class. Result type of this method (none). called on start-up input type and name The print-with-end-of-line method for object System.out. The standard output stream object, pre-defined in the System class. The “System” class.

  25. Types almost everywhere! class ItsAFac { public static doublefac(intN) { if (N < 2) { return 1.0; } else { return N*fac(N-1); } } public static voidmain(String[]args) { int x = 5; double solution = fac(x)/2.0; System.out.println("solution: " + solution); } } Where are the types in this Java program? Recursion and loops are equally welcome! You don't need to tell a function the type you're sending . But then how does Java know ? This seems odd for a language so concerned with types!

  26. Java compilesto byte code class ItsAFac { public static doublefac(int N) { if (N<2) { return 1.0; } else { return N*fac(N-1); } } public static voidmain(String[] args) { int x = 4; x = x+1; double solution = fac(x)/2.0; System.out.println("solution: " + solution); } } public static double fac(int); Code: 0: iload_0 1: iconst_2 2: if_icmpge 7 5: dconst_1 6: dreturn 7: iload_0 8: i2d 9: iload_0 10: iconst_1 11: isub 12: invokestatic #2; //Method fac; 15: dmul 16: dreturn Where did the types go? public static void main(java.lang.String[]); Code: 0: iconst_4 1: istore_1 2: iload_1 3: iconst_1 4: iadd 5: istore_1 6: iload_1 7: invokestatic #2; //Method fac:(I)D < more -- too much to fit here! > Hmmm… this looks familiar!

  27. Java's all about data Primitive types bits (with a type to interpret them) int, double, boolean, char, byte, short, long, float a container holding data 42 101010 int x int x = 5; double d = 42.0; These four are by far the most common… boolean b = true; char c = 't';

  28. Java's built-in (primitive) types int x = 5; These eight are the only primitive types. holds from -2,147,483,648 to 2,147,483,647 byte b = 3; holds from -128 to 127 short s = 4; holds from -32768 to 32767 long l = 6; holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 double d = 42.0; holds up to 1.0 * 10**307 (pos. or neg.) float f = 42.0; about 7 places of precision (vs. double's 15) boolean b = true; holds either true or false char c = 't'; used to hold single characters

  29. Java's built-in (primitive) types int x = 5; These eight are the only primitive types. holds from -2,147,483,648 to 2,147,483,647 byte b = 3; holds from -128 to 127 short s = 4; holds from -32768 to 32767 long l = 6; holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 double d = 42.0; holds up to 1.0 * 10**307 (pos. or neg.) float f = 42.0; about 7 places of precision (vs. double's 15) boolean b = true; holds either true or false char c = 't'; used to hold single characters Note the single vs. double quotes ! let's sketch these two… String s = "this is a string of text"; Strings are Objects, not primitive types. Class names typically begin with Capital letters…

  30. primitive vs. Class types primitive type: a box w/ data bits! char c = 't'; Class type: a reference (a box w/ memory-address bits)! String s = "this is a string";

  31. Java's all about data primitive types boxes o' bits (with a type to interpret them) int, double, boolean, char, byte, short, long, float a container holding data 42 101010 int x Objects named collections of named data String s 101010 "Oh Yeah!" a container holding a memory reference to structured data "on the heap" Point p a String object… 4 2 101111 y x a Point object…

  32. Data in Java Primitive types Objects int, double, boolean, char, float, short, byte, long int x; Point p; 0 p x You must declare your variables but only once!

  33. Data in Java Primitive types Objects int, double, boolean, char, float, short, byte, long Point p = new Point(4,2); int x = 41; a Java reference the same memory location as above – but now holding a different memory location – one pointing to a newly-born (or poured) object! this is the same memory location as above! 41 really holds a memory address 4 2 p x y x You may define them, too… … as many times as you'd like

  34. How Java "thinks" stack local variables for each function call are stored on the stack Much more stack and I'll be in a heapof trouble! these are just object references new Objects are always allocated from the heap heap Your memory in Java full?

  35. class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public static void main(String[] args) { Point p1 = new Point(7, 8); Point p2 = new Point(p1.x, p1.y); p2.x = 42.0; System.out.println("p1.x is " + p1.x); } Point p1 STACK Point p2 HEAP

  36. class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public static void main(String[] args) { Point p1 = new Point(7, 8); Point p2 = new Point(p1.x, p1.y); p2.x = 42.0; System.out.println("p1.x is " + p1.x); } the Constructor builds an Object from scratch (all of its data!) Point p1 STACK all Objects are created on the HEAP by calling new and a Constructor Point p2 inputs via = 7 8 local variables are stored on the STACK y x it's still 7 7 HEAP 8 42 local variables "die" (go out of scope) when their closing curly brace is reached y x

  37. The assignment operator, =, always does the same thing… ... it copies the VALUEon the right to the LOCATIONon the left. p2.x = 42 LHS is a LOCATION RHS is a VALUE that value might be a reference! numbers 42 p2.x p2 = p1 objects 7 8 p1 p2 y x

  38. class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public static void main(String[] args) { Point p1 = new Point(7, 8); Point p2 = p1; p2.x = 42; System.out.println("p1.x is " + p1.x); } p1 STACK p2 multiple names are possible for the same data ~ aliasing 7 42 8 HEAP y x

  39. class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public static void main(String[] args) { Point p1 = new Point(7, 8); Point p2 = new Point(7, 8); if (p1 == p2) System.out.println("p1 and p2 are equal"); else System.out.println("p1 and p2 are NOT equal"); System.out.println("p1 is " + p1); System.out.println("p2 is " + p2); } p1 STACK p2 7 8 y x No curlies? HEAP 7 8 y x What will print?

  40. class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public boolean equals( Point p ) { return (this.x == p.x && this.y == p.y); } public String toString() { return "(" + x + "," + y + ")"; } public static void main(String[] args) { Point p1 = new Point(7, 8); Point p2 = new Point(7, 8); if (p1.equals(p2)) System.out.println("p1 and p2 are equal"); else System.out.println("p1 and p2 are NOT equal"); System.out.println("p1 is " + p1); System.out.println("p2 is " + p2); } "deep" comparison instead of reference (shallow) comparison equals (1) What's this saying? (2) Which Point is p? (3) Anything seem missing here? here toString is called

  41. import static java.lang.Math.*; class Point { private double x; private double y; public Point(double x_in, double y_in) public Point add( Point p ) { } public double dist( Point p ) { } public static void main(String[] args) { Point p1 = new Point(7, 8); Point p2 = new Point(4, 4); Point p3 = p1.add(p2); // p3 is the sum double dist = p1.dist(p2); // Euclidean dist. // then we might print... } } Write two Point methods: add + dist Hint: Use the Point constructor! The final data picture Hint: Power is pow(b,p); sqrt(x) is also available.

  42. hw6pr1 (isn't) Complex For problem #1, you'll build a Complex class Filename: Complex.java private double real; Data members: private double imag; Methods: public Complex( real_in, imag_in ) public Complex( ) public String toString() public Complex negate() public Complex conjugate() public boolean equals(Complex c) public boolean add(Complex c) public boolean multiply(Complex c) public boolean divide(Complex c) This all feels oddly familiar…

  43. Two good references for looking up Java syntax… http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/ wikipedia, for sure for checking out just one thing!

  44. M.T. Review (1) For up to two points! of extra credit on the midterm… Pair up with s/o you don't know (or don't know well) and find something unusual you have in common… Names: • Big-O analysis of algorithms • What does the big-O running time of an algorithm measure? • What does big-O runtime ignore?What's N in O(N) or O(2**N)? • Name an algorithm, perhaps one that you've implemented, that runs in each of these asymptotic runtimes: • O(1), O(log(N)), O(N), O(N**2), O(2**N), O(N!) • What is the difference among best-case, balanced-case, and worst-case running times? (Balanced-case applies to binary search trees.) • Why is the best-case run-time for determining the number of nodes in a binary search tree NOT O(1)? What is it? • What are the big-O running times for the list-, tree-, and graph-algorithms you've written this term? In particular, you should feel comfortable reasoning about the big-O running time of algorithms similar to those and writing a sentence of justification as to why. • Practice: What is the big-O run time of append and reverse, written in Racket? Of find, reach, and taut that you wrote in Prolog? • Logic programming with Prolog • Prolog "programs" are a collection of facts and rules. Make sure that you understand the examples from class and on homework. • Why can prolog produce multiple identical answers to a query? • What is the fundamental procedure that prolog uses in seeking bindings that satisfy predicates? [Search – in fact, depth-first search] • What is "unification"? [Answer: it's instantiating a variable (in whole or in part) by binding a value or structure to it.] What are the differences among prolog's =, ==, and is operators? Similarly, what are the differences between \+,\==, and \= ? • Why does the order in which prolog clauses are placed sometimes matter to its inference of variable/value bindings? • What type of problems is Prolog well-suited for? • You should feel comfortable composing a Prolog solution to a structural-recursion problem on lists or trees or graphs • What does it mean for a logical statement (that may include variables) to be satisfiable or unsatisfiable or a tautology? • What does fail. do? Why does Prolog not have epicfail. ? Thing in common: Then, read over these & circle those you find least familiar… • Functional Programming in Racket/Scheme • Make sure that you understand and feel comfortable with Scheme's syntax and basic functions like first, rest, etc. • You should be comfortable about the differences in the assumptions that append, list, and cons make about their inputs. • What does assoc do and what is an association list? • Make sure that you can write basic Scheme functions like those developed in lecture and in the assignments. Nothing so large as Unicalc, to be sure, but a small function that might use one helper function would make a reasonable exam question. In particular, recursion is the secret to all happiness in functional programming -- you may want to review decomposing problems recursively. • What are anonymous functions and where are they useful? How is lambda used in Scheme? • What is tail recursion and how can it improve performance? • You should be familiar with the Scheme higher-order functions such as map, foldr, filter, and sort. You certainly don't need to memorize them, but you should feel like you could implement them and other higher-order functions from a definition using recursion. You should also feel comfortable using them in order to implement higher-level tasks. • How can objects such as graphs and trees (for example, binary search trees) be encoded as lists? Make sure that you feel comfortable writing Scheme functions to manipulate these information structures. • You should feel comfortable with the use-it-or-lose-it approach to problem decomposition and solving. How do make-change, reach?, min-dist, and most-multiples work? • How does the merge technique work to combine two sorted lists into a single, overall sorted list? What is its big-O running time? • How many miles per fortnight is one furlongs per hour?

  45. Have you noticed that KoolAid is always SMiley? stay Kool amidst this week's midterms !

  46. Java: the strong and not-so-silent type Java C++ C Python Prolog Racket every variable must have its type declared, known, and CORRECT, when you compile before you run the program each variable has its type checked during execution or run-time Statically Typed Dynamically Typed Tradeoffs? you're too static - and just not my type

  47. Representations that abstract away the details of implementation, and provide task-specific capabilities: lists, trees, and graphsin Racket and Prolog, for example! previous weeks Information structures vs. Data structures this/next week… These allow (and require) the programmer to specify exactly how to manipulate memory and data: classesin Java, for instance.

  48. Programming paradigms Declarative programming Imperative programming NAME (Scheme) (Java) • defining functions • defining & changing data IDEAS • composing functions • sequences of commands Binding definitions Assignment statements RAW MATERIAL (define x 41) (define x (+ x 1)) int x = 41; x = x+1; Watch out for the assignment operator! LHS = RHS assignment ~ location binding ~ label

  49. The assignment operator, =, always does the same thing! y = x x is treated as a VALUE that value might be a reference! y is treated as a LOCATION 41 x y ... it takes the CONTENTS of x and copies it into y . 7 8 p1 p2 y x p2 = p1

  50. class Point { private double x; private double y; public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } public static void main(String[] args) { Point p1 = new Point(7, 8); Point p2 = new Point(p1.x, p1.y); p2.x = 42.0; System.out.println("p1.x is " + p1.x); } the Constructor builds an Object from scratch (all of its data!) p1 STACK all Objects are created on the HEAP by calling new and a Constructor p2 function inputs are assigned with = 7 8 local variables are stored on the STACK y x it's still 7 7 HEAP 8 42 local variables "die" (go out of scope) when their closing curly brace is reached y x

More Related