SHARE
TWEET

/r/DailyProgrammer Challenge 321 Talking Clock

a guest Nov 8th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * No more hiding from your alarm clock! You've decided you
  3.  * want your computer to keep you updated on the time so
  4.  * you're never late again. A talking clock takes a 24-hour
  5.  * time and translates it into words.
  6.  *
  7.  * Input
  8.  *
  9.  * An hour (0-23) followed by a colon followed by the minute (0-59).
  10.  *
  11.  * 00:00
  12.  * 01:30
  13.  * 12:05
  14.  * 14:01
  15.  * 20:29
  16.  * 21:00
  17.  *
  18.  * Output
  19.  *
  20.  * The time in words, using 12-hour format followed by am or pm.
  21.  *
  22.  * It's twelve am
  23.  * It's one thirty am
  24.  * It's twelve oh five pm
  25.  * It's two oh one pm
  26.  * It's eight twenty nine pm
  27.  * It's nine pm
  28.  */
  29.  
  30. public class Main {
  31.     public static void main(String[] args) {
  32. /*      System.out.println("Hours:");
  33.         for (int i = 0; i < 24; i++) {
  34.             System.out.printf("%s = %s %s%n", i, translateHourIntoWords(i), periodOfHour(i));
  35.         }
  36.         System.out.println();
  37.         System.out.println("Minutes:");
  38.         for (int i = 0; i < 60; i++) {
  39.             System.out.printf("%s %s%n", i, translateMinuteIntoWords(i));
  40.         }*/
  41.         String[] times = {"00:00", "01:30", "12:05", "14:01", "20:29", "21:00"};
  42.         for (String time : times) {
  43.             String timeInWords = translateTimeIntoWords(time);
  44.             System.out.println("It's " + timeInWords);
  45.         }
  46.     }
  47.    
  48.     public static String translateTimeIntoWords(String time) {
  49.         String[] arr = time.split(":");
  50.         assert arr.length == 2;
  51.         int hour = Integer.parseInt(arr[0]);
  52.         int minute = Integer.parseInt(arr[1]);
  53.         String hourInWords = translateHourIntoWords(hour);
  54.         String period = periodOfHour(hour);
  55.         String minuteInWords = translateMinuteIntoWords(minute);
  56.         if (minuteInWords != null) {
  57.             return hourInWords + " " + minuteInWords + " " + period;
  58.         } else {
  59.             return hourInWords + " " + period;
  60.         }
  61.     }
  62.    
  63.     public static String periodOfHour(int hour) {
  64.         return hour < 12 ? "am" : "pm";
  65.     }
  66.    
  67.     public static String translateHourIntoWords(String hour) {
  68.         return translateHourIntoWords(Integer.parseInt(hour));
  69.     }
  70.    
  71.     public static String translateHourIntoWords(int hour) {
  72.         switch (hour % 12) {
  73.             case 0: return "twelve";
  74.             case 1: return "one";
  75.             case 2: return "two";
  76.             case 3: return "three";
  77.             case 4: return "four";
  78.             case 5: return "five";
  79.             case 6: return "six";
  80.             case 7: return "seven";
  81.             case 8: return "eight";
  82.             case 9: return "nine";
  83.             case 10: return "ten";
  84.             case 11: return "eleven";
  85.             default: throw new RuntimeException();
  86.         }
  87.     }
  88.    
  89.     public static String translateMinuteIntoWords(String minute) {
  90.         return translateMinuteIntoWords(Integer.parseInt(minute));
  91.     }
  92.    
  93.     public static String translateMinuteIntoWords(int minute) {
  94.         switch (minute) {
  95.             case 0: return null;
  96.             case 1: return "oh one";
  97.             case 2: return "oh two";
  98.             case 3: return "oh three";
  99.             case 4: return "oh four";
  100.             case 5: return "oh five";
  101.             case 6: return "oh six";
  102.             case 7: return "oh seven";
  103.             case 8: return "oh eight";
  104.             case 9: return "oh nine";
  105.             case 10: return "ten";
  106.             case 11: return "eleven";
  107.             case 12: return "twelve";
  108.             case 13: return "thirteen";
  109.             case 14: return "fourteen";
  110.             case 15: return "fifteen";
  111.             case 16: return "sixteen";
  112.             case 17: return "seventeen";
  113.             case 18: return "eighteen";
  114.             case 19: return "nineteen";
  115.             case 20: return "twenty";
  116.             case 30: return "thirty";
  117.             case 40: return "forty";
  118.             case 50: return "fifty";
  119.             default: switch(minute % 10) {
  120.                 case 0: return translateMinuteIntoWords(minute - minute % 10);
  121.                 case 1: return translateMinuteIntoWords(minute - minute % 10) + " one";
  122.                 case 2: return translateMinuteIntoWords(minute - minute % 10) + " two";
  123.                 case 3: return translateMinuteIntoWords(minute - minute % 10) + " three";
  124.                 case 4: return translateMinuteIntoWords(minute - minute % 10) + " four";
  125.                 case 5: return translateMinuteIntoWords(minute - minute % 10) + " five";
  126.                 case 6: return translateMinuteIntoWords(minute - minute % 10) + " six";
  127.                 case 7: return translateMinuteIntoWords(minute - minute % 10) + " seven";
  128.                 case 8: return translateMinuteIntoWords(minute - minute % 10) + " eight";
  129.                 case 9: return translateMinuteIntoWords(minute - minute % 10) + " nine";
  130.                 default: throw new RuntimeException();
  131.             }
  132.         }
  133.     }
  134. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top