Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
10/15/2018
Programming Data Structures
1
• review: polymorphism and inheritance• review: exception handling• review: file I/O• More about file I/O• QA session for take-home assignments
Quiz 2 will be on next Wednesday, Oct. 24!
Open-book(entire-class)
Quiz 2: 2.5 points
1. (0.1) array
2. (0.6) polymorphism and inheritance
3. (0.9) exception handling
4. (0.9) file I/O
2
Review: polymorphism
Quiz example question
In Java, a derived class can have ____ base class(es).
a. one
b. two
c. three
d. there is no limit
3
Review: polymorphism
Quiz example question
Explain the difference between overloading and overriding.
4
Review: polymorphism
Quiz example question
There is a bug in the following code. Point out the bug and fix the code.
5
public abstract class Mammal {
private String name;
public Mammal(String name){
name = name;
}
}
Review: polymorphism
Quiz example question
Given the following code, write a public method check in class Main, the input is a Mammal type object, the output is a boolean type, represents if the input object is a Dog.
6
public abstract class Mammal {
public abstract void speak();
}
public class Dog extends Mammal {
public void speak(){System.out.println("Woof-woof");}
}
public class Main {...}
Review: Downcasting
Quiz example question
There is a bug in the following code. Point out the bug and fix the code.
7
public void downcasting(Mammal m){
Cat c = (Cat) m;
}
Review: exception handling
Quiz example question
Define an exception class, NegativeNumberException. The class should have a constructor with no parameters. When getMessage() is applied to an exception created with this constructor, it should return “Negative number not allowed!”
8
Review: File I/O
Quiz example question
Write a method readfile. The return type of the method is int. The input is a String variable storing a file name. The method reads every line of the file and return the number of the lines of the file.
9
Review: File I/O
Quiz example question
Given the following code, please explain what flush() does on line 8?
10
public void writefile(String content){
1 FileOutputStream os = null;
2 try {
3 os = new FileOutputStream("newfile");
4 } catch (FileNotFoundException e) {
5 e.printStackTrace();
}
6 PrintWriter writer = new PrintWriter(os);
7 writer.print("headline");
8 writer.flush();
9 writer.print(content);
10 writer.close();
}
Scanner method: hasNext and next
More about File I/O
11
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream("readme.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(0);
}
Scanner scanner = new Scanner(inputStream);
while(scanner.hasNext()) {
String c = scanner.next();
System.out.println("next:" + c);
}
scanner.close();
More about File I/O
BufferedReader
12
public static void main(String[] argv){
try {
FileReader stream = new FileReader("readme.txt");
BufferedReader reader = new BufferedReader(stream);
String line = reader.readLine();
System.out.println(line);
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
More about File I/O
BufferedReader: How to read all the lines of a file?
13
try {
FileReader stream = new FileReader("readme.txt");
BufferedReader reader = new BufferedReader(stream);
String line = reader.readLine();
while (line != null){
System.out.println(line);
line = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
More about File I/O
Overwriting vs Appending
14
public static void main(String[] argv){
try {
FileOutputStream outputStream = new FileOutputStream("writeme.txt", true);
PrintWriter writer = new PrintWriter(outputStream);
writer.println("abc");
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}