Question A1 a) The object factory design pattern is an alternative to constructors for the controlled instantiation of objects. i) Give an example of the application of the object factory design pattern in the standard Java libraries. ii) List three advantages of an object factory compared with the use of constructors for object instantiation. b) Consider the following Name class. public final class Name { private final String firstName; private final String lastName; public Name(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } } i) Explain why it is not necessary to define a separate interface for the Name class? ii) There are no constraints on the values of firstName and lastName passed to the constructor of the Name class. Suggest appropriate checks to perform on the parameters and the exception(s) to throw if the parameters do not comply. iii) Convert the Name class into a factory for Name objects. The factory must guarantee that, from the point of view of its clients, each unique combination of firstName and lastName is represented by a single instance of the Name class (there can be no two Name instances with the same firstName and lastName). For example, the factory will only ever provide a single Name object with first name “Mark” and last name “Smith”. Show your complete implementation of the Name class as a factory. Note: it is not necessary to define any new classes iv) Do you need to override the Object equals method in your modified class? Briefly explain your answer. Question A2 a) A hash table data structure can be used to implement a set or a map. i) With the aid of a diagram, describe the structure of a hash table that is used to implement a set. Explain how the structure enforces the no duplicate elements invariant of sets. ii) Under normal conditions, a hash table provides fast access to its elements (e.g. add and remove are O(1) operations). Explain why this is and under what conditions (and how) performance of a hash table degrades. iii) Explain why the inconsistent implementation of Object equals and hashCode methods can undermine the integrity of Java hash table data structures. What constraint must be placed on the computation of an object’s hashCode? b) Provide an implementation of the compareTo method for the following Name class that orders instances of Name by lastName then firstName. public final class Name implements Comparable { private final String firstName; private final String lastName; public Name(String firstName, String lastName){ this.firstName = firstName; this.lastName = lastName; } } public String getFirstName(){return firstName;} public String getLastName(){return lastName;} public int compareTo(Name name){ // provide implementation of this method } } c) Provide a Comparator that orders instances of Name by firstName then lastNamae A SortedSet uses a binary tree to maintain its elements in their sorted order. Draw the state of the tree after execution of the following sequence of code: SortedSet names = new TreeSet(); names.add(new Name(“Mark”, “Smith”)); names.add(new Name(“Elena”, “Poulou”)); names.add(new Name(“Stephen”, “Hanley”)); names.add(new Name(“Paul”, “Hanley”)); names.add(new Name(“Marc”, “Riley”)); names.add(new Name(“Craig”, “Scanlon”)); names.add(new Name(“Jim”, “Watts”)); Use circles labelled with full names to illustrate each node of the tree. You may show intermediate states. d) Two possible implementations of the List data structure are a linked list and a resizable array. For each of the following scenarios, state which of these two implementations would be the most efficient to use and why. i) A queue of people at a cinema ticket office. ii) The names in a mobile phone address book. e) A Set would be a more suitable data structure than a List for the names in a mobile phone address book, the scenario from d) ii) above. State the reason for this and also which of the Java set implementations (HashSet or TreeSet) you would use if you were to implement this and why. Question A3 a) Describe and illustrate with a diagram the structure of a doubly linked list. b) It is possible to use a resizeable array or a doubly linked list to implement a list. Preferably using the Big ‘O’ notation, compare and contrast the performance of the two data structures for the following list operations: i) get or set an element at an arbitrary position in the list, ii) add or remove an element at the head of the list, and iii) insert or remove an element at an arbitrary position in the list (other than the head or tail). In each case, explain any difference between worst-case performance for a resizeable array and a linked list. c) Explain whether the worst-case performance of the following method is better for a collection implemented using a resizable array or a linked list. void removeBelowLimit(Collection vals, int lim) { for (Iterator i = vals.iterator(); i.hasNext(); ) { if (i.next() < lim) { i.remove(); } } } Question B1 a) Describe and illustrate with a diagram the life cycle of a thread from being created to termination. List the ways by which a thread can be blocked and unblocked. [10 marks] b) Write a program that creates two threads, one to print out “Hip, hip!”, and the other to print out “Hooray!” (both in finite counting loops). After creating the two threads, the main program should terminate. [10 marks] c) Modify your program so that the thread printing out “Hip, hip!” would run first, and the thread printing out “Hooray!” be allowed to run only after the “Hip, hip!” thread has finished. Question B2 a) What are the distinguishing characteristics of an event driven program as opposed to a conventional program? Provide an overview of the basic architecture of an event driven program, including a description of the event processing mechanism. What is the difference between an event handler and an event dispatcher? b) Describe briefly the basic steps that a Java programmer needs to take in order to set up event handlers and deal with events. c) You have been asked to implement a simple length conversion program as an event-driven program with a graphical user interface. The user is able to enter a length in miles into a text box. On pressing a button, the length entered is converted into kilometres and displayed in another text box (1 mile = 1.609 kilometre). Explain how you would construct such a program, by identifying the basic Java Swing components you would use, and specifying how events are handled. Provide outline code for the relevant parts of the application.
The post The object factory design pattern is an alternative to constructors for the controlled instantiation of objects. appeared first on Homework Aider.


What Students Are Saying About Us

.......... Customer ID: 12*** | Rating: ⭐⭐⭐⭐⭐
"Honestly, I was afraid to send my paper to you, but you proved you are a trustworthy service. My essay was done in less than a day, and I received a brilliant piece. I didn’t even believe it was my essay at first 🙂 Great job, thank you!"

.......... Customer ID: 11***| Rating: ⭐⭐⭐⭐⭐
"This company is the best there is. They saved me so many times, I cannot even keep count. Now I recommend it to all my friends, and none of them have complained about it. The writers here are excellent."


"Order a custom Paper on Similar Assignment at essayfount.com! No Plagiarism! Enjoy 20% Discount!"