Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package code; /** * You are given a partial definition of a Recipe class. Your t

ID: 3597316 • Letter: P

Question

package code;

/**

* You are given a partial definition of a Recipe class. Your task is to complete this class by

* writing a constructor and 3 getter methods for the 3 instance variables

*/

public class Recipe {

// Do not edit these variables

private String name;

private String ingredientOne;

private String ingredientTwo;

// 15 Competency Points

// Write a public constructor for the Recipe class that takes 3 Strings as parameters. In the constructor, set

// the first parameter to the name instance variable, the second to ingredientOne, and the third to

// ingredientTwo

public Recipe(String a, String b, String c){

   this. name = a;

this. ingredientOne = b;

   this. ingredientTwo = c;

}

// 15 Competency Points

// Write a public method named getName that doesn't take any parameters and has return type String. This

// method should return the value of the name instance variable

public String getName(){

   return name;

}

// 15 Competency Points

// Write a public method named getIngredientOne that doesn't take any parameters and has return type String. This

// method should return the value of the ingredientOne instance variable

public String getIngredientOne(){

   return ingredientOne;

}

  

// 15 Competency Points

// Write a public method named getIngredientTwo that doesn't take any parameters and has return type String. This

// method should return the value of the ingredientTwo instance variable

public String getIngredientTwo(){

   return ingredientTwo;

}

}

package code;

import java.util.ArrayList;

/**

* You are given an empty Kitchen class that will be written entirely by you. You will write an instance

* variable, a constructor, and several methods in this class.

*

* It is recommended that you start with the Recipe class.

*/

public class Kitchen {

  

  

private static final String String = null;

  

   // 15 Competency Points

// Create a private instance variable with type ArrayList of String named ingredients

private ArrayList<String> ingredients;

// 15 Competency Points

// Write a public constructor that doesn't take any parameters and initializes the ingredients variable

// to a new ArrayList

public void donttake(){

   ArrayList<String> map = new ArrayList<String>() ;

      

  

}

  

// 15 Competency Points

// Write a public method named addIngredient that takes one String as a parameter and has return type void.

// This method will add the input String to the ingredients ArrayList

public void addIngredients(String a){

   this.ingredients.add(a);

}

  

// 15 Competency Points

// Write a public method named removeIngredient that takes one String as a parameter and has return type void.

// This method will remove one instance of the input String from the ingredients ArrayList.

// If the input is not in the ArrayList this method will have no effect

// Hint: The ArrayList class has a remove method

  

// 15 Competency Points

// Write a public method named containsIngredient that takes one String as a parameter and has return type boolean.

// This method will return true if the input String is in the ingredients ArrayList and false if it is not

   public boolean containsIngredients(String a){

       if(this.ingredients.contains(ingredients)){

           return true;

          

       }

       return false;

   }

  

// 15 Competency Points

// Write a public method named getQuantity that doesn't take any parameters and has return type int.

// This method will return the total number of ingredients in this kitchen

public int getQuantity(){

   return this.ingredients.size();

}

  

  

// 1 Proficiency Point (Due 24 hours after the end of lab)

// Write a public method named canCook that takes a Recipe as a parameter and has return type boolean.

// This method will true if this Kitchen contains both ingredients in the recipe, false otherwise

  

// 1 Proficiency Point (Due 24 hours after the end of lab)

// Write a public method named cook that takes a Recipe as a parameter and has return type void.

// If the recipe can be cooked, this method will remove one of each of the ingredients in the recipe.

// If the recipe can not be cooked, this method will have no effect.

  

}

Explanation / Answer

public class Recipe {
// Do not edit these variables
private String name;
private String ingredientOne;
private String ingredientTwo;

// 15 Competency Points
// Write a public constructor for the Recipe class that takes 3 Strings as
// parameters. In the constructor, set
// the first parameter to the name instance variable, the second to
// ingredientOne, and the third to
// ingredientTwo
public Recipe(String a, String b, String c) {
this.name = a;
this.ingredientOne = b;
this.ingredientTwo = c;
}

// 15 Competency Points
// Write a public method named getName that doesn't take any parameters and
// has return type String. This
// method should return the value of the name instance variable
public String getName() {
return name;
}

// 15 Competency Points
// Write a public method named getIngredientOne that doesn't take any
// parameters and has return type String. This
// method should return the value of the ingredientOne instance variable
public String getIngredientOne() {
return ingredientOne;

}

// 15 Competency Points
// Write a public method named getIngredientTwo that doesn't take any
// parameters and has return type String. This
// method should return the value of the ingredientTwo instance variable
public String getIngredientTwo() {
return ingredientTwo;

}
}

