Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions exercises/practice/all-your-base/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Instructions append

## Input format

The digits are given as an `int[]`, most significant digit first.
An empty array represents the number 0, and leading zeros are allowed (`{0, 6, 0}` in base 7 is 42).
`convertToBase` should return the digits of the number in the new base without leading zeros, so 0 converts to `{0}`.

## Exception messages

Sometimes it is necessary to [throw an exception](https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html).
When you do this, you should always include a **meaningful error message** to indicate what the source of the error is.
This makes your code more readable and helps significantly with debugging.

This particular exercise requires that you use the [throw keyword](https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html)
to "throw" an `IllegalArgumentException` for invalid input.
The tests will only pass if you both `throw` the `exception` and include the exact message for each case:

- If either base is less than 2 (checked in the constructor for the original base, and in `convertToBase` for the new base):

```java
throw new IllegalArgumentException("Bases must be at least 2.");
```

- If any digit is negative:

```java
throw new IllegalArgumentException("Digits may not be negative.");
```

- If any digit is greater than or equal to the original base:

```java
throw new IllegalArgumentException("All digits must be strictly less than the base.");
```

Check the original base before the digits, so that `new BaseConverter(1, new int[]{1})` reports the invalid base.