Decisions

Calculating different Tax Regimes The switch..case construct
Summary Practice
Exercise

Learning Outcomes

Download pdf version

Introduction

In all aspects of life we make decisions. Examples are if the PMV’s are running we shall go into town, otherwise we shall study. In programming we have to make decisions as well or to be more precise we shall tell the computer how to make decisions. In our case we shall modify the simple payroll programme we met in Chapter 1 so that it will calculate tax in two different ways depending on the value of the gross pay.

Calculating different Tax Regimes

Go to top

In Listing 1.1 tax was calculated as a flat 25% of the gross.  Usually tax calculation is not quite this simple. Most governments have different levels of taxation depending on a person’s income level.  In our case we shall assume that any gross which is K500 or less will be taxed at 25% whereas any gross that exceeds K500 will be tax at 25% of the first K500 and 33% of the remainder. Let us look below at our modified code to see how this is done.

Listing 3.1

 1

public class Untitled1

 2

{

 3

    public static void main(String[] args)

 4

    {

 5

        double hours,rate,gross,tax,nett;

 6

        hours = 40;

 7

        rate = 10;

 8

        gross = hours * rate;

 9

        if(gross<500)

 10

        {

 11

            tax = gross * .25;

 12

        }

 13

        else

 14

        {

 15

            tax = 125 + (gross - 500) * .33;

 16

        }

 17

        nett = gross - tax;

 18

        System.out.println("Hours " + hours);

 19

        System.out.println("Rate " + rate);

 20

        System.out.println("Gross " + gross);

 21

        System.out.println("Tax " + tax);

 22

        System.out.println("Nett " + nett);

 23

    }

 24

}

 

The code in Listing 3.1 is the same as that in Listing 1.1 until we get to line 9. Here we have an if construct which extends as far as line 16. After this the code is, once again, the same as that in Listing 1.1.

At line 9 the if construct begins with the keyword if. This is followed by a pair of brackets and between those is the condition that is to be tested – in this case gross<500.

In our case, since hours has a value of 40 and rate a value of 10 then gross will have a value of 400.  This will make the condition true, and therefore the body of the if is executed.  This body is between the curly brackets at lines 10 and 12 and contains only the code for calculating 25% of the gross pay.

On the other hand if the value of rate was 20 then gross would have a value of 800. In this case the condition at line 9 would be false. The body of the if would therefore be skipped and programme control would jump to line 13 for the else part of the construct.  The body of the else is between the curly brackets at line 14 and 16.  Earlier we mentioned that if the gross pay is greater than 500 then the tax is calculated at 25% of the first 500 and 33% of the remainder.  25% of 500 is 125.  To get the remainder we subtract 500 from gross and multiply the result by .33.  Adding the two of them together gives us the tax.

Notice at lines 9 and 13 that there is no semi colon following the if or else.  This is because neither of them are complete statements. A body of code is expected to follow each of them and the curly braces surrounding this body specify the limits of the constructs.  Thus the curly brackets at lines 12 and 16 denote the ends of the bodies of the if and the else.

The bodies of the if and else can have as many lines of code as are required. They can even contain their own if..else constructs. In our case the bodies only contain one line of code each. In the case where the body contains only one line of code the curly brackets are optional. For that reason lines 9 – 16 could be abbreviates as follows:

 9

        if(gross<500)

 10

            tax = gross * .25;

 11

        else

 12

            tax = 125 + (gross - 500) * .33;

In this case, when Java sees no curly brackets following the if it simply presumes that its body only contains line 10. It makes a similar assumption about the else in line 11.

The switch..case construct

Go to top

The if..else construct is fine when we have only to choose between two alternatives as above. On the other hand if we have to choose between a larger number of alternatives, the if..else construct, even though it can handle the situation, can be very clumsy.  In this situation we use the switch..case construct.

In order to explore this construct we shall extend our payroll application further so that it can handle superannuation calculation. Superannuation calculation normally involves taking a percentage of an employee’s pay and putting it into his superannuation account. In our case the percentage taken will be according to the following table:

Superannuation code

Percentage

0

0%

1

5%

2

7%

3

10%

4

20%

 

What this means is that if the superannuation code is 0 then the employee pays no superannuation, whereas if the superannuation code is 1 then the employee pays 5% of his gross pay towards the superannuation. The same applies for the other possible values of the superannuation code.

