Mini Project: Console Calculator
Build a small calculator function using switch without console input. Handle +, -, *, /, validate division by zero, and design clean tests.
Mini Project: Console Calculator is a free Java Academy lesson on CoddyKit — 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, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
defaultfor unknown operators - Return a result from each case
Zero Division
Division by zero
- Check
b == 0whenop == '/' - 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.
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, and the Java Academy course includes 3 lessons in total. 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.
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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
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
- Comparison & Logical Operators Deep Dive
- Switch Statements
- Mini Project: Console Calculator