代做Advanced Programming (IT)帮做Java编程

DEGREES of MSc Information Technology, MSc in Software Development and MSc IT Cyber Security

Advanced Programming (IT)

Monday 29 April 2019

1.      (a) All Java objects have a method called hashCode that allows them to be hashed.

Briefly describe what hashing is and how it is used in a data structure such as a HashSet.   [4 marks]

(b) Explain why the length of the String would likely be a poor hash function for hashing Strings containing words in the English language.      [2 marks]

(c) Within the context of hashing, what is a collision?       [2 marks]

(d) Consider the following class:

public class Meal {

private final String mainCourse;

private final String dessert;

public Meal(String mainCourse, String dessert) {

this.mainCourse = mainCourse;

this.dessert = dessert

}

public String getMain() {

return mainCourse;

}

public boolean equals(Object o) {

String therMain = ((Meal)o).getMain();

if(mainCourse.equals(otherMain)) {

return true;

}else {

return false;

}

}

public String toString() {

return mainCourse + " and " + dessert;

}

}

With reference to objects in general, explain why String.equals() is used in the equals method as opposed to ==.       [4 marks]

(e) A programmer decides to override hashCode in this class, using the method

below. Explain why this hashCode necessitates a change elsewhere in the class and rewrite the relevant part of the original.

public int hashCode() {

return mainCourse.hashCode() + dessert.hashCode();

}                         [5 marks]

(f) Consider the following code that makes use of this object:

String mainDish = "Fish";

String dessert = "Tiramisu";

Meal m = new Meal(mainDish,dessert);

mainDish = " and chips";

System.out.println(m);

Explain, with reference to the particular properties of Strings, why the output is "Fish and Tiramisu" and not "Fish and chips and Tiramisu" despite the object being referred to by the mainDish reference having changed. [3 marks]

2.      (a) Briefly explain the purpose of the decorator design pattern.            [3 marks]

(b) What is the advantage of a decorator over sub-classing?                     [2 marks]

(c) Consider the following two classes:

public abstract class AbstractOffice {

public void openDoor() {

System.out.println("The door has been opened");

}

}

public class ConcreteOffice extends AbstractOffice {

private String address;

public ConcreteOffice(String address) {

this.address = address;

}

}

You are tasked with using the decorator pattern to add a burglar alarm to an office. Firstly, create an abstract decorator class for AbstractOffice.            [5 marks]

(d) Now create a concrete burglar alarm decorator. Think about any new methods and attributes that you need.     [5 marks]

(e) Write a main method that creates an office decorated with your decorator.           [3 marks]

(f) State another design pattern that can be used to add functionality to classes without inheritance or large-scale modifications.           [2 marks]

3.      (a) The wait and notify methods provided with all Java Objects allow Threads to be placed in a waiting state until notified by another Thread. With reference to the Thread states runnable, blocked, waiting, describe the process by which a Thread acquires an object’s monitor (via e.g. entering a synchronized block),  waits and is awaken via notification by another Thread.          [5 marks]

(b)     The following three classes define a system including an Object (SendReceive)

that permits the Sender object to send messages (in the form of a String) to the Receiver object. The Sender should be able to send a message only when there isn’t one waiting to be received. If there is one waiting, it should wait until it has gone. The receiver should wait until a message appears to be received. The SendReceive object is missing the implementation of the send and receive methods.

public class Sender extends Thread {

private SendReceive sr;

public Sender(SendReceive sr) {

this.sr = sr;

}

public void run() {

String[] messages = {"Hi","How’re you?","Bye!"};

for(String message: messages) {

sr.send(message);

}

}

public static void main(String[] args) {

SendReceive sr = new SendReceive();

new Sender(sr).start();

new Receiver(sr).start();

}

}

public class Receiver extends Thread {

private SendReceive sr;

public Receiver(SendReceive sr) {

this.sr = sr;

}

public void run() {

while(true) {

System.out.println(sr.receive());

}

}

}

