CodeGym
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
Vladyslav Korol
Level 16
Coral Springs
  • 20.05.2021
  • 243views
  • 1comment

Why is last condition not satisfied

Question about the task Horse racing
Java Core,  Level 6,  Lesson 5
Under discussion

Figure out what the program does.
Implement the calculateHorsesFinished method.
It must:
1. Calculate and return the number of horses that have finished. Use the isFinished() method.
2. If a horse has not yet crossed the finish line (!IsFinished()), then:
2.1. Display "Waiting for " + horse.getName().
2.2. Wait until it finishes the race. Think about what method you need to use to do this.
2.3. Not treat such a horse as finished.

Requirements:
  • The calculateHorsesFinished method must return the number of horses that have finished.
  • The calculateHorsesFinished method must call the isFinished method on each horse in the passed list.
  • If any of the horses in the passed list has not yet finished, then the calculateHorsesFinished method should display "Waiting for " + horse.getName(). Example output for the first horse: "Waiting for Horse_01".
  • If any of the horses in the passed list has not yet finished, then the calculateHorsesFinished method must wait until it finishes. Use the correct method for waiting.
  • After the program is finished, the console must indicate that all the horses have finished. Example output for the first horse: "Horse_01 has finished the race!".
package com.codegym.task.task16.task1607; import java.util.ArrayList; import java.util.List; /* Horse racing */ public class Solution { public static void main(String[] args) throws InterruptedException { List<Horse> horses = prepareHorsesAndStart(10); while (calculateHorsesFinished(horses) != horses.size()) { } } public static int calculateHorsesFinished(List<Horse> horses) throws InterruptedException { int finishedCount = 0; for (int i = 0; i < horses.size(); i++) { if (horses.get(i).isFinished()) { finishedCount++; } else { System.out.println("Waiting for " + horses.get(i).getName()); horses.get(i).join(); } } return finishedCount; } public static List<Horse> prepareHorsesAndStart(int horseCount) { List<Horse> horses = new ArrayList<>(horseCount); String number; for (int i = 1; i < horseCount + 1; i++) { number = i < 10 ? ("0" + i) : "" + i; horses.add(new Horse("Horse_" + number)); } for (int i = 0; i < horseCount; i++) { horses.get(i).start(); } return horses; } public static class Horse extends Thread { private boolean isFinished; public Horse(String name) { super(name); } public boolean isFinished() { return isFinished; } public void run() { String s = ""; for (int i = 0; i < 1001; i++) { // Delay s += "" + i; if (i == 1000) { s = " has finished the race!"; System.out.println(getName() + s); isFinished = true; } } } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Vladyslav Korol
Level 16 , Coral Springs, United States
20 May 2021, 19:31
Added a space between lines 20 & 21 and it worked
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.