Question: I have an assignment I am working on that requires us to create a recipe manager and I am working on the ingredient class. here

I have an assignment I am working on that requires us to create a recipe manager and I am working on the ingredient class. here is what we have to do:

- Create the necessary class attributes/variables as mentioned in the guidelines (nameOfIngredient, ingredientAmount, unitOfMeasurement, numberOfCaloriesPer, totalCalories). This is based on the ideas from Stepping Stone Lab Two.

- Create "getter" and "setter" methods for each of these variables so that the variable values can be retrieved and modified.

- In the main method you want to test the attributes and methods of your class by prompting the user to enter in each of these attributes of an Ingredient.

- You should also use branching to validate each of the user's input to verify it is of the correct data type and also within the correct range

My code looks like the following, I know there are issues with my branching and I can't figure out what I have done wrong. Can anyone help me clear this up so I can submit my assignment?

import java.util.Scanner;

public class Ingredient { private String nameOfIngredient; private String unitMeasurement; private float ingredientAmount; private int numberCaloriesPerUnit; private float totalCalories; //constructor with no inputs public Ingredient(){ this.nameOfIngredient = ""; this.unitMeasurement = ""; this.ingredientAmount = 0.0f; this.numberCaloriesPerUnit = 0; this.totalCalories = 0.0f; } //constructor with inputs public Ingredient(String nameOfIngredient, String unitMeasurement, float ingredientAmount, int numberCaloriesPerUnit, float totalCalories){ this.nameOfIngredient = nameOfIngredient; this.unitMeasurement = unitMeasurement; this.ingredientAmount = ingredientAmount; this.numberCaloriesPerUnit = numberCaloriesPerUnit; this.totalCalories = totalCalories; } //return the name of the ingredient public String getNameOfIngredient(){ return this.nameOfIngredient; } //return the unit of measurement public String getUnitMeasurement(){ return this.unitMeasurement; } //return the ingredient amount public float getIngredientAmount(){ return this.ingredientAmount; } //return the number of calories per unit public int getnumberCaloriesPerUnit(){ return this.numberCaloriesPerUnit; } //return total calories public float getTotalCalories(){ return this.totalCalories; } //set the name of ingredient public void setNameOfIngredient(String nameOfIngredient){ this.nameOfIngredient = nameOfIngredient; } //set the unit of measurement public void setUnitMeasurement(String unitMeasurement){ this.unitMeasurement = unitMeasurement; } //set the ingredient amount public void setIngredientAmount(float ingredientAmount){ this.ingredientAmount = ingredientAmount; } //set the number of calories per unit public void setNumberCaloriesPerUnit(int numberCaloriesPerUnit){ this.numberCaloriesPerUnit = numberCaloriesPerUnit; } //set total calories public void setTotalCalories(float totalCalories){ this.totalCalories = totalCalories; } //main method

/** * * @param args */ public static void main(String args[]){ //create new ingredient object Ingredient new_ingredient = new Ingredient();

//display all attributes /*System.out.println(new_ingredient.getNameOfIngredient() + " uses " + new_ingredient.getIngredientAmount() + " " + new_ingredient.getUnitMeasurement() + " and has " + new_ingredient.getTotalCalories() + " calories.");*/

String tempNameOfIngredient = ""; //initalize name of ingredient String tempUnitMeasurement = ""; //initialize unit of measurement float tempIngredientAmount = 0.0f; //initialize ingredient amount int tempNumberCaloriesPerUnit = 0; //intialize calories per unit float tempTotalCalories = 0.0f; //initalize total calories float MAX_AMOUNT = 15.0f; //initalize max amount of ingredient int MAX_CALORIES = 5000; //initialize max amount of calories Scanner scnr = new Scanner(System.in);

//Prompt the user for the name of ingredient System.out.println("Please enter the name of ingredient: "); tempNameOfIngredient = scnr.nextLine(); if(nameOfIngredient <= a && nameOfIngredient != int){ this.ingredientName = ingredientName; } else { System.out.println("Please enter valid ingredient name."); }

//Prompt the user for the ingredient amount System.out.println("Please enter the amount of " + tempNameOfIngredient + ": "); tempIngredientAmount = scnr.nextFloat();

if(ingredientAmount >= 0 && ingredientAmount<= MAX_AMOUNT){ this.ingredientAmount = ingredientAmount; } else { System.out.println("Please enter valid ingredient amount."); }

//Prompt the user for the unit of measurement System.out.println("Please enter the unit of measurement for " + tempNameOfIngredient + ": "); tempUnitMeasurement = scnr.next();

if(unitMeasurement == "cups"|| unitMeasurement == "ounces" || unitMeasurement == "tablespoons" || unitMeasurement == "teaspoons"){ this.unitMeasurement = unitMeasurement; } else { System.out.println("Incorrect unit of measurement. Pleases enter valid unit of measurement"); }

//Prompt the user for calories per unit System.out.println("Please enter the calories per " + tempUnitMeasurement + ": "); tempNumberCaloriesPerUnit = scnr.nextInt();

if(numberCaloriesPerUnit >= 1 && numberCaloriesPerUnit <= MAX_CALORIES){ this.numberCaloriesPerUnit = numberCaloriesPerUnit; } else { System.out.println("Please enter correct number of calories per unit."); }

//compute total calories (Number cups X number calories per cup) tempTotalCalories = tempIngredientAmount * tempNumberCaloriesPerUnit;

//set ingredient attributes new_ingredient.setNameOfIngredient(tempNameOfIngredient); new_ingredient.setUnitMeasurement(tempUnitMeasurement); new_ingredient.setIngredientAmount(tempIngredientAmount); new_ingredient.setNumberCaloriesPerUnit(tempNumberCaloriesPerUnit); new_ingredient.setTotalCalories(tempTotalCalories);

//Display all the attributes System.out.println(new_ingredient.getNameOfIngredient() + "uses " + new_ingredient.getIngredientAmount() + " " + new_ingredient.getUnitMeasurement() + " and has " + new_ingredient.getTotalCalories() + " calories."); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!