Download 11files - Calvin College

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Clusterpoint wikipedia , lookup

Functional Database Model wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
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 centuries
Bring 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.
Some hint goes here
(maybe an image
and/or audio)…
User’s guess…
Give up
© Calvin College, 2009
4
Example: Design
●
The design includes the following classes:
CharacterPanel
CharacterDrillController
+setImage()
+setPronunciation()
CharacterDrill
+myAnswerIndex
+myHintCount
CharacterDrillTest
+guess()
+reset()
+getHintImageFilename()
+getHintPronunciationFilename()
+getHintText()
Character
1
*
+myTranslation
+myPinyin
+myImageFilename
+myPronunciationFilename
+mySentence
© Calvin College, 2009
5
Iteration 0
●
Analysis
●
Design
●
Implementation
●
Test
© 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:
Input Stream
Program
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 systemindependent 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
11
Iteration 1
●
Analysis
●
Design
●
Implementation
●
Test
© Calvin College, 2009
12
Example: File Input
Given: the data filename and path
Algorithm:
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
14
Iteration 2
●
Analysis
●
Design
●
Implementation
●
Test
© Calvin College, 2009
15
Example: Record Input
Given: the data filename and path
Algorithm:
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.
ReadRecordsConsole
Soldier
+myName
+myRank
+mySerialNumber
+myNicknames
+Soldier(String)
+toString()
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));
© 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
22
Iteration 3
●
Analysis
●
Design
●
Implementation
●
Test
© Calvin College, 2009
23
Example: Record Output
Given: the output filename and path
Algorithm:
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.
Input Stream
buffer
Program
Output Stream
buffer
Program
© 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
Database
are very common
problems.
Files
Database Management
Records
Systems are designed
Fields
to address these
Characters/Strings/Integers
problems.
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
Rank
Serial Number
name
String
Joe
Colonel
1425321
rank
String
Mary
Lieutenant
8375679
serialNumber
String
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
--------Mary
Bob
RANK
-------Lieutenant
Private
© Calvin College, 2009
32
Edgar F. Codd (1923-2003)
Relational Data Model
●
●
Codd developed the relational model in
the early 1970s.
Included features for:
–
Data definition
– Data queries
●
Most current database systems use the
relational model.
image from wikipedia, June, 2006
© Calvin College, 2009
33
Privacy
●
What’s the
Big Idea
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.
© Calvin College, 2009