Solutions to the Try It Out Exercises in Java For Dummies, 7th Edition
by Barry Burd

Chapter 12: Using Collections and Streams (When Arrays Aren't Good Enough)

In this chapter:

Largest in an ArrayList

import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add(85); list.add(19); list.add(0); list.add(103); list.add(13); int largest = Integer.MIN_VALUE; for (Integer i : list) { if (i > largest) { largest = i; } } System.out.println(largest); } }

Insert a word into an ArrayList

import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { ArrayList<String> words = new ArrayList<>(); words.add("cat"); words.add("dog"); words.add("horse"); words.add("zebra"); Scanner keyboard = new Scanner(System.in); String newWord = keyboard.next(); int i = 0; while (i < words.size() && newWord.compareToIgnoreCase(words.get(i)) > 0) { i++; } words.add(i, newWord); for (String string : words) { System.out.println(string); } keyboard.close(); } }

Word processor with lines stored in an ArrayList

import static java.lang.System.out; import java.util.ArrayList; import java.util.Scanner; public class Editor { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); ArrayList<String> lines = new ArrayList<>(); String command = ""; while (!command.equals("q")) { out.println(); for (int i = 0; i < lines.size(); i++) { out.println(i + ": " + lines.get(i)); } out.println(); int lineNumber = 0; out.print("Command (i #, r #, d #, q): "); command = keyboard.next(); if (!command.equals("q")) { lineNumber = keyboard.nextInt(); keyboard.nextLine(); System.out.println(command + " " + lineNumber); switch (command) { case "i": out.print("Type the new line: "); while (lineNumber > lines.size()) { lines.add(""); } lines.add(lineNumber, keyboard.nextLine()); break; case "r": out.print("Type the new line: "); lines.set(lineNumber, keyboard.nextLine()); break; case "d": lines.remove(lineNumber); } } } keyboard.close(); } }

Pay bonuses to employees

import java.text.NumberFormat; import java.util.ArrayList; public class Bonuses { public static void main(String[] args) { ArrayList<Employee> employees = new ArrayList<>(); NumberFormat currency = NumberFormat.getCurrencyInstance(); fillTheList(employees); double total = employees.stream() .filter((employee) -> employee.getScore() >= 3) .map((employee) -> 100.00) .reduce(0.0, (bonus1, bonus2) -> bonus1 + bonus2); System.out.println(currency.format(total)); } static void fillTheList(ArrayList<Employee> employees) { employees.add(new Employee("Barry", 2)); employees.add(new Employee("Harriet", 3)); employees.add(new Employee("Jennie", 5)); employees.add(new Employee("Sam", 5)); } }

Average time to cook a vegetarian recipe

public enum Ingredient { HAM, BEEF, PEAS, CARROTS, APPLES } import java.util.ArrayList; public class Recipe { private String name; private ArrayList<Ingredient> ingredients; private int time; public Recipe(String name, ArrayList<Ingredient> ingredients, int time) { this.name = name; this.ingredients = ingredients; this.time = time; } public boolean isVegetarian() { return !ingredients.contains(Ingredient.HAM) && !ingredients.contains(Ingredient.BEEF); } public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList<Ingredient> getIngredients() { return ingredients; } public void setIngredients(ArrayList<Ingredient> ingredients) { this.ingredients = ingredients; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } } import java.util.ArrayList; public class Recipes { public static void main(String[] args) { ArrayList<Recipe> recipes = new ArrayList<>(); fillTheList(recipes); int total = recipes.stream() .filter((recipe) -> recipe.isVegetarian()) .map((recipe) -> recipe.getTime()) .reduce(0, (time1, time2) -> time1 + time2); int count = recipes.stream() .filter((recipe) -> recipe.isVegetarian()) .map((recipe) -> 1) .reduce(0, (value1, value2) -> value1 + value2); System.out.println(total / (double) count); } static void fillTheList(ArrayList<Recipe> recipes) { ArrayList<Ingredient> ingredients = new ArrayList<>(); ingredients.add(Ingredient.BEEF); ingredients.add(Ingredient.HAM); ingredients.add(Ingredient.APPLES); recipes.add(new Recipe("Recipe 1", ingredients, 100)); ingredients = new ArrayList<>(); ingredients.add(Ingredient.PEAS); ingredients.add(Ingredient.CARROTS); recipes.add(new Recipe("Recipe 2", ingredients, 200)); ingredients = new ArrayList<>(); ingredients.add(Ingredient.PEAS); ingredients.add(Ingredient.APPLES); recipes.add(new Recipe("Recipe 3", ingredients, 300)); } }