0
0
Javaprogramming~15 mins

Unary operators in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Unary Operators in Java
📖 Scenario: You are working on a simple Java program that helps a cashier calculate the number of items left after selling some. You will use unary operators to increase or decrease the count.
🎯 Goal: Build a Java program that uses unary operators to update the count of items sold and items left.
📋 What You'll Learn
Create an integer variable for the initial number of items
Create an integer variable for the number of items sold
Use unary operators to increase and decrease counts
Print the final count of items left
💡 Why This Matters
🌍 Real World
Cashiers and inventory managers often need to update counts quickly when items are sold or restocked.
💼 Career
Understanding unary operators helps in writing concise code for counters, loops, and state changes in many software jobs.
Progress0 / 4 steps
1
Create initial item count
Create an integer variable called items and set it to 10.
Java
Need a hint?

Use int to declare a whole number variable.

2
Create sold items count
Create an integer variable called sold and set it to 0.
Java
Need a hint?

Initialize sold with zero because no items are sold yet.

3
Use unary operators to update sold count
Use the unary increment operator ++ to increase sold by 1.
Java
Need a hint?

The ++ operator adds one to the variable.

4
Calculate and print items left
Use the unary decrement operator -- to decrease items by 1, then print the value of items.
Java
Need a hint?

The -- operator subtracts one from the variable. Use System.out.println(items); to print.