Objects with ArrayLists as Attributes CS102 Written by Robert Carver Edited by Dean Zeller
Objects can have attributes of almost any datatype Objects can have attributes of almost any datatype. This includes Arrays and ArrayLists. When an object has a list as an attribute, the contents of that list can only be accessed through object methods. Objects and Lists
Example of an Object with a List as an Attribute: 1 import java.util.*; 2 public class RecipeTest 3 { 4 public static void main(String[] args) 5 { 6 // Instantiate the list. Then fill the list with values. 7 List<String> ingredients = new ArrayList<String>(); 8 ingredients.add("Eggs"); 9 ingredients.add("Flower"); 10 ingredients.add("Sugar"); 11 12 Recipe cake = new Recipe(ingredients); 13 14 cake.addIngredient("Sprinkles"); 15 cake.removeIngredient("Flower"); 16 cake.searchIngredient("Beef"); 17 System.out.println(cake); 18 } 19 } Output: Flower has been removed from the list. Beef is not in the ingredient list. Ingredients: Eggs Sugar Sprinkles
Object code: Breakdown on following slides 1 import java.util.*; 2 public class Recipe 3 { 4 // Variables and constructor 5 private List<String> ingredients; 6 // No parameter constructor 7 public Recipe() 8 { 9 this.ingredients = new ArrayList<String>(); 10 } 11 // Constructor with parameter 12 public Recipe(List<String> ingredients_) 13 { 14 for(int i = 0; i < ingredients_.size(); i++) 15 ingredients.add(ingredients_.get(i)); 17 } 18 // Add item to list 19 public void addIngredient(String ingredient_) 20 { 21 ingredients.add(ingredient_); 22 } 23 // Remove item from list 24 public Boolean removeIngredient(String toRemove_) 25 { 26 if(ingredients.contains(toRemove_)) 27 { 28 ingredients.remove(ingredients.indexOf(toRemove_)); 29 return true; 30 } 31 else return false; 32 } 33 // search for item within list 34 public Boolean searchIngredient(String target_) 35 { 36 if(ingredients.contains(target_)) 37 { 38 return true; 39 } 40 else return false; 41 } 42 // toString method for food object 43 public String toString() 44 { 45 String result="Ingredients: \n"; 46 for(int i = 0; i < ingredients.size(); i++) 47 result+=(ingredients.get(i)+"\n"); 48 return result; 49 } 50 } Object code: Breakdown on following slides
Constructors Constructors for objects can assign all, some, or none of the attributes of an object. The objects attributes must be assigned before they are accessed otherwise Java will run into an error. Constructor with no parameters 7 public Recipe() 8 { 9 this.ingredients = new ArrayList<String>(); 10 } 12 public Recipe(List<String> ingredients_) 13 { 14 for(int i = 0; i < ingredients_.size(); i++) 15 ingredients.add(ingredients_.get(i)); 17 } Constructor with parameters
Code breakdown - constructors 1 import java.util.*; 2 public class Recipe 3 { 4 // Variables and constructor 5 private List<String> ingredients; 6 // No parameter constructor 7 public Recipe() 8 { 9 this.ingredients = new ArrayList<String>(); 10 } 11 // Constructor with parameter 12 public Recipe(List<String> ingredients_) 13 { 14 for(int i = 0; i < ingredients_.size(); i++) 15 ingredients.add(ingredients_.get(i)); 17 } We initialize the class with a single attribute: a ArrayList of strings called ingredients. We then create the constructor that accepts a List<String> parameter to be passed into the object. And also another constructor that includes no parameters.
Code breakdown – add method // Add item to list 18 public void addIngredient(String ingredient_) 19 { 20 ingredients.add(ingredient_); 21 } We create a public void method called addIngredient. We pass a String parameter into this method. The method then adds the String into the ingredients list.
Code breakdown – remove method // Remove item from list 23 public Boolean removeIngredient(String toRemove_) 24 { 25 if(ingredients.contains(toRemove_)) 26 { 27 ingredients.remove(ingredients.indexOf(toRemove_)); 28 return true; 29 } 30 else return false; 31 } We create another public Boolean method called removeIngredients. We pass a String parameter into this method. The method first calls the .contains method of the ArrayList class on the String to check if the ingredients list contains the toRemove_ String. If the list contains the String the method removes the String using the .remove and .indexOf methods and returns true. If the list does not contain the String, the method returns false.
Code breakdown – search method // search for item within list 33 public Boolean searchIngredient(String target_) 34 { 35 if(ingredients.contains(target_)) 36 { 37 return true; 38 } 39 else return false; 40 } We create another public Boolean method called searchIngredients. We pass a String parameter, target_ into this method. The method does a .contains test, if the ingredients list contains target_ then return true. If the ingredients list does not contain target_ then return false.
Code breakdown – toString method // toString method for food object 42 public String toString() 43 { 44 String result="Ingredients: \n"; 45 for(int i = 0; i < ingredients.size(); i++) 46 result+=(ingredients.get(i)+"\n"); 47 return result; 48 } We create a public String method toString that we pass no parameters into. The method creates a String and then iterates through the ingredients list using a For loop, appending each item in the list to the String. Example Output: Ingredients: Eggs Sugar Sprinkles