← Back to Book Detail

7 Indicator Methods (7/11) -- Patterns for Beginning Programmers

Browse
63%

7 Indicator Methods

7 Indicator Methods Sometimes the value needed to perform a calculation is known, and other times it too must be calculated. This is true of both “traditional” values and indicators (of the kind discussed in Chapter 6). This chapter considers problems in which an indicator’s value must be calculated before it can be used in another expression. Motivation There’s a famous saying among performers, “the show must go on”, which means that there must be a show whenever there’s an audience. In the context of Chapter 6 on indicators, this means that the number of seats sold for a particular showtime must be used to calculate an indicator, setting it to 0 when no tickets have been sold and setting it to 1 otherwise. This is an example of a threshold indicator in which the threshold is 1. So, given that some number of tickets has been sold for a particular show time, you must determine the number of shows that must be offered at that time (i.e., either 0 or 1 ). Letting shows denote the number of shows and sold denote the number of tickets sold, you want to calculate shows from sold in such a way that it takes on the value 0 when sold is 0 and it takes on the value 1 for all positive values of sold . Review You could, of course, calculate the value of the variable shows as follows: if (sold >= 1) { shows = 1; } else { shows = 0; } However, you should also know that this is a bad practice, since it is likely that you will need to use this simple algorithm in more than one place. Hence, to avoid code duplication, you should instead write a method like the following: public static int shows(int sold) { if (sold >= 1) { return 1; } else { return 0; } } and use it as needed. The Pattern The entertainment example is, obviously, a very particular problem. However, it can easily be generalized to uncover a pattern. In particular, the entertainment problem is a particular example of a general problem in which you need to determine whether a particular value exceeds a particular threshold. Further, there is an even more general problem in which you need a method that returns a value of 0 or 1 based on the value of the parameters it is passed. For threshold indicators, the solution to the problem is the following method: public static int indicator(int value, int threshold) { if (value >= threshold) { return 1; } else { return 0; } } For other indicators, the solution is a method with parameters that vary with the specifics of the problem. Examples It’s useful to consider examples of both threshold indicators and other indicators. Threshold Indicators Continuing with the entertainment example, the threshold is 1 (i.e., if the size of the audience is 1 or more then there must be a show), so given the size of the audience (represented by the variable size ), the number of shows is given by the following: shows = indicator(sold, 1); Returning to the parking ticket example used in Chapter 6, letting the number of prior tickets be given by priors , the fine can be calcul
← Previous Chapter Next Chapter →