Suppose that you develop a big program around an Attraction
class
definition that includes getters and setters for the minutes
instance
variable, as well as for an imaginary hours
instance variable.
Next, suppose that you discover that your program accesses to hours
more often than to minutes
. If speed is a great concern, you should
arrange to store a number representing hours, rather than a number
representing minutes, to reduce the number of multiplications and
divisions performed.
If you work with the instance variables in attractions using constructors, getters, and setters only, you need to change what happens in only the constructor, getter, and setter instance methods:
public class Attraction { // First, define instance variable: public double hours; // Define zero-parameter constructor: public Attraction () {hours = 1.25;} // Define one-parameter constructor, presumed to take minutes: public Attraction (double m) {hours = m / 60.0;} // Define getters: public int getMinutes () { return (int)(hours * 60);} public double getHours () { return hours; } // Define setters: public void setMinutes (int m) { hours = m / 60.0; } public void setHours (double h) { hours = h; } }