Spaces:
Running
Running
Create 04. James' video calls
Browse files
Week 5: Class hierarchies/04. James' video calls
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The classes 'Call' and 'Videocall' are defined in the attached programme.
|
| 2 |
+
See the definitions of the classes.
|
| 3 |
+
|
| 4 |
+
Then type the method
|
| 5 |
+
public static ArrayList<VideoCall> jamesCalls(ArrayList<VideoCall> calls)
|
| 6 |
+
which takes a list of video calls as its parameter.
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
The function of the method is to create and return a new list, to which only those calls are added from the original list where
|
| 10 |
+
one of the persons is named "James Bond"
|
| 11 |
+
the call has video on it.
|
| 12 |
+
The order of the calls must follow the order of the original list.
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
import java.util.Random;
|
| 20 |
+
import java.util.ArrayList;
|
| 21 |
+
import java.util.Collections;
|
| 22 |
+
|
| 23 |
+
public class Test{
|
| 24 |
+
public static void main(String[] args){
|
| 25 |
+
final Random r = new Random();
|
| 26 |
+
r.setSeed(Long.parseLong(args[0]));
|
| 27 |
+
|
| 28 |
+
ArrayList<VideoCall> testList = new ArrayList<>();
|
| 29 |
+
testList.add(new VideoCall("James Bond", "Jack Nicholson", 23, true));
|
| 30 |
+
testList.add(new VideoCall("James Stallone", "James Bond", 53, true));
|
| 31 |
+
testList.add(new VideoCall("James Bond", "Andy McDuck", 11, false));
|
| 32 |
+
testList.add(new VideoCall("Mike Mikeson", "James Bond", 59, false));
|
| 33 |
+
testList.add(new VideoCall("Lisa Lisason", "Jack Nicholson", 2, true));
|
| 34 |
+
testList.add(new VideoCall("Mike Mikeson", "Nick Nickson", 47, true));
|
| 35 |
+
testList.add(new VideoCall("James Bond", "Mandy Madeup", 99, true));
|
| 36 |
+
testList.add(new VideoCall("Bond James", "James Bond", 143, true));
|
| 37 |
+
|
| 38 |
+
Collections.shuffle(testList, r);
|
| 39 |
+
|
| 40 |
+
System.out.println("Testlist:");
|
| 41 |
+
print(testList);
|
| 42 |
+
|
| 43 |
+
System.out.println("James' video calls:");
|
| 44 |
+
ArrayList<VideoCall> correctList = jamesCalls(testList);
|
| 45 |
+
print(correctList);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
public static void print(ArrayList<VideoCall> calls) {
|
| 49 |
+
for (VideoCall vc : calls) {
|
| 50 |
+
System.out.println(vc);
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
//ADD
|
| 57 |
+
public static ArrayList<VideoCall> jamesCalls(ArrayList<VideoCall> calls) {
|
| 58 |
+
// initialise list
|
| 59 |
+
ArrayList<VideoCall> listJamesCalls= new ArrayList<>();
|
| 60 |
+
|
| 61 |
+
for (VideoCall vc: calls) {
|
| 62 |
+
// check1 - videocall
|
| 63 |
+
if (vc.isVideoCall()) {
|
| 64 |
+
// check2 - James Bond as a person on either side
|
| 65 |
+
if (vc.getPerson1().equals("James Bond") || vc.getPerson2().equals("James Bond")) {
|
| 66 |
+
listJamesCalls.add(vc);
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
return listJamesCalls;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
class Call {
|
| 84 |
+
private String person1;
|
| 85 |
+
private String person2;
|
| 86 |
+
private int duration;
|
| 87 |
+
|
| 88 |
+
public Call(String person1, String person2, int duration) {
|
| 89 |
+
this.person1 = person1;
|
| 90 |
+
this.person2 = person2;
|
| 91 |
+
this.duration = duration;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
public String getPerson1() {
|
| 95 |
+
return person1;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
public String getPerson2() {
|
| 99 |
+
return person2;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
public int getDuration() {
|
| 103 |
+
return duration;
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
class VideoCall extends Call {
|
| 108 |
+
private boolean isVideoCall;
|
| 109 |
+
|
| 110 |
+
public VideoCall(String person1, String person2, int duration, boolean isVideoCall) {
|
| 111 |
+
super(person1, person2, duration);
|
| 112 |
+
this.isVideoCall = isVideoCall;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
public boolean isVideoCall() {
|
| 116 |
+
return isVideoCall;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
public String toString() {
|
| 120 |
+
return getPerson1() + " - " + getPerson2() + ", " + getDuration() + " min., video: " +
|
| 121 |
+
(isVideoCall ? "on" : "off");
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
Testlist:
|
| 131 |
+
James Bond - Mandy Madeup, 99 min., video: on
|
| 132 |
+
James Bond - Jack Nicholson, 23 min., video: on
|
| 133 |
+
Lisa Lisason - Jack Nicholson, 2 min., video: on
|
| 134 |
+
James Stallone - James Bond, 53 min., video: on
|
| 135 |
+
James Bond - Andy McDuck, 11 min., video: off
|
| 136 |
+
Mike Mikeson - James Bond, 59 min., video: off
|
| 137 |
+
Bond James - James Bond, 143 min., video: on
|
| 138 |
+
Mike Mikeson - Nick Nickson, 47 min., video: on
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
James' video calls:
|
| 142 |
+
James Bond - Mandy Madeup, 99 min., video: on
|
| 143 |
+
James Bond - Jack Nicholson, 23 min., video: on
|
| 144 |
+
James Stallone - James Bond, 53 min., video: on
|
| 145 |
+
Bond James - James Bond, 143 min., video: on
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|