14
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)

PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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)

Page 2: PowerPoint Presentation · 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 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

Page 3: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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

Page 4: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

Review: polymorphism

Quiz example question

Explain the difference between overloading and overriding.

4

Page 5: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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;

}

}

Page 6: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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 {...}

Page 7: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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;

}

Page 8: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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

Page 9: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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

Page 10: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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();

}

Page 11: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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();

Page 12: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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();

}

}

Page 13: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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();

}

Page 14: PowerPoint Presentation · Point out the bug and fix the code. 5 public abstract class Mammal {private String name; public Mammal(String name){name = name;}} Review: polymorphism

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();

}

}