A Calculator

The name 'computer' means something that can compute, usually refering to numerical computations. One simple application of this is demonstrated by a normal desk calculator. You can use it to make arithmetic calculations, 30*0.6, (100+3)/4, etc. You can, thus,use it to compute total expense for a project, monthly installment, total saving in a day, and so on.
It is easy to use any programming language as a rudimentary calculator. Remember the System.out.print instruction we saw earlier. We, there, gave it a string to print - a piece of text. We can put in any ex-pression to print.

Consider the problem of computing service tax on a set of items purchased from the shop. You know the rate of service tax is 2.4% (hypothetical), and the tax is computed by finding the total amount payable and taking its 2.4%. Assume we bought 5 items costing 10, 15, 20, 50 and 60 Rs. So the tax would be = (10+15+20+50+60) * 0.024. We can turn this into a Java program quite easily:

````System.out.println((10+15+20+50+60) * 0.024);`
```

Note that there is no quote marks around what is to be printed. Try the same with quotes:

````System.out.println("(10+15+20+50+60) * 0.024");`
```

Instead of the tax, you get the same ex-pression back. That is because, the quote told the system to treat the expression as a string - as a sequence of characters. It did not interpret them as an expression.

For this reason, numbers are treated differently by all languages - the difference between "2" and 2. The former is a string and the latter is a number. You can do 2+3 to add two numbers to get 5, but you will not get
5 from "2"+"3". [This summation actually produces "23", since the plus sign is interpreted as concatenation operator - more on this later].

The print instruction can take a number, apart from a string. The number can be a direct number as in: System.out.println(3), or a result of an ex-pression.

So, System.out.println((10+15+20+50+60) * 0.024), does indeed produce the tax amount.

Try different ex-pressions and see if everything works fine. You can use, + for addition, - for subtraction, * for multiplication and / for division.

It is possible that the tax calculation may need to be done multiple times in the program. It is also possible that the tax rate may change from place to place, and time to time. Modifying the program by changing the number everywhere is an error prone task, and too time consuming. Remember that all occurences of 0.024 may not be referring to service task percentage, and there is no way to distinguish?

Yes, there is a way. We can give names to these numbers, using variables. These can be visualised as named containers. Instead of the number 0.024, we can say, 'the content of service_tax container'. We need to define these containers, assign contents and use them. Since we are now dealing with numbers, we define service_tax as a number:

````float service_tax;`
```

floats are decimal pointers, those with an explicit decimal point. There is another kind of numbers - integers, what we call whole numbers. 1, 2, 20, 120, etc are integers. 1.3, 5.6, 10002.2, etc are floats. We can define another container named 'n_items' to denote number of items purchased. This would be an integer container and will be defined as:

````int n_items;`
```

int is short for integer (Java has something called 'Integer' - we will see that later on).

You assign values to a container by an assignment statement. We will need:

````service_tax = 0.024;`
```

The right side can be any number or an ex-pression which evaluates to a number. If we are used to seeing the service tax as a percentage, we can write the same as:

````service_tax = 2.4/100;`
```

Now, we can modify the print statement as:

````System.out.println((10+20+25+50+60)* service_tax);`
```

This produces the same output. Check it out. If you get any error, you may have typed the variable name incorrectly somewhere. Make sure that your definition, assignment and use has exactly the same name.

Now, if the service_tax need to be changed, you can just change the line assigning 0.024 to whatever is appropriate, and the change will be consistently applied throughout your program.

What if you want the service tax on each item individually? You can write:

``````System.out.println(10 * service_tax);
System.out.println(20 * service_tax);
System.out.println(25 * service_tax);
System.out.println(50 * service_tax);
System.out.println(60 * service_tax);```
```

So, with variables, you can re-use an item any number of times.

Use of variables is a major aspect of programming - though it appears to be a very simple concept. As we will see later on, things are not that simple as we make our requirements more and more complex. Keep your questions ready…

Exercise:

Print the annual interest on various deposit amounts, 1380, 49850, 40000, etc at an interest rate of 9%.

Print the annual interest on an amount of 14300 at various interest rates of 7.5%, 8%, 9% and 10.3%.

Which numbers are worth being assigned to variables, and why?

page revision: 0, last edited: 26 Oct 2008 08:04