Java Program:


import java.util.Scanner;


public class Watch 

{

    int hour,min,sec;

    void inputTime()

    {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter hour = ");

        hour=sc.nextInt();

        if(hour<0 || hour>23)

        {

            System.out.print("Invalid hour Enter hour again = ");

            hour=sc.nextInt();

        }

        System.out.print("Enter Minute = ");

        min=sc.nextInt();

        if(min<0 || min>59)

        {

            System.out.print("Invalid Minute Enter Minute again = ");

            min=sc.nextInt();

        }

        System.out.print("Enter Seconds = ");

        sec=sc.nextInt();

        if(sec<0 || sec>59)

        {

            System.out.print("Invalid Seconds Enter Seconds again = ");

            sec=sc.nextInt();

        }

    }

    void showTime12F()

    {   

        String t;

        t=(hour==0)? "Midnight": (hour>=1 && hour<=11)? "A.M." :

                (hour==12)? "Noon" : (hour>=13 && hour<=23)?"P.M." : "Invalid";

        double x;

        x = (hour==13) ? 1 : (hour==14)? 2 : (hour==15)? 3 :

                (hour==16)? 4 : (hour==17)? 5 :(hour==18)? 6 : 

                (hour==19)? 7 : (hour==20)? 8 : (hour==21)? 9 :(hour==22)? 10 : (hour==23)? 11 : 0  ;

        

        System.out.println("Time is "+x+" hours "+min+" minutes "+sec+" second "+t);

    }

    void showTime24F()

    {

        System.out.println("Time is "+hour+" hours "+min+" minutes "+sec+" second");

    }

    public static void main(String[] args)

    {

        Watch w = new Watch();

        w.inputTime();

        Scanner s = new Scanner(System.in);

        System.out.print("Enter 1 to see time in 12 Hour format\n"

                + "Enter 2 to see time in 24 Hour format\n"

                + "Enter your Option = ");

        int i=s.nextInt();

        if(i==1)

        {

           w.showTime12F();

        }

        else

        {

           w.showTime24F();

        }

    }

}




Try it Yourself