Spaces:
Running
Running
File size: 4,357 Bytes
8afb4b9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
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;
}
@Override
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;
}
@Override
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;
}
@Override
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;
}
@Override
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
|