Spaces:
Running
Running
| In the program, a class named 'FairyTaleCharacter' and several other classes inheriting from it have been defined. | |
| Write a method: | |
| public static FairyTaleCharacter oldestCharacter(ArrayList<FairyTaleCharacter> characters) | |
| which takes a 'list of descendants' of the fairy tale character as a 'parameter'. | |
| The method finds and returns the oldest character in the list. | |
| Note that the classes do not currently implement the Comparable interface, so the oldest character must be found in some other way. | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.Random; | |
| public class Test{ | |
| public static void main(String[] args){ | |
| final Random r = new Random(); | |
| int age = r.nextInt(5) + 5; | |
| ArrayList<Integer> ages = new ArrayList<>(); | |
| for (int i=0; i<20; i++) { | |
| ages.add(age); | |
| age += r.nextInt(10) + 1; | |
| } | |
| ArrayList<FairyTaleCharacter> characters = new ArrayList<>(); | |
| String[] colors = "blue yellow red black brown white green".split(" "); | |
| for (String name : new String[] {"Buddy", "Bunny", "Rudy"}) { | |
| String color = colors[r.nextInt(colors.length)]; | |
| characters.add(new Nephew(name, ages.remove(r.nextInt(ages.size())), color)); | |
| } | |
| String[] wands = "oak licorice birch alder walnut plastic".split(" "); | |
| for (String name : new String[] {"Harry", "Hermione", "Ron"}) { | |
| String wand = wands[r.nextInt(wands.length)]; | |
| characters.add(new Wizard(name, ages.remove(r.nextInt(ages.size())), wand)); | |
| } | |
| String[] superpowers = "supervision flight superstrength literacy super sneeze".split(" "); | |
| for (String name : new String[] {"Batguy", "Mouseman", "Bulk"}) { | |
| String power = superpowers[r.nextInt(superpowers.length)]; | |
| characters.add(new Superhero(name, ages.remove(r.nextInt(ages.size())), power)); | |
| } | |
| System.out.println("All fairy tale characters: "); | |
| characters.stream().forEach(c -> System.out.println("" + c)); | |
| System.out.println("Oldest: " + oldestCharacter(characters)); | |
| } | |
| //add | |
| public static FairyTaleCharacter oldestCharacter(ArrayList<FairyTaleCharacter> characters) { | |
| // INITIALISE OLDEST AS 1ST CHARACTER | |
| FairyTaleCharacter oldest = characters.get(0); | |
| for (FairyTaleCharacter character : characters) { | |
| if (character.getAge() > oldest.getAge()) { | |
| oldest = character; | |
| } | |
| } | |
| return oldest; | |
| } | |
| } | |
| class FairyTaleCharacter { | |
| private String name; | |
| private int age; | |
| public FairyTaleCharacter(String name, int age) { | |
| this.name = name; | |
| this.age = age; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| public String toString() { | |
| return name + ", " + age + " years old"; | |
| } | |
| } | |
| class Nephew extends FairyTaleCharacter { | |
| private String hatColor; | |
| public Nephew(String name, int age, String hatColor) { | |
| super(name, age); | |
| this.hatColor = hatColor; | |
| } | |
| public String toString() { | |
| return super.toString() + ", " + hatColor + " hat"; | |
| } | |
| } | |
| class Wizard extends FairyTaleCharacter { | |
| private String wand; | |
| public Wizard(String name, int age, String wand) { | |
| super(name, age); | |
| this.wand = wand; | |
| } | |
| public String toString() { | |
| return super.toString() + ", " + wand + " wand"; | |
| } | |
| } | |
| class Superhero extends FairyTaleCharacter { | |
| private String superpower; | |
| public Superhero(String name, int age, String superpower) { | |
| super(name, age); | |
| this.superpower = superpower; | |
| } | |
| public String toString() { | |
| return super.toString() + ", superpower: " + superpower; | |
| } | |
| } | |
| All fairy tale characters: | |
| Buddy, 46 years old, black hat | |
| Bunny, 69 years old, yellow hat | |
| Rudy, 111 years old, yellow hat | |
| Harry, 65 years old, birch wand | |
| Hermione, 11 years old, birch wand | |
| Ron, 71 years old, birch wand | |
| Batguy, 88 years old, superpower: flight | |
| Mouseman, 55 years old, superpower: superstrength | |
| Bulk, 6 years old, superpower: literacy | |
| Oldest: Rudy, 111 years old, yellow hat | |