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

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

In this chapter:

Our Best Players

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class BestPlayers { public static void main(String[] args) throws FileNotFoundException { ArrayList<Player> players = new ArrayList<>(); var hankeesData = new Scanner(new File("Hankees.txt")); Player player; for (int num = 0; num < 9; num++) { player = new Player(hankeesData.nextLine(), hankeesData.nextDouble()); hankeesData.nextLine(); players.add(player); } for (int num = 0; num < 9; num++) { player = players.get(num); if (player.getAverage() >= 0.100) { System.out.println(player.getName() + " " + player.getAverageString()); } } } }

Our Biggest Number

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 in Order

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(); } }

Be Generous

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)); } }

Save Time

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)); } }

Record Sales

record Sale (String item, double price) {} import java.text.NumberFormat; import java.util.ArrayList; public class TallySalesAgain { public static void main(String[] args) { ArrayList<Sale> sales = new ArrayList<>(); NumberFormat currency = NumberFormat.getCurrencyInstance(); fillTheList(sales); double total = sales.stream() .filter((sale) -> sale.item().equals("DVD")) .map((sale) -> sale.price()) .reduce(0.0, (price1, price2) -> price1 + price2); System.out.println(currency.format(total)); } static void fillTheList(ArrayList<Sale> sales) { sales.add(new Sale("DVD", 15.00)); sales.add(new Sale("Book", 12.00)); sales.add(new Sale("DVD", 21.00)); sales.add(new Sale("CD", 5.25)); } }