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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In the program, a class named 'FairyTaleCharacter' and several other classes inheriting from it have been defined.
|
| 2 |
+
|
| 3 |
+
Write a method:
|
| 4 |
+
public static FairyTaleCharacter oldestCharacter(ArrayList<FairyTaleCharacter> characters)
|
| 5 |
+
which takes a 'list of descendants' of the fairy tale character as a 'parameter'.
|
| 6 |
+
The method finds and returns the oldest character in the list.
|
| 7 |
+
|
| 8 |
+
Note that the classes do not currently implement the Comparable interface, so the oldest character must be found in some other way.
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
import java.util.ArrayList;
|
| 17 |
+
import java.util.Collections;
|
| 18 |
+
import java.util.Random;
|
| 19 |
+
|
| 20 |
+
public class Test{
|
| 21 |
+
public static void main(String[] args){
|
| 22 |
+
final Random r = new Random();
|
| 23 |
+
|
| 24 |
+
int age = r.nextInt(5) + 5;
|
| 25 |
+
ArrayList<Integer> ages = new ArrayList<>();
|
| 26 |
+
for (int i=0; i<20; i++) {
|
| 27 |
+
ages.add(age);
|
| 28 |
+
age += r.nextInt(10) + 1;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
ArrayList<FairyTaleCharacter> characters = new ArrayList<>();
|
| 32 |
+
String[] colors = "blue yellow red black brown white green".split(" ");
|
| 33 |
+
|
| 34 |
+
for (String name : new String[] {"Buddy", "Bunny", "Rudy"}) {
|
| 35 |
+
String color = colors[r.nextInt(colors.length)];
|
| 36 |
+
characters.add(new Nephew(name, ages.remove(r.nextInt(ages.size())), color));
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
String[] wands = "oak licorice birch alder walnut plastic".split(" ");
|
| 40 |
+
for (String name : new String[] {"Harry", "Hermione", "Ron"}) {
|
| 41 |
+
String wand = wands[r.nextInt(wands.length)];
|
| 42 |
+
characters.add(new Wizard(name, ages.remove(r.nextInt(ages.size())), wand));
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
String[] superpowers = "supervision flight superstrength literacy super sneeze".split(" ");
|
| 46 |
+
for (String name : new String[] {"Batguy", "Mouseman", "Bulk"}) {
|
| 47 |
+
String power = superpowers[r.nextInt(superpowers.length)];
|
| 48 |
+
characters.add(new Superhero(name, ages.remove(r.nextInt(ages.size())), power));
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
System.out.println("All fairy tale characters: ");
|
| 52 |
+
characters.stream().forEach(c -> System.out.println("" + c));
|
| 53 |
+
System.out.println("Oldest: " + oldestCharacter(characters));
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
//add
|
| 60 |
+
public static FairyTaleCharacter oldestCharacter(ArrayList<FairyTaleCharacter> characters) {
|
| 61 |
+
|
| 62 |
+
// INITIALISE OLDEST AS 1ST CHARACTER
|
| 63 |
+
FairyTaleCharacter oldest = characters.get(0);
|
| 64 |
+
|
| 65 |
+
for (FairyTaleCharacter character : characters) {
|
| 66 |
+
if (character.getAge() > oldest.getAge()) {
|
| 67 |
+
oldest = character;
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
return oldest;
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
class FairyTaleCharacter {
|
| 92 |
+
private String name;
|
| 93 |
+
private int age;
|
| 94 |
+
|
| 95 |
+
public FairyTaleCharacter(String name, int age) {
|
| 96 |
+
this.name = name;
|
| 97 |
+
this.age = age;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
public String getName() {
|
| 101 |
+
return name;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
public int getAge() {
|
| 105 |
+
return age;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
@Override
|
| 109 |
+
public String toString() {
|
| 110 |
+
return name + ", " + age + " years old";
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
class Nephew extends FairyTaleCharacter {
|
| 115 |
+
private String hatColor;
|
| 116 |
+
|
| 117 |
+
public Nephew(String name, int age, String hatColor) {
|
| 118 |
+
super(name, age);
|
| 119 |
+
this.hatColor = hatColor;
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
@Override
|
| 123 |
+
public String toString() {
|
| 124 |
+
return super.toString() + ", " + hatColor + " hat";
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
class Wizard extends FairyTaleCharacter {
|
| 129 |
+
private String wand;
|
| 130 |
+
|
| 131 |
+
public Wizard(String name, int age, String wand) {
|
| 132 |
+
super(name, age);
|
| 133 |
+
this.wand = wand;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
@Override
|
| 137 |
+
public String toString() {
|
| 138 |
+
return super.toString() + ", " + wand + " wand";
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
class Superhero extends FairyTaleCharacter {
|
| 143 |
+
private String superpower;
|
| 144 |
+
|
| 145 |
+
public Superhero(String name, int age, String superpower) {
|
| 146 |
+
super(name, age);
|
| 147 |
+
this.superpower = superpower;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
@Override
|
| 151 |
+
public String toString() {
|
| 152 |
+
return super.toString() + ", superpower: " + superpower;
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
All fairy tale characters:
|
| 164 |
+
Buddy, 46 years old, black hat
|
| 165 |
+
Bunny, 69 years old, yellow hat
|
| 166 |
+
Rudy, 111 years old, yellow hat
|
| 167 |
+
Harry, 65 years old, birch wand
|
| 168 |
+
Hermione, 11 years old, birch wand
|
| 169 |
+
Ron, 71 years old, birch wand
|
| 170 |
+
Batguy, 88 years old, superpower: flight
|
| 171 |
+
Mouseman, 55 years old, superpower: superstrength
|
| 172 |
+
Bulk, 6 years old, superpower: literacy
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
Oldest: Rudy, 111 years old, yellow hat
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
|