Encapsulation Practice PDF
Encapsulation Practice PDF
Animal Farm
You should be familiar with encapsulation already. For this problem, you’ll be working with the Animal Farm project.
It contains a class Chicken. Chicken contains several fields, a constructor, several properties and several methods. Your
task is to encapsulate or hide anything that is not intended to be viewed or modified from outside the class.
In case the value inside a field is needed elsewhere, use getters to reveal it.
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."
Examples
Input Output
Mara Chicken Mara (age 10) can produce 1 eggs per
10 day.
Mara Age should be between 0 and 15.
17
Page 1 of 5
Create a program in which each command corresponds to a person buying a product. If the person can afford a
product add it to his bag. If a person doesn’t have enough money, print an appropriate message ("[Person name]
can't afford [Product name]").
On the first two lines you are given all people and all products. After all purchases print every person in the order of
appearance and all products that he has bought also in order of appearance. If nothing is bought, print the name of
the person followed by "Nothing bought".
In case of invalid input (negative money exception message: "Money cannot be negative") or empty name: (empty
name exception message "Name cannot be empty") break the program with an appropriate message. See the
examples below:
Examples
Input Output
Pesho=11;Gosho=4 Pesho bought Bread
Bread=10;Milk=2; Gosho bought Milk
Pesho Bread Gosho bought Milk
Gosho Milk Pesho can't afford Milk
Gosho Milk Pesho - Bread
Pesho Milk Gosho - Milk, Milk
END
Mimi=0 Mimi can't afford Kafence
Kafence=2 Mimi – Nothing bought
Mimi Kafence
Jeko=-3 Money cannot be negative
Chushki=1;
Jeko Chushki
END
Your job is to model the classes in such a way that they are properly encapsulated and to provide a public method
for every pizza that calculates its calories according to the ingredients it has.
Page 2 of 5
example, a white dough has a modifier of 1.5, a chewy dough has a modifier of 1.1, which means that a white chewy
dough weighting 100 grams will have (2 * 100) * 1.5 * 1.1 = 330.00 total calories. You are given the modifiers below:
Modifiers:
• White – 1.5;
• Wholegrain – 1.0;
• Crispy – 0.9;
• Chewy – 1.1;
• Homemade – 1.0;
Everything that the class should expose is a getter for the calories per gram. Your task is to create the class with a
proper constructor, fields, getters and setters. Make sure you use the proper access modifiers.
Make sure that if invalid flour type or an invalid baking technique is given a proper exception is thrown with the
message "Invalid type of dough.".
The allowed weight of a dough is in the range [1..200] grams. If it is outside of this range throw an exception with
the message "Dough weight should be in the range [1..200].".
Exception Messages
• "Invalid type of dough."
• "Dough weight should be in the range [1..200]."
Make a test in your main method that reads Doughs and prints their calories until an "END" command is read.
Examples
Input Output
Dough White Chewy 100 330.00
END
Dough Tip500 Chewy 100 Invalid type of dough.
END
Dough White Chewy 240 Dough weight should be in the range [1..200].
END
Modifiers:
Page 3 of 5
• Meat – 1.2;
• Veggies – 0.8;
• Cheese – 1.1;
• Sauce – 0.9;
Your task is to create the class with a proper constructor, fields, getters and setters. Make sure you use the proper
access modifiers.
Make sure the topping is one of the provided types, otherwise throw a proper exception with the message "Cannot
place [name of invalid argument] on top of your pizza.".
The allowed weight of a topping is in the range [1..50] grams. If it is outside of this range throw an exception with
the message "[Topping type name] weight should be in the range [1..50].".
Exception Messages
• "Cannot place [name of invalid argument] on top of your pizza."
• "[Topping type name] weight should be in the range [1..50]."
Make a test in your main method that reads a single dough and prints its calories.
Examples
Input Output
Dough White Chewy 100 330.00
Topping meat 30 72.00
END
Dough White chewy 100 330.00
Topping buhu 500 Cannot place buhu on top of your pizza.
END
Dough White Chewy 100 330.00
Topping Meat 500 Meat weight should be in the range [1..50].
END
The input for a pizza consists of several lines. On the first line is the pizza name and the number of toppings it has.
On the second line you will get input for the dough. On the next lines, you will receive every topping the pizza has.
The number of lines for the toppings will correspond to the number of toppings declared on the first line.
If creation of the pizza was successful print on a single line the name of the pizza and the total calories it has.
Page 4 of 5
Step 6. Validate Data for the Pizza Class
The name of the pizza should not be an empty string. Also it should not be longer than 15 symbols. If it doesn’t fit
this throw and exception with the message "Pizza name should be between 1 and 15 symbols."
The number of toppings should be in range [0...10]. If not, throw an exception with the message "Number of
toppings should be in range [0..10]."
Your task is to print the name of the pizza and the total calories it has according to the examples below.
Examples
Input Output
Pizza Meatless 2 Meatless – 370.00 Calories.
Dough Wholegrain Crispy 100
Topping Veggies 50
Topping Cheese 50
END
Pizza Meatfull 5 Meatfull – 1028.00 Calories.
Dough White cheWy 200
Topping Meat 50
Topping Cheese 50
Topping meat 20
Topping sauce 10
Topping Meat 30
END
Pizza Bulgarian 20 Number of toppings should be in range
Dough Tip500 Balgarsko 100 [0..10].
Topping Sirene 50
Topping Cheese 50
Topping Buhu 20
Topping Meat 10
END
Pizza Bulgarian 2 Invalid type of dough.
Dough Tip500 Balgarsko 100
Topping Sirene 50
Topping Cheese 50
Topping Buhu 20
Topping Meat 10
END
Pizza Bulgarian 2 Cannot place Sirene on top of your pizza.
Dough White Chewy 100
Topping Sirene 50
Topping Cheese 50
Topping Buhu 20
Topping Meat 10
END
Page 5 of 5