On the other hand,if the value for superannuation is any number other than those in the range 0 – 4, then calculation of the payroll ceases and a message is printed indicating that faulty data has been entered.

The modified code appears below in listing 3.2.

Listing 3.2

 1

public class Untitled1

 2

{

 3

    public static void main(String[] args)

 4

    {

 5

        double hours,rate,gross,tax,nett,superAmount;

 6

        int superCode;

 7

        hours = 40;

 8

        rate = 10;

 9

        superCode = 5;

 10

        gross = hours * rate;

 11

        if(gross<500)

 12

        {

 13

            tax = gross * .25;

 14

        }

 15

        else

 16

        {

 17

            tax = 125 + (gross - 500) * .33;

 18

        }

 19

        switch(superCode)

 20

        {

 21

        case 0:

 22

            superAmount = 0;

 23

            break;

 24

        case 1:

 25

            superAmount = gross * .05;

 26

            break;

 27

        case 2:

 28

            superAmount = gross * .07;

 29

            break;

 30

        case 3:

 31

            superAmount = gross * .1;

 32

            break;

 33

        case 4:

 34

            superAmount = gross * .2;

 35

            break;

 36

        default:

 37

            superAmount = -1;

 38

        }

 39

        if(superAmount == -1)

 40

            System.out.println("Faulty data. Calculation cancelled");

 41

        else

 42

        {

 43

            nett = gross - tax - superAmount;

 44

            System.out.println("Hours " + hours);

 45

            System.out.println("Rate " + rate);

 46

            System.out.println("Gross " + gross);

 47

            System.out.println("Tax " + tax);

 48

            System.out.println("Superannuation " + superAmount);

 49

            System.out.println("Nett " + nett);

 50

        }

 51

    }

 52

}

The first change we notice here is that two new variables have been added. At line 5 a new double variable – superAmount has been added and at line 6 an int variable – superCode has been added. There is no further change from Listing 3.1 until we get to line 19.  Here we meet the switch construct. As with all of the constructs we have met so far it has opening and closing curly brackets – the opening one at line 20 and the closing at line 38.

The code at line 19 – switch(superCode) – is equivalent to the statement check the value of the variable superCode. Check it against what?  At lines 21, 24, 27, 30 and 33 we have case 0: case 1: etc. These lines are equivalent to saying if the value of superCode is 0 or 1 or 2 etc.  Thus, if the value of superCode is 0 then the code at line 22 is executed and the programme control passes to line 23. Here the break statement causes the programme control to jump out of the switch construct to the next line after that construct. In this case the programme would jump to line 39.

Similarly if the value of superCode is 1 then the code at line 25 is executed and control passes to line 26 where the break once again causes it to jump to line 39.  The same logic applies to the other lines containing case.

At line 36 a new keyword - default is introduced. This keyword is equivalent to if the value of superCode is some other value that has not been tested above, in other words if it is not in the range 0 – 4, then execute line 37.  Notice that there is no break following line 37.  The reason is that it is the last line of the switch construct and thus programme control has no choice but to go to the next line of code which is line 39. A break could be entered after it but it makes no difference one way or the other.

One might think at this stage that the break statement at the end of each case is somewhat clunky. Why not dispense with the break completely and once programme control reaches the last line of any case construct it would simply jump out of the switch construct.  Why not indeed – in fact that is how some languages such as Visual Basic handle it. On the other hand the system that Java uses can be useful occasionally. To check that let us alter the correspondence between the superannuation code and the percentage as follows:

Superannuation code

Percentage

0

0%

1

5%

2

5%

3

5%

4

7%

5

10%

6

10%

7

20%

8

20%

9

20%

The modified code for the switch construct now appears as below.

Listing 3.3

 1

        switch(superCode)

 2

        {

 3

        case 0:

 4

            superAmount = 0;

 5

            break;

 6

        case 1:

 7

        case 2:

 8

        case 3:

 9

            superAmount = gross * .05;

 10

            break;

 11

        case 4:

 12

            superAmount = gross * .07;

 13

            break;

 14

        case 5:

 15

        case 6:

 16

            superAmount = gross * .1;

 17

            break;

 18

        case 7:

 19

        case 8:

 20

        case 9:

 21

            superAmount = gross * .2;

 22

            break;

 23

        default:

 24

            superAmount = -1;

 25

        }

 

