Java Arena
Step 5 of 5

The end of part 11

This step ends this module, and with it the MOOC's part 11. First a look back at the whole part, and then a last program that puts its class diagrams, exceptions and files to work together.

What part 11 covered

  • Module 40, class diagrams. A UML class diagram shows a program's classes as boxes: the name, the fields, and the constructors and methods, with - for private and + for public, and the types after colons. An arrow shows that one class refers to another, with a multiplicity such as 1, 0..1 or * at its end, and a line with a hollow triangle shows inheritance, dashed for a class that implements an interface. You wrote classes from boxes, and drew boxes from classes.
  • Module 41, packages. Classes are grouped in packages, which are folders: a file in quiz/domain/ starts with package quiz.domain;. A class of another package is used by its full name or after an import, and from another package a class sees only what's public (and a subclass also what's protected). Larger programs split their classes into packages by what they're responsible for, such as domain, logic and ui.
  • Module 42, exceptions. try and catch, several catch blocks and finally; checked exceptions, which must be caught or declared with throws, and unchecked ones, which needn't be; try-with-resources; throw new IllegalArgumentException(...) to refuse a bad parameter; and reading a stack trace.
  • Module 43, writing files. A PrintWriter writes a file and replaces what it held, a FileWriter with true appends to it, and close(), or better a try-with-resources, makes sure the text reaches the file. Objects go into a file one per line, with a separator that reading can split at, and a class with save and load keeps its data from one run to the next.

Other ways to write

Java has more ways to write a file than PrintWriter. One is Files.write, the writing twin of module 42's Files.readAllLines: Files.write(Paths.get("todo.txt"), tasks) writes every String of the list tasks into the file, one per line, in a single call. This course sticks with PrintWriter, which works just like System.out, but you may meet the others in code that other people write.

The end of part 11

Part 12 comes next, starting with type parameters (generics): classes of your own that take a type as a parameter, as ArrayList does in ArrayList<String>. Part 11 ends with the MOOC's conclusion page on its own site.

Your task

Challenge 1 of 3

A last program for part 11: a notes app that keeps its notes in a file, one note per line, with the note's title and text separated by ;, like Door code;4417. Here's its plan:

The diagram as text
  • Notebook, a class
    • public constructor Notebook()
    • public method add(note: Note): void
    • public method count(): int
    • public method printAll(): void
    • public method save(fileName: String): void
    • public method load(fileName: String): void
  • Note, a class
    • private field title: String
    • private field text: String
    • public constructor Note(title: String, text: String)
    • public method getTitle(): String
    • public method getText(): String
    • public method toString(): String

Connections:

  • Each Notebook has any number of Note objects (notes).

Most of it is written. What's left:

  • In Note.java, a title with a ; in it would break the file's format, so the constructor refuses one: it throws an IllegalArgumentException with the message a title can't contain ;. A note's text may contain ;.
  • In Notebook.java, save writes every note as its title, ; and its text, one per line, replacing what the file held. load reads such a file and adds a note for each line to the notebook. The title ends at the line's first ;, and everything after that ; is the text. Both pass exceptions on with throws Exception.

A note prints as its title, a colon and its text. The check loads the file shown below, adds a shopping note, saves the notebook into the same file and prints every note:

Wifi: the password is on the router
Door code: 4417
Shopping: milk; eggs; bread

and notes.txt then holds the three notes, the new one as Shopping;milk; eggs; bread.

Challenge 2

A last look at exceptions and files together. save throws an IllegalArgumentException for an empty item, and one of the files is to go into a folder that doesn't exist. Files.readAllLines returns a file's lines in a list. What does the program print?

Challenge 3

Two roommates keep their own shopping lists, mine.txt and yours.txt, one item per line in small letters. Write the method merge(String first, String second, String target), which writes every item of the files first and second into the file target, each item only once, in alphabetical order. The method prints nothing, and passes exceptions on with throws Exception.

The check runs merge("mine.txt", "yours.txt", "shared.txt"); with the files shown below, and after it shared.txt should hold:

apples
bread
coffee
milk