|
Java Tutorial |
MakingDecisionsThese pages make up the course notes for my Java programming course (run at Dallam, Milnthorpe, Cumbria). Hopefully they also make a useful self-learning tutorial. Variables, discussed in the previous section, are one element of programming common to pretty much every programming language, another is a means of controlling how a program executes. Controlling the program flow comes in two distinct flavours, decision making and looping. A decision is made by checking the value of something and acting on that value. A loop is a way of repeating a block of code (set of instructions) a number of times. For now we are only going to look at making decisions. Decision making is fundamental to most things we do, lets go back to making tea (lots of excuses for tea-breaks in this tutorial). Our simple recipe for tea in the first section, just assumed you would make decisions, when has the water boiled, when is the cup full, when has the tea brewed. Anyone who has had a cup of tea from a vending machine will soon realise that making these decisions is not so simple, even though we can do it without really thinking about it. To make decisions in Java we use an if statement. We can use if on its own or we can combine it with an else statement. The way we write this in Java is
if ( kettleLevel < 5 ) {
//add water to the kettle
…
}
…carry on with making tea
and using an else
if (sale == winterSale) {
discount = 0.20f
}
else {
discount = 0.10f
}
Like classes and methods the code block associated with an if and else is enclosed in braces -{}. if statements let us compare the value of things, if the comparison succeeds (technically if it is “true”) then the code inside the if block will be executed. If the comparison fails (is false) then the code inside the if block will not be executed; in this case any code inside an else block will be executed, if there is an else block. Notice the comparisons used above in the first example we use < (less than) in the second we use == (equality, two equals signs). Notice that to assign values we used one = and to test if something is equal we used two ==. Lets modify the
public class VatBill {
/**
* @param args
*/
public static void main(String[] args) {
float item1 = 4.99f; //cost of item 1
float item2 = 7.99f; //cost of item 2
float item3 = 9.99f; //cost of item 3
float vatRate = 17.5f; //VAT as a percentage
//add up the items to get the net price
float netPrice = item1 + item2 + item3;
System.out.println("The net price is " + netPrice);
// give a 10% discount on amounts more than £20.00
if (netPrice > 20) {
float discount = netPrice * 0.1f;
netPrice = netPrice - discount;
System.out.println("You have earned a 10% discount of " + discount);
}
//calculate the amount of VAT on the net price
float vatAmount = vatRate/100 * netPrice;
System.out.println("The amount of VAT is " + vatAmount);
//calculate the total price payable
float totalAmount = netPrice + vatAmount;
System.out.println("The gross amount (inc VAT) is " + totalAmount);
}
}
Try changing the item amounts so that they add up to less than 20 and more than 20, and see what happens. As you will see there is an if statement in the code, inside the if statement we calculate the discount, deduct this from the netAmount and display a message saying how much discount was given. Debugging Sometimes, even if a program compiles and runs it does not work as expected (even if you have been programming for over two decades!), but even if your program works fine, it is sometimes nice to see exactly what is going on as the program executes (runs). Many modern programming languages, Java included, have facility for 'debugging'. As mentioned before our IDE, EasyEclipse, has a built in debugger. The best way to understand what a debugger does is to have a go. First we need to tell the debugger that we want it to stop somewhere in our code, to do this double-click in the grey bar at the left of the editing window, a blue "dot" will appear to indicate the position of the breakpoint. (Breakpoints can only be placed on a line of executable code, so if a blue dot does not appear, you may be clicking on a blank line or a comment). Next Right click somewhere in the code editor and choose
![]() When you do this, the layout of the IDE will change to the debugging "perspective". When you are debugging you usually want to see different information than that displayed whilst creating and editing code. Eclipse uses the term "Perspective" to describe the different ways the IDE appears for doing certain types of task. So we have gone from the "Java" perspective to the "Debug" perspective - it is the same project and code viewed in a different way. Notice that there are now some extra tools on the tool bar, we have some buttons to control the execution of the debugger and some to switch perspective. Use the step button to step through the code and you will see the cursor move down as each line is executed. Also notice that the variables and their values are displayed in the variables window at the top right (yours may be in a different place). This work is Copyright Chris Hunter 2007, you may use it for non-commercial purposes |