In the case where superCode has a value of 0 then there is no difference between this and the pervious version. On the other hand if superCode has a value of 1 then control enters at line 6. Since there is no break statement the control falls through to lines 7 and 8 until actual code is reached at line 9. The break at line 10 causes control to jump to the next line after the switch construct. If superCode has a value of 2 then control enters at 7 and falls through to line 10 as before. If it has a value of three control enters at line 8 and again falls through to line 10. A similar path would be followed for other values of superCode.

Summary

Go to top

If we wish to make a choice between two different pathways, depending on the value of a condition then we use the if..else construct.  The body of the if is executed if the condition is true, otherwise the body of the else is executed. The bodies of both can be as small as one line of code or as large as we wish.

If we wish to test a variable and take a number of different paths depending on its value then we use the switch..case statement.

Practice

Go to top

Copy listing 3.1 into your computer and compile and run it. Next change the value of hours or rate or both. For each change that you make, calculate the values of gross, tax and nett manually. Then run the programme and check that the output is the same as what you expected.

Make about 5 or 6 changes. Ensure that some of the changes give a gross that is less than 500 while others give a gross that is greater. Try values that give a gross of 500 exactly.

Next copy Listing 3.2 and try it out for different values like you did for the previous one.  With Listing 3.2 concentrate more on altering the value of superCode.


Exercises

Go to top

Exercise 3.1

1)      Describe the structure of the if..else construct.

2)      In Listing 3.1 state what lines would be executed and what lines would be skipped with the current values of the variables hours and rate.

3)      In the same listing if the value of rate was to change to 20, again state what lines would be executed and what lines would be skipped.

4)      Describe the structure of the switch..case construct.

5)      In the switch..case construct what is the use of the keyword break?

6)      In what situations would you use the switch..case construct instead of the if..else construct?

7)      In Listing 3.3 what lines would be executed and what lines would be skipped if the value of superCode was 1.

8)      Repeat the above for a value of 2 for superCode.

Exercise 3.2

Modify Exercise 1.2 so that if the amount sold is greater than 10 then a discount of 5% is given to the purchaser.  To do this you must add two more variables to your programme, one for price before discount and the second to hold the discount itself.  This is the variable that the programme will first put value into.  The programme will then put value into the variable for amount sold.

Now an if..else construct will check the value of amount sold.  If this value is more than 10 then discount is calculated as 5% of the price before discount, otherwise the discount is set to zero.

Price of a single lawnmower is now calculated as price before discount less the discount.

There is no change to the calculation of GST, price including GST and total charged to customer – so that part of the code need not be changed.

Modify the output of your programme so that it will also show how much of a discount was given for each lawnmower.

Input Data

Expected output

Actual output

Price of

L’mower

Amount

Sold

Discount

Price after

discount

GST

Price

inc GST

Customer

Total

Discount

Price after

discount

GST

Price

inc GST

Customer

Total

400

6

0

400

40

440

2640

 

 

 

 

 

400

12

20

380

38

418

5016

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Use the above table as an example of testing your modified programme

Exercise 3.3

Modify Exercise 3.2 as follows: instead of the user entering the price of a single lawnmower a category number is entered instead. There is a correspondence between the category number and the price as follows

Category Number

Price

1

100

2

250

3

400

4

600

5

750

6

800

You must add a new variable to your programme to hold the category number.  The programme now will place values in the category number and the amount sold.  The programme now uses a switch..case construct to calculate the price of a lawnmover from the category number.

The discount, price after discount, gst, price including GST and customer total are calculated as before.

Input Data

Expected output

Actual output

Category

Amount

Sold

Price of

L’mower

Discount

Price after

discount

GST

Price

inc GST

Customer

Total

Price of

L’mower

Discount

Price after

discount

GST

Price

inc GST

Customer

Total

1

12

100

5

95

9.50

104.50

 

 

 

 

 

 

 

2

9

250

0

250

25

275

 

 

 

 

 

 

 

3

20

400

20

380

38

418

 

 

 

 

 

 

 

4

16

600

30

570

57

627

 

 

 

 

 

 

 

5

3

750

0

750

75

825

 

 

 

 

 

 

 

6

5

800

0

800

80

880

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Above is some test data to check if your modifications to the programme are correct.  Enter some more test data yourself and test the programme further.