Project design
- Java course
- Beginner
- Lesson 19/116
- 6 min
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 Mainand amainmethodstaticmethods (add,subtract,multiply,divide)iffor zero checkstry/catchfor error handling- A
forloop 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
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
static int add(int a, int ____) { return a + b; }
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 |