---
# System prepended metadata

title: Jam 03 - Exercise 5 - Base-3 Converter
tags: [refactoring, input validation, jam03, regex, "3 \U0001F9EA in testing", "4 \U0001F973 done"]

---

---
title: "Jam 03 - Exercise 5 - Base-3 Converter"
tags: 
  - 3 🧪 in testing
  - 4 🥳 done
  - jam03
  - regex
  - refactoring
  - input validation
---
<!-- markdownlint-disable line-length single-h1 no-inline-html -->
<!-- markdownlint-configure-file { "ul-indent": { "indent": 4 }, "link-fragments": {"ignore_case": true} } -->

{%hackmd dJZ5TulxSDKme-3fSY4Lbw %}

# Exercise 5: Base-3 Converter with Regex and Refactoring

Before you start coding, take a few minutes to read through this entire exercise. Understanding the full requirements and seeing the example output will help you plan your approach.

:::info
💪 **You've Got This!**

You now have all the skills needed to tackle this challenge:

- You know how to create and test regex patterns
- You've learned powerful debugging techniques
- You understand how to organize code through refactoring

Remember:

- ✨ **Trust Your Skills**: Don't immediately jump to searching for solutions online. You're better than that!
- 🧠 **Use Your Brain**: The satisfaction of solving a problem on your own is worth the extra effort
- 👥 **Work Together**: Complex problems are more manageable when you discuss them with classmates
- 🌱 **Be Patient**: Take time to think through the problem before seeking help

The goal isn't just to make it work - it's to develop your problem-solving abilities and confidence as a programmer.
:::

## Understanding Base-3 Numbers

Before we start coding, let's understand what base-3 (or ternary) numbers are:

- Just like decimal (base-10) uses digits 0-9
- And binary (base-2) uses digits 0-1
- Base-3 uses only digits 0, 1, and 2

Each position in a base-3 number represents a power of 3

```text
In decimal:   147 = 1×100 + 4×10  + 7×1
                  = 1×10² + 4×10¹ + 7×10⁰

In base-3:    121 = 1×9   + 2×3   + 1×1
                  = 1×3²  + 2×3¹  + 1×3⁰
                  = 9     + 6     + 1
                  = 16 (in decimal)
```

Some examples:

- Base-3: `101` → Decimal: `10` (because `1`×9 + `0`×3 + `1`×1 = 9 + 0 + 1 = 10)
- Base-3: `212` → Decimal: `23` (because `2`×9 + `1`×3 + `2`×1 = 18 + 3 + 2 = 23)

## Program Requirements

Create a class called `BaseConverter` that:

1. **Input Handling**:
   - Asks the user for a base-3 number
   - Validates input using regex (only digits 0, 1, 2 allowed)
   - Discards any initial zeros
   - Prompts again if input is invalid

2. **Conversion**:
   - Converts valid base-3 numbers to decimal
   - Must implement conversion logic without using Integer.parseInt() or similar methods

3. **User Experience**:
   - Provides clear error messages for invalid input
   - Asks if user wants to try again (y/n prompt)
   - Exits gracefully when user is done

### Example Output

```text
Welcome to the base-3 converter.

Enter a base-3 number: 101
10

Try again? [Y|N] y

Enter a base-3 number: 232
Invalid input! Please use only 0, 1, or 2.
Try again. Enter a base-3 number: 212
23

Try again? [Y|N] y

Enter a base-3 number: 3
Invalid input! Please use only 0, 1, or 2.
Try again. Enter a base-3 number: 2
2

Try again? [Y|N] y

Enter a base-3 number: 12012012012012012012
2011606385

Try again? [Y|N] n
Goodbye!
```

:::warning
🎯 **The Key to Success: Iterative Development**

Don't try to write the entire program at once! Students who attempt to build everything in one go often end up frustrated and with code that doesn't work at all. Instead:

1. **Start Small, Test Often**:
   - Begin with just the input validation
   - Make sure it correctly identifies valid and invalid base-3 numbers
   - Only move on when this part works perfectly

2. **Add Features One at a Time**:
   - Next, implement the base-3 to decimal conversion
   - Then add the "try again" functionality
   - Test each piece thoroughly before adding the next

3. **Keep What Works**:
   - Each time you get something working, commit it!
   - This way, if something breaks later, you can go back
   - You'll also have parts working for partial credit if you run into trouble

Remember: It's better to have some parts working perfectly than everything half-broken!
:::

### Implementation Tips

1. **Input Validation**:
   Remember the regex concepts from Exercise 1:
   - What character class matches specific digits?
   - How do you ensure the entire string matches?
   - Test your pattern on various cases (valid and invalid)

2. **Code Organization**:
   Look for opportunities to extract methods, similar to the log analyzer:
   - What parts of your code have distinct responsibilities?
   - Which pieces might need to change independently?
   - How can you make the code more maintainable?

3. **Testing Strategy**:
   - Use the debugger to inspect your conversion logic
   - Test edge cases thoroughly
   - Verify each part works before moving on

> 🔍 **Final Checkpoint**: Before submitting, verify that:
>
> - Your regex correctly validates base-3 numbers
> - Your conversion works for all test cases
> - Your code is well-organized and refactored
> - Your program handles errors gracefully

## Save Your Work - Exercise 5

Verify what files are uncommitted:

```bash
git status
```

Stage your changes:

```bash
git add src/main/java/jam03/BaseConverter.java
```

Commit your work:

```bash
git commit -m "jam03: Complete base-3 converter with regex and refactoring"
```

Your working directory should now be clean.

:::warning
🚧 **Check Your Working Directory**

If your working directory is not clean after committing, run `git status` and check:

1. **Modified Files**:
   - Did you forget to commit changes from earlier exercises?
   - Are there IDE configuration changes that need to be committed?

2. **Untracked Files**:
   - The course `.gitignore` is already set up to handle generated files
   - Any untracked files you see likely need to be committed or deleted.  Where did it come from?

For each file in the status output, ask yourself:

- Is this a file I modified while working on the exercises?
- Did I forget to commit something from an earlier exercise?
- Is this a new file I created that should be part of my solution?

Remember: IDE configuration files should be committed to ensure consistent project setup. If you're unsure about a file, ask rather than modifying `.gitignore` or just ignoring it yourself.
:::
