Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Similar documents
Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

For example, here is a code snippet that supplies a legal data set to the constructor:

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

1 Shyam sir JAVA Notes

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Abstract Classes and Interfaces

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Language Features. 1. The primitive types int, double, and boolean are part of the AP

CO Java SE 8: Fundamentals

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS506 Web Design & Development Final Term Solved MCQs with Reference

ENCAPSULATION AND POLYMORPHISM

COMP 250 Winter 2011 Reading: Java background January 5, 2011

This page intentionally left blank

VALLIAMMAI ENGINEERING COLLEGE

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

Computer Science II (20073) Week 1: Review and Inheritance

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Introduction to Computing II (ITI 1121) Final Examination

Introduction to Programming Using Java (98-388)

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Fall 2017 CISC124 10/1/2017

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

COP 3330 Final Exam Review

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

Fall 2017 CISC124 9/16/2017

Objects and Iterators

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Java Magistère BFA

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Java SE 8 Programming

CMSC 132: Object-Oriented Programming II

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Data Types. Lecture2: Java Basics. Wrapper Class. Primitive data types. Bohyung Han CSE, POSTECH

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Murach s Beginning Java with Eclipse

Chapter 1 Getting Started

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Distributed Systems Recitation 1. Tamim Jabban

CSCI-142 Exam 2 Review September 25, 2016 Presented by the RIT Computer Science Community

Chapter 6 Introduction to Defining Classes

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Java SE 8 Programming

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Chapter 4 Defining Classes I

Implements vs. Extends When Defining a Class

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Introduction to Computing II (ITI 1121) Final Examination

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

CS 251 Intermediate Programming Inheritance

Index COPYRIGHTED MATERIAL

This exam is open book. Each question is worth 3 points.

CMSC 331 Second Midterm Exam

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

CMSC 341. Nilanjan Banerjee

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

UMBC CMSC 331 Final Exam

Rules and syntax for inheritance. The boring stuff

Midterm Exam CS 251, Intermediate Programming March 6, 2015

This page intentionally left blank

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

Class, Variable, Constructor, Object, Method Questions

TeenCoder : Java Programming (ISBN )

Operators and Expressions

CMSC 331 Second Midterm Exam

Transcription:

QUEEN'S UNIVERSIY SCHOOL O COMPUING HAND IN Answers Are Recorded on Exam Paper CMPE212, WINER ERM, 2016 INAL EXAMINAION 9am to 12pm, 19 APRIL 2016 Instructor: Alan McLeod If the instructor is unavailable in the examination room and if doubt exists as to the interpretation of any problem, the candidate is urged to submit with the answer paper a clear statement of any assumptions made. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. Please write your answers in the boxes provided. Extra space is available on the last page of the exam. he back of any page can be used for rough work. his exam is three hours long and refers exclusively to the use of the Java language. Comments are not required in the code you write. or full marks, code must be efficient as well as correct. his is a closed book exam. No computers or calculators are allowed. Student Number: SOLUION Problem 1: / 40 Problem 4: / 10 Problem 2: / 20 Problem 5: / 10 Problem 3: / 10 Problem 6: / 50 OAL: / 140 his material is copyrighted and is for the sole use of students registered in CMPE212 and writing this exam. his material shall not be distributed or disseminated. ailure to abide by these conditions is a breach of copyright and may also constitute a breach of academic integrity under the University Senate's Academic Integrity Policy Statement.

Student Number: Page 2 of 24 Problem 1) [40 marks] Indicate if each of the following statements is rue or alse by writing a or on the line before the statement: Proper object encapsulation is based on having attributes declared as private. he implements keyword is used to create a child class from a parent class. he first line of code in the constructor of a child class must be a call to the constructor of the parent class. An immutable class has a mutator for each attribute. An attribute of type String must be cloned by its accessor to avoid a privacy leak. he keyword this can be used to access the immediate parent object from within the child object. A fully concrete child class must implement all abstract methods inherited from an abstract parent class. An interface can contain final static attributes. An object can be declared to be of an interface type. An abstract class can be instantiated. It is not possible to overload constructors. Only constructors can throw exceptions. he throws decoration is used in a class declaration to indicate that the class contains methods that throw exceptions. A constructor must be invoked during instantiation. Java automatically provides a default constructor for a class that does not have its own constructor.

