Spaces:
Running
Running
ArrayList<objClassName>
Browse files
Week 4: Writing classes/14B. Find the oldest dog
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In the attached program, the class Dog is defined.
|
| 2 |
+
|
| 3 |
+
See the definition of the class, and then write the test class method
|
| 4 |
+
public static String oldestDog(ArrayList<Dog> dogs)
|
| 5 |
+
|
| 6 |
+
which takes as its parameter a list of dogs. The method searches for the oldest dog's name and returns it.
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
import java.util.ArrayList;
|
| 14 |
+
import java.util.Arrays;
|
| 15 |
+
import java.util.Collections;
|
| 16 |
+
import java.util.Random;
|
| 17 |
+
|
| 18 |
+
public class Test{
|
| 19 |
+
public static void main(String[] args){
|
| 20 |
+
final Random r = new Random();
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
String[] nt = {"Fifi","Jackie","Lassie","Rin-Tin-Tin","Snoopy","Scooby-Doo"};
|
| 24 |
+
|
| 25 |
+
for (int testi = 1; testi <= 3; testi++) {
|
| 26 |
+
System.out.println("Test " + testi);
|
| 27 |
+
ArrayList<String> names = new ArrayList<String>(Arrays.asList(nt));
|
| 28 |
+
|
| 29 |
+
int year = r.nextInt(100) + 1900;
|
| 30 |
+
ArrayList<Dog> dogs = new ArrayList<>();
|
| 31 |
+
while (!names.isEmpty()) {
|
| 32 |
+
Dog k = new Dog(names.remove(r.nextInt(names.size())), year);
|
| 33 |
+
year += r.nextInt(15) + 1;
|
| 34 |
+
dogs.add(k);
|
| 35 |
+
}
|
| 36 |
+
Collections.shuffle(dogs);
|
| 37 |
+
|
| 38 |
+
System.out.println("Dogs: " + dogs);
|
| 39 |
+
System.out.println("Oldest dog: " + oldestDog(dogs));
|
| 40 |
+
System.out.println("List after method call: " + dogs);
|
| 41 |
+
System.out.println("");
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
//ADD HERE
|
| 49 |
+
public static String oldestDog(ArrayList<Dog> dogs) {
|
| 50 |
+
int oldestDogYear = 9999;
|
| 51 |
+
String oldestDogName = "";
|
| 52 |
+
for (Dog dog: dogs) {
|
| 53 |
+
// we want the smallest dog year (for the oldest dog)
|
| 54 |
+
if (dog.getBirthyear() < oldestDogYear) {
|
| 55 |
+
oldestDogYear = dog.getBirthyear();
|
| 56 |
+
oldestDogName = dog.getName();
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
return oldestDogName;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
class Dog {
|
| 75 |
+
private String name;
|
| 76 |
+
private int birthyear;
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
public Dog(String name, int birthyear) {
|
| 80 |
+
this.name = name;
|
| 81 |
+
this.birthyear = birthyear;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
public String getName() {
|
| 85 |
+
return name;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
public int getBirthyear() {
|
| 89 |
+
return birthyear;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
@Override
|
| 93 |
+
public String toString() {
|
| 94 |
+
return name + " (" + birthyear + ")";
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
Test 1
|
| 105 |
+
Dogs: [Lassie (2018), Snoopy (1995), Fifi (1993), Scooby-Doo (2013), Rin-Tin-Tin (2000), Jackie (2012)]
|
| 106 |
+
Oldest dog: Fifi
|
| 107 |
+
List after method call: [Lassie (2018), Snoopy (1995), Fifi (1993), Scooby-Doo (2013), Rin-Tin-Tin (2000), Jackie (2012)]
|
| 108 |
+
|
| 109 |
+
Test 2
|
| 110 |
+
Dogs: [Scooby-Doo (1931), Lassie (1937), Jackie (1961), Snoopy (1948), Fifi (1964), Rin-Tin-Tin (1976)]
|
| 111 |
+
Oldest dog: Scooby-Doo
|
| 112 |
+
List after method call: [Scooby-Doo (1931), Lassie (1937), Jackie (1961), Snoopy (1948), Fifi (1964), Rin-Tin-Tin (1976)]
|
| 113 |
+
|
| 114 |
+
Test 3
|
| 115 |
+
Dogs: [Scooby-Doo (1937), Fifi (1947), Snoopy (1931), Jackie (1921), Lassie (1962), Rin-Tin-Tin (1946)]
|
| 116 |
+
Oldest dog: Jackie
|
| 117 |
+
List after method call: [Scooby-Doo (1937), Fifi (1947), Snoopy (1931), Jackie (1921), Lassie (1962), Rin-Tin-Tin (1946)]
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|