Java Tutorial

Java Project: Calculator

A four-function calculator with methods per operator and a check for divide-by-zero.

What you will build

This project is a four-function calculator: add, subtract, multiply, and divide. Each operator lives in its own method. Division checks the divisor before it runs. The examples use hardcoded numbers so they compile at once. Click Try it in Java to open /java/try.

One method per operator

Each operator takes two double values and returns a double. Using double keeps 10 divided by 4 as 2.5, not 2.

Example

public class Main {
  static double add(double a, double b) { return a + b; }
  static double subtract(double a, double b) { return a - b; }

  public static void main(String[] args) {
    System.out.println(add(10.0, 4.0));
    System.out.println(subtract(10.0, 4.0));
  }
}

Guard divide-by-zero

Return a boolean for success and print a message when the divisor is zero.

Example

public class Main {
  static boolean divide(double a, double b, double[] out) {
    if (b == 0.0) return false;
    out[0] = a / b;
    return true;
  }

  public static void main(String[] args) {
    double[] q = {0};
    if (divide(10.0, 4.0, q)) System.out.println("10 / 4 = " + q[0]);
    else System.out.println("Cannot divide by zero");
    if (divide(10.0, 0.0, q)) System.out.println("10 / 0 = " + q[0]);
    else System.out.println("Cannot divide by zero");
  }
}

Complete calculator

Pick an operator with a char and an if / else if chain. Unknown operators get their own message.

Example

public class Main {
  static double add(double a, double b) { return a + b; }
  static double subtract(double a, double b) { return a - b; }
  static double multiply(double a, double b) { return a * b; }

  static void apply(double a, char op, double b) {
    if (op == '+') System.out.println(a + " + " + b + " = " + add(a, b));
    else if (op == '-') System.out.println(a + " - " + b + " = " + subtract(a, b));
    else if (op == '*') System.out.println(a + " * " + b + " = " + multiply(a, b));
    else if (op == '/') {
      if (b == 0) System.out.println("Cannot divide by zero");
      else System.out.println(a + " / " + b + " = " + (a / b));
    } else System.out.println("Unknown operator");
  }

  public static void main(String[] args) {
    apply(10, '+', 4);
    apply(10, '-', 4);
    apply(10, '*', 4);
    apply(10, '/', 4);
    apply(10, '/', 0);
  }
}

Practice

  1. Compile the complete program at /java/try.
  2. Change one input value and compile again.
  3. Replace a hardcoded number with Scanner when you want typed input.

FAQ: Java Project: Calculator

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java calculator project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java calculator project in this Java Java lesson (Java Project: Calculator).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.