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 […]

CS 145 Lecture 18 – Common array algorithms

Agenda summarizing optimizing filtering linear search state-capital lookup TODO Read sections 7.3-7.4. Reverse Engineering Code CapitalLookup.java package preexam2; import java.util.Scanner; public class CapitalLookup { private static String[] states = { “Alabama”, “Alaska”, “Arizona”, “Arkansas”, “California”, “Colorado”, “Connecticut”, “Delaware”, “Florida”, “Georgia”, “Hawaii”, “Idaho”, “Illinois”, “Indiana”, “Iowa”, “Kansas”, “Kentucky”, “Louisiana”, “Maine”, “Maryland”, “Massachusetts”, “Michigan”, “Minnesota”, “Mississippi”, “Missouri”, […]

CS 145 Lecture 17 – Birthday problem II

Agenda test April 13, covering logical operators, if statements, and arrays review session? evaluating a postfix expression finding birthday repeats accumulating or minimizing with the soFar pattern implementing a raffle TODO Start preassignment 3. Code Birthday.java package preexam2; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Birthday { public static void main(String[] args) throws FileNotFoundException […]

CS 330 Lecture 25 – Generics/templates II

Agenda Java before generics genericizing in Java type erasure adding templates to C templates in C++ Code ArrayList.java package cs330; public class ArrayList { private Object[] stuff; private int size; public ArrayList() { stuff = new Object[10]; size = 0; } public Object get(int i) { return stuff[i]; } public int size() { return size; […]

CS 145 Preassignment 3 – due before Monday, April 16

See the PDF. I strongly suggest you finish this preassignment before the exam on April 13, so that you may benefit from its completion on the exam. Homework 3 will be assigned immediately after this assignment is due.

CS 330 Lecture 24 – Generics/templates

Agenda named constructor pattern typing: strong and static and dynamic a peril of static typing: code Java generics C++ templates TODO Read http://docs.oracle.com/javase/tutorial/java/generics/index.html. Code makefile drawer: drawer.cpp Vector.h Matrix.h Matrix.cpp Vector.cpp g++ -Wall -o drawer Vector.cpp Matrix.cpp drawer.cpp Vector.cpp #include <cassert> #include “Vector.h” Vector2::Vector2() : x(0.0f), y(0.0f) { } Vector2::Vector2(float x, float y) : x(x), y(y) […]