CodeGym
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Zach
Level 22
Fort Collins
  • 16.06.2021
  • 252views
  • 1comment

Can't get the first task to pass?

Question about the task Big Ben
Java Core,  Level 6,  Lesson 7
Under discussion

1. Figure out what the program does.
2. Implement the printTime method so that the time is given every second, beginning with the time specified in the constructor.

Example:
In London, the time is now 23:59:58!
In London, the time is now 23:59:59!
It's currently midnight in London!
In London, the time is now 00:00:01!

Requirements:
  • The printTime method should run for about a second.
  • The printTime method should increase (increment) the number of seconds stored in the variable seconds.
  • After incrementing the time, the second count cannot be greater than 59. The number of minutes should increase.
  • After incrementing the time, the minute count cannot be greater than 59. The number of hours should increase.
  • After incrementing the time, the hour count cannot be greater than 23.
package com.codegym.task.task16.task1613; /* Big Ben */ public class Solution { public static volatile boolean isStopped = false; public static void main(String[] args) throws InterruptedException { Clock clock = new Clock("London", 23, 59, 57); Thread.sleep(4000); isStopped = true; Thread.sleep(1000); isStopped = false; } public static class Clock extends Thread { private String cityName; private int hours; private int minutes; private int seconds; public Clock(String cityName, int hours, int minutes, int seconds) { this.cityName = cityName; this.hours = hours; this.minutes = minutes; this.seconds = seconds; start(); } public void run() { try { while (!isStopped) { printTime(); } } catch (InterruptedException e) { } } private void printTime() throws InterruptedException { //write your code here // increment seconds seconds++; // create conditionals for hours, minutes and seconds if (seconds == 60) { minutes++; seconds = 0; if (minutes == 60) { hours++; minutes = 0; if (hours == 24) { hours = 0; } } } // else if (seconds == 1) { // isStopped = true; // } if (hours == 0 && minutes == 0 && seconds == 0) { System.out.println(String.format("It's currently midnight in %s!", cityName)); } else { System.out.println(String.format("In %s, the time is now %02d:%02d:%02d!", cityName, hours, minutes, seconds)); } } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Thomas
Level 31 , Bayreuth, Germany
17 June 2021, 05:32
Thread.sleep(1000); // ??
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.