package Milestone1; import java.util.Scanner; public class Ingredient { private
ID: 3910308 • Letter: P
Question
package Milestone1;
import java.util.Scanner;
public class Ingredient {
private String name;
private double amount;
private String unitOfMeasure;
private int calories;
private Scanner sc = new Scanner(System.in);
public Ingredient() {
System.out.println("Please enter the name of the ingredient: ");
name = sc.nextLine();
System.out.println("Please enter the number of cups of " + name + " we'll need: ");
boolean valid = false;
while(!valid) {
try {
amount = Double.parseDouble(sc.nextLine());
valid = true;
} catch(Exception e) {
System.out.println("Invalid value. Try again: ");
}
}
System.out.println("Please enter the unit of measure: ");
unitOfMeasure = sc.nextLine();
System.out.println("Please enter the number of calories per cup: ");
valid = false;
while(!valid) {
try {
calories = Integer.parseInt(sc.nextLine());
valid = true;
} catch(Exception e) {
System.out.println("Invalid value. Try again: ");
}
}
}
public String getName() {
return name;
}
public double getAmount() {
return amount;
}
public int getCalories() {
return calories;
}
public String getUnitOfMeasure() {
return unitOfMeasure;
}
@Override
public String toString() {
return "Ingredient [name=" + name + ", amount=" + amount + ", unitOfMeasure=" + unitOfMeasure + ", calories="
+ calories + "]";
}
public static void main(String args[]) {
Ingredient i = new Ingredient();
System.out.println(" " + i);
}
}
I have created primitive data types for this "Ingredient". This Java program is desinged to manage recipes. I am now working on my final project and need to make a few changes to this program.
* 1. Create a new java class named Ingredient
* Done*
* 2. Adapt the code from this SteppingStone to include the following changes:
*
* a. Rename the variable, numberCups, to represent the more general
* ingredientAmount;
* Please Check*
* b. Add a new text variable, unitMeasurement to store unit of measurement
* for the ingredient amount (e.g. cups, oz., etc.);
*Please Check*
* c. Prompt the user to input the measurement unit;
*Please Check*
*3. Add Inline comments directed toward software engineers about design decisions to facilitate teh programs ongoing maintenance.
*Please Add*
*4. Adapt your Ingredient java file to include
* data type validation steps for each of the variables in the class:
*
* ingredientName (String)
* ingredientAmount (float)
* unitMeasurment (String)
* number of Calories (double)
*
*/
*Please Check*
Explanation / Answer
Solution:
########Ingredient.java
/*
* Author : Your Name here
* Created on : Today's date
* File Name : Ingredient.java
* Description : Write the small description here
*
* */
package Milestone1;
import java.util.Scanner;
public class Ingredient {
// Declaring Ingredient class memebers/fields
private String ingredientName;
private float ingredientAmount;
private String unitMeasurement;
private double numberofCalories;
// Creating scanner object to take the input form the keyboard
private Scanner sc = new Scanner(System.in);
// Default COnstructor
public Ingredient() {
// Asking for the ingredient name and storing into the respective field/variable
System.out.println("Please enter the name of the ingredient: ");
ingredientName = sc.nextLine();
// Asking for the number of cups of the specified ingredient and storing into the respective field/variable
System.out.println("Please enter the number of cups of " + ingredientName + " we'll need: ");
boolean valid = false;
// Validating the input
while(!valid) {
try {
ingredientAmount = Float.parseFloat(sc.nextLine());
valid = true;
} catch(Exception e) {
System.out.println("Invalid value. Try again: ");
}
}
// Asking for the unit of the measurement
System.out.println("Please enter the unit of measure: ");
unitMeasurement = sc.nextLine();
// Asking for the number of calories per cup
System.out.println("Please enter the number of calories per cup: ");
valid = false;
// Validating the input
while(!valid) {
try {
numberofCalories = Integer.parseInt(sc.nextLine());
valid = true;
} catch(Exception e) {
System.out.println("Invalid value. Try again: ");
}
}
}
// Getters
public String getIngredientName() {
return ingredientName;
}
public float getIngredientAmount(){
return ingredientAmount;
}
public double getNumberofCalories() {
return numberofCalories;
}
public String getUnitMeasurement() {
return unitMeasurement;
}
// Overriding toString method to display useful information about the object
@Override
public String toString() {
return "Ingredient Name = " + ingredientName + " Ingredient Amount = " + ingredientAmount + " Unit Measurement = " + unitMeasurement + " Number of Calories = "
+ numberofCalories;
}
// Driver Method to test the methods
public static void main(String args[]) {
// Creating instance of(object) Ingredient
Ingredient i = new Ingredient();
// Displaying the object's information
System.out.println(" " + i);
}
}
Sample Run:
Please enter the name of the ingredient:
Sugar
Please enter the number of cups of Sugar we'll need:
subrat
Invalid value. Try again:
12
Please enter the unit of measure:
kcal
Please enter the number of calories per cup:
iut
Invalid value. Try again:
24
Ingredient Name = Sugar
Ingredient Amount = 12.0
Unit Measurement = kcal
Number of Calories = 24.0
Note: For any queries please do comment below before giving any negative feedback. If you really liked the solution then please do give a thumbs up.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.