Student Number: Page 3 of 24 Problem 1, Cont.) It is possible to define a class within another class. An outer class can only invoke non-static inner class methods from an instance of that inner class. A child class instance can be assigned to a pointer of a parent class type. Object is the base class for all classes in the Java API. Late binding is another name for the process of polymorphism. Early binding must be satisfied for the process of polymorphism to take place. Only attributes can be declared outside a class definition. In JavaX, the root Pane class does not have a built-in layout manager algorithm. It can only use absolute layout positions. he StackPane class is used to place nodes in a grid layout. When using a GridPane, nodes can be added to any row, column position in any order. Visible grid lines can be used to help with layout when using a GridPane. By default, a GridPane column is set to be as wide as the widest node in that column. It is not possible to make a GridPane row any taller than the tallest node in that row. he relative positions of nodes within an HBox pane can change if the pane size changes. he relative positions of nodes within a ilepane can change if the pane size changes.

Student Number: Page 4 of 24 Problem 1, Cont.) A *.css file or stylesheet can be used to change the appearance of individual nodes, all nodes of a certain type or all nodes in a JavaX window. Only one node can be the root node for a Scene object. All nodes must be added directly to the Stage object so that they can be seen. A JavaX window is modal by default. An event handler to listen for a mouse click event can implement the EventHandler<ActionEvent> interface. he EventHandler<ActionEvent> interface is a functional interface. he root node is typically a Pane object. A Button s.setonaction() mutator can be used to attach an event handler to the button. It is not possible to use a Lambda function to define an event handler. A Lambda function can only contain a single line of code. A Lambda function can only be written for methods defined in a functional interface that accept a single argument. Settings made in the stylesheet will override any appearance settings made in the *.fxml file in the same JavaX project. he *.fxml file must know the name and folder location of the controller *.java file that it is supposed to work with. Nodes must be instantiated in the controller *.java file rather than in the *.fxml file that it is associated with. he initialize() method in the controller *.java class is invoked only after all nodes have been injected into the file.

Student Number: Page 5 of 24 Problem 1, Cont.) he @XML annotation is used to specify methods and attributes in the controller class that are not to be processed by the *.fxml file. By default, built-in JavaX dialog windows are modal in behaviour. It is not possible to change the graphic image used in a JavaX dialog window. Dialogs cannot be used to obtain text from the user. Change listeners must be added to node properties rather than to the node itself. A change listener added to a text field property can obtain the text in the field as it was before it was changed by the user. Change listeners can be used to detect changes to spinners and sliders. Drawing methods can be invoked directly on an instance of a Canvas object. he border colour of a filled oval can be different from its fill colour. he (0, 0) pixel position in a Canvas object is the lower, left corner of the Canvas. he Canvas object is not capable of displaying images loaded from files. A Canvas object can interact with the cursor once an event handler has been added to the Canvas. A ransition animation can be used to animate several nodes at once. A imeline animation can only be constructed with a single Keyrame object. A Duration object must be supplied to the constructor of a Keyrame object. A Duration object is used to specify the time delay between Keyrame objects.

Student Number: Page 6 of 24 Problem 1, Cont.) An Animationimer object can be used to create an event that will be triggered every time the window s frame is re-drawn. An Animationimer object must be attached to a imeline object. A imeline object is started as soon as it is instantiated. A imeline object cannot be halted until the application containing the object is closed. Keyrame objects can be used to trigger events when the Keyrame has completed. here is no point in designing an animation that updates at any rate higher than the monitor refresh rate. A loop that endlessly iterates until a second has passed and then re-starts can be added to the Application thread in a JavaX program without changing the responsiveness of the window. A javafx.concurrent.ask object cannot access scenegraph nodes directly, but must bind with their properties instead. A ask object cannot be re-started. A ask object is a generic class. he implementation of a ask object requires you to override the call() method that returns an object of type. A javafx.concurrent.service object is used to manage a ask object. A Service object cannot be re-started. A Service object must be wrapped in a hread object in order to be changed to a Daemon thread. Daemon threads are automatically halted before any user application threads are stopped. here is no way to change the thread priority of a thread in a JavaX program.

Student Number: Page 7 of 24 Problem 1, Cont.) he hread.sleep() method will not propagate a thread interrupt exception. he hread.yield() method is used to halt a thread s execution. or I am really happy that this is the last true/false question! Problem 2) [20 marks]: Answer the following questions as briefly as possible: What is the underlying data structure used by an ArrayList<> collection? Object[] What is the convenience obtained by a class when it implements the Comparable<> interface? hat object can be sorted by built-in sorting methods from the Java API such as Arrays.sort(). What is the reason for making non-final attributes private? Private attributes cannot be changed directly from outside the object. If permitted at all, changes can only be made through mutator methods, which can check possible attribute values for legality. What is the advantage to declaring class members static? All instances of that class will share the same static member they are not duplicated, making memory use more efficient. What is a null pointer? A null pointer is a variable of an object type that has not been assigned to point to an instantiated object. It does not yet contain a memory address.

Student Number: Page 8 of 24 Problem 2, Cont.) What does it mean to say that an exception is propagated after it is thrown? If an exception is thrown but not caught inside the method in which it was thrown, the method will be halted and the exception will be propagated to the next method down on the call stack. Name one advantage of using inheritance. Avoids code duplication. Enforces class structure on child classes. Allows the use of polymorphism. Why is a try-with-resources block better than a normal try-catch block when used with ile I/O? Resources in a try-with-resources block will be automatically closed as soon as the end of the try block is reached. No explicit.close() calls are required, which is good because they will not work well if the stream is already in an erroneous state. Which file format binary or text produces the most compact file for storing floating point numeric data and why? Binary. Numbers are stored using the exact same bit pattern as used in RAM 4 bytes for a float, 8 bytes for a double. o store numbers in a text file all digits would have to become text characters taking up at least one byte each. What is a Scene Graph as used in JavaX? A Scene Graph refers to the hierarchy of nodes that will be added to the root of a Scene object. A node is an individual control or container in a window.

Student Number: Page 9 of 24 Problem 3) [10 marks] Provide the console window output of each of the following println statements. If you think the statement will cause an error, write error instead. System.out.println ( 15 / 2.0 );... System.out.println ( (double)( 7 / 2 ) );... System.out.println ( 6 * ( 2 / 3 ) + 7 );... System.out.println ( true && 1 );... System.out.println ( 6 * 2 / 3.0 + 7 );... System.out.println ( 6 * 20 / ( 3 + 7 ) );... System.out.println ( 5 > 2 3 <= 1 );... System.out.println ( 2 ** 3 );... System.out.println ( 26 % 3 );... System.out.println ( 4 + 6 * 2-7 + 10 / 5-1 );... System.out.println ( true && 9!= 7 );... System.out.println ("4" + 3 + 1);... System.out.println (2 + 1 + "7" - 4);... System.out.println ( 5 > 2 && 6!= 5 7 < 3 );... System.out.println ( 6 + 7 <> 13 );... System.out.println ( 2.4 / (int) 1.2 );... System.out.println ( (int) ( 9.6 / 2.0 ) );... System.out.println ( 7 + 2 > 3 && 9 >= 2 + 2 + 2 );... System.out.println ( Character.isDigit("123") );... System.out.println ( Character.toLowerCase('B') );... 7.5 3.0 7 error 11.0 12 true error 2 10 true 431 error true error 2.4 4 true error b

Student Number: Page 10 of 24 Problem 4) [10 marks] Write the output of the following complete program, which runs without error, in the box provided: public class Problem1 { public static double fiddle(int num1, int[] nums1, int[] nums2, String str) { str = str.tolowercase(); int size = str.length(); String flipped = str.charat(size 1) + str.substring(1, size 1) + str.charat(0); System.out.println(flipped); num1 = size; for (int num : nums1) num *= 10; System.out.println(nums1[0]); double sum = 0; for (int i = 0; i < nums2.length; i++) { sum += nums2[i]; nums2[i] *= 10; System.out.println(nums2[0]); return sum; // Displays numbers on one line public static void showarray(int[] array) { System.out.print("Array: "); for (int num : array) System.out.print(num + ", "); System.out.println(); public static void main(string[] args) { int[] array1 = {2, 3, 4, 5; int[] array2 = {1, 2, 3, 4; String astring = "Hello Class!"; int anum = 20; double aval = fiddle(anum, array1, array2, astring); System.out.println(aNum); showarray(array1); showarray(array2); System.out.println(aString); System.out.println(aVal);!ello classh 2 10 20 Array: 2, 3, 4, 5, Array: 10, 20, 30, 40, Hello Class! 10.0

Student Number: Page 11 of 24 Problem 5) [10 marks] Here are a bunch of classes and an interface from the same Java project: public abstract class Base { public int sum(int a, int b) { return a + b; public int multiply(int a, int b) { return a * b; public abstract int subtract(int a, int b); // end Base interface Dividing { int divide(int a, int b) throws DivideByZero; // end Dividing public class DivideByZero extends Exception { public DivideByZero(String message) { super(message); // end DivideByZero public class Concrete extends Missing { public int multiply(int a, int b, int c) { return a * b * c; // end Concrete here are two more classes in this project. he one on the next page contains a main method that tests the other classes:

Student Number: Page 12 of 24 Problem 5, Cont.) public class Demonstration { public static int remainder(int a, int b) { return a % b; public static void main(string[] args) { Concrete test = new Concrete(); // Prints: System.out.println(test.sum(4, 5)); // 9 System.out.println(test.sum(4, 5, 6)); // 15 System.out.println(remainder(12, 5)); // 2 System.out.println(test.multiply(4, 5)); // 20 System.out.println(test.multiply(3, 4, 5)); // 60 System.out.println(test.subtract(4, 5)); // 1 try { System.out.println(test.divide(10, 0)); catch (DivideByZero e) { System.out.println(e.getMessage()); //Attempt to divide by 0! try { System.out.println(test.divide(10, 2)); // 5 catch (DivideByZero e) { System.out.println(e.getMessage()); try { Dividing dest = new Missing(); System.out.println(dest.divide(15, 2)); // 7 catch (DivideByZero e) { System.out.println(e.getMessage()); // end main // end Demonstration As you can see the listing shown above contains the output of the program when run as in-line comments. One class is Missing. he Missing class extends one class and implements an interface. Write the Missing class on the next page:

Student Number: Page 13 of 24 Problem 5, Cont.) public class Missing extends Base implements Dividing { public int subtract(int a, int b) { return a b; public int divide(int a, int b) throws DivideByZero { if (b == 0) throw new DivideByZero("Attempt to divide by zero!"); return a / b; public int sum(int a, int b, int c) { return a + b + c;

Student Number: Page 14 of 24 Problem 6) [50 marks] or this problem you are going to write a properly encapsulated class call CallLog that is designed to hold data for a call centre s single phone call response. Start below by writing an exception class called LogException that will be used by the CallLog class: Write the LogException class below: public class LogException extends Exception { 2 marks public LogException(String message) { super(message); he CallLog class encapsulates the following attributes: he operator s name. Must contain at least one space and be at least three characters in length. he month for the call. An int that must lie between 1 and 12 inclusive. he day for the call. An int that must lie between 1 and 31 inclusive. he start time for the call as an int in the form hhmm, where hh is the hour that must be between 08 and 20 (8am to 8pm) and mm is the minute that must lie between 00 and 59. he finish time for the call in the same form as the start time. he finish time must be later than or the same as the start time. If an attempt is made to instantiate or mutate a CallLog instance with illegal arguments, the constructor or mutator being used must throw the LogException exception with an appropriate message. he CallLog class has one constructor that accepts arguments for all parameters and a second constructor that does not need the finish time, but assumes that the finish time is the same as the start time. Write a mutator for each attribute.

Student Number: Page 15 of 24 Problem 6, Cont.) You can write as many accessors as you need, but at a minimum you must have accessors for the name, the month and the day. As you continue to work on this problem you may find that additional accessors will help simplify your code. You also need to write the other four standard methods: compareo, equals, tostring and clone. Comparison using compareo is based just on the full starting time of the call. his means a time that consists of the month, the day and the starting time of the call. You will see examples of the use of this method later in the description of this problem. Equality is defined as the name, the month, the day and the starting time all being equal. You must override the equals method inherited from the Object class. You will see examples of the output of the tostring method later on in this description. he clone method must return a deep copy of the current CallLog object. All string comparisons in this problem can be assumed to be case sensitive. You can use the String methods.equals(otherstring) which returns a boolean and.compareo(otherstring) which returns a negative number if the supplied string comes after the current string (the one used to invoke the method) in the alphabet, zero if they are equal and a positive number if the supplied string comes before the current string. Start writing the CallLog class below and continue on the next five pages. Note that this problem continues after these five blank pages and that output examples are provided at the end of the problem statement. he CallLog class: 32 marks public class CallLog implements Comparable<CallLog> { private String name; private int month; private int day; private int startime; private int finishime;

Student Number: Page 16 of 24 Problem 6, Cont.) he CallLog class, Cont.: public CallLog(String name, int month, int day, int start, int finish) throws LogException { setname(name); setmonth(month); setday(day); setstart(start); setinish(finish); if (finish < start) throw new LogException("Start time must be before finish time."); public CallLog(String name, int month, int day, int start) throws LogException { this(name, month, day, start, start); public void setname(string name) throws LogException { if (name.length() > 2 && name.contains(" ")) this.name = name; else throw new LogException("Illegal name."); public void setmonth(int month) throws LogException { if (month > 0 && month < 13) this.month = month; else throw new LogException("Illegal month."); public void setday(int day) throws LogException { if (day > 0 && day < 32) this.day = day; else throw new LogException("Illegal day."); private int extractminutes(int time) { return time 100 * (time / 100); public void setstart(int start) throws LogException { if (start >= 800 && start <= 2000 && extractminutes(start) < 60) startime = start; else throw new LogException("Illegal start time.");

Student Number: Page 17 of 24 Problem 6, Cont.) he CallLog class, Cont.: public void setinish(int finish) throws LogException { if (finish >= 800 && finish <= 2000 && extractminutes(finish) < 60) finishime = finish; else throw new LogException("Illegal finish time."); public String getname() { return name; public int getmonth() { return month; public int getday() { return day; public int getduration() { int hours = finishime / 100 startime / 100; int minutes = extractminutes(finishime) extractminutes(startime); int duration = hours * 60 + minutes; if (duration == 0) return 1; else return duration; // Must be public if use Lambda function for Comparator<> public int getullime() { return (month * 100 + day) * 10000 + startime; public static int compareowithname(calllog left, CallLog right) { int namecompare = left.name.compareo(right.name); if (namecompare == 0) return left.getullime() right.getullime(); else return namecompare; public int compareo(calllog other) { return getullime() other.getullime();

Student Number: Page 18 of 24 Problem 6, Cont.) he CallLog class, Cont.: public boolean equals(object other) { if (other instanceof CallLog) { CallLog olog = (CallLog)other; return olog.name.equals(name) && olog.getullime() == getullime(); return false; public CallLog clone() { CallLog out = null; try { out = new CallLog(name, month, day, startime, finishime); catch (LogException e) { // empty return out; public String tostring() { return "Call log for " + name + " on month " + month + ", day " + day + ", starting at " + startime + ", finishing at " + finishime;

Student Number: Page 19 of 24 Problem 6, Cont.) he CallLog class, Cont.:

Student Number: Page 20 of 24 Problem 6, Cont.) he CallLog class, Cont.:

Student Number: Page 21 of 24 Problem 6, Cont.) Imagine, but do not write (!), another class containing static methods that is using the CallLog object you have just completed. Also, imagine that there are other CallLog child type objects that extend your CallLog class that are designed to hold other pieces of information in addition to the ones you encapsulated above. In the box below, write a static method for inclusion in this class called totalcallime that accepts as arguments: An ArrayList collection of objects of type CallLog or of any object that extends CallLog, An operator s name, A month as an int between 1 and 12, and A day as an int between 1 and 31. his method searches the supplied collection for all objects that have been created for the operator name supplied and for the supplied month and day. It then sums up and returns the total time in minutes for all calls that match the search criteria. If a call has the same finish time as the start time, assume a one minute duration for this call. Write just the totalcallime method below. You may add accessor(s) to the CallLog class above in order to support the work of the totalcallime method if you wish. he totalcallime method: public static int totalcallime(arraylist<? extends CallLog> list, String name, int month, int day) { int sum = 0; for (CallLog item : list) if (item.getname().equals(name) && item.getmonth() == month && item.getday() == day) sum += item.getduration(); return sum; 6 marks

Student Number: Page 22 of 24 Problem 6, Cont.) Without all the javadoc comments the Comparable<> interface from the Java API is: package java.lang; public interface Comparable<> { public int compareo( o); And the relevant part of the Comparator<> interface is: package java.util; @unctionalinterface public interface Comparator<> { int compare( o1, o2); he ArrayList.sort() method accepts a Comparator object that can be used to specify the sort criteria to be used by this method. Write a line of code to be included in some method in this other class that will sort an array list called loglist that is of type ArrayList<CallLog> using the ArrayList.sort() method. Note that if you supply null to the.sort() method it sorts using the natural ordering of the objects contained in the ArrayList. loglist.sort(null); 1 mark Write another line of code that uses the ArrayList.sort() method with a Comparator object that changes the ordering to sort first by name then by time. Modify your CallLog class if you wish. loglist.sort(calllog::compareowithname); 9 marks or loglist.sort((calllog left, CallLog right) > { int namecompare = left.getname().compareo(right.getname()); if (namecompare == 0) return left.getullime() right.getullime(); else return namecompare; );

Student Number: Page 23 of 24 Problem 6, Cont.) or example, here is the output generated by the code: loglist.foreach(item > System.out.println(item)) for some test data in the same order as it was added to the ArrayList loglist : Call log for Joe Keen on month 2, day 12, starting at 1005, finishing at 1010 Call log for Joe Keen on month 1, day 28, starting at 1005, finishing at 1010 Call log for Joe Keen on month 2, day 12, starting at 905, finishing at 912 Call log for Joe Keen on month 2, day 12, starting at 1407, finishing at 1410 Call log for Joe Keen on month 2, day 12, starting at 1225, finishing at 1227 Call log for Joe Keen on month 2, day 12, starting at 1358, finishing at 1405 Call log for Sue Keen on month 2, day 12, starting at 1005, finishing at 1010 Call log for Joe Keen on month 7, day 12, starting at 1005, finishing at 1010 Call log for Joe Keen on month 2, day 12, starting at 1005, finishing at 1005 Call log for Joe Keen on month 2, day 9, starting at 1105, finishing at 1110 Call log for Joe Keen on month 2, day 14, starting at 1005, finishing at 1010 Call log for Amy Keen on month 2, day 12, starting at 1005, finishing at 1010 he totalcallime method, if invoked for Joe Keen, month 2 and day 12, would return the value of 25 minutes. After sorting using natural ordering, loglist would look like: Call log for Joe Keen on month 1, day 28, starting at 1005, finishing at 1010 Call log for Joe Keen on month 2, day 9, starting at 1105, finishing at 1110 Call log for Joe Keen on month 2, day 12, starting at 905, finishing at 912 Call log for Joe Keen on month 2, day 12, starting at 1005, finishing at 1010 Call log for Sue Keen on month 2, day 12, starting at 1005, finishing at 1010 Call log for Joe Keen on month 2, day 12, starting at 1005, finishing at 1005 Call log for Amy Keen on month 2, day 12, starting at 1005, finishing at 1010 Call log for Joe Keen on month 2, day 12, starting at 1225, finishing at 1227 Call log for Joe Keen on month 2, day 12, starting at 1358, finishing at 1405 Call log for Joe Keen on month 2, day 12, starting at 1407, finishing at 1410 Call log for Joe Keen on month 2, day 14, starting at 1005, finishing at 1010 Call log for Joe Keen on month 7, day 12, starting at 1005, finishing at 1010 After sorting with the new Comparator object: Call log for Amy Keen on month 2, day 12, starting at 1005, finishing at 1010 Call log for Joe Keen on month 1, day 28, starting at 1005, finishing at 1010 Call log for Joe Keen on month 2, day 9, starting at 1105, finishing at 1110 Call log for Joe Keen on month 2, day 12, starting at 905, finishing at 912 Call log for Joe Keen on month 2, day 12, starting at 1005, finishing at 1010 Call log for Joe Keen on month 2, day 12, starting at 1005, finishing at 1005 Call log for Joe Keen on month 2, day 12, starting at 1225, finishing at 1227 Call log for Joe Keen on month 2, day 12, starting at 1358, finishing at 1405 Call log for Joe Keen on month 2, day 12, starting at 1407, finishing at 1410 Call log for Joe Keen on month 2, day 14, starting at 1005, finishing at 1010 Call log for Joe Keen on month 7, day 12, starting at 1005, finishing at 1010 Call log for Sue Keen on month 2, day 12, starting at 1005, finishing at 1010

Student Number: Page 24 of 24 Extra Page