Computer Science, asked by Vyomsingh, 4 months ago

No spammers please
Java Challenge=>
Write a program in java to set the timer of 2 minute and show the working procesing :-

Answers

Answered by Oreki
11

\textsf{\large Program}

\texttt{public class CUI\_Timer \{ }

   \texttt{// Stops the current Thread for the value passed.}

   \texttt{static void tick(long seconds) \{}

       \texttt{try \{}

           \texttt{Thread.sleep(seconds * 1000);}

       \texttt{\} catch (InterruptedException ignored) \{ \}}

   \texttt{\}}

   \texttt{public static void main(String\[[]\] args) \{}

       \texttt{// 2 minutes or 120 seconds}

       \texttt{final int TIME\_LIMIT = 2 * 60;}

       \texttt{for (int i = TIME\_LIMIT; i > -1; i--) \{}

           \texttt{int minutes = i / 60,}

               \texttt{seconds = i \% 60;}

           \texttt{System.out.printf("Time Left - \%d:\%02d \symbol{92}r", minutes, seconds);}

           \texttt{tick(1);}

       \texttt{\}}  

       \texttt{System.out.println("\symbol{92}nTimes Up!!!");}

   \texttt{\}}  

\texttt{\}}

\textsf{\large Algorithm}

\begin{description}  \item \textendash \:\: First setting the timer limit as 2 minutes or 120 seconds.  \item \textendash \:\: Iterating from \textit{TIME\_LIMIT} (120) until 0.  \item \textendash \:\: Printing the time left in the timer and using the '\textbackslash r' (return carriage) \\ to clear the line.  \item \textendash \:\: Stopping for 1000 milliseconds or 1 second before each iteration ends.  \item \textendash \:\: Hence, creating a timer.\end{description}

Attachments:
Similar questions