public class SendReceive {

/*

hasMessage should be set to true

when there is a message

waiting to be received,

false otherwise

*/

private boolean hasMessage = false;

private String message;

public void send(String message) {

// YOUR CODE HERE

}

public String receive() {

// YOUR CODE HERE

}

}

Using wait and notify, write code for the send and receive methods. You may change the method decorators if you wish.   [10 marks]

(c)     Using an example, describe what is meant by the term Race condition. For your example, describe how it could be avoided.   [5 marks]

4. Please write the answers to the following 10 multiple-choice questions clearly in your answer booklet. There is only one correct answer in each case. Each correct answer is worth 2 marks. Incorrect answers will result in a penalty of two thirds ofa mark to discourage guessing.

(a) Which of the following statements about inheritance is true:

A.  A class can extend many classes and implement many interfaces.

B.  Private attributes are directly accessible by subclasses.

C.  A final class cannot be subclasses.

D.  A class can implement only one interface.               [2 marks]

(b) Which of the following statements is false:

A.  An object referred to by a final reference cannot be modified.

B.  A final primitive cannot be modified.

C.  A final method cannot be overridden.

D.  A final object reference cannot be reassigned to a different object.             [2 marks]

(c) Which of the following correctly describes the output of this code snippet:

public static void main(String[] args) {

Double a = 6.5;

Double b = a;

b *= 2;

System.out.println("a: " + a + ", b: " + b);

}

A.  a: 13.0, b: 13.0

B.  a: 13.0, b: 6.5

C.  a: 6.5, b: 6.5

D.  a: 6.5, b: 13.0                 [2 marks]

(d) Which of the following correctly describes the output of this snippet:

public class ExamQ {

public static int aValue = 0;

public ExamQ() {

aValue++;

}

public static void main(String[] args) {

ExamQ a = new ExamQ();

ExamQ b = new ExamQ();

System.out.println(""+ a.aValue + " " + b.aValue);

}

}

A.  2 2

B.  0 0

C.   1 2

D.  1 1                              [2 marks]

(e) Which of the following statements is false:

A.  The unknown order of operation of Threads makes it hard to predict when deadlocks might occur.

B.  A deadlock describes a situation when two Threads are waiting for one another.

C.  Deadlocks can always be avoided through the use of synchronized blocks.

D.  Conditions can be used to remove deadlocks.             [2 marks]

(f) An object is garbage collected in Java when:

A.  The method in which it was created ends.

B.  When main has finished.

C.  When there exists no references to it.

D.  When it is unreachable from main.                   [2 marks]

(g) Which of the following statements about synchronized and locks is true:

A.  Synchronized cannot span multiple methods whereas locks can be locked in one method and unlocked in another.

B.  Locks are always preferable to synchronized blocks.

C.  Synchronized can only be used to synchronize the class it is being used within.

D.  Synchronized blocks are powerful because they allow us to use conditions.               [2 marks]

(h) Which of the following is false:

A.  Thread.join() is a blocking method.

B.  InterruptedException must be caught when using blocking methods.

C.  Thread.sleep() cannot be used in main.

D.  Calling interrupt() on a Thread that is sleeping causes an Exception.                  [2 marks]

(i) Consider an Abstract class A that is subclassed by B. Which of the following is true:

A.  It is not possible to subclass an abstract class.

B.  B b = new B(); A a = b; is permissible

C.  A a = new B(); B b = a; is permissible

D.  It is essential to use Abstract classes in Java programming.                         [2 marks]

(j) Using the classes A and B from the previous question. A has a single method getName(). B overrides the method and also introduces a method getAge(). Which of the following is false:

A.  B b = new B(); b.getAge(); will compile and run fine.

B.  A a = new B(); a.getName(); will use the version of the getName() method defined in class A.

C.  A a = new A(); a.getAge(); will fail to compile.

D.  A[] a = new A[2]; a[0] = new A(); a[1] = new B(); will compile and run fine.           [2 marks]





热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图