Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 78
- public class BoundedCounter {
- private int value;
- private int upperLimit;
- public BoundedCounter(int upperLimit) {
- // write code here
- this.value=0;
- this.upperLimit=upperLimit;
- }
- public void next() {
- // write code here
- this.value++;
- if(this.value>this.upperLimit)
- this.value=0;
- }
- public String toString() {
- // write code here
- if (this.value<10)
- return "0"+this.value;
- return ""+this.value;
- }
- public int getValue() {
- // write here code that returns the value
- return this.value;
- }
- public void setValue(int value){
- if (value>0 && value<=this.upperLimit)
- this.value=value;
- }
- }
- ////////////////////////////////////////////////////////////////////
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- // write here code to ensure that BoundedCounter works as expected
- // before starting 78.3 remove the extra code and use the skeleton shown
- // in the assignment
- Scanner reader = new Scanner(System.in);
- BoundedCounter seconds = new BoundedCounter(59);
- BoundedCounter minutes = new BoundedCounter(59);
- BoundedCounter hours = new BoundedCounter(23);
- System.out.print("seconds: ");
- int s =reader.nextInt(); // read the initial value of seconds from the user
- System.out.print("minutes: ");
- int m =reader.nextInt(); // read the initial value of minutes from the user
- System.out.print("hours: ");
- int h =reader.nextInt(); // read the initial value of hours from the user
- seconds.setValue(s);
- minutes.setValue(m);
- hours.setValue(h);
- int i = 0;
- while ( i < 121 ) {
- // like in previous but seconds taken into account
- System.out.println(hours.toString()+":"+minutes.toString()+":"+seconds.toString());
- seconds.next();
- if (seconds.getValue()==0)
- minutes.next();
- if(minutes.getValue()==0 && seconds.getValue()==0)
- hours.next();
- i++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement