Question
Good Morning, I have the task of writing a short application that uses conditionals to create a forked branching structure for the recipe manager. Whenever
Good Morning,
I have the task of writing a short application that uses conditionals to create a "forked" branching structure for the recipe manager.
Whenever a program accepts user input, it is best practice to be sure the input is what you as the programmer expect. As you continue to develop the recipe manager, you will need to be able to validate user input and ensure that the user enters values that are valid. In this lab, you will write a short program that first, tests that the input is numerical, then checks that the number is within a specific range, and checks that the maximum number of cups of our main ingredient is 100 and that the minimum number of cups is 1.
This application uses the Scanner class to accept a number between 1 and 100 from the user. The Scanner class is useful for parsing primitive values, including numbers.
Specifically, you will create a branching structure that leads to the following output:
If the number entered is between 1 and 100 (inclusive), the application will display a message that says, Good job! The number you entered is___.
However, if the number entered is not between 1 and 100 (inclusive), an error message will be displayed to inform the user that the entry does not fit the expected range: The number entered was not between 1 and 100!
Needs to be completed as a Java file
Extending This Lab for Your Final Project For your final project, adapt your Ingredient Java file to include data-type validation steps for each of the variables in the class: ingredientName (String) ingredientAmount (float) unitMeasurement (String) Number of calories (double)
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package SteppingStones;
import java.util.Scanner;
/** * * @author snhu.edu */ public class SteppingStone3_Branches { public static void main(String[] args) { int numberCups = -1; /** * Add a CONSTANT variable MAX_CUPS assigned to the value 100 */ Scanner scnr = new Scanner(System.in); System.out.println("Please enter the number of cups (between 1 and 100): "); //The following "if branch" uses the scanner method hasNextInt() to //check to see if the input is an int. if (scnr.hasNextInt()) { numberCups = scnr.nextInt(); /**NESTED BRANCH: * Insert a nested branch that follows the following pattern: * * if numberCups is greater 1 AND less than or equal to MAX_CUPS: * print numberCups + " is a valid number of cups!" * * else: * print numberCups + " is a not valid number of cups!" * print "Please enter another number of cups between 1 and 100: " * numberCups = scnr.nextInt(); * * if numberCups is greater 1 AND less than or equal to MAX_CUPS: * print numberCups + " is a valid number of cups!" * * else if numberCups < 1: * print numberCups + "is less than 1. Sorry you are out of" * attempts." * * * else * print numberCups + "is greater than 100. Sorry you are out of * attempts." * */ } } else { System.out.println("Error: That is not a numer. Try again."); } } }
/** * * For your Final Project, 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) * */
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