📖 Scenario: Imagine you are helping a cashier at a store. The cashier needs to add prices, subtract discounts, multiply quantities, and compare amounts quickly. Operators in programming help us do these math and comparison tasks easily.
🎯 Goal: You will create a simple Java program that uses operators to calculate the total price of items bought, apply a discount, and check if the total is above a certain amount.
📋 What You'll Learn
Create variables to hold item prices and quantities
Create a variable for discount percentage
Use arithmetic operators to calculate total price before and after discount
Use a comparison operator to check if total price after discount is greater than 100
Print the final total price and the comparison result
💡 Why This Matters
🌍 Real World
Cashiers, online stores, and billing systems use operators to calculate prices and discounts quickly.
💼 Career
Understanding operators is essential for any programming job that involves calculations, data processing, or decision making.
Progress0 / 4 steps
1
Create variables for item prices and quantities
Create two int variables called price1 and price2 with values 50 and 30 respectively. Also create two int variables called quantity1 and quantity2 with values 2 and 3 respectively.
Java
Hint
Use int to create whole number variables and assign the exact values given.
2
Create a variable for discount percentage
Add a double variable called discount and set it to 0.1 to represent a 10% discount.
Java
Hint
Use double for decimal numbers and assign 0.1 for 10%.
3
Calculate total price before and after discount
Create a double variable called totalPrice that calculates the sum of price1 * quantity1 and price2 * quantity2. Then create another double variable called finalPrice that applies the discount to totalPrice using the formula totalPrice * (1 - discount).
Java
Hint
Use * to multiply and + to add. Use parentheses to apply discount correctly.
4
Check if final price is greater than 100 and print results
Create a boolean variable called isExpensive that checks if finalPrice is greater than 100 using the > operator. Then print finalPrice and isExpensive using System.out.println.
Java
Hint
Use boolean for true/false and System.out.println to print values.
Practice
(1/5)
1. Why do we need operators in Java programming?
easy
A. To perform calculations and compare values
B. To create new classes
C. To write comments in code
D. To define the program's main method
Solution
Step 1: Understand the role of operators
Operators are symbols that perform operations like addition, subtraction, or comparison on values.
Step 2: Identify what operators enable
They allow the program to calculate results and make decisions based on comparisons.
Final Answer:
To perform calculations and compare values -> Option A
Quick Check:
Operators = calculations and comparisons [OK]
Hint: Operators do math and comparisons in code [OK]
Common Mistakes:
Thinking operators create classes
Confusing operators with comments
Believing operators define methods
2. Which of the following is the correct way to use the addition operator in Java?
easy
A. int sum = 5 plus 3;
B. int sum = 5 add 3;
C. int sum = 5 & 3;
D. int sum = 5 + 3;
Solution
Step 1: Recognize the addition operator syntax
In Java, the plus sign (+) is used to add two numbers.
Step 2: Check each option for correct syntax
int sum = 5 + 3; uses '+' correctly; others use invalid keywords or symbols.
Final Answer:
int sum = 5 + 3; -> Option D
Quick Check:
Use '+' for addition [OK]
Hint: Use '+' symbol for addition in Java [OK]
Common Mistakes:
Using words like 'plus' instead of '+'
Using bitwise '&' instead of '+'
Writing invalid keywords like 'add'
3. What will be the output of this Java code?
int a = 10;
int b = 3;
int result = a / b;
System.out.println(result);
medium
A. 3
B. 4
C. 3.3333
D. Error
Solution
Step 1: Understand integer division in Java
Dividing two integers results in an integer by dropping the decimal part.
Step 2: Calculate 10 divided by 3 as integer division
10 / 3 equals 3 with remainder 1, but remainder is discarded.
Final Answer:
3 -> Option A
Quick Check:
Integer division truncates decimals [OK]
Hint: Integer division drops decimals, so 10/3 = 3 [OK]
Common Mistakes:
Expecting decimal result from integer division
Confusing division operator with modulo
Thinking code causes error
4. Find the error in this Java code snippet:
int x = 5;
int y = 10;
if (x = y) {
System.out.println("Equal");
}
medium
A. Missing semicolon after if statement
B. Using '=' instead of '==' in if condition
C. Variables x and y are not declared
D. Print statement syntax is wrong
Solution
Step 1: Check the if condition syntax
The '=' operator assigns value; '==' compares values in conditions.
Step 2: Identify the mistake in the condition
Using '=' tries to assign y to x, which is invalid in if condition and causes error.
Final Answer:
Using '=' instead of '==' in if condition -> Option B
Quick Check:
Use '==' to compare values in if [OK]
Hint: Use '==' for comparison, '=' is assignment [OK]
Common Mistakes:
Confusing assignment '=' with comparison '=='
Thinking semicolon needed after if condition
Assuming variables are undeclared
5. You want to check if a number is between 10 and 20 (inclusive) in Java. Which operator combination correctly does this?
int num = 15;
if ( ? ) {
System.out.println("In range");
}
hard
A. num == 10 && num == 20
B. num > 10 || num < 20
C. num >= 10 && num <= 20
D. num >= 10 || num <= 20
Solution
Step 1: Understand the range condition
Number must be greater than or equal to 10 AND less than or equal to 20.
Step 2: Identify correct logical operators
'&&' means both conditions must be true; '>=' and '<=' check inclusive bounds.
Final Answer:
num >= 10 && num <= 20 -> Option C
Quick Check:
Use '&&' for both conditions true [OK]
Hint: Use '&&' to combine conditions for range checks [OK]
Common Mistakes:
Using '||' which allows out-of-range values
Checking equality to both ends simultaneously
Confusing inclusive and exclusive operators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.