Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need some help with a Java program based on collections Create a class called Food with these four fields, amount: which stores the amount needed

Need some help with a Java program based on collections

Create a class called Food with these four fields, amount: which stores the amount needed (ex... 2 , 15, .50 etc) It will be be important to store this in a way that two amounts can be compared to see which is larger. unit: which stores the unit of measure associated with the quantity (ex... oz. cups, pints, etc.)... foodType: which stores the name of the amount(ex.. flour, eggs, sugar etc) and then the calorieAmount which stores the calories for each food type(example... a cup of sugar contains 773 calories. Any time we list any number of cups of sugar, we will use 773 as the number of calorieAmount.) ... Make a constructor for the Food class with a parameter for each field and then also add an accessor method for each field of the Food Class.... Lastly add a method called toString and it doesn't have any parameters but returns a string. Specifically, it should return the combination of the quantity and unit of measure, right-justified in a field width of 16 characters, followed by a space and the type. (example, if you called this method on a Food that was 2.5 cups of flour, you would get a string " 2.5 cups of flour". For this, you need to use the static method format for the String class and it takes the same parameters as System.out.printf, but instead of printing it returns a formatted string.

Next create a class called Recipe with these fields... name: which stores the name of the recipe(ex Peanut Butter Cookies, Tomato Soup etc) .... category: which stores the type of food that is created(ex, Dessert, Lunch, Appetizer, etc) next add a field called ingredients, that stores a colletion of stuff needed for the recipe. The ingredients do not need to be in any particular order, but it is important that we can add more ingredients later and that we cannot accidentally add the same ingredient more than once. Each ingredient will be an instance of the Food class that was created before. Add one last field to Recipe called instructions that stores a collection of steps that must be taken to prepare the food. It is important that the instructions be stored in a specific order, that the number of steps can grow over time, and that the same step be allowed to be used repeatedly. Each step will simply be a string ("Add sugar to the bowl", "Bake at 350 for 40 minutes", etc.).

Add a constructor to the Recipe class with parameters for the name and category of the recipe. A new recipe starts with no ingredients and no instructions.... Then add accessor methods to the Recipe class for the name and category fields.... Add a method called addIngredient to Recipe with one parameter which is a Food object, and it should add that object to the collection of ingredients. Add another method also called addIngredient to the Recipe class, this version has four parameters, amount, unit, foodType, and calorieAmount.... Next add a method called addStep that has one parameter which is a step to add at the end of the instructions.... Add a method called insertStep to the Recipe class with two parameters, a step to add, then the (0-based) number of the step it will go before.(example, fi we currently had steps "A", "B", and "C", and called this method with parameters "D" and 1, the new list of instructions would be "A", "D", "B", and "C". If the second parameter is greater than or equal to the number of steps, just add the new step to the end of the list.

Add a method called toString to the Recipe class with no parameters and returns a string. The string returned should use the following format:

The text "Name: " followed by the name, on one line.

The text "Category: " followed by the category, on one line.

empty line

A tab character followed by the text "Ingredients:", on one line.

The result of calling toString on each Ingredient, each on their own line.

empty line

A tab character followed by the text "Instructions:", on one line.

Each step of the instructions on its own line, with the text "0: " before the rst, "1: "before the second, "2: " before the third, etc.

Add a method called getTotalCalories to the recipe class with no parameters and returns the total number of calories in the food. (example, something made of 2 cups of sugar (773 calories per cup) and 3 tbsp of butter (102 calories per tablespoon) would have 2 * 773 + 3 * 102 = 1852 calories.

Lastly, add a method called getDoubled to the recipe class. it doesnt have any parameters and should return a Recipe. Specifically, it should return a copy of itself, in which the quantities of all ingredients have been multiplied by 2.

Finally, you can write a Main class that tests the twos classes and all the fields/methods within them.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions