CodeBlanks Logo Icon CodeBlanks

Project design

Design the calculator class skeleton.

Project: Calculator

In this 5-lesson project you'll build a calculator. Each lesson adds one piece, and by the final lesson you'll have a complete app.

You'll use:

  • public class Main and a main method
  • static methods (add, subtract, multiply, divide)
  • if for zero checks
  • try/catch for error handling
  • A for loop for repetition

Ready? Let's start.

Class design

We'll put the calculator in a single Main class. The class holds both the calculation methods and the main method.

public class Main {
    // methods here
    public static void main(String[] args) {
        // calls here
    }
}

The calculation methods will be static, so they can be called directly as add(2, 3) without creating an object. They'll return int since they produce integer results.

Practice Write the first skeleton

Write a Main class with a main method. Print Calculator with println.

Sample solution

Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("Calculator");
    }
}

Expected output

Calculator

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does a calculator do?

  • Saves a file
  • Queries a database
  • Takes two numbers and performs an operation Correct answer
  • Declares a class

Explanation: A calculator takes two numbers and performs an operation.

2 Complete the method signature

Question
static int add(int a, int ____) { return a + b; }
Answer
static int add(int a, int b) { return a + b; }

Explanation: add(int a, int b) takes two parameters.

3 Match the project stages

Term Meaning
Class Main
Methods add, subtract
main Entry point
println Output

All lessons in Beginner