/* MINUTES and SECONDS PART II This program is a modification of the program that adds minutes and seconds. The math involved in solving the basic problem is the same. However, this program has a much more powerful, object-oriented structure. The way the information is stored (how many minutes and seconds), and the way the operation of adding minutes and seconds is invoked, is like that of the bank account and footballteam programs I showed you earlier. You should try to compare this program with the first minutes-and-seconds program I gave you. What are the problems with the first program? It is much more restrictive. Specifically, once a pair of time values have been added, the result is printed and forgotten. It's not possible to keep adding multiple times together. Also, the program is less flexible: it prints the result of the added time values each time. Maybe we don't always want to see the result printed. Maybe we want the "timekeeper" to simply update itself internally. Finally, it's not possible with the first program to keep track of multiple time values. The new program below organizes the information pertaining to "time keeper" objects inside a class. Each timekeeper object keeps track of a pair of values: the current time in minutes and seconds. It has the ability (method) to add a specified amount of time to the current time, as well as the ability to advance or reverse the time by individual seconds. Printing of the time values is only effected when the printtime() method is called. And it's possible to have multiple time keepers that keeps track of different times. In short, the new program can be used much more flexibly than the first - it can serve multiple purposes in different settings; it can be incorporated into other programs. It has the advantages of object-oriented programming. Before talking about the details of how the class is defined, it's necessary to be clear as the interface (set of methods) it will have: void reset() : resets the current minutes and seconds to zero void addtime(int min2, int sec2) : adds min2, sec2 to current time inside object void settime(int min1, int sec1) : sets current time to sepcified values. void tick() : advances current time by one second void revTick() : rewinds current time by one second (and rings alarm when time gets down to zero) void printtime() : prints current time value with System.out.print As for the details of how the class is defined, most of the variables used in the first program is still here, except now they're declared in very different places. In the original program, there was min1 and sec1. Now these variables are called "minutes" and "seconds" and declared not in the "main" method, but as part of the internal information encapsulated by timekeeper objects - they are defined directly inside the class. The variables min2 and sec2 of the original program are also present, but now they are declared as parameters to the addtime method. That is, they are now extra information you have to provide when you call a timekeeper's addtime method. The other two time values, minutes and seconds, are already inside the objects. The totalsecs variable used in the original program, is also present, and here it's used and declared in the same way: as a temporary variable holding an intermediate result. It is thus declared inside the function addtime. Finally, the variables msum and ssum are nolonger present. Why? Because our new program doesn't just print the added time value, it actually updates an existing time value. When addtime is called, the sum of the two time values become the new values of minutes and seconds. In short, variables can be declared in three places: 1. Inside the body of a method (totalsecs) 2. As parameters when defining a method (min2, sec2) 3. As internal variables when defining a class (minutes, seconds). This program illustrates all three kinds of variable declarations and their uses. This program does not contain any methods that return values - I've decided to leave those out for the time being for simplicity's sake. */ // class for objects that keeps track of time: class timekeeper { // encapsulated data: private int minutes; private int seconds; // methods on timekeeper: // reset timekeeper's time to zero: public void reset() { minutes = 0; seconds = 0; } // addtime specified number of minutes and seconds to current time: public void addtime(int min2, int sec2) { int totalsecs; // holds intermediate value totalsecs = (minutes*60)+(min2*60)+seconds+sec2; minutes = totalsecs / 60; seconds = totalsecs % 60; } // sets the current time kept to provided values: public void settime(int m0, int s0) { minutes = m0; seconds = s0; } // increment time value by one second. public void tick() { int totalsecs; // intermediate value; totalsecs = (minutes*60) + seconds + 1; minutes = totalsecs / 60; seconds = totalsecs % 60; } // downard tick public void revTick() { int totalsecs; // intermediate value; totalsecs = (minutes*60) + seconds - 1; minutes = totalsecs / 60; seconds = totalsecs % 60; // ring alarm when timer gets down to zero: if (totalsecs == 0) System.out.println(" RING! "+(char)7); } // prints information public void printtime() { System.out.print("The time is "+ minutes+ " minutes"); System.out.println(" and "+ seconds +" seconds"); } } public class sep15 { public static void main(String[] args) { // define 2 timekeeper objects timekeeper timer1 = new timekeeper(); timekeeper timer2 = new timekeeper(); timer1.settime(3,50); // set first timer to 3 min, 50 secs. timer2.settime(0,3); // timer 2 set to 0 min, 3 seconds timer1.addtime(2,35); timer1.tick(); timer1.printtime(); timer2.revTick(); timer2.revTick(); timer2.revTick(); } }