Problem overview
This project prints a tiny receipt for one item. The starter code gives you item ("Coffee Mug"), price (12) and quantity (3). The program computes the total and prints two lines: the item with its quantity, then the total.
Coffee Mug x3
Total: $36
The lesson practises three basics together: variables, arithmetic and joining strings. The two lines must be joined with a newline into one string called result, which is printed with a single System.out.println(result).
Approach: numbers first, then text
First compute the number, total = price * quantity, with int arithmetic. Then build each line as its own String, then join the lines, and print once. Keeping the calculation apart from the formatting makes each variable do one job, and a mistake is easy to find because you can print any piece on its own.
Step by step
- Create
totalasprice * quantity. It is the cost of all the mugs. - Create
line1: the item, then the text" x"(with its leading space), then the quantity. Java's+converts theintto text when the other side is aString. - Create
line2: the text"Total: $"followed bytotal. - Create
resultfromline1,"\n"andline2. The newline character starts a new row where it sits. - Print
resultwithSystem.out.println.
The solution in Java
String item = "Coffee Mug";
int price = 12;
int quantity = 3;
int total = price * quantity;
String line1 = item + " x" + quantity;
String line2 = "Total: $" + total;
String result = line1 + "\n" + line2;
System.out.println(result);
The newline is a single character written as the two symbols \n inside the quotes. The second row starts after it, so println displays two rows even though it was called once.
Edge cases and common mistakes
Operator order with +
The + operator works from left to right, and it adds numbers or joins text depending on what is on its sides. With the starter values:
"Total: $" + price * quantityprintsTotal: $36, because*is evaluated before+."Total: $" + price + quantityprintsTotal: $123: after the text, each+joins, so 12 and 3 are glued side by side."Total: $" + (price + quantity)printsTotal: $15, because the parentheses add first.
Computing total in its own variable, as the lesson does, avoids these surprises.
Missing spaces
The first row is Coffee Mug x3, so the text piece is " x" with a space before the x and none after it. The tests compare the text exactly, so a missing space fails.
Integer arithmetic
Because price and quantity are int values, the total is a whole number. This is fine for whole-dollar prices. Prices with cents would need another representation, which this lesson does not need.
Testing
Change the three starter values and check both lines. The rows below come from the same code:
item, price, quantity | Output |
|---|---|
| "Notebook", 4, 10 | Notebook x10 then Total: $40 |
| "Pen", 1, 1 | Pen x1 then Total: $1 |
| "Lamp", 25, 0 | Lamp x0 then Total: $0 |
The quantity appears twice in the program's output, once in the first row and once inside the total. Both come from the same variable, so a single change to quantity updates both rows. A quantity of 0 is accepted without any special handling: the total is 0.
Key takeaways
- Compute numbers in
intvariables, then build the text. "\n"inside a string starts a new row.- Put arithmetic in its own variable, or in parentheses, before joining it to text.