________________

Kitchen.java

import java.util.ArrayList;

/**
* You are given an empty Kitchen class that will be written entirely by you. You will write an instance
* variable, a constructor, and several methods in this class.
*
* It is recommended that you start with the Recipe class.
*/
public class Kitchen {


// private static final String String = null;

// 15 Competency Points
// Create a private instance variable with type ArrayList of String named ingredients
private ArrayList < String > ingredients;


// 15 Competency Points
// Write a public constructor that doesn't take any parameters and initializes the ingredients variable
// to a new ArrayList
public Kitchen() {

ingredients = new ArrayList < String > ();


}

// 15 Competency Points
// Write a public method named addIngredient that takes one String as a parameter and has return type void.
// This method will add the input String to the ingredients ArrayList
public void addIngredients(String a) {
ingredients.add(a);
}

// 15 Competency Points
// Write a public method named removeIngredient that takes one String as a parameter and has return type void.
// This method will remove one instance of the input String from the ingredients ArrayList.
// If the input is not in the ArrayList this method will have no effect
// Hint: The ArrayList class has a remove method
public void removeIngredient(String a) {
for (int i = 0; i < ingredients.size(); i++) {
if (ingredients.get(i).equals(a)) {
ingredients.remove(i);
}
}
}

// 15 Competency Points
// Write a public method named containsIngredient that takes one String as a parameter and has return type boolean.
// This method will return true if the input String is in the ingredients ArrayList and false if it is not
public boolean containsIngredients(String a) {


if (ingredients.contains(a))
return true;

return false;
}

// 15 Competency Points
// Write a public method named getQuantity that doesn't take any parameters and has return type int.
// This method will return the total number of ingredients in this kitchen
public int getQuantity() {
return ingredients.size();
}

// 1 Proficiency Point (Due 24 hours after the end of lab)
// Write a public method named canCook that takes a Recipe as a parameter and has return type boolean.
// This method will true if this Kitchen contains both ingredients in the recipe, false otherwise
public boolean canCook(Recipe r) {
for (int i = 0; i < ingredients.size(); i++) {
if (ingredients.contains(r.getIngredientOne()) && ingredients.contains(r.getIngredientTwo()))
return true;

}
return false;
}

// 1 Proficiency Point (Due 24 hours after the end of lab)
// Write a public method named cook that takes a Recipe as a parameter and has return type void.
// If the recipe can be cooked, this method will remove one of each of the ingredients in the recipe.
// If the recipe can not be cooked, this method will have no effect.
public void cook(Recipe r) {
if (canCook(r)) {
ingredients.remove(r.getIngredientOne());
}
}

}

__________________

Test.java

public class Test {

public static void main(String[] args) {
Kitchen k = new Kitchen();

k.addIngredients("Red chillies");
k.addIngredients("Garlic Cloves");
k.addIngredients("Salt");
k.addIngredients("Sugar");
k.addIngredients("Water");
k.addIngredients("Vinegar");
k.addIngredients("Coriander Powder");


System.out.println("No of Ingredients in the Kitchen :" + k.getQuantity());
boolean bool = k.containsIngredients("Red chillies");
if (bool) {
System.out.println("Kitchen Contains Red Chillies");
} else {
System.out.println("Kitchen does not Contains Red Chillies");
}
Recipe r = new Recipe("Chilly Chicken", "Red chillies", "Salt");


if (k.containsIngredients("Sugar")) {
k.removeIngredient("Sugar");
System.out.println("Sugar Removed from Kitchen");
} else {
System.out.println("Sugar is not in Kitchen");
}

Recipe r1 = new Recipe("Gobi Manchurian", "Red chillies", "Salt");
if (k.canCook(r1)) {
System.out.println("Kitchen contains both ingredients to do the reciepe " + r.getName());
} else {
System.out.println("Kitchen not contains required ingredients to do the reciepe " + r.getName());
}


}

}

____________________

Output:

No of Ingredients in the Kitchen :7
Kitchen Contains Red Chillies
Sugar Removed from Kitchen
Kitchen contains both ingredients to do the reciepe Chilly Chicken