/* Farm.java Dave Klick CIS 260 2016-01-27 This program displays the lyrics to a randomly generated Old McDonald song. */ public class Farm { private static final int MIN = 1; private static final int MAX = 10; public static void main(String[] args) { String[] names = { "Spot", "Fido", "Blue" }; // ***** Create a new Random number generator named rnd // ***** Create a new ArrayList named animals which holds Animal references int num = 5; if (args.length == 1) { try { int n = Integer.parseInt(args[0]); if (n >= MIN && n <= MAX) num = n; else System.out.println("Ignoring argument"); } catch (NumberFormatException e) { System.out.println("Ignoring invalid argument"); } } else if (args.length > 1) { System.out.println("Ignoring arguments"); } // ***** Add num Animal objects to the ArrayList you created. The animals // ***** must be randomly chosen to be one of the Animal subclasses. If // ***** you are creating a Dog object, then you must also choose one of // ***** the dog names (from the names array) at random. for (Animal a : animals) { String t = a.getType(); String art1 = "a"; char c = t.toLowerCase().charAt(0); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') art1 = "an"; String s = a.getSound(); String art2 = "a"; c = s.toLowerCase().charAt(0); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') art2 = "an"; System.out.printf("Old McDonald had a farm,%nE-I-E-I-O.%n%n"); System.out.printf("And on his farm he had %s %s,%nE-I-E-I-O.%n", art1, t); System.out.printf("With %1$s %2$s, %2$s here,%nand %1$s %2$s, %2$s there,%n", art2, s); System.out.printf("Here %1$s %2$s, there %1$s %2$s,%nEverywhere %1$s %2$s, %2$s,%n%n", art2, s); } System.out.printf("Old McDonald had a farm,%nE-I-E-I-O.%n%n"); } }