Spaces:
Running
Running
| 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 | |