Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need assistance with creating CONTROL STRUCTURE for a recipe program

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need assistance with creating CONTROL STRUCTURE for a recipe program

    Hello please help with creating a user-defined method with control structure that calculates the cost of the recipe. I also need a test file to provide sample output. Any assistance would be greatly appreciated.

    so far I have:
    /*
     * 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 steppingstonesTesting;
    import java.util.ArrayList;
     
    import java.util.Arrays;
     
    import java.util.List;
     
    import java.util.Scanner;
    /**
     *
     * @author rijackson
     */
    public class Recipe1 {
     
    private String recipeName;
     
    private int servings;
     
    private List<String> recipeIngredients;
     
    private int totalRecipeCalories;
     
    // default constructor.
     
    public Recipe1() {
     
    this.recipeName = "";
     
    this.servings = 0;
     
    this.recipeIngredients = new ArrayList<String>();
     
    this.totalRecipeCalories = 0;
     
    }
     
    // overloaded constructor.
     
    public Recipe1(String recipeName, int servings, List<String> recipeIngredients, int totalRecipeCalories) {
     
    this.recipeName = recipeName;
     
    this.servings = servings;
     
    this.recipeIngredients = recipeIngredients;
     
    this.totalRecipeCalories = totalRecipeCalories;
     
    }
     
    // accessors and mutators
     
    public String getRecipeName() {
     
    return recipeName;
     
    }
     
    public void setRecipeName(String recipeName) {
     
    this.recipeName = recipeName;
     
    }
     
    public int getServings() {
     
    return servings;
     
    }
     
    public void setServings(int servings) {
     
    this.servings = servings;
     
    }
     
    public List<String> getRecipeIngredients() {
     
    return recipeIngredients;
     
    }
     
    public void setRecipeIngredients(List<String> recipeIngredients) {
     
    this.recipeIngredients = recipeIngredients;
     
    }
     
    public int getTotalRecipeCalories() {
     
    return totalRecipeCalories;
     
    }
     
    public void setTotalRecipeCalories(int totalRecipeCalories) {
     
    this.totalRecipeCalories = totalRecipeCalories;
     
    }
     
     
     
    public void printRecipe() {
     
    int singleServingCalories = (totalRecipeCalories / servings);
     
    System.out.println("Recipe: " + recipeName);
     
    System.out.println("Serves: " + servings);
     
    System.out.println("Ingredients: ");
     
    for (int i = 0; i < recipeIngredients.size(); i++) {
     
    String ingredient = recipeIngredients.get(i);
     
    System.out.println(ingredient);
     
    }
     
    System.out.println("Each Serving Has " + singleServingCalories + " Calories.");
     
    }
     
    // 
     
    // static method to create new recipe.
     
    public static Recipe1 createNewRecipe() {
     
    Scanner sc = new Scanner(System.in);
     
    System.out.print("\nEnter recipe name: ");
     
    String recipeName = sc.nextLine();
     
    System.out.print("\nEnter number of servings: ");
     
    int numServings = sc.nextInt();
     
    sc.nextLine(); //  next line in the input.
     
    System.out.println("\nEnter all ingredients (separated by commas): ");
     
    List<String> ingredients = Arrays.asList(sc.nextLine().split(","));
     
     
    System.out.println("\nEnter total calories of the recipe: ");
     
    int totalCalories = sc.nextInt(); //user input for total calories
     
    sc.nextLine(); //  next line in the input.
     
    // create new recipe object and return it.
     
    return new Recipe1(recipeName, numServings, ingredients, totalCalories);
     
    }
     
    }

    and this for a different test file

     
    package steppingstonesTesting;
     
    /*
     * 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.
     */
    import  java.util.Arrays;
     
    public class RecipeTest {
     
    public static void main(String[] args) {
     
    // create one hardcoded recipe.
     
    String[] ingredients = {"Rice", "Chicken", "Chilli", "Onion", "Tomato", "Masala", "Ginger", "Garlic"};
     
    Recipe1 masalachickenRecipe = new Recipe1("Chicken Masala", 2, Arrays.asList(ingredients), 1400);
     
    masalachickenRecipe.printRecipe();
     
    // create a new recipe by taking input from the user.
     
    Recipe1 createdRecipe = Recipe1.createNewRecipe();
     
    createdRecipe.printRecipe();
     
        }
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need assistance with creating CONTROL STRUCTURE for a recipe program

    Please fix the indentations of the posted code. Code without indentations is hard to read and understand.

    creating a user-defined method with control structure that calculates the cost of the recipe.
    Can you describe in more detail what the method is supposed to do?
    What class is the method supposed to be in?
    What arguments should the method take?
    What should the method return?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help For Assignment (Problem with Loop Control Structure)
    By Newja in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 22nd, 2017, 08:11 AM
  2. Program not working - assistance please
    By parkerteam in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 31st, 2014, 06:03 PM
  3. [SOLVED] Java assignment creating a game program need assistance
    By joeyzx in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 31st, 2013, 08:23 PM
  4. repetition control structure
    By trussell0371 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 28th, 2013, 09:37 AM
  5. NEED ASSISTANCE W/ JAVA PROGRAM
    By Neddrick in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 29th, 2010, 08:20 PM

Tags for this Thread