Loading...

Numbers and Calculations in Java

A Thought Before We Begin

When you buy snacks from a shop, the cashier instantly knows how to add up the prices, subtract discounts, and tell you the total. Computers can do that too, in fact, they love math! In this lesson, you’ll learn how to make Java perform simple calculations like addition and subtraction.
You’ll use it to create a Mini Café Bill Calculator, your first step toward programs that can actually “think with numbers.”

The Project: Mini Café Bill Calculator

Imagine you’re designing a small café app. The program should print the total bill for the customer after adding the prices of three items.

Here’s how your output will look:

text
8 lines
|
39/ 500 tokens
1
2
3
4
5
6
7
8
========================
   JAVA CAFÉ BILL
   Tea: 150
   Sandwich: 350
   Cookie: 100
   --------------------
   Total Bill: 600
========================

You’ll store the prices in variables and let Java calculate the total automatically.

The Concept: Arithmetic in Java

Numbers in Java can be stored in two main ways:

  • int: for whole numbers like 150 or 10
  • double: for decimal numbers like 12.5 or 7.99

Once stored, you can perform math using symbols called operators.

Here are the main ones:

  • + for addition
  • - for subtraction
  • * for multiplication
  • / for division

For example:

java
1 lines
|
5/ 500 tokens
1
int total = 5 + 3;
Code Tools

After this line, the variable total will store the number 8.
It’s that simple, you’ve just taught the computer how to count!

Why This Matters

Every app that deals with money, distance, scores, or time relies on calculations.
Your calculator app, your game score counter, even your phone’s battery meter, all use arithmetic under the hood.

By learning to calculate, your programs move from static (showing fixed text) to dynamic (doing real work). This is where your code starts to feel alive.

Let’s Build the Café Bill

Here’s the complete program:

java
18 lines
|
157/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class CafeBill {
    public static void main(String[] args) {
        int tea = 150;
        int sandwich = 350;
        int cookie = 100;

        int total = tea + sandwich + cookie;

        System.out.println("========================");
        System.out.println("   JAVA CAFÉ BILL");
        System.out.println("   Tea: " + tea);
        System.out.println("   Sandwich: " + sandwich);
        System.out.println("   Cookie: " + cookie);
        System.out.println("   --------------------");
        System.out.println("   Total Bill: " + total);
        System.out.println("========================");
    }
}
Code Tools

When you run this program, it adds the prices together and displays the total at the end.
If you change any price, the total updates automatically, no need to edit the whole receipt.

How It Works

  1. You declare three variables for each item’s price.
  2. The line int total = tea + sandwich + cookie; adds them together.
  3. Finally, you print everything on screen using System.out.println().

That’s the magic of arithmetic: small numbers working together to make something meaningful.

Learn Together with AI

If you’re using an AI Copilot, you can just tell it what you want to build.

Try this prompt:

“Write a Java program that calculates a café bill by adding the prices of three menu items and prints the total.”

The AI will write it instantly for you.
Then try experimenting:

  • Ask it to include a discount of 10%.
  • Ask it to print the final amount after tax.
  • Ask it to show decimal values (use double instead of int).

Every small change helps you understand how numbers behave in Java.

Practice Challenge

Your turn now!
Write a Java program that calculates the total cost for a bookstore purchase.
Each book has its own price, and your output should match this format exactly:

text
8 lines
|
40/ 500 tokens
1
2
3
4
5
6
7
8
========================
   BOOKSTORE RECEIPT
   Book 1: 850
   Book 2: 1250
   Book 3: 600
   --------------------
   Total Bill: 2700
========================

Use variables to store the book prices, and one more variable to calculate the total.

Example Solution

java
18 lines
|
158/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class BookstoreBill {
    public static void main(String[] args) {
        int book1 = 850;
        int book2 = 1250;
        int book3 = 600;

        int total = book1 + book2 + book3;

        System.out.println("========================");
        System.out.println("   BOOKSTORE RECEIPT");
        System.out.println("   Book 1: " + book1);
        System.out.println("   Book 2: " + book2);
        System.out.println("   Book 3: " + book3);
        System.out.println("   --------------------");
        System.out.println("   Total Bill: " + total);
        System.out.println("========================");
    }
}
Code Tools

If your output matches the one above, congratulations, your first calculator works perfectly! You’ve now taught Java how to add, store, and display results, the foundation of all logic-based programs.

Coming Up Next

Now that you can make Java calculate, it’s time to make your programs decide.
In the next lesson, you’ll learn about if–else statements, how to teach your code to think, compare, and make choices, just like a real cashier deciding if a discount should apply.