CS 330 Lecture 32 – Scheme, Part II
Agenda why functional? why not functional? reverse engineering finish merge program this double, square, cosine higher-order functions map and filter variable-length argument lists symbolic differentiation TODO http://en.wikipedia.org/wiki/Method_chaining http://www.paulgraham.com/icad.html Reverse Engineering Program This Choose one or both: Write contains, which takes two arguments: a list and an atom. Return true if the list contains the atom. […]
CS 145 Lecture 21 – 2-D arrays, Image
Agenda 2-D arrays (or arrays of arrays) writing an image portable graymap Code Birthday2.java package preexam2; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Birthday2 { public static void main(String[] args) throws FileNotFoundException { File file = new File(“/home/user/bdays.csv”); Scanner in = new Scanner(file); int[][] counters = new int[12][31]; while (in.hasNextInt()) { int month = […]
CS 145 Lecture 20 – NDeckerBurger, our first object
Agenda objects instance variables constructors methods static vs. instance Code NDeckerBurger.java package prefinal; public class NDeckerBurger { private int nDecks; private boolean hasCheese; public static final int CALORIES_PER_DECK = 211; public NDeckerBurger(int givenDeckCount) { nDecks = givenDeckCount; } public NDeckerBurger(int givenDeckCount, boolean hasCheese) { nDecks = givenDeckCount; this.hasCheese = hasCheese; } public int getDeckCount() { […]
CS 330 Lecture 31 – Hi, Scheme
Agenda Thinking functionally recursion over iteration chaining vs. sequence atoms, lists, and S-expressions Polish notation car, cdr, cons, cond our first functions arithmetic expressions logical expressions factorial lambda list functions length contains nth-element merge remove map filter TODO http://racket-lang.org/ Program This Choose one or both: Write contains, which takes two arguments: a list and an […]
CS 330 Lecture 30 – ANTLR and C++
Agenda A Cartesian grid walker language Translating calculator + functions to C++ TODO New ANTLR C++ target: http://www.antlr.org/wiki/pages/viewpage.action?pageId=29130826 ANTLR C++ REPL Code makefile all: cart CartesianLexer.cpp CartesianParser.cpp: Cartesian.g Player.h java -jar antlr-3.4-with-cpp.jar Cartesian.g cart: cart_interpreter.cpp CartesianLexer.cpp CartesianParser.cpp Player.h g++ -I include -o cart cart_interpreter.cpp CartesianLexer.cpp CartesianParser.cpp clean: rm -f CartesianLexer.cpp CartesianParser.cpp cart CartesianLexer.hpp CartesianParser.hpp Cartesian.g […]
CS 330 Lecture 29 – Garbage Collection
Agenda how does polymorphism work? Jim Blinn’s terrible approach the elegant vtable approach garbage collection programmers don’t get explicit reclamation right OS does reclaim memory when program stops what happens when… … we don’t call free? … we call free twice? … we call free once but leave pointer untouched? some approaches: mark-and-sweep stop-and-copy reference […]
CS 330 Lecture 28 – Inheritance and polymorphism
Agenda fixing our stack? oop: encapsulation polymorphism inheritance is-a vs. has-a inheritance tightly couples, destroys encapsulation modern thinking: limit inheritance, favor shallow hierarchies and has-a composition when inheritance is a good idea when refactoring a library you maintain when writing a GUI widget polymorphism: lets you write conductor code easy in dynamically-typed languages in statically-typed, […]
CS 145 Lecture 19 – Passwords, stack vs. heap
Agenda brute force password generation Java is copy-by-value primitives hold values objects are references, which hold addresses (http://xkcd.com/138/) Pointer Fun with Binky stack vs. heap == vs. equals Code Account.java package preexam2; public class Account { public static boolean authenticate(String password) { return password.equals(“horsieso”); } } Cracker.java package preexam2; import java.util.Arrays; public class Cracker { […]
CS 330 Lecture 27 – Inheritance and polymorphism
Agenda constructors and destructors a destructor’s job: release dynamically-allocated members a linked list in C++ a Stack using inheritance in C++ implementation inheritance vs. interface inheritance the enlightened stance: favor composition, shallow hierarchies, interfaces how polymorphism works TODO Why extends is evil: http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html?page=1 What OOP isn’t: http://hacksoflife.blogspot.com/2010/12/what-oop-isnt.html Program This Given the List<T> implementation discussed in class, write […]
CS 330 Lecture 26 – C++’s standard template library
Agenda map vector using STL to write a probababble engine http://www.gutenberg.org/ Code bsearch.cpp #include <iostream> template<typename T> int binarysearch(T *haystack, T needle, int first, int last) { if (first > last) return -1; int mid = (first + last) / 2; if (needle < haystack[mid]) { return binarysearch<T>(haystack, needle, first, mid – 1); } else […]