MOOC JAVA言語プログラム設計進級第二ユニット作業

7830 ワード

簡単だが問題を見極めなければならない.
 1 public class Main {

 2 

 3     public static void main(String[] args) {

 4         

 5         java.util.Scanner in = new java.util.Scanner(System.in);

 6         Clock clock = new Clock(in.nextInt(), in.nextInt(), in.nextInt());

 7         clock.tick();

 8         System.out.println(clock);

 9         in.close();

10         

11     }

12 

13 }

14 class Clock{

15     private Display hour=new Display(24);

16     private Display minute=new Display(60);

17     private Display second=new Display(60);    

18     public Clock(int h,int m,int s){

19         

20         hour.setvalue(h);

21         minute.setvalue(m);

22         second.setvalue(s);

23     }

24     public void tick()

25     {

26         second.increase();    

27         if(second.getvalue()==0)

28         {    

29             minute.increase();

30             if(minute.getvalue()==0)

31                 hour.increase();

32         }                    

33     }

34     public String toString()

35     {

36         return String.format("%02d:%02d:%02d",hour.getvalue(),minute.getvalue(),second.getvalue());

37     }

38 }

39 class Display{

40     private int limit=0;

41     private int value=0;

42     public Display(int limit){

43         this.limit=limit;        

44     }

45     public void setvalue(int value)

46     {

47         this.value=value;

48     }

49     public int getvalue ()

50     {

51         return value;

52     }

53     public int getlimit ()

54     {

55         return limit;

56     }

57     public void increase()

58     {

59         value++;

60         if(value>=limit)

61             value=0;

62     }

63 }