Skip to Content
Course content

23: Practice Exercise: Building a Simple Calculator

Click on the "Edit" button in the top corner of the screen to edit your slide content.

When most people sit down to build their first calculator, they spend 90% of their time worrying about the math. They think, "How do I handle the addition? What about the multiplication?" I'll let you in on a secret: the math is the easiest part. Java has operators for that built right in. The actual challenge—and where most of your bugs will live—is the interaction loop and input validation.

Thinking the Math Logic is the Hard Part

I often see students write a "calculator" that looks like this: it asks for number A, asks for number B, asks for an operator, prints the result, and then the program immediately ends. To a beginner, this feels like a completed project. But in the real world, that's not a tool; it's a script. A real application persists. It stays open, allows for multiple calculations, and doesn't explode the moment a user accidentally hits the "m" key instead of the "5" key.

// The "Naïve" approach that crashes easily
System.out.print("Enter a number: ");
double num1 = scanner.nextDouble(); // What if the user types "hello"? 
// Crash! InputMismatchException.

If you write your code assuming the user is a perfect robot, your software will fail the second a human touches it. That's the misconception. The "calculator" part of a calculator is trivial; the "application" part is where the engineering happens.

Designing the Control Loop and Input Guardrails

Instead of focusing on the arithmetic, I want you to focus on the shell of the program. You need a loop that keeps the program alive until the user explicitly tells it to stop. I usually prefer a while(true) loop with a specific "sentinel value" (like typing 'q' to quit) to break out of it.

More importantly, you need to protect your program from crashing. Instead of just calling nextDouble() and praying, you should be thinking about how to handle invalid input. While we've touched on exceptions, this is the perfect place to apply them. If a user enters a string where a number should be, you don't want a stack trace filling the console; you want a polite message saying, "That's not a number, try again."

Handling the Edge Cases of Arithmetic

Once you have a stable loop, you can tackle the math, but don't forget the "gotchas." The most common one in a calculator is division by zero. In Java, dividing an integer by zero throws an ArithmeticException, but dividing a double by zero actually results in Infinity. Depending on how you want your calculator to behave, you'll need to decide if Infinity is an acceptable answer or if you should manually check the divisor and throw a custom warning to the user.

I recommend structuring your logic like this:

  • The Outer Loop: Keeps the app running.
  • The Input Stage: Validates that the numbers are actually numbers.
  • The Operation Stage: A switch statement or if-else block that handles the specific math based on the user's choice.
  • The Error Handler: Catches invalid inputs and keeps the loop spinning rather than letting the program die.



📋 Practical Task

Build a Robust Command-Line Arithmetic Engine

Your task is to create a calculator program that doesn't just do math, but survives user error. Implement the following requirements:

  • The Persistent Loop: The program must continue to ask for calculations until the user types 'q' as their operator to quit.
  • Input Validation: Use a try-catch block or Scanner.hasNextDouble() to ensure that if a user enters non-numeric text for a number, the program prints "Invalid input. Please enter a number" and asks for that specific number again, rather than crashing.
  • Four Basic Operations: Support addition (+), subtraction (-), multiplication (*), and division (/).
  • The Zero Guard: If the user attempts to divide by zero, do not allow the operation to proceed. Print a specific warning: "Error: Cannot divide by zero" and return the user to the main menu.
  • Clean Output: Format the results to two decimal places to keep the console tidy.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.