


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Quiz – next Wednesday. Material in Chapters 1 and 2. Review Labs
Look at insertMoney method – there is a new statement here. if ( amount > 0 ) { balance = balance + amount; } else { System.out.println(“Use a positive amount”); } What does this set of statements do? It compares the value of amount with 0, and executes one of two sets of statements, based on whether the result of the comparison was true or false. Important – only one of the sets will ever be executed – never both. In this case, if amount > 0 then the balance is modified. Otherwise (if amount <= 0 ) an error message is printed. This So, this statement makes sure that the user has passed a reasonable value to the method before modifying the balance in the machine. This statement is called a conditional statement or an if statement. General form: if ( test that evaluates to true or false ) { Statements to execute when expression is true } else { Statements to execute when expression is false } The test in a conditional statement is an example of a boolean expression. Boolean expressions can have only one of two values: true, false. In the above example, either amount was greater than 0 (true) or it was not (false). A conditional statement uses these two values to make a choice about what statements to execute. The statements in each part are enclosed in a block – depending upon the value of the expression, either every statement in the block will be executed, or none will. boolean is also a type – in one of the earlier projects we saw a boolean instance variable – does anyone remember what it was? (isVisible in shapes project). Did it make sense to use a boolean for that instance variable?
We want to return the balance, but also change its value to 0. We need to save the balance in a temporary location, set the balance to 0, and then return the value we put in the temporary place. Look at the refundBalance method. It creates a new variable (amountToRefund) that it uses to store the balance, then sets the balance to 0, then returns the amountToRefund. What kind of variable is amountToRefund?