E N D
Nelson Speaker Series Today's software is riddled with errors that can lead to catastrophic failures and vulnerabilities that compromise system integrity. This talk argues that we can greatly improve software reliability by capturing the "design rules" that govern a program and automatically preventing violations of such rules in the code. We have demonstrated the usefulness of this paradigm by developing concrete programming tools that can detect memory leaks statically, find root causes of complex errors, and translate application-specific design rules automatically into static and dynamic checkers. Monica Lam, Stanford Wed., Dec. 1, 7:00 pm in Galileo ESA’s Ariane 5 rocket 6/4/96 The Ariane problem was due to a type error: a cast from a long integer to a short integer, which should only have been applied to a number less than 32768, was erroneously applied to a larger number that represented the "horizontal bias" of the flight. There was no code to handle the error, so it followed the usual fate of such exceptions and crashed the entire software, the on-board computers, and the mission.
CS 5 Today • Final project: a movie database (inspired by www.imdb.com ) • HW 14 (1 problem) due Sunday, 12/5 at midnight M/T sections due Monday, 12/6 at midnight W/Th sections Recitation -- Friday @ 8am Human vs. Computer views of the same information… Software Engineering:
Building from Hw 12: CS5App class Choose an option from the following menu: 0 Display All Directors 1 Display All Films 2 Display Films by Title (or piece) 3 Display Films by Director 4 Display Films by Year 5 Display Films by Rating (G, PG, PG-13, R, NC-17, UC) 6 Display Films by Review (0 to 10) 7 Add a New Film 8 Save database to file 9 Read database from file 42 Quit start with Hw14Pr1.zip
public FilmDB(int capacity) public boolean isFull() public void addFilm(Film f) public void displayAllFilms() public void saveAllFilms() public void displayFilmsByTitle(String titlepiece) public void displayFilmsByYear(int year) public void displayFilmsByRating(String rating) public void displayFilmsByReview(double minreview) public boolean checkForFilm(String fulltitle) public int getCount() public int getCapacity() Methods class FilmDB others are OK, too private Film[] films; private int count; private int capacity; class Film public Film(String title, int year, String rating, double review, Director dir) public String getTitle() public int getYear(); public String getRating() public double getReview() public void display() public void save() private String title; private int year; private String rating; private double review; private Director dir; public DirectorDB(int capacity) public void displayAllDirectors() public void displayFilmsByDirector(String fname, String lname) public Director findDirectorByName(String fname, String lname) public void addDirector(Director dir) public int getCount() public boolean isFull() class DirectorDB private Director[] dirs; private int count; private int capacity; class Director public Director(String fname, String lname, int capacity) public String getFullName() public String getLName() public FilmDB getFilmDB() public String getFName() private String fname; private String lname; private FilmDB filmDB; public static void main(String[] args) static void printMenu() static void saveDB(String filePart) static void readFilmEntry() static void readDB(String filePart) class CS5App static FilmDB F; static DirectorDB D;
A film database: “Human” view 100 2 Lord of the Rings: Return of the King Jackson Peter 2003 PG-13 9.0 Gigli Brest Martin 2003 R 2.2 this is in the file twofilms.txt
A film database: “Human” view capacity of database 100 2 Lord of the Rings: Return of the King Jackson Peter 2003 PG-13 9.0 Gigli Brest Martin 2003 R 2.2 count of Films that follow title L name F name use H.nl() to input Strings! year rating review the same sequence of data for each Film object this is in the file twofilms.txt
A film database: “Computer” view objects: class DirectorDB D int count Director[] Director[] dirs dirs[0] dirs[1] dirs class Director d FilmDB filmDB String fname FilmDB filmDB String lname class Film String rating f String title double review Director int year Director dir dir class FilmDB F int count Film[] films[0] films[1] Film[] films films
A film database: “Computer” view class DirectorDB int count Director[] Director[] dirs dirs[0] dirs[1] dirs relationships among data… class Director FilmDB filmDB String fname FilmDB filmDB String lname class Film String rating String title double review Director int year Director dir dir class FilmDB int count Film[] films[0] films[1] Film[] films films
Never send a human to do a machine’s job - Agent Smith, The Matrix
Human Computer translation code in Hw14Pr1.zip public static void saveDB(String fileNamePart) { String fullFileName = "../" + fileNamePart + ".txt"; H.pl("Saving the database's contents to the file " + fullFileName); H.outputToFile(fullFileName); // redirect the output to a file H.pl(F.getCapacity()); // print the capacity (length of the array) H.pl(F.getCount()); // print the count (# of actual films) F.saveAllFilms(); // print all film data to the file H.outputToConsole(); // redirect subsequent output to the console } public static void readDB(String fileNamePart) { String fullFileName = "../" + fileNamePart + ".txt"; H.pl("Loading database contents from file " + fullFileName); H.inputFromFile(fullFileName); // redirect input to come from the file int capacity = H.ni(“Capacity:”); // reads in the capacity, an int F = new FilmDB(capacity); // trashes old F and creates a new one D = new DirectorDB(capacity); // trashes old D and creates a new one int count = H.ni(“Count:”); // reads in the count, an int for (int i = 0; i < count; i++) // for each film listed (up to count)... { readFilmEntry(); // read that film's entry into F } H.inputFromConsole(); // take subsequent input from the console } public static void readFilmEntry() // method to read 1 film entry
Saving the database code in Hw14Pr1.zip public static void saveDB(String fileNamePart) { String fullFileName = "../" + fileNamePart + ".txt"; H.pl("Saving to the file " + fullFileName); H.outputToFile(fullFileName); H.pl(F.getCapacity()); H.pl(F.getCount()); F.saveAllFilms(); H.outputToConsole(); } 100 2 LOTR: ROTK Jackson Peter 2003 PG-13 9.0 Gigli Brest Martin 2003 R 2.2 file
Reading the database code in Hw14Pr1.zip public static void readDB(String fileNamePart) { String fullFileName = "../" + fileNamePart + ".txt"; H.pl("Loading DB from file " + fullFileName); H.inputFromFile(fullFileName); int capacity = H.ni(“Capacity:”); F = new FilmDB(capacity); D = new DirectorDB(capacity); int count = H.ni(“Count:”); for (int i = 0; i < count; i++) { readFilmEntry(); } H.inputFromConsole(); } 100 2 LOTR: ROTK Jackson Peter 2003 PG-13 9.0 Gigli Brest Martin 2003 R 2.2 file
Constructors FilmDB data private Film[] films; private int count; • create an object Fof typeFilmDB private static FilmDB F; // outside of main public static void main(String[] args) { F = new FilmDB(100); // capacity == 100 in CS5App public FilmDB(int capacity) in FilmDB • same thing for the object Dof typeDirectorDB
Option 7: readFilmEntry Be sure to find/create/add all of the objects needed… checkForFilm 1) Prompt for the title * Make sure that title is not yet in F 2) Prompt for the director: LName, FName * Check to see if that director is already in D Create and add the director to D, if necessary 3) Get the year, rating (G, PG), and review (0.0 - 10.0) 4) Create and add the film to F 5) Add the film to the director’s personal FilmDB findDirectorByName
Constructors Director data private FilmDB filmDB; private String fname; private String lname; • when adding a film… create a Director (if necessary) // prompt for the director’s last and first names Director d = D.findDirectorByName(LN, FN); if (d == null) d = new Director(FN,LN,F.getCapacity()); in readFilmEntry in CS5App public Director(String FN, String LN, int capacity) in Director • create a Film Film f = new Film(…)
null: the nonexistent object a reference pointing to nothing! Film f; Film f = null; equivalent statements Film f Any object (or array) can be set to null. All objects (and arrays) are initially set to null. Don’t try to call a method on a null reference! The new operator creates a real (non-null) object. Film f = new Film(…); String title int year String rating … Film methods & data f
The database(s) FilmDB class int count Film[] films Film[] films[0] films[1] films[2] films[3] films[4] FilmDB films F What is the count vs. the capacity of a FilmDB ?
Count vs. capacity FilmDB class int count Film Film Film Film[] films Film Film Film[] films[0] films[1] films[2] films[3] films[4] FilmDB films F in main (option 7) if (F.isFull()) H.pl(“Sorry! Buy me more memory!”); public boolean isFull() { in FilmDB
Getters / Setters Film class String title Director dir String rating double review int year Director dir Film f else where if (f.getYear() == 2006) H.pl(“That movie hasn’t come out yet!”); public int getYear() { in Film
Retrieving information All films of a certain year... FilmDB class Film[] films int count Film Film Film Film[] films FilmDB films[0] films[1] films[2] films[3] F public void displayFilmsByYear(int year) { in FilmDB
Displaying/Saving a film… Film class String title Director dir String rating double review int year Director dir Film f else where f.display(); // prints contents of f to the screen f.save(); // prints contents of f to a file public void save() // the formatting is crucial! { in Film
A film database: “Human” view 100 2 Lord of the Rings: Return of the King Jackson Peter 2003 PG-13 9.0 Gigli Brest Martin 2003 R 2.2 this is in the file twofilms.txt
Quiz Recursive Movies (Hw14 ExCr) For extra credit this week, use recursion for display, finding, & adding films and directors. Use recursion to display all of the films that match the input year. Assume that x starts at 0. public void displayFilmsByYear(int year, int x) { } in the FilmDB class
Quiz, p.2 Write the checkForFilm method that returns true if a film with the input title is already in the database, and false otherwise. Use recursion,if you like; assume that x starts at 0. public boolean checkForFilm(String title, int x) { } in the FilmDB class
public FilmDB(int capacity) public boolean isFull() public void addFilm(Film f) public void displayAllFilms() public void saveAllFilms() public void displayFilmsByTitle(String titlepiece) public void displayFilmsByYear(int year) public void displayFilmsByRating(String rating) public void displayFilmsByReview(double minreview) public boolean checkForFilm(String fulltitle) public int getCount() public int getCapacity() Methods class FilmDB others are OK, too private Film[] films; private int count; private int capacity; class Film public Film(String title, int year, String rating, double review, Director dir) public String getTitle() public int getYear(); public String getRating() public double getReview() public void display() public void save() private String title; private int year; private String rating; private double review; private Director dir; public DirectorDB(int capacity) public void displayAllDirectors() public void displayFilmsByDirector(String fname, String lname) public Director findDirectorByName(String fname, String lname) public void addDirector(Director dir) public int getCount() public boolean isFull() class DirectorDB private Director[] dirs; private int count; private int capacity; class Director public Director(String fname, String lname, int capacity) public String getFullName() public String getLName() public FilmDB getFilmDB() public String getFName() private String fname; private String lname; private FilmDB filmDB; public static void main(String[] args) static void printMenu() static void saveDB(String filePart) static void readFilmEntry() static void readDB(String filePart) class CS5App static FilmDB F; static DirectorDB D;
public FilmDB(int capacity) public boolean isFull() public void addFilm(Film f) public void displayAllFilms() public void saveAllFilms() public void displayFilmsByTitle(String titlepiece) public void displayFilmsByYear(int year) public void displayFilmsByRating(String rating) public void displayFilmsByReview(double minreview) public boolean checkForFilm(String fulltitle) public int getCount() public int getCapacity() Methods class FilmDB others are OK, too private Film[] films; private int count; private int capacity; class Film public Film(String title, int year, String rating, double review, Director dir) public String getTitle() public int getYear(); public String getRating() public double getReview() public void display() public void save() private String title; private int year; private String rating; private double review; private Director dir; public DirectorDB(int capacity) public void displayAllDirectors() public void displayFilmsByDirector(String fname, String lname) public Director findDirectorByName(String fname, String lname) public void addDirector(Director dir) public int getCount() public boolean isFull() class DirectorDB private Director[] dirs; private int count; private int capacity; class Director public Director(String fname, String lname, int capacity) public String getFullName() public String getLName() public FilmDB getFilmDB() public String getFName() private String fname; private String lname; private FilmDB filmDB; public static void main(String[] args) static void printMenu() static void saveDB(String filePart) static void readFilmEntry() static void readDB(String filePart) class CS5App static FilmDB F; static DirectorDB D;
Adding to the Database(s) ? H. P. Film[] films S. W. int count Film Film Film Film[] films films[0] films[1] films[2] films[3] films[4] a Film to be added to our database… String title (“LOTR: ROTK”) int year (2003) … Film f methods & other data Goal: output films in alphabetical order
Imposing order (Method 1) Add the new film at the end and call sort From Justin to Kelly The World of Batwoman Da Hip Hop Witch Gigli films[5] films[0] films[1] films[2] films[3] films[4]
Keeping order (Method 2) Travel down the list, moving films over to find the right place for the new film Lord of the Rings: ROTK Shawshank Redemption Seven Samurai films[5] films[0] films[1] films[2] films[3] films[4]
Keeping order (2) Travel down the list, moving films over to find the right place for the new film Lord of the Rings: ROTK Shawshank Redemption Godfather Seven Samurai films[5] films[0] films[1] films[2] films[3] films[4] start
Keeping order (2) Travel down the list, moving films over to find the right place for the new film compare Lord of the Rings: ROTK Shawshank Redemption Godfather Seven Samurai films[5] films[0] films[1] films[2] films[3] films[4] start
Keeping order (2) Travel down the list, moving films over to find the right place for the new film Lord of the Rings: ROTK Shawshank Redemption Godfather Seven Samurai move over films[5] films[0] films[1] films[2] films[3] films[4] next up
Keeping order (2) Travel down the list, moving films over to find the right place for the new film Lord of the Rings: ROTK Shawshank Redemption Godfather compare Seven Samurai films[5] films[0] films[1] films[2] films[3] films[4] next up
Keeping order (2) Travel down the list, moving films over to find the right place for the new film Lord of the Rings: ROTK Shawshank Redemption Godfather Seven Samurai move over films[5] films[0] films[1] films[2] films[3] films[4] next up
Keeping order (2) Travel down the list, moving films over to find the right place for the new film compare Lord of the Rings: ROTK Shawshank Redemption Godfather Seven Samurai films[5] films[0] films[1] films[2] films[3] films[4] next up
Keeping order (2) Travel down the list, moving films over to find the right place for the new film Lord of the Rings: ROTK Shawshank Redemption Godfather place Seven Samurai films[5] films[0] films[1] films[2] films[3] films[4] DONE!
Keeping order (2) Travel down the list, moving films over to find the right place for the new film or, equivalently... Godfather Seven Samurai Shawshank Redemption Lord of the Rings: ROTK films[5] films[0] films[1] films[2] films[3] films[4]
compareTo method String s = “startrek”; s.compareTo(“starwars”) is< 0 s.compareTo(“starman”) is> 0 s.compareTo(“startrek”) is== 0 s.compareToIgnoreCase(…) 4 6 1 3 0 2 5 7 works the same way, but without considering upper/lower case distinctions
Keeping order public void addFilm(Film f) (in FilmDB) {
Other notes: indexOf • When printing out all films that match a certain title, allow matches to anypart of the title… use indexOf(): Returns the index of the location of the substring. String s = “startrek”; s.indexOf(“trek”) is 4 s.indexOf(“art”) is 2 s.indexOf(“wart”) is -1 -1 if the input is NOT a substring at all. Use this to decide which films to display…
Parting Wisdom There is a difference between knowing the path and walking the path. - Morpheus, The Matrix Lab this week (optional): You may (will) want to start early on this one…
Other notes • When finding a director, look through the list for matches and return null if not yet in the database... public Director findDirByName(String LN, String FN) { // loop through all of the directors // if both names match, return that director object // if nothing matches, return null } • When printing out all films that match a certain title, allow matches to anypart of the title… use indexOf(): String s = “startrek”; Negative if the input is NOT contained as a substring. Use this to determine which ones to display… s.indexOf(“trek”) is 4 s.indexOf(“art”) is 2 s.indexOf(“wart”) is -1
A film database: class DirectorDB int count Director[] Director[] dirs dirs[0] dirs[1] dirs class Director FilmDB filmDB String fname FilmDB filmDB String lname class Film String rating String title double review Director int year Director dir dir class FilmDB int count Film[] films[0] films[1] Film[] films films
Adding to the Database(s) J. Park Star Wars Titanic Film[] films int count Film Film Film Film[] films films[0] films[1] films[2] films[3] films[4] a brand new Film String title (“LordOfTheRings”) int year (2001) … Film f methods & other data Goal: output films in alphabetical order
Other hints • When finding a film name, allow the user to input a part of the name to look up -- useindexOf(): String s = “startrek”; useful for checking if a string contains a particular substring! s.indexOf(“trek”) is 4 s.indexOf(“art”) is 2 s.indexOf(“wart”) is -1
CS 5 Today • The complexity of programs or, the need for speed... We can sort a list of 5 elements, how about one of 500,000 ? 80 10 42 7 73 L H vs. problems we don’t know how to do at all! (Next week) • HW problem problems? Let me know! • HW 14 (1 problem) due Sunday, 12/7 at midnight M/T sections due Monday, 12/8 at midnight W/Th sections Recitation -- Friday @ 8am
Methods class FilmDB public FilmDB(int capacity) public boolean isFull() public int getCount() public void addFilm(Film f) public Film findFilmByTitle(String title) public void displayAllFilms() public void displayFilmsByTitle(String titlepiece) public void displayFilmsByYear(int year) public void displayFilmsByRating(String rating) public void displayFilmsByReview(double minreview) class Film public Film(String title, int year, String rating, double review, Director dir) public String getTitle() public int getYear() public String getRating() public double getReview() public void display() class DirectorDB public DirectorDB(int capacity) public void displayAllDirectors() public void displayFilmsByDirector(String fname, String lname) public Director findDirectorByName(String fname, String lname) public void addDirector(Director dir) public int getCount() public boolean isFull() class Director public Director(String fname, String lname, int capacity) public String getFullName() public String getLName() public FilmDB getFilmDB() public String getFName()
Methods class FilmDB public FilmDB(int capacity) public boolean isFull() public int getCount() public void addFilm(Film f) public boolean checkForFilm(String fulltitle) public void displayAllFilms() public void displayFilmsByTitle(String titlepiece) public void displayFilmsByYear(int year) public void displayFilmsByRating(String rating) public void displayFilmsByReview(double minreview) private Film[] films; private int count; private int capacity; others are OK, too class Film public Film(String title, int year, String rating, double review, Director dir) public String getTitle() public int getYear() public String getRating() public double getReview() public void display() private String title; private int year; private String rating; private double review; private Director dir; class DirectorDB public DirectorDB(int capacity) public void displayAllDirectors() public void displayFilmsByDirector(String fname, String lname) public Director findDirectorByName(String fname, String lname) public void addDirector(Director dir) public int getCount() public boolean isFull() private Director[] dirs; private int count; private int capacity; class Director public Director(String fname, String lname, int capacity) public String getFullName() public String getLName() public FilmDB getFilmDB() public String getFName() private String fname; private String lname; private FilmDB filmDB;
public FilmDB(int capacity) public boolean isFull() public int getCount() public void addFilm(Film f) public void displayAllFilms() public void saveAllFilms() public void displayFilmsByTitle(String titlepiece) public void displayFilmsByYear(int year) public void displayFilmsByRating(String rating) public void displayFilmsByReview(double minreview) public boolean checkForFilm(String fulltitle) Methods class FilmDB others are OK, too private Film[] films; private int count; private int capacity; public Film(String title, int year, String rating, double review, Director dir) public String getTitle() public int getYear() public String getRating() public double getReview() public void display() public void save() class Film private String title; private int year; private String rating; private double review; private Director dir; public DirectorDB(int capacity) public void displayAllDirectors() public void displayFilmsByDirector(String fname, String lname) public Director findDirectorByName(String fname, String lname) public void addDirector(Director dir) public int getCount() public boolean isFull() class DirectorDB private Director[] dirs; private int count; private int capacity; class Director public Director(String fname, String lname, int capacity) public String getFullName() public String getLName() public FilmDB getFilmDB() public String getFName() private String fname; private String lname; private FilmDB filmDB; public static void main(String[] args) static void printMenu() static void saveDB(String filePart) static void readFilmEntry() static void readDB(String filePart) class CS5App static FilmDB F; static DirectorDB D;
Sorting recursively A 80 10 42 7 73 L U void moveMinToLeft(double[] A, int L, int U) { if (L == U) return; if (A[L] > A[U]) // should we swap the last? { double x = A[L], y = A[U]; A[L] = y; A[U] = x; } moveMinToLeft(A,L,U-1); // need to swap the rest… } void sort(double[] A, int L, int U) { if (L >= U) return; moveMinToLeft(A,L,U); // sort the first element sort(A,L+1,U); // sort the rest of them }