Ex3 Animal Farm from Encapsulation - Exercises
Здравейте,
опитвам различни варианти, но получавам 90/100. Благодаря предварително за съдействието.
https://www.mediafire.com/file/xyzy1m8doton5vw/animalFarm.zip/file
https://judge.softuni.bg/Contests/Compete/Index/1536#1
-
Animal Farm
You should be familiar with encapsulation already. For this problem, you’ll need to create class called Chicken. Chicken should contain several fields, a constructor, and several methods. Your task is to encapsulate or hide anything that is not intended to be viewed or modified from outside the class.
Chicken |
|
- |
name: String |
- |
age: int |
+ |
Chicken(String, int) |
- |
setName(String) : void |
- |
setAge (int): void |
+ |
productPerDay (): double |
+ |
toString(): Override |
- |
calculateProductPerDay() : double |
Chicken lives for 15 years. Chicken have name for sure, at least 1 symbol long. Chicken producing eggs:
-
First 6 years it produces 2 eggs per day [0 - 5]
-
Next 6 years it produces 1 egg per day [6 - 11]
-
And after that it produces 0.75 eggs per day
Step 1. Encapsulate Fields
Fields should be private. Leaving fields open for modification from outside the class is potentially dangerous. Make all fields in the Chicken class private.
In case the value inside a field is needed elsewhere, use getters to reveal it.
Step 2. Ensure Classes Have a Correct State
Having getters and setters is useless if you don’t actually use them. The Chicken constructor modifies the fields directly which is wrong when there are suitable setters available. Modify the constructor to fix this issue.
Step 3. Validate Data Properly
Validate the chicken’s name (it cannot be null, empty or whitespace). In case of invalid name, print exception message "Name cannot be empty."
Validate the age properly, minimum and maximum age are provided, make use of them. In case of invalid age, print exception message "Age should be between 0 and 15."
Step 4. Hide Internal Logic
If a method is intended to be used only by descendant classes or internally to perform some action, there is no point in keeping them public. The calculateProductPerDay() method is used by the productPerDay() public method. This means the method can safely be hidden inside the Chicken class by declaring it private.
Step 4. Submit Code to Judge
Submit your code as a zip file in Judge. Make sure you have a public Main class with a public static void main method in it.
Examples
Input |
Output |
Mara 10 |
|
Mara 17 |
Age should be between 0 and 15. |
Gosho 6 |
Chicken Gosho (age 6) can produce 1.00 eggs per day. |