Question
How to create a test class in java? Please Help! I need to create both a recipe class, as well as a recipe test class.
How to create a test class in java? Please Help!
I need to create both a recipe class, as well as a recipe test class. I have created the recipe class, but I am not sure what I am supposed to do with the provided test code. Is this something I create in my existing program below my recipe class? or is this a whole new program I create and then somehow import the recipe class into it? Very confused!
These are the instructions I am provided with:
*To test the functionality of your finished code, use the SteppingStone5_RecipeTest.java file
*Replace the public static void main(String[] args) with public SteppingStone5_Recipe createNewRecipe()
This is my recipe class code, and below that is what I have for the test code. Thanks in advance for any help! :
package steppingstone5_recipe;
/**
*
* @author
*/
//Imported classes
import java.util.Scanner;
import java.util.ArrayList;
public class SteppingStone5_Recipe {
// Declared the variables
private String recipeName;
private double totalRecipeCalories;
private ArrayList recipeIngredients;
private int servings;
// Created a default constructor.
public SteppingStone5_Recipe()
{
this.recipeName = "";
this.servings = 1; //<--- assigned value with data type
this.recipeIngredients = new ArrayList(); //<-- assigned value for ArrayList
this.totalRecipeCalories = 0;
}
public SteppingStone5_Recipe(String recipeName, int servings,
ArrayList
{
this.recipeName = recipeName;
this.servings = servings;
this.recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
}
//This will print the recipe once user types "end" after ingredients are entered.
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.");
}
//Main Method
public static void main(String[] args) {
int totalRecipeCalories = 0;
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: ");
//corrected data type & Scanner assignment method for 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 {
//Adds the ingredient name to recipeIngredients
recipeIngredients.add(ingredientName);
System.out.println("Please enter the ingredient amount (Cups): ");
float ingredientAmount = scnr.nextFloat();
System.out.println("Please enter the ingredient Calories: ");
int ingredientCalories = scnr.nextInt();
/**
* Adds the total Calories from this ingredient
* to the totalRecipeCalories
*/
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;
}
}
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
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();
}
}
________________________Recipe Test file________________________
//SteppingStone5_recipeTester.java
import java.util.ArrayList;
/*
* 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;
/**
*
* @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
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
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