...

/

Variables, Strings, Numbers, and Operators

Variables, Strings, Numbers, and Operators

Learn Java variables, data types, and basic math by building a receipt generator that calculates totals and averages.

The project

Let’s revisit the POS systems: We’ve all been out shopping. We get to the mart, buy our stuff, get to the cash counter, put our stuff through the system, and experience the point-of-sale. There’s this machine there. There is a person behind the counter, but there is a machine that prints receipts. Someone has built a system using code for that machine and the receipt it prints.

You have been hired to create this point-of-sale system. You can see the sample receipt below:

You know how to print stuff on the screen already, but you don’t want to hardcode everything. You want a flexible system, such that if the price of an item changes, you don’t have to change the code manually at various places. The same is the case for the quantity of items.

The programming concept

The programming concept that we’ll explore today is a variable. In programming languages such as Java, a variable is a named place where information can be stored. That information might be text (String), a whole number (int), or a decimal number (double).

We will also look at how Java works with numbers. Many applications, such as a receipt-generating system, need to perform calculations like addition, subtraction, multiplication, division, and percentages. This allows the system to calculate the total bill automatically. A point-of-sale worker should not need to reach for a calculator and add everything by hand while the customer ...