Question

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

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<String> 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("\nRecipe: " + 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<String> 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<Ingredient> recipeIngredients = new ArrayList();

  

ArrayList<Ingredient> 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();

}

  

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//SteppingStone5_recipeTester.java

import java.util.ArrayList;

package SteppingStones;


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<Ingredient> recipeIngredients = new ArrayList();
       ArrayList<Ingredient> 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();
   }
  
}

Add a comment
Know the answer?
Add Answer to:
How to build Java test class? I am supposed to create both a recipe class, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input...

    Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:  The instance variables for the class (recipeName, serving size, and...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditiona...

    Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditional and iterative control structures that repeat actions as needed. The unit measurement is missing from the final output and I need it to offer options to lbs, oz, grams, tbsp, tsp, qt, pt, and gal. & fl. oz. Can this be added? The final output...

  • I cannot submit my entire project due to the word count limit. I think my project...

    I cannot submit my entire project due to the word count limit. I think my project looks ok, but my recipe box class and recipe test class is running errors and it may have something to do with the menu portion of the recipe box. Please take a look: "You will create a program that will help you manage a collection of recipes. You will implement three classes: one for the main recipe items, one for the ingredients that are...

  • *Please Add Inline Comments Directed toward software engineers about design decisions to facilitate the programs ongoing...

    *Please Add Inline Comments Directed toward software engineers about design decisions to facilitate the programs ongoing maintenance* import java.util.ArrayList; import java.util.Scanner; /** * * @author Eli Mishkit */ public class SteppingStone6_RecipeBox { /** * Declare instance variables: * a private ArrayList of the type SteppingStone5_Recipe named listOfRecipes * */ private ArrayList<SteppingStone5_Recipe> listOfRecipes; /** * Add accessor and mutator for listOfRecipes * */ public ArrayList<SteppingStone5_Recipe> getListOfRecipes() { return listOfRecipes; } public void setListOfRecipes(ArrayList<SteppingStone5_Recipe> listOfRecipes) { this.listOfRecipes = listOfRecipes; } /** *...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • Java Branches code not working please HELP!! I am trying to build this class that will...

    Java Branches code not working please HELP!! I am trying to build this class that will ask for an ingredient, and the number of cups of the ingredient. It should then branch into an if/else branch off to verify that the number entered is valid and that it is within range. Once that branch completes it should then continue on to ask for the calories per cup, and then calculate total calories. Once I get it to enter the branch...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT