CodeGym
Promotion
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
Agata
Level 20
Warsaw
  • 25.05.2020
  • 592views
  • 3comments

I don't meet last 4 requirements, please help me :)

Question about the task Chicken factory
Java Core,  Level 4,  Lesson 6
Resolved

Write a factory (HenFactory) for making chickens (Hen):

1. Write a Hen class.
1.1. Make it abstract.
1.2. Add an int getMonthlyEggCount() method to the abstract class.
1.3. Add a String getDescription() method to the class. It should return "I am a chicken."
2. Create an NorthAmericanHen class that inherits Hen.
3. Create an EuropeanHen class that inherits Hen.
4. Create an AsianHen class that inherits Hen.
5. Create an AfricanHen class that inherits Hen.
6. In each of these four classes, write your own implementation of the getMonthlyEggCount method.
The method should return the number of eggs per month produced by that type of chicken.
7. In each of these four classes, write your own implementation of the getDescription method.

The method must return a string as follows:
<parent class>.getDescription() + " I come from <continent>. I lay <n> eggs a month."
where <continent> is the name of the continent
and <n> is the number of eggs per month.

8. In the HenFactory class, implement the getHen method. It should return a chicken breed corresponding to the continent that the chicken came from.
9. All the classes you create must be in separate files.

Requirements:
  • The Hen class must be abstract.
  • The Hen class must have an abstract int getMonthlyEggCount() method.
  • The Hen class must implement a String getDescription() method, which returns "I am a chicken."
  • The NorthAmericanHen, EuropeanHen, AsianHen, and AfricanHen classes must inherit the Hen class and be located in separate files.
  • The NorthAmericanHen, EuropeanHen, AsianHen, and AfricanHen classes must implement the getMonthlyEggCount method, which should return the number of eggs per month laid by the corresponding type of chicken.
  • The NorthAmericanHen, EuropeanHen, AsianHen, and AfricanHen must override the parent class's getDescription method so that the returned string looks like this: <parent class>.getDescription() + " I come from <continent>. I lay <n> eggs a month." where <continent> is the name of the continent, and <n> is the number of eggs per month.
  • The getHen method must be implemented in the HenFactory class and must return the chicken type that corresponds to the passed continent.
package com.codegym.task.task14.task1408; /* Chicken factory */ public class Solution { public static void main(String[] args) { Hen hen = HenFactory.getHen(Continent.NORTHAMERICA); hen.getMonthlyEggCount(); //System.out.println(hen.getMonthlyEggCount()); //System.out.println(hen.getDescription()); } static class HenFactory { static Hen getHen(String continent) { Hen hen = null; if (continent.equals(Continent.NORTHAMERICA)){ hen = new AmericanHen(); } else if (continent.equals(Continent.EUROPE)){ hen = new EuropeanHen(); } else if (continent.equals(Continent.ASIA)){ hen = new AsianHen(); } else if (continent.equals(Continent.AFRICA)){ hen = new AfricanHen(); } return hen; } } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Tiko hakobyan
Level 20 , Yerevan, Armenia
15 June 2020, 13:06
shouldn't line 22 in solution class contain new NorthAmericanHen() instead of new AmericanHen(); ?
0
Mike McKenna
Level 25 , Wilmington, United States
26 May 2020, 02:09
Hi, quite a task with files . solution.java,continent.java,hen.java Ok . in African Hen, Asian Hen, North American Hen European Hen . if u make following changes task should work. line 3 remove implements Continent line 5 return #; only remove line 6. remove lines 8-10 . replace with
@Override
       String getDescription(){
        return super.getDescription() + " I come from " + Continent.AFRICA + ". I lay " + getMonthlyEggCount()+ " eggs a month.";


       }
Hope I helped .
0
Agata
Level 20 , Warsaw, Poland
26 May 2020, 09:17
Tahnks :) but still 4 of them are not met which really confuses me and now my code looks like this (each in different section/classes): public class AsianHen extends Hen { int getMonthlyEggCount(){ return 15; } @Override public String getDescription(){ return super.getDescription()+" I come from "+Continent.ASIA+". I lay "+getMonthlyEggCount()+" eggs a month."; } } public class AfricanHen extends Hen { int getMonthlyEggCount(){ return 2; } @Override public String getDescription(){ return super.getDescription()+" I come from "+Continent.AFRICA+". I lay "+getMonthlyEggCount()+" eggs a month."; } } public class AmericanHen extends Hen { int getMonthlyEggCount(){ return 5; } @Override public String getDescription(){ return super.getDescription()+" I come from "+Continent.NORTHAMERICA+". I lay "+getMonthlyEggCount()+" eggs a month."; } } public class EuropeanHen extends Hen { int getMonthlyEggCount(){ return 10; } @Override public String getDescription(){ return super.getDescription()+" I come from "+Continent.EUROPE+". I lay "+getMonthlyEggCount()+" eggs a month."; } } (I didn't change anything in solution)
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.