Transcript Lecture 15

Lecture 15
March 28, 2000
Exercise 5 Solution
•
•
[ Source Code ]
Answers to Questions:
1.
2.
3.
4.
5.
Both invoked Object’s toString() method, and printed the class name,
“@” and the hash code for the object.
Now the child class prints “I am object of class ChildClass”, but the
parent class object still uses Object’s toString() method and prints the
class name, etc.
Now each print statement invokes the toString() method from its
respective class.
The child class prints the concatenation of the string returned by
super.toString() and its own string.
When rootObject points to a ChildClass object, ChildClass’ toString()
method is used, but when rootObject points to a ParentClass object,
ParentClass’ toString() method is used.
Exercise 6a
• If argv.length < 2, print an error message and exit.
• Set nextArg to 0 and isCaseSensitive to true.
• If argv[0] is “-i”
– Set nextArg to 1 and isCaseSensitive to false.
• If argv.length < nextArg+1, print an error message
and exit.
• Print a message about case sensitivity.
• For each remaining argument, print it.
Exercise 6b
• Save the search string and its length.
• For each file name:
– Try:
• Create a BufferedReader
• For each line:
– For each “valid” position in the line:
» Extract a substring
» Compare the substring to the search string
» If they match, print the file name and the line, and
continue to the next line in the file
0 .. 15
0 1 2 3
13
Which Substrings to Compare
(Search string length 3 and text line length 16.)
There Will be a Quiz Next
Tuesday
• I will drop your bottom two quiz scores.
– Assuming there are four.
– Missed quizzes count as zeros.
• Topics will be object-based and objectoriented programming concepts.
• Exam 2 will be April 18.
File Operations
(From Chapter 17)
read
A Java Program in
memory
write
A File Contains a Sequence of
Bytes
• Java calls this a “stream.”
• Most file reads and writes are done sequentially.
The “system” (Java and the operating system)
keeps track of the current position in the file so
sequential reads/writes move through the file.
• Random access is possible.
– Involves skipping directly to a particular position in the
file.
Classes for File I/O
• For character streams (“text” files)
–
–
–
–
BufferedReader
BufferedWriter
FileReader
FileWriter
• For byte streams (“binary” files)
– FileInputStream
– FileOutputStream
Class File
• For performing operations on files.
– Test them in various ways.
– Move, copy, delete them.
– Use to create FileInputStream, FileReader, etc.
objects.
• Write a [ program to tell about files ] named
by the user.