Skip to content

Feb 29: Editor Lab, Diagrams

Learning Objectives

After today's class, you should be able to:

  • Use a PrintWriter to save an ArrayList to a text file.
  • Use a Scanner to load file contents into an ArrayList.
  • Replace throws declarations with try-catch blocks.

Reminders

  • By Friday 11pm: Do the Week 7 reading and take Quiz06
  • By Friday 11pm: Submit HW 6 stubs (including Javadoc)
    • Must pass Compile, CompileOfficialTests, and Style
  • By Monday 11pm: Complete Homework 6 (due Mar 04)

Lesson Outline

Individual Lab [45 min]

Notice how Lab 5 tests whether FileNotFoundException is thrown:

assertThrows(FileNotFoundException.class, () -> {
    new Document("THISISNOTAFILE.txt");
});

The -> operator defines a lambda expression. In programming, a "lambda" is a block of code that can be assigned to a variable. Lambda expressions allow you to pass code as an argument to a method.

In the above example, we define a lambda that takes no parameters (). The lambda calls the Document constructor with an invalid filename. assertThrows() checks whether the lambda throws FileNotFoundException.

Group Activity [30 min]