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
Michal
Level 26
Lodz
  • 28.05.2020
  • 347views
  • 2comments

What is the problem with my Hippodrome class?

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


Now let's move on to the Hippodrome class and the main method.
We need to create a Hippodrome object and add some horses to it.

To start, in the Hippodrome class:
Create a static Hippodrome game field.

The main method requires the following:
a) Create a Hippodrome object and save it in the game field.
b) Create three Horse objects. Make up their names yourself. Each horse's initial speed is 3, and its distance is 0.
c) Add the created horses to the hippodrome's list of horses (horses). You can use the getHorses method to get the list of horses.

Requirements:
  • A Hippodrome game field must be created in the Hippodrome class.
  • The game field must be static.
  • The game field must NOT be private.
  • The game field must be initialized in the main method.
  • Three horses must be added to the horse list in the main method.
  • The speed of all horses must be 3, and their distance must be 0.
package com.codegym.task.task21.task2113; import java.util.ArrayList; import java.util.List; public class Hippodrome { private static List<Horse> horses; public static Hippodrome game; public static List<Horse> getHorses() { return horses; } public Hippodrome(List<Horse> horses) { this.horses = horses; } public static void main(String[] args) { horses = new ArrayList<Horse>(); game = new Hippodrome(getHorses()); Horse h1 = new Horse("Tom", 3, 0); Horse h2 = new Horse("Jerry", 3, 0); Horse h3 = new Horse("Spike", 3, 0); horses.add(h1); horses.add(h2); horses.add(h3); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
JianQiu Hwang
Level 35 , Washington
6 June 2020, 00:23
public static Hippodrome game;
private List<Horse> horses;
public static void main(String[] args) {
    Horse horse1 = new Horse("horse1", 3, 0);
    Horse horse2 = new Horse("horse2", 3, 0);
    Horse horse3 = new Horse("horse3", 3, 0);
    List<Horse> horses = new ArrayList<>();
    horses.add(horse1);
    horses.add(horse2);
    horses.add(horse3);
    game = new Hippodrome(horses);
}
0
Guadalupe Gagnon
Level 37 , Tampa, United States
28 May 2020, 20:53
#1 The horses ArrayList from line 7 should not be static. You will need to remove that modifier. When that is changed this will mean that 'horses' is not a class variable and could only exist within a Hippodrome object. Therefore the code in the main method above would not work because on line 19 'horses' does not exist as no Hippodrome object that would contain it is created. Rethink the logic and use of the Hippodrome's constructor to make an object with a properly initialized 'horses' list.
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.