Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects with ArrayLists as Attributes

Similar presentations


Presentation on theme: "Objects with ArrayLists as Attributes"— Presentation transcript:

1 Objects with ArrayLists as Attributes
CS102 Written by Robert Carver Edited by Dean Zeller

2 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

3 Example of an Object with a List as an Attribute:
1 import java.util.*; 2 public class RecipeTest 3 { public static void main(String[] args) { // Instantiate the list. Then fill the list with values List<String> ingredients = new ArrayList<String>(); ingredients.add("Eggs"); ingredients.add("Flower"); ingredients.add("Sugar"); Recipe cake = new Recipe(ingredients); cake.addIngredient("Sprinkles"); cake.removeIngredient("Flower"); cake.searchIngredient("Beef"); System.out.println(cake); } 19 } Output: Flower has been removed from the list. Beef is not in the ingredient list. Ingredients: Eggs Sugar Sprinkles

4 Object code: Breakdown on following slides
1 import java.util.*; 2 public class Recipe { // Variables and constructor private List<String> ingredients; // No parameter constructor public Recipe() { this.ingredients = new ArrayList<String>(); } // Constructor with parameter public Recipe(List<String> ingredients_) { for(int i = 0; i < ingredients_.size(); i++) ingredients.add(ingredients_.get(i)); } // Add item to list public void addIngredient(String ingredient_) { ingredients.add(ingredient_); } // Remove item from list public Boolean removeIngredient(String toRemove_) { if(ingredients.contains(toRemove_)) { ingredients.remove(ingredients.indexOf(toRemove_)); return true; } else return false; } // search for item within list public Boolean searchIngredient(String target_) { if(ingredients.contains(target_)) { return true; } else return false; } // toString method for food object public String toString() { String result="Ingredients: \n"; for(int i = 0; i < ingredients.size(); i++) result+=(ingredients.get(i)+"\n"); return result; } 50 } Object code: Breakdown on following slides

5 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 public Recipe() { this.ingredients = new ArrayList<String>(); } public Recipe(List<String> ingredients_) { for(int i = 0; i < ingredients_.size(); i++) ingredients.add(ingredients_.get(i)); } Constructor with parameters

6 Code breakdown - constructors
1 import java.util.*; 2 public class Recipe { // Variables and constructor private List<String> ingredients; // No parameter constructor public Recipe() { this.ingredients = new ArrayList<String>(); } // Constructor with parameter public Recipe(List<String> ingredients_) { for(int i = 0; i < ingredients_.size(); i++) ingredients.add(ingredients_.get(i)); } 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.

7 Code breakdown – add method
// Add item to list public void addIngredient(String ingredient_) { ingredients.add(ingredient_); } 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.

8 Code breakdown – remove method
// Remove item from list public Boolean removeIngredient(String toRemove_) { if(ingredients.contains(toRemove_)) { ingredients.remove(ingredients.indexOf(toRemove_)); return true; } else return false; } 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.

9 Code breakdown – search method
// search for item within list public Boolean searchIngredient(String target_) { if(ingredients.contains(target_)) { return true; } else return false; } 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.

10 Code breakdown – toString method
// toString method for food object public String toString() { String result="Ingredients: \n"; for(int i = 0; i < ingredients.size(); i++) result+=(ingredients.get(i)+"\n"); return result; } 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


Download ppt "Objects with ArrayLists as Attributes"

Similar presentations


Ads by Google