The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. 
Home Create Your Own Objects Pure Functions  
Search the VIAS Library  Index  
Pure Functions
A method is considered a pure function if the result depends only on the arguments, and it has no side effects like modifying an argument or printing something. The only result of invoking a pure function is the return value. One example is after, which compares two Times and returns a boolean that indicates whether the first operand comes after the second: public static boolean after (Time time1, Time time2) {if (time1.hour > time2.hour) return true; if (time1.hour < time2.hour) return false; if (time1.minute > time2.minute) return true; if (time1.minute < time2.minute) return false; if (time1.second > time2.second) return true; return false; } What is the result of this method if the two times are equal? Does that seem like the appropriate result for this method? If you were writing the documentation for this method, would you mention that case specifically? A second example is addTime, which calculates the sum of two times. For example, if it is 9:14:30, and your breadmaker takes 3 hours and 35 minutes, you could use addTime to figure out when the bread will be done. Here is a rough draft of this method that is not quite right: public static Time addTime (Time t1, Time t2) {Time sum = new Time (); sum.hour = t1.hour + t2.hour; sum.minute = t1.minute + t2.minute; sum.second = t1.second + t2.second; return sum; } Although this method returns a Time object, it is not a constructor. You should go back and compare the syntax of a method like this with the syntax of a constructor, because it is easy to get confused. Here is an example of how to use this method. If currentTime contains the current time and breadTime contains the amount of time it takes for your breadmaker to make bread, then you could use addTime to figure out when the bread will be done. Time currentTime = new Time (9, 14, 30.0);Time breadTime = new Time (3, 35, 0.0); Time doneTime = addTime (currentTime, breadTime); printTime (doneTime); The output of this program is 12:49:30.0, which is correct. On the other hand, there are cases where the result is not correct. Can you think of one? The problem is that this method does not deal with cases where the number of seconds or minutes adds up to more than 60. In that case, we have to "carry" the extra seconds into the minutes column, or extra minutes into the hours column. Here's a second, corrected version of this method. public static Time addTime (Time t1, Time t2) {Time sum = new Time (); sum.hour = t1.hour + t2.hour; sum.minute = t1.minute + t2.minute; sum.second = t1.second + t2.second; if (sum.second >= 60.0) { sum.second = 60.0; sum.minute += 1; } if (sum.minute >= 60) { sum.minute = 60; sum.hour += 1; } return sum; } Although it's correct, it's starting to get big. Later, I will suggest an alternate approach to this problem that will be much shorter. This code demonstrates two operators we have not seen before, += and =. These operators provide a concise way to increment and decrement variables. They are similar to ++ and , except (1) they work on doubles as well as ints, and (2) the amount of the increment does not have to be 1. The statement sum.second = 60.0; is equivalent to sum.second = sum.second  60;


Home Create Your Own Objects Pure Functions 