0% found this document useful (0 votes)
197 views5 pages

Encapsulation Practice PDF

The document describes steps to model classes for a pizza calories calculator application. It involves creating classes for Dough, Topping, and Pizza. The Dough and Topping classes encapsulate data on ingredients and calculate calories per gram based on modifiers. Validation is added to check for valid ingredient types and weights. The Pizza class uses the Dough and Topping classes, and calculates total calories by summing the calories of its ingredients.

Uploaded by

Ioana Macarie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
197 views5 pages

Encapsulation Practice PDF

The document describes steps to model classes for a pizza calories calculator application. It involves creating classes for Dough, Topping, and Pizza. The Dough and Topping classes encapsulate data on ingredients and calculate calories per gram based on modifiers. Validation is added to check for valid ingredient types and weights. The Pizza class uses the Dough and Topping classes, and calculates total calories by summing the calories of its ingredients.

Uploaded by

Ioana Macarie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercises: OOP

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.

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 getter. This
means the method can safely be hidden inside the Animal class by declaring it private.

Examples
Input Output
Mara Chicken Mara (age 10) can produce 1 eggs per
10 day.
Mara Age should be between 0 and 15.
17

Problem 1. Shopping Spree


Create two classes: class Person and class Product. Each person should have a name, money and a bag of products.
Each product should have name and cost. Name cannot be an empty string. Money cannot be a negative number.

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

Problem 2. *Pizza Calories


A Pizza is made of a dough and different toppings. You should model a class Pizza which should have a name, dough
and toppings as fields. Every type of ingredient should have its own class. Every ingredient has different properties:
the dough can be white or wholegrain and in addition it can be crispy, chewy or homemade. The toppings can be of
type meat, veggies, cheese or sauce. Every ingredient should have a weight in grams and a method for calculating its
calories according its type. Calories per gram are calculated through modifiers. Every ingredient has 2 calories per
gram as a base and a modifier that gives the exact calories. For 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 100 * 1.5
* 1.1 = 330.00 total calories.

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.

Step 1. Create a Dough Class


The base ingredient of a Pizza is the dough. First you need to create a class for it. It has a flour type which can be
white or wholegrain. In addition, it has a baking technique which can be crispy, chewy or homemade. A dough
should have a weight in grams. The calories per gram of a dough are calculated depending on the flour type and the
baking technique. Every dough has 2 calories per gram as a base and a modifier that gives the exact calories. For

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.

Step 2. Validate Data for the Dough Class


Change the internal logic of the Dough class by adding a data validation in the setters.

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

Step 3. Create a Topping Class


Next you need to create a Topping class. It can be of four different types – meat, veggies, cheese or a sauce. A
topping has a weight in grams. The calories per gram of a topping are calculated depending on its type. The base
calories per gram are 2. Every different type of topping has a modifier. For example, meat has a modifier of 1.5, so a
meat topping will have 1.5 calories per gram (1 * 1.5). Everything that the class should expose is a getter for calories
per gram. You are given the modifiers below:

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.

Step 4. Validate Data for the Topping Class


Change the internal logic of the Topping class by adding a data validation in the setter.

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

Step 5. Create a Pizza Class!


A Pizza should have a name, some toppings and a dough. Make use of the two classes you’ve made earlier. Also a
pizza should have public getters for its name, number of toppings and the total calories. The total calories are
calculated by summing the calories of all the ingredients a pizza has. Create the class using a proper constructor,
expose a method for adding a topping, a public setter for the dough and a getter method for the total calories.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy