Java Academy · Lesson

Mini Project: Console Calculator

Build a small calculator function using switch without console input. Handle +, -, *, /, validate division by zero, and design clean tests.

Lesson 3 of 38 steps

Mini Project: Console Calculator is a free Java Academy lesson on CoddyKit. This is lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 3 lessons in total.

Project Brief

Goal: Implement a reusable calculator method without console input. The method takes two numbers and an operator, returns the result, and validates division by zero.

API Design

API

static double calc(double a, double b, char op)
  • op: '+', '-', '*'/'x', '/'
  • Invalid operator: throw IllegalArgumentException
  • No user input inside the method

Switch Logic

Switch logic

  • Group multiply labels: *, x, X
  • Use default for unknown operators
  • Return a result from each case

Zero Division

Division by zero

  • Check b == 0 when op == '/'
  • Throw IllegalArgumentException("Division by zero")
  • Do not rely on IEEE Infinity for beginners

Testing Plan

Testing

  • Happy paths: calc(7, 8, '+'), calc(9, 3, '/')
  • Edge: calc(5, 0, '/') should throw
  • Invalid op: calc(1, 2, '?') should throw

Calculator Demo

Run it: The demo calls calc directly with sample values and prints results. No console input required.

public class Main {
  static double calc(double a, double b, char op) {
    switch (op) {
      case '+':
        return a + b;
      case '-':
        return a - b;
      case '*':
      case 'x':
      case 'X':
        return a * b;
      case '/':
        if (b == 0) throw new IllegalArgumentException("Division by zero");
        return a / b;
      default:
        throw new IllegalArgumentException("Unknown operator: " + op);
    }
  }

  public static void main(String[] args) {
    System.out.println("7 + 8 = " + calc(7, 8, '+'));
    System.out.println("10 / 2 = " + calc(10, 2, '/'));

    try {
      System.out.println("10 / 0 = " + calc(10, 0, '/'));
    } catch (IllegalArgumentException e) {
      System.out.println("Error: " + e.getMessage());
    }

    System.out.println("6 * 3 = " + calc(6, 3, '*'));
    System.out.println("6 x 3 = " + calc(6, 3, 'x'));
  }
}

Zero Division Policy

Quick check: Given calc(10, 0, '/' ), what should happen?

Recap & Next

Recap: You designed a clean API, implemented a switch-based calculator, validated division by zero, and tested without any console input.

Next: Move on to loops for repeated calculations and patterns.

Free to start

Learn Java with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
104
Lessons
374

Frequently Asked Questions

Is the “Mini Project: Console Calculator” lesson free?

Yes — the full text of “Mini Project: Console Calculator” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 3 lessons in total.

What will I learn in “Mini Project: Console Calculator”?

Build a small calculator function using switch without console input. Handle +, -, *, /, validate division by zero, and design clean tests. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “Mini Project: Console Calculator” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Comparison & Logical Operators Deep Dive
  2. Switch Statements
  3. Mini Project: Console Calculator
← Back to Java Academy