Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2003 SOLVED QUESTION 2 : TIME

Monday, March 18, 2019

ISC COMPUTER SCIENCE PRACTICAL 2003 SOLVED QUESTION 2 : TIME


Given a time in numbers we can convert it into words. For example:- 5:00 five o’clock 5:10 ten minutes past five 5:15 quarter past five 5:30 half past five 5:40 twenty minutes to six 5:45 quarter to six 5:47 thirteen minutes to six Write a program which first inputs two integers, the first between 1 and 12 (both inclusive) and second between 0 and 59(both inclusive) and then prints out the time they represent, in words. Your program should follow the format of the example below:-

 SAMPLE  DATA :
INPUT :  
TIME  : 3:0 
OUTPUT : 3:00  Three O’clock    
INPUT :  
TIME  : 7:29  
OUTPUT : 7:29  Twenty Nine Minutes Past Seven   
INPUT :    TIME  : 6:34  
OUTPUT : 6:34  Twenty Six  Minutes To Seven     
Test your program for the data values given in the example above and some random data.
Just go through the code, you will get the logic!


import java.util.*;
public class ISC2003Q2 {
    public static void main(String[] args)
        {
        int hr,min;
        int choice;
        Scanner in = new Scanner(System.in);
        do {
              do {
                     System.out.println("Enter the hour");
                     hr = in.nextInt();
                     if(hr <1 || hr >12)
                     System.out.println("INVALID INPUT, TRY AGAIN");
                     }while(hr <1 || hr >12);
              do {
                     System.out.println("Enter the minute");
                     min = in.nextInt();
                     if(min <0 || min >59)
                     System.out.println("INVALID INPUT, TRY AGAIN");
                     }while(min <0 || min >59);
                     Time obj = new Time(hr,min);
                     obj.printTime();
                     System.out.println("Want to enter another value? press 1 to continue 0 to exit:");
                     choice=in.nextInt();
              }while(choice==1);
        in.close();
        }
       }
class Time {
       public void printTime() {
       System.out.println("OUTPUT:");
       if(minute < 10)
              System.out.println("TIME: "+hour+":0"+minute);
       else
              System.out.println("TIME: "+hour+":"+minute);
       if(minute==0)
              System.out.println(returnHour(hour,minute)+" O'Clock");
       else
       System.out.println(returnMinute(minute)+" "+returnHour(hour,minute));
       }
       private String returnHour(int hr,int min) {
              String s;
              if(min<=30)
                     s=" "+Hour[hr-1];
              else {
                     if(hr==12)
                           s=" One";
                     else
                           s=""+Hour[hr];
                     }
              return s;    
       }
       private String returnMinute(int min) {
              String s="";
              if(min<=30)   {
                     if(min==15)
                           s=Min[0];
                     else if(min==30)
                           s=Min[1];
                     else if(min<=20)
                           s= min<2 ? Min_past[min-1]+" Minute Past":Min_past[min-1]+" Minutes Past";
                     else if(min >20)
                           s="Twenty "+Min_past[min%10-1]+" Minutes Past";
              }
              else {
                     int tmp = 60-min;
                     if(min==45)
                           s=Min[2];
                     else if(min >= 40)
                           s= tmp<2 ? Min_past[tmp-1]+" Minute To":Min_past[tmp-1]+" Minutes To";
                     else if(min <40)
                           s="Twenty "+Min_past[tmp%10-1]+" Minutes To";
              }
              return s;
       }
       Time(int hr, int min){
              hour=hr;
              minute=min;
       }
       private int hour;
       private int minute;
       private String[] Min_past = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen","Twenty"};
       private String[] Min= {"Quarter past","Half past","Quarter to"};
       private String[] Hour= {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve"};
}

No comments:

Post a Comment