DOC PREVIEW
Stanford CS 106A - Lecture Notes

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Eric Roberts Handout #55CS 106A February 26, 2010Slides for Data-Driven ProgramsData-Driven ProgramsEric RobertsCS 106AFebruary 26, 2010Computing and the CountercultureTwo recent books argue that the personal computing revolutionowes as much to the counterculture of the 1960s as it does to thetechnological strength and entrepreneurial spirit of Silicon Valley.Ted Nelson’s Cyberspace DreamsThe countercultural vision comes across particularly clearly in thetwo-sided book Computer Lib/Dream Machines which was writtenby cyberspace visionary Ted Nelson in 1974.Data-Driven Programs• In most programming languages, data structures are easier tomanipulate than code. As a result, it is often useful to designapplications so that as much of their behavior as possible isrepresented as data rather than in the form of methods.Programs that work this way are said to be data driven.• In a data-driven system, the actual program (which is called adriver) is usually very small. Such driver programs operate intwo phases:Read data from a file into a suitable internal data structure.1.Use the data structure to control the flow of the program.2.• To illustrate the idea of a data-driven system, we’re going tospend most of this lecture building a programmed-instruction“teaching machine” of the sort that Ted Nelson discusses(mostly critically) in Dream Machines.The Course Data FileIn our teaching machine application, the course designer—who isan expert in the domain of instruction and not necessarily aprogrammer—creates a data file that serves as the driver. Thegeneral format of the whole file is shown on the left, and a specificexample of a question and its answers appears on the right.Choosing an Internal RepresentationThe first step in building the teaching machine is to design a set ofclasses that can represent the data and relationships in the file. Allof the relevant data should be accessible from a single structurethat contains all relevant information in a nested series of classes.– 2 –Converting External to Internal FormJava programming review1Would you like help withint or boolean type?-----int: 2boolean: 102True or false: Integers canhave fractional parts.-----true: 3false: 53No. Floating-point numbershave fractional parts;integers do not.True or false: Integers canbe negative.-----true: 5false: 4/* * File: TMQuestion.java * --------------------- * This file defines a class to represent a single question. */import acm.util.*;import java.io.*;import java.util.*;/** * This class models a single question in the course data base. */class TMQuestion {/** * Creates a new question by reading its data from the specified reader. * If no data is left in the reader, this method returns <code>null</code> * instead of an <code>TMQuestion</code> value. Note that this is a * static method, which means that you need to call * *<pre><code> * TMQuestion.readQuestion(rd) *</code></pre> * * @param rd The reader from which the question data is read */Code for the TMQuestion Classpage 1 of 4 public static TMQuestion readQuestion(BufferedReader rd) { try { String line = rd.readLine(); if (line == null) return null; TMQuestion question = new TMQuestion(); question.questionNumber = Integer.parseInt(line); question.questionText = new ArrayList<String>(); while (true) { line = rd.readLine(); if (line.equals(MARKER)) break; question.questionText.add(line); } question.answerTable = new HashMap<String,Integer>(); while (true) { line = rd.readLine(); if (line == null || line.length() == 0) break; parseAnswerLine(question, line); } return question; } catch (IOException ex) { throw new ErrorException(ex); } catch (NumberFormatException ex) { throw new ErrorException("Illegal question number"); } }Code for the TMQuestion Classpage 2 of 4/** * Returns the number of this question. */ public int getQuestionNumber() { return questionNumber; }/** * Returns an ArrayList containing the text for this question. */ public ArrayList<String> getQuestionText() { return questionText; }/** * Looks up the answer in the table of possible answers for this question. * If a match is found, the number of the associated next question is * returned. If not, the method returns -1. */ public int lookupAnswer(String answer) { Integer value = answerTable.get(answer.toUpperCase()); if (value == null) return -1; return value; }Code for the TMQuestion Classpage 3 of 4/** * This method scans the answer line to separate the text of the answer * from the number of the next question. The value of the next question * is entered into the HashMap stored as part of this TMQuestion structure. */ private static void parseAnswerLine(TMQuestion question, String line) { int colon = line.indexOf(":"); if (colon == -1) { throw new ErrorException("Missing colon in " + line); } String response = line.substring(0, colon).toUpperCase().trim(); int nextQuestion = Integer.parseInt(line.substring(colon + 1).trim()); question.answerTable.put(response, new Integer(nextQuestion)); }/* Private constants */ private static String MARKER = "-----";/* Instance variables */ private int questionNumber; private ArrayList<String> questionText; private HashMap<String,Integer> answerTable;}Code for the TMQuestion Classpage 4 of 4/* * File: TMCourse.java * ------------------- * This class defines the data structure for a course for use with * the TeachingMachine program. */import acm.util.*;import java.io.*;import java.util.*;public class TMCourse {Code for the TMCourse Classpage 1 of 3– 3 –/** * Creates a new course for the teaching machine by reading the * data in the specified file. The file format for the data is * defined in Handout #56 ("Data-Driven Programs"). * * @param filename The name of the data file */ public TMCourse(String filename) { questions = new TreeMap<Integer,TMQuestion>(); try { BufferedReader rd = new BufferedReader(new FileReader(filename)); title = rd.readLine(); while (true) { TMQuestion question = TMQuestion.readQuestion(rd); if (question == null) break; questions.put(question.getQuestionNumber(), question); } rd.close(); } catch


View Full Document

Stanford CS 106A - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?