Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with the following Java code: SteppingStone3_Branches Java Code below: /* * To change this license header, choose License Headers in Project Properties. *

Need help with the following Java code: SteppingStone3_Branches

Java Code below:

/* * 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) * */

Here is the previous code. The tasks are already completed in bold:

/* * 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 SteppingStone2_IngredientCalculator {

/** * @param args the command line arguments */ public static void main(String[] args) { /** *Assign the following variables with the appropriate data type and value: *VARIABLE NAME VALUE *nameOfIngredient "" *numberCups 0 *numberCaloriesPerCup 0 *totalCalories 0.0 */ String nameOfIngredient = ""; int numberCups = 0; int numberCaloriesPerCup = 0; double totalCalories = 0; Scanner scnr = new Scanner(System.in); System.out.println("Please enter the name of the ingredient: "); nameOfIngredient = scnr.next(); System.out.println("Please enter the number of cups of " + nameOfIngredient + " we'll need: "); numberCups = scnr.nextFloat(); System.out.println("Please enter the name of calories per cup: "); numberCaloriesPerCup = scnr.nextInt(); /** * Write an expression that multiplies the number of cups * by the Calories per cup. * Assign this value to totalCalories */ totalCalories = numberCaloriesPerCup * numberCups; System.out.println(nameOfIngredient + " uses " + numberCups + " cups and has " + totalCalories + " calories."); } }

/**

* * Final Project * *For your Final Project: * * 1. Create a new java class named Ingredient * * 2. Adapt the code from this SteppingStone to include the following changes: * * a. Rename the variable, numberCups, to represent the more general * ingredientAmount; * * b. Add a new text variable, unitMeasurement to store unit of measurement * for the ingredient amount (e.g. cups, oz., etc.); * * c. Prompt the user to input the measurement unit; * * /

public class Ingredient{ private String nameOfIngredient = ""; private int numberCups = 0; private int numberCaloriesPerCup = 0; public Ingredient(String nameOfIngredient, int numberCups, int numberCaloriesPerCup) { this.nameOfIngredient = nameOfIngredient; this.numberCups = numberCups; this.numberCaloriesPerCup = numberCaloriesPerCup; } public String getNameOfIngredient() { return nameOfIngredient; } public int getNumberCups() { return numberCups; } public int getNumberCaloriesPerCup() { return numberCaloriesPerCup; } public double getTotalCalories() { return numberCaloriesPerCup * numberCups; } @Override public String toString() { return numberCups + " cups of " + nameOfIngredient + ", total Calories: " + getTotalCalories(); scrn.close(); } }

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

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions

Question

Define human resource ethics.

Answered: 1 week ago