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
hidden #10625598
Level 23
  • 26.07.2020
  • 316views
  • 2comments

what should i display here if not this? line 39

Question about the task Hippodrome (part 15)
Java Multithreading,  Level 1,  Lesson 16
Under discussion


Add code to announce the winner.
We'll make two methods in the Hippodrome class:
public Horse getWinner() and public void printWinner()

The getWinner method must return the horse that has run the farthest.
The printWinner method displays the winner's name as follows:
The winner is <name>!

Example:

The winner is Lucky!

Requirements:
  • A getWinner method with no parameters must be created in the Hippodrome class.
  • A printWinner method with no parameters must be created in the Hippodrome class.
  • The getWinner method must return the horse that has run the farthest.
  • The printWinner method should display the winner's name on the screen in the format specified in the task conditions.
package com.codegym.task.task21.task2113; import java.util.*; public class Hippodrome { private List<Horse> horses; private Horse horse; public static Hippodrome game; public Hippodrome(){ } public static void main(String[] args) throws InterruptedException{ List<Horse> horse = new ArrayList<>(); horse.add(new Horse("one", 3, 0)); horse.add(new Horse("two", 3, 0)); horse.add(new Horse("tre", 3, 0)); game = new Hippodrome(horse); game.run(); } public Hippodrome(List<Horse> horses){ this.horses = horses; } public Horse getWinner(){ Horse winner = null; double distance = 0; for(Horse h : horses){ if(h.getDistance() > distance){ winner = h; distance = h.getDistance(); } } return winner; } public void printWinner(){ System.out.print("The winner is " + getWinner() + "!"); } public List getHorses(){ List list = horses; return list; } public void run() throws InterruptedException { for(int i = 0; i < 100; i++){ move(); print(); Thread.sleep(200); } } public void move(){ for(Horse h : horses){ h.move(); } } public void print(){ for(Horse h : horses){ h.print(); } for(int i = 0; i < 10; i++){ System.out.println(); } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Nouser
Level 36 , Germany
26 July 2020, 16:16
getWinner() returns a Horse object.
0
hidden #10625598
Level 23
26 July 2020, 16:28
oh, thanks :)
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.