Question
For the code below I have tested using an online java complier. However when looking at code I don't exactly see getters/setters or accessors/mutators in
For the code below I have tested using an online java complier.
However when looking at code I don't exactly see getters/setters or accessors/mutators in the code below where the code contains get or set as shown in the examples in the link below
https://www.c-sharpcorner.com/UploadFile/3614a6/accessors-and-mutators-in-java/
If there are accessors/mutators below could you explain where the are located?
If there are no accessors/mutators could you provide a for the code below where the inline comments prompts the user to add accessors/mutators for vamiable?
Code below...
import java.util.Scanner; import java.util.ArrayList;
public class SteppingStone5_Recipe {
private String recipeName; private int servings; // Variable to store the number of servings private ArrayList recipeIngredients; // ArrayList to store the recipe ingredients private double totalRecipeCalories; // Variable to store the total calorie count of the recipe
/** * Add mutators and accessors for the class variable. * */
// Constructor without arguments public SteppingStone5_Recipe() { this.recipeName = ""; this.servings = 0; // Initialize servings with an appropriate value this.recipeIngredients = new ArrayList<>(); // Initialize the ArrayList this.totalRecipeCalories = 0; }
// Constructor with arguments public SteppingStone5_Recipe(String recipeName, int servings, ArrayList recipeIngredients, double totalRecipeCalories) { this.recipeName = recipeName; this.servings = servings; this.recipeIngredients = recipeIngredients; this.totalRecipeCalories = totalRecipeCalories; }
public void printRecipe() { int singleServingCalories = (int) (totalRecipeCalories / servings);
System.out.println("Recipe: " + recipeName); System.out.println("Serves: " + servings+" "); System.out.println("Ingredients:");
for (String ingredient : recipeIngredients) { System.out.println(ingredient); }
System.out.println(" Each serving has " + singleServingCalories + " Calories."); }
public static void main(String[] args) { double totalRecipeCalories = 0; // Initialize the totalRecipeCalories variable ArrayList recipeIngredients = new ArrayList<>(); // Makes an empty 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; } else { recipeIngredients.add(ingredientName);
System.out.println("Please enter the ingredient amount: "); float ingredientAmount = scnr.nextFloat();
System.out.println("Please enter the ingredient Calories: "); int ingredientCalories = scnr.nextInt();
totalRecipeCalories += (ingredientCalories * ingredientAmount); } } while (addMoreIngredients);
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories); recipe1.printRecipe(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started