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
Satya Vath
Level 31
Vijayawada
  • 14.04.2019
  • 1235views
  • 3comments

Help please

Question about the task Countdown
Java Core,  Level 6,  Lesson 7
Resolved

1. Figure out what the program does.
2. Implement logic in the printCountdown method so that every half a second the program displays an object from the variable list. The output should be in reverse order: from the index passed to Countdown down to zero.

Example:
Index 3 is passed

Example console output:
Line 2
Line 1
Line 0

Requirements:
  • The printCountdown method should run for about half a second.
  • The printCountdown method should reduce (decrement) the value of the variable countFrom.
  • The printCountdown method must display the list item with an index equal to the new value of countFrom.
  • The main method must create a Countdown object.
  • The program's output must match the example in the task conditions.
package com.codegym.task.task16.task1614; import java.util.ArrayList; import java.util.List; /* Countdown */ public class Solution { public static volatile List<String> list = new ArrayList<>(5); static { for (int i = 0; i < 5; i++) { list.add("Line " + i); } } public static void main(String[] args) throws InterruptedException { Thread t = new Thread(new Countdown(3), "Countdown"); t.start(); } public static class Countdown implements Runnable { private int countFrom; public Countdown(int countFrom) { this.countFrom = countFrom; } public void run() { try { while (countFrom > 0) { printCountdown(); } } catch (InterruptedException e) { } } public void printCountdown() throws InterruptedException { //write your code here Thread.sleep(500); while(true) { countFrom--; System.out.println(Solution.list.get(countFrom)); if(this.countFrom==0) break; } } } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
14 April 2019, 16:19solution
Your logic is a little jumbled. #1a) line 34 already places the printCountdown method in a while loop, you do not need a loop inside the method. #1b) line 34 already has a condition that breaks the loop when there are no more things to print, make sure you also remove the 'break' inside the method along with the loop it breaks. #2) you want it to print to screen every half second, which you do accomplish, however your sleep should occur after the first print, not before.
+9
Satya Vath
Level 31 , Vijayawada, India
16 April 2019, 12:38
Thanks For Your Support Gagnon
0
Mateo
Level 26 , Zagreb, Croatia
1 July 2019, 09:26
Guadalupe is CodeGyms MVP!
+4
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.