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 as1,0..1or*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 withpackage quiz.domain;. A class of another package is used by its full name or after animport, and from another package a class sees only what'spublic(and a subclass also what'sprotected). Larger programs split their classes into packages by what they're responsible for, such asdomain,logicandui. - Module 42, exceptions.
tryandcatch, severalcatchblocks andfinally; checked exceptions, which must be caught or declared withthrows, 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
trueappends to it, andclose(), 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 withsaveandloadkeeps 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.