




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The concepts of modularization and abstraction in programming, using the example of a digital clock. It explains how to divide the clock display problem into simpler sub-parts, implement number display objects, and use their interfaces to communicate between objects. The document also covers creating a clockdisplay class and using it in a clockdemo program.
Typology: Slides
1 / 28
This page cannot be seen from the preview
Don't miss anything!
public void setValue(int newValue)
/* Set currValue to the new value. If the new value is less than zero or over maxValue, don't set it.
*/
{ if ((newValue >= 0) && (newValue < maxValue))
currValue = newValue;
}
public int getValue()
{ return currValue; }
public String getDisplayValue() // return currValue as a string { if (currValue < 10) return "0" + currValue; //pad string with leading 0 else return "" + currValue; }
public void increment() /* Increment currValue, rolling over to zero if the maxValue is reached. */ { currValue = (currValue + 1) % maxValue; }
} // end of NumberDisplay class
public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String currTimeString; // the current time as a string
public ClockDisplay() // intialize the clock to 00: { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); setTimeString (); }
private void setTimeString()
/* store the current time as a string of the form "hours:minutes" */ { currTimeString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); }
public void minIncrement() // increment the clock by one minute; // hour increments when minutes roll over to 0 { minutes.increment(); if (minutes.getValue() == 0) // mins rolled hours.increment(); setTimeString (); } // end of minIncrement()
} // end of ClockDisplay class