© Calvin College, 2009 1 All our knowledge brings us nearer to our ignorance, All our ignorance...
-
Upload
esther-whinnery -
Category
Documents
-
view
220 -
download
1
Transcript of © Calvin College, 2009 1 All our knowledge brings us nearer to our ignorance, All our ignorance...
© Calvin College, 2009
1
All our knowledge brings us nearer to our ignorance,
All our ignorance brings us nearer to death,But nearness to death, no nearer to God.Where is the Life we have lost in living?Where is the wisdom we have lost in knowledge?Where is the knowledge we have lost in
information?The cycles of Heaven in twenty centuriesBring us farther from God and nearer to the Dust.
- T. S. Eliot, Choruses From ‘The Rock’, Selected Poems
(New York: Harcourt, Brace & World, 1964), p. 107.
© Calvin College, 2009
2
File Input & Output
● Example● Streams:
– Input Streams– Output Streams– Buffering– Applications
● Database Management Systems● Privacy
© Calvin College, 2009
3
Example: Analysis● We’d like to modify
the guessing game so that drills students on Chinese Characters.
● A sketch of a solution achieving this goal is shown here.
User’s guess… Give up
Some hint goes here (maybe an image and/or audio)…
© Calvin College, 2009
4
Example: Design● The design includes the following
classes:
CharacterDrillController
CharacterDrill
+myAnswerIndex+myHintCount
+guess()+reset()+getHintImageFilename()+getHintPronunciationFilename()+getHintText()
Character
+myTranslation+myPinyin+myImageFilename+myPronunciationFilename+mySentenceCharacterDrillTest
*1
CharacterPanel
+setImage()+setPronunciation()
© Calvin College, 2009
6
Limitations of Hard-Coding Data
● Our initial iteration assumes that:– we can code the data directly in the program;– the data never (or rarely) changes;– people who change data know how to program.
● This approach does not scale well to real data-based applications.
© Calvin College, 2009
7
Input & Output Streams
● Input and output in Java is accomplished using stream classes:
Program
Input Stream
Output Stream
Program
© Calvin College, 2009
8
Java Streams
● Simple I/O uses predefined streams:– System.in– System.out– System.err
● Create file streams using:– File– Scanner– PrintWriter
© Calvin College, 2009
9
File● The File class models a system-
independent view of a file comprising:– Filename;– Directory pathname:
• Relative;• Absolute.
● Patterns:new File(pathAndFilenameString)new File(pathnameString, filenameString)
© Calvin College, 2009
10
Scanner● The Scanner class can scan:
– Keyboard input stream;– File;– String.
● Pattern: new Scanner(inputStreamOrFileOrString)
● The API includes these methods:– next() nextInt() nextLine() ...– hasNext() hasNextInt() hasNextLine() ...
– close()
© Calvin College, 2009
12
Example: File InputGiven: the data filename and pathAlgorithm:
Open a read stream/scanner to the given file.While the file has more tokens:
Read the token.Process the token.
Close the file stream/scanner.
Scanner fileIn = new Scanner(new File(path, filename));
List<Integer> scores = new ArrayList<Integer>();while (fileIn.hasNext()) scores.add(fileIn.nextInt());fileIn.close();System.out.println(scores);
© Calvin College, 2009
15
Example: Record InputGiven: the data filename and pathAlgorithm:
Open a read stream/scanner to the given file.While the file has more lines:
Read the line.Process the fixed and variant tokens.
Close the file stream/scanner.
Scanner fileIn = new Scanner(new File(path, filename));List<Soldier> soldiers = new ArrayList<Soldier>();while (fileIn.hasNextLine()) soldiers.add(new Soldier(fileIn.nextLine()));fileIn.close();for (int i = 0; i < soldiers.size(); i++) System.out.println(soldiers.get(i));
ReadRecordsConsole
Soldier
+myName+myRank+mySerialNumber+myNicknames
+Soldier(String)+toString()
© Calvin College, 2009
16
Example: Record Input (cont.)public class Soldier {
private String myName, myRank, mySerialNumber; private List<String> myNickNames;
public Soldier(String line) { Scanner scanner = new Scanner(line); myName = scanner.next(); myRank = scanner.next(); mySerialNumber = scanner.next(); myNickNames = new ArrayList<String>(); while (scanner.hasNext()) { myNickNames.add(scanner.next()); } scanner.close(); } // other code...}
© Calvin College, 2009
21
PrintWriter● The PrintWriter class can print
formatted text to a text-output stream.
● Pattern:new PrintWriter(outputFile)
● The API includes these methods:– print() println() ...– printf()– close() flush()
© Calvin College, 2009
23
Example: Record OutputGiven: the output filename and pathAlgorithm:
Open a print writer stream to the given file.Loop forever:
Prompt for and read a line of data.If the line is the sentinel
Quit.else
Output the line.Close the file stream/scanner.
© Calvin College, 2009
24
Example: Record Output (cont.)
PrintWriter fileOut = new PrintWriter(new File(path, filename));
String line = "";while (true) { System.out.print("enter record (just enter to quit): "); line = keyboard.nextLine(); if (line.equals("")) { break; } else { fileOut.println(line); } }
fileOut.close();System.out.println("data stored to: " + path + filename);
© Calvin College, 2009
26
Buffering
● I/O is slow relative to processing speed, so it is generally buffered.
Program
Input Stream
Output Stream
Program
buffer
buffer
© Calvin College, 2009
27
Java Buffering
● Buffering can be added to file streams to improve the efficiency of I/O operations.
● A buffer is a memory area that stores up input/output data so that it can be input/output all at once.
● Patterns: new BufferedReader(new FileReader(fileNamePath));
new BufferedWriter(new FileWriter(fileNamePath));
© Calvin College, 2009
28
Uses for File I/O
● Using simple text files is unusual, but they are used, e.g.:– Log files;– Configuration files.
● Applications are more likely to use:– Database management systems;– XML files;– Lexing and parsing tools.
© Calvin College, 2009
29
Data Management
● Storing, retrieving and manipulating data sets are very common problems.
● Database Management Systems are designed to address these problems.
Database
Files
Records
Fields
Characters/Strings/Integers
Bits
© Calvin College, 2009
30
The Relational Data Model
● Most current DBMSs use the relational data model.
● Relational models separate the structure of the data from the data itself.
Schema: Data:Field Field Type
name String
rank String
serialNumber String
Name Rank Serial Number
Joe Colonel 1425321
Mary Lieutenant 8375679
Bob Private 2367532
© Calvin College, 2009
31
Structured Query Language
● SQL is the standard query language for relational databases.
● Example:
SQL> SELECT name, rank FROM SoldierTable WHERE ID > 2000000;
NAME RANK --------- -------- Mary Lieutenant Bob Private
© Calvin College, 2009
32
Edgar F. Codd (1923-2003) Relational Data Model
image from wikipedia, June, 2006
● Codd developed the relational model in the early 1970s.
● Included features for:– Data definition– Data queries
● Most current database systems use the relational model.
© Calvin College, 2009
33
Privacy
● Database systems allow us to build and maintain large data sets:– This can be useful for many applications.– It can also be dangerous.
● Guidelines:– Collect only what you need to collect.– Have a clear policy on what information is
collected and on how it is used.– Keep the data accurate and secure.
What’s theBig Idea