Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class.

How to build Java test class?

I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance!

This is my recipe class:

package steppingstone5_recipe;

/**

*

* @author

*/

//Imported classes

import java.util.Scanner;

import java.util.ArrayList;

public class SteppingStone5_Recipe {

private String recipeName;

private double totalRecipeCalories;

private ArrayList recipeIngredients;

private int servings;

public SteppingStone5_Recipe()

{

this.recipeName = "";

this.servings = 1;

this.recipeIngredients = new ArrayList();

this.totalRecipeCalories = 0;

}

public SteppingStone5_Recipe(String recipeName, int servings,

ArrayList recipeIngredients, int totalRecipeCalories)

{

this.recipeName = recipeName;

this.servings = servings;

this.recipeIngredients = recipeIngredients;

this.totalRecipeCalories = totalRecipeCalories;

}

public void printRecipe() {

float singleServingCalories = (float) getTotalRecipeCalories() / getServings();

System.out.println(" Recipe: " + getRecipeName());

System.out.println("Serves: " + getServings());

System.out.println("Ingredients:");

for (int i = 0; i < getRecipeIngredients().size(); i++) {

System.out.println("\t" + getRecipeIngredients().get(i));

}

System.out.println("Each serving has " + singleServingCalories + " Calories.");

public static void main(String[] args) {

int totalRecipeCalories = 0;

ArrayList recipeIngredients = new 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 == true);

SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName,

servings, recipeIngredients, totalRecipeCalories);

recipe1.printRecipe();

}

public String getRecipeName() {

return recipeName;

}

public void setRecipeName(String recipeName) {

this.recipeName = recipeName;

}

public double getTotalRecipeCalories() {

return totalRecipeCalories;

}

public void setTotalRecipeCalories(double totalRecipeCalories) {

this.totalRecipeCalories = totalRecipeCalories;

}

//returns the recipeIngredients

public ArrayList getRecipeIngredients() {

return recipeIngredients;

}

//sets the recipeIngredients

public void setRecipeIngredients(ArrayList recipeIngredients) {

this.recipeIngredients = recipeIngredients;

}

//returns the servings

public int getServings() {

return servings;

}

//sets the servings

public void setServings(int servings) {

this.servings = servings;

}

}

And this is what is provided for the recipe tester, but I don't understand what it is that I am supposed to do:

//SteppingStone5_recipeTester.java

import java.util.ArrayList;

package SteppingStones;

/**

*

* @author

*/

public class SteppingStone5_RecipeTest {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// Create two recipe objects first

SteppingStone5_Recipe myFirstRecipe = new SteppingStone5_Recipe();

ArrayList recipeIngredients = new ArrayList();

ArrayList recipeIngredientsTwo = new ArrayList();

String ingredientName = "Anchovies";

Ingredient tempIngredient = new Ingredient().addNewIngredient(ingredientName);

recipeIngredients.add(tempIngredient);

SteppingStone5_Recipe mySecondRecipe = new SteppingStone5_Recipe("Pizza", 2, recipeIngredients, 300);

// Initialize first recipe

String ingredientNameTwo = "Noodles";

Ingredient tempIngredientTwo = new Ingredient().addNewIngredient(ingredientNameTwo);

recipeIngredientsTwo.add(tempIngredientTwo);

myFirstRecipe.setRecipeName("Ramen");

myFirstRecipe.setServings(2);

myFirstRecipe.setRecipeIngredients(recipeIngredientsTwo);

myFirstRecipe.setTotalRecipeCalories(150);

// Print details of both recipes

myFirstRecipe.printRecipe();

mySecondRecipe.printRecipe();

}

}

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

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

ISBN: 1292097612, 978-1292097619

More Books

Students also viewed these Databases questions