Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, two JAVA files have been created based on the following requirements: THE PROGRAM DOES NOT RUN AND HAVE SOME ERRORS PLEASE HELP: A Recipe

Hello, two JAVA files have been created based on the following requirements: THE PROGRAM DOES NOT RUN AND HAVE SOME ERRORS PLEASE HELP:

A Recipe class should include the following items:

Instance variables: recipeName, servings, recipeIngredients, and totalRecipeCalories

Accessors and mutators for the instance variables

Constructors

A printRecipe() method

A-createNewRecipe() method to build a recipe from user input

Pseudocode for the custom method selected from the list in Stepping Stone Lab Five:

See file below:

package milestone.two.recipe.pkgclass;

import java.util.Scanner;

import java.util.ArrayList;

public class MilestoneTwoRecipeClass {

private String recipeName; private int servings; ArrayList recipeIngredients = new ArrayList(); private int totalRecipeCalories; public void setRecipeName(String recipeName){ this.recipeName = recipeName; } public String getRecipeName(){ return recipeName; } public void setServings(int servings){ this.servings = servings; } public int getServings(){ return servings; } public void setRecipeIngredients(ArrayList recipeIngredients){ this.recipeIngredients = recipeIngredients; } public ArrayList getRecipeIngredients(){ return recipeIngredients; } public void setTotalRecipeCalories(int totalRecipeCalories){ this.totalRecipeCalories = totalRecipeCalories; } public int getTotalRecipeCalories(){ return totalRecipeCalories; } //constructor public MilestoneTwoRecipeClass() { this.recipeName = ""; this.servings = 0; this.recipeIngredients = new ArrayList<>(); this.totalRecipeCalories = 0; } public MilestoneTwoRecipeClass(String recipeName, int servings, ArrayList recipeIngredients, double totalRecipeCalories) { this.recipeName = recipeName; this.servings = servings; this.recipeIngredients = recipeIngredients; this.totalRecipeCalories = (int) totalRecipeCalories; } public void printRecipe() { int singleServingCalories = (totalRecipeCalories / servings); System.out.println("Recipe: " + recipeName); System.out.println("Serves: " + servings); System.out.println("Ingredients: "); for (int i = 0; i recipeIngredients = new ArrayList(); boolean addMoreIngredients = true; Scanner scnr = new Scanner(System.in); System.out.println("Please enter the recipe name: "); String recipeName = scnr.nextLine(); System.out.println("Please enter the number of servings: "); int servings = scnr.nextInt(); do { System.out.println("Please enter the ingredient name or type end if you are finished entering ingredients: "); String ingredientName = scnr.next(); if (ingredientName.toLowerCase().equals("end")) { addMoreIngredients = false; break; } else { recipeIngredients.add(ingredientName); addMoreIngredients = true; System.out.println("Please enter the ingredient amount: "); float ingredientAmount = scnr.nextFloat(); System.out.println("Please enter the ingredient Calories: "); int ingredientCalories = scnr.nextInt(); /** * Add the total Calories from this ingredient * (ingredientCalories * ingredientAmount) * to the totalRecipeCalories * */ totalRecipeCalories = (int) (ingredientCalories * ingredientAmount); } } while (addMoreIngredients == true) ; MilestoneTwoRecipeClass recipe1 = new MilestoneTwoRecipeClass(recipeName, servings, recipeIngredients, totalRecipeCalories); recipe1.printRecipe(); return recipe1; } }

A Recipe_Test.java file containing a main() method that:

Uses a constructor to create a new recipe

Accesses the printRecipe() method to print the formatted recipe

Invokes the createNewRecipe() method to accept user input

see file below:

package milestone.two.recipe.pkgclass;

public class RecipeTest {

/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here final milestone.two.recipe.pkgclass newmilestone.two.recipe.pkgclass = new milestone.two.recipe.pkgclass(); milestone.two.recipe.pkgclass addNewmilestone.two.recipe.pkgclass = newmilestone.two.recipe.pkgclass.addNewmilestone.two.recipe.pkgclass(); newmilestone.two.recipe.pkgclass.printmilestone.two.recipe.pkgclass(); } }

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

Management Accounting

Authors: Leslie G. Eldenburg, Albie Brooks, Judy Oliver, Gillian Vesty, Rodney Dormer, Vijaya Murthy, Nick Pawsey

4th Edition

0730369382, 978-0730369387

More Books

Students also viewed these Programming questions