Spaces:
Running
Running
| In the program, the class Recipe is defined. | |
| Define the following attributes for the class: | |
| String type: dish | |
| Integer types: cookingtime and portions | |
| String-type list: ingredients | |
| import java.util.Random; | |
| import java.util.ArrayList; | |
| public class Test{ | |
| public static void main(String[] args){ | |
| System.out.println("Testing forming objects..."); | |
| ArrayList<String> ai1 = new ArrayList<>(); | |
| ArrayList<String> ai2 = new ArrayList<>(); | |
| ai1.add("water"); | |
| ai1.add("salt"); | |
| ai1.add("oats"); | |
| ai2.add("tomatoes"); | |
| ai2.add("cucumber"); | |
| ai2.add("salad"); | |
| Recipe porridge = new Recipe("Porridge", 15, 2, ai1); | |
| Recipe salad = new Recipe("Salad", 5, 1, ai2); | |
| System.out.println(porridge); | |
| System.out.println(salad); | |
| } | |
| } | |
| class Recipe { | |
| //ADD ATTRIBUTES | |
| String dish; | |
| int cookingtime; | |
| int portions; | |
| ArrayList<String> ingredients; | |
| //CONSTRUCTOR | |
| public Recipe(String d, int ct, int p, ArrayList<String> ing) { | |
| this.dish = d; | |
| this.cookingtime = ct; | |
| this.portions = p; | |
| this.ingredients = ing; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Recipe [dish=" + dish + ", cooking time=" + cookingtime + ", portions=" + portions | |
| + ", ingredients=" + ingredients + "]"; | |
| } | |
| } | |
| Testing forming objects... | |
| Recipe [dish=Porridge, cooking time=15, portions=2, ingredients=[water, salt, oats]] | |
| Recipe [dish=Salad, cooking time=5, portions=1, ingredients=[tomatoes, cucumber, salad]] | |