Advertisement
jaVer404

level16.lesson07.task03

Sep 10th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package com.javarush.test.level16.lesson07.task03;
  2.  
  3. /* Big Ben clock
  4. 1. Разберись, что делает программа.
  5. 2. Реализуй логику метода
  6.  
  7. printTime так, чтобы каждую секунду выдавалось время начиная с установленного в конструкторе
  8.  
  9. Пример:
  10. В г. Лондон сейчас 23:59:58!
  11. В г. Лондон сейчас 23:59:59!
  12. В г. Лондон сейчас полночь!
  13. В г. Лондон сейчас 0:0:1!
  14. */
  15.  
  16. public class Solution {
  17.     public static volatile boolean isStopped = false;
  18.  
  19.     public static void main(String[] args) throws InterruptedException {
  20.         Clock clock = new Clock("Лондон", 23, 59, 57);
  21.         Thread.sleep(4000);
  22.         isStopped = true;
  23.         Thread.sleep(1000);
  24.     }
  25.  
  26.     public static class Clock extends Thread {
  27.         private String cityName;
  28.         private int hours;
  29.         private int minutes;
  30.         private int seconds;
  31.  
  32.         public Clock(String cityName, int hours, int minutes, int seconds) {
  33.             this.cityName = cityName;
  34.             this.hours = hours;
  35.             this.minutes = minutes;
  36.             this.seconds = seconds;
  37.             start();
  38.         }
  39.  
  40.         public void run() {
  41.             try {
  42.                 while (!isStopped) {
  43.                     printTime();
  44.                 }
  45.             } catch (InterruptedException e) {
  46.             }
  47.         }
  48.  
  49.         private void printTime() throws InterruptedException {
  50.             //add your code here - добавь код тут
  51.            /**/
  52.  
  53.             seconds++;
  54.             if (seconds == 60) {
  55.                 seconds = 0;
  56.                 minutes++;
  57.                 if (minutes==60) {
  58.                     minutes=0;
  59.                     hours++;
  60.                     if (hours>23) {
  61.                         hours=0;
  62.                     }
  63.                 }
  64.             }
  65.             sleep(1000);
  66.  
  67.  
  68.  
  69.             if (hours == 0 && minutes == 0 && seconds == 0) {
  70.                 System.out.println(String.format("В г. %s сейчас полночь!", cityName));
  71.             } else {
  72.                 System.out.println(String.format("В г. %s сейчас %d:%d:%d!", cityName, hours, minutes, seconds));
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement