Spaces:
Running
Running
File size: 4,071 Bytes
d30e5a4 |
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 |
The classes 'Call' and 'Videocall' are defined in the attached programme.
See the definitions of the classes.
Then type the method
public static ArrayList<VideoCall> jamesCalls(ArrayList<VideoCall> calls)
which takes a list of video calls as its parameter.
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
one of the persons is named "James Bond"
the call has video on it.
The order of the calls must follow the order of the original list.
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
public class Test{
public static void main(String[] args){
final Random r = new Random();
r.setSeed(Long.parseLong(args[0]));
ArrayList<VideoCall> testList = new ArrayList<>();
testList.add(new VideoCall("James Bond", "Jack Nicholson", 23, true));
testList.add(new VideoCall("James Stallone", "James Bond", 53, true));
testList.add(new VideoCall("James Bond", "Andy McDuck", 11, false));
testList.add(new VideoCall("Mike Mikeson", "James Bond", 59, false));
testList.add(new VideoCall("Lisa Lisason", "Jack Nicholson", 2, true));
testList.add(new VideoCall("Mike Mikeson", "Nick Nickson", 47, true));
testList.add(new VideoCall("James Bond", "Mandy Madeup", 99, true));
testList.add(new VideoCall("Bond James", "James Bond", 143, true));
Collections.shuffle(testList, r);
System.out.println("Testlist:");
print(testList);
System.out.println("James' video calls:");
ArrayList<VideoCall> correctList = jamesCalls(testList);
print(correctList);
}
public static void print(ArrayList<VideoCall> calls) {
for (VideoCall vc : calls) {
System.out.println(vc);
}
}
//ADD
public static ArrayList<VideoCall> jamesCalls(ArrayList<VideoCall> calls) {
// initialise list
ArrayList<VideoCall> listJamesCalls= new ArrayList<>();
for (VideoCall vc: calls) {
// check1 - videocall
if (vc.isVideoCall()) {
// check2 - James Bond as a person on either side
if (vc.getPerson1().equals("James Bond") || vc.getPerson2().equals("James Bond")) {
listJamesCalls.add(vc);
}
}
}
return listJamesCalls;
}
}
class Call {
private String person1;
private String person2;
private int duration;
public Call(String person1, String person2, int duration) {
this.person1 = person1;
this.person2 = person2;
this.duration = duration;
}
public String getPerson1() {
return person1;
}
public String getPerson2() {
return person2;
}
public int getDuration() {
return duration;
}
}
class VideoCall extends Call {
private boolean isVideoCall;
public VideoCall(String person1, String person2, int duration, boolean isVideoCall) {
super(person1, person2, duration);
this.isVideoCall = isVideoCall;
}
public boolean isVideoCall() {
return isVideoCall;
}
public String toString() {
return getPerson1() + " - " + getPerson2() + ", " + getDuration() + " min., video: " +
(isVideoCall ? "on" : "off");
}
}
Testlist:
James Bond - Mandy Madeup, 99 min., video: on
James Bond - Jack Nicholson, 23 min., video: on
Lisa Lisason - Jack Nicholson, 2 min., video: on
James Stallone - James Bond, 53 min., video: on
James Bond - Andy McDuck, 11 min., video: off
Mike Mikeson - James Bond, 59 min., video: off
Bond James - James Bond, 143 min., video: on
Mike Mikeson - Nick Nickson, 47 min., video: on
James' video calls:
James Bond - Mandy Madeup, 99 min., video: on
James Bond - Jack Nicholson, 23 min., video: on
James Stallone - James Bond, 53 min., video: on
Bond James - James Bond, 143 min., video: on
|