BlueJ: Time in words

/** * The following class time displays the time in words. (eg 5:45 will be displayed as Quater to Six) * @author (Gulzar Ahmed)  */


SOURCE CODE WRITTEN IN BLUEJ


import java.io.*;
class time
{
static int hour,minute;
time()//default constructor
{
hour=minute=0;
}
String arr_hour[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","One"};
String arr_na[]={"","Quater","Half","Quater"};
String teens[]={"","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Ninety"};
String end_with_zero[]={"","Ten","Twenty","Thirty","Fourty","Fifty"};
void accept()throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);     
System.out.print("HOUR :");
hour=Integer.parseInt(br.readLine());
System.out.print("MINUTES :");
minute=Integer.parseInt(br.readLine());   
 }
void display()throws IOException
{
System.out.println("Time : "+hour+":"+minute);
        if(minute==0)//Special Case
        System.out.println(arr_hour[hour]+" O'clock");
        if(minute>30)
        System.out.println(convert(60-minute)+" to "+arr_hour[hour+1]);
        else
        System.out.println(convert(minute)+" past "+arr_hour[hour]);
       }
String convert(int m)
    {
       if(m%15==0)
           return arr_na[m/15];
           else
           {
             if(m==1||m==0)
             return arr_hour[m]+" minute";
             if(m>1&&m<=9)
             return arr_hour[m]+" minutes";
             if(m%10==0)
             return end_with_zero[m/10]+" minutes";
             if(m>=11&&m<=19)
             return teens[m%10]+" minutes";
             if(m%10!=0)
             return end_with_zero[m/10]+" "+arr_hour[m%10]+" minutes";
            }
            
        return "Fatal ERROR";   
    }
public static void main()throws IOException
{
time ob =new time();
ob.accept();
if(minute>=60||minute<0||hour>12||hour<1)
System.out.println("Wrong INPUT");
else
ob.display();
}
}

OUTPUT:


No comments:

Post a Comment