From cc33ed5db71dd991c41a088b7df2bd5998320668 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Tue, 25 Nov 2025 10:21:35 +0100 Subject: [PATCH] start week 13 --- main.typ | 2 +- week13/Printer.java | 53 +++++++++++++++++++++++++++++++++++++++++++++ week13/common | 1 + week13/doc.typ | 41 +++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 week13/Printer.java create mode 120000 week13/common create mode 100644 week13/doc.typ diff --git a/main.typ b/main.typ index ae999d6..b22c62c 100644 --- a/main.typ +++ b/main.typ @@ -15,7 +15,7 @@ Collection of solutions to programming exercises as part of Introduction to Prog ) #{ - let count = 12; + let count = 13; for week in range(1, count + 1).filter(it => it != 8) { let a = "./week" + str(week) + "/doc.typ" include a diff --git a/week13/Printer.java b/week13/Printer.java new file mode 100644 index 0000000..cd51724 --- /dev/null +++ b/week13/Printer.java @@ -0,0 +1,53 @@ +package week13; + +import common.StdRandom; + +public class Printer { + static class OutOfPaperException extends Exception { + @Override + public String getMessage() { + return "No Paper pack found in printer.\n" + + "Check that there's enough paper left for this print job, that the Paper pack is clean and that the paper pack chip is undamaged. "+ + "Please note that third-party Paper packs may not be supported. Check that the Paper pack is made for this model."; + } + } + static class OutOfTonerException extends Exception { + @Override + public String getMessage() { + return "Printing supply pack is empty or was not found.\n" + + "Check that an official Printing supply pack is correctly inserted in the printer, try re-seating if necessary. " + + "Check that Printing supply packs are inserted into the correct color coded slots. Check that your Printing resupply subscription is active. " + + "Please note that third-party Printing supply packs may not be supported. Check that the Paper pack is made for this model."; + } + } + static class PaperJamException extends Exception { + @Override + public String getMessage() { + return "lp0 on fire."; + } + } + + private static Exception getExceptionFromErrorCode(int errorCode) { + return switch(errorCode / 10) { + case 0 -> new OutOfPaperException(); + case 1 -> new OutOfTonerException(); + case 2 -> new PaperJamException(); + default -> throw new IllegalArgumentException(); + }; + } + + public static void print() throws Exception { + var rnd = StdRandom.uniformInt(30); + var exception = getExceptionFromErrorCode(rnd); + throw exception; + } + + public static void main(String[] args) { + try { + print(); + System.out.println("Printed successfully, thanks for your patronage."); + } catch(Exception e) { + System.err.println(e.getMessage()); + } + } +} diff --git a/week13/common b/week13/common new file mode 120000 index 0000000..60d3b0a --- /dev/null +++ b/week13/common @@ -0,0 +1 @@ +../common \ No newline at end of file diff --git a/week13/doc.typ b/week13/doc.typ new file mode 100644 index 0000000..c62b46b --- /dev/null +++ b/week13/doc.typ @@ -0,0 +1,41 @@ +#import "./common/common.typ" : * + +#show: template + += Week 13 + +== Exercise 13.01 + +Explain - in your own words - what is an exception? + +An object containing information about why a semi-hidden branching happened, usually used for error states. +Thrown exceptions bubble up, as if a return was used, until there's a registered exception handler for a given code region, usually by try/catch. + +== Exercise 13.05 + +For each of the following exceptions, mark whether it is a checked or unchecked: + + - `NullPointerException` - unchecked + - `IOException` - checked + - `IllegalArgumentException` - unchecked + - `ArrayIndexOutOfBoundsException` - unchecked + - `NumberFormatException` - unchecked + - `ConcurrentModificationException` - unchecked + - `InterruptedException` - unchecked + +How many of these have you experienced? + +All of these except ConcurrentModificationException as I didn't write multithreaded programs in Java. + +== Exercise 13.07 + +Write a class to represent a gearbox with five gears and a gear for reverse. Add a method `changeGear(int gear)` to change the current gear. The method must `throw IllegalArgumentException` if the gear is not one of `-1`, `1`, `2`, `3`, `4`, and `5`. Here reverse is represented as `-1`. Write a `class IllegalGearChangeException`, which `extends RuntimeException`, and throw this exception: + +a. when switching from any gear other than the first gear into reverse (and vice versa), and +b. when skipping one or more gears. For example, it is illegal to switch directly from the first gear to the third gear. It is also not allowed to switch directly from reverse to the fourth gear. + +== Exercise 13.09 + +Write a class to represent a printer from hell. The class should have a single method `print()`. Whenever this method is called, the printer randomly throws one of the following exceptions: `OutOfPaperException`, `OutOfTonerException`, `PaperJamException`. Write classes for these exceptions. Write a main method, which calls `print()`, catches any exception, prompts the user to take action (e.g. "replace toner"), waits for confirmation from the user, and then calls `print()` again. Bonus points for infuriating or vaguely worded instructions. + +#embedClass(name: "Printer") \ No newline at end of file