CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Baljinder Singh
Level 20
Toronto
  • 15.10.2018
  • 1119views
  • 3comments

How can we determine time here?

Question about the task Countdown at the races
Java Core,  Level 6,  Lesson 10
Under discussion

1. Figure out what the program does.
2. Implement the run method so that each second
the count is displayed, separated by spaces and starting from numSeconds down to 1, and then "Go!" (see the examples).
3. If the thread runs for 3.5 seconds or more, interrupt it using the interrupt method and within the thread display "Interrupted!"

Example for numSeconds = 4 :
"4 3 2 1 Interrupted!"

4. If the thread runs less than 3.5 seconds, it should terminate itself.
Example for numSeconds = 3 :
"3 2 1 Go!"

PS: The sleep method throws an InterruptedException.

Requirements:
  • The RacingClock class's run method must have a loop.
  • The RacingClock object must decrease the numSeconds value by one every second.
  • The main method should call Thread.sleep(3500).
  • The main method must call the interrupt method on the Clock object.
  • If numSeconds is 3, then the program should display "3 2 1 Go!".
  • If numSeconds is 4, then the program should display "4 3 2 1 Interrupted!".
package com.codegym.task.task16.task1617; /* Countdown at the races 2. Implement the run method so that each second the count is displayed, separated by spaces and starting from numSeconds down to 1, and then "Go!" (see the examples). 3. If the thread runs for 3.5 seconds or more, interrupt it using the interrupt method and within the thread display "Interrupted!" Example for numSeconds = 4 : "4 3 2 1 Interrupted!" 4. If the thread runs less than 3.5 seconds, it should terminate itself. Example for numSeconds = 3 : "3 2 1 Go!" */ public class Solution { public static volatile int numSeconds = 3; public static void main(String[] args) throws InterruptedException { RacingClock clock = new RacingClock(); //write your code here } public static class RacingClock extends Thread { public RacingClock() { start(); } public void run() { //write your code here while(numSeconds >= 1) { System.out.print(numSeconds + " "); numSeconds -= 1; } if (isInterrupted()) { System.out.print("Interrupted!"); } else { System.out.print("Go!"); } } } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Learner
Level 16 , India
15 October 2018, 06:34
The answer to your question is given in the task example itself. Example for numSeconds = 4 : "4 3 2 1 Interrupted!" So, you have to check whether numSeconds = 4 or less than 4. If 4 or more than 4 then, print interrupted. If less than 4 then print Go.
0
Baljinder Singh
Level 20 , Toronto, Canada
15 October 2018, 15:59
I did it like this and it works, is it ok?
public class Solution {
    public static volatile int numSeconds = 4;

    public static void main(String[] args) throws InterruptedException {
        RacingClock clock = new RacingClock();
        if ((float)numSeconds > 3.5) {
            clock.interrupt();
        }
        Thread.sleep(3500);
    }

    public static class RacingClock extends Thread {
        public RacingClock() {
            start();
        }

        public void run() {
            try {
                while (numSeconds >= 1) {
                    Thread.sleep(1000);
                    System.out.print(numSeconds + " ");
                    numSeconds -= 1;
                }
                System.out.print("Go!");
            } catch (InterruptedException e) {
                while (numSeconds >= 1) {
                    System.out.print(numSeconds + " ");
                    numSeconds -= 1;
                }
                System.out.print("Interrupted!");
            }
        }
    }
}
0
Learner
Level 16 , India
16 October 2018, 05:25
Your code is throwing UnhandledException. Are you sure your code works?
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.