代写COMPSCI 5002 Advanced Programming (IT)代做Java程序

Friday 21 August 2020

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

Advanced Programming (IT)

COMPSCI 5002

1.      (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]

2.      (a) The composite design pattern consists of three classes:

i)   component

ii)  leaf

iii)   composite

Describe the role of each including what type of component it is (class, abstract class, interface etc) and the relationships between them.   [6 marks]

(b) The  diagram below represents a hierarchy of objects,  each represented by an integer. Leaf objects have an integer attribute. The integer corresponding to a composite object is the maximum value of its components.

Using the composite design pattern, write the code for the three objects (minor syntactical errors will not be penalised):

i)   component          [2 marks]

ii)  leaf                 [4 marks]

iii) composite           [5 marks]

(c) Write a main method that uses your classes to create the data shown in the figure.

The last line in your main should display the value of the top node.            [3 marks]

3.      (a) Describe two situations (with justification) in which multi-threaded programming would be beneficial.     [4 marks]

(b) Thread.join() and Thread.sleep() are two examples of blocking methods.

What is a blocking method, and what, programmatically, does they necessitate?          [4 marks]

(c) Assuming that Thready is a class that extends Thread, the following code snippet has an error that stops it actually running concurrently. What is it?

public class ExamClass {

public static void main(String[] args) {

Thready[] t = new Thready[100];

for(int i=0;i<100;i++) {

t[i] = new Thready();

t[i].run();

}

}

}                                     [2 marks]

(d) You have been asked to write code to sort the values in a large array. Assuming you have written a method called sort that sorts values in an array but it’s too slow.  Sketch out the objects you would  need to  create  (and how they would interact) to create a multi-threaded solution to this problem. (Note that full code is not necessary here.)           [5 marks]

(e) When you implement your solution to part (d) and run it on a standard desktop machine you find that it doesn’t improve speed. Why might this be?               [1 mark]

(f) The following code snippet defines a class that extends SwingWorker. Assuming that countText is an output component that has a setText() method, explain the roles of publish and process.

private class CountTask extends SwingWorker{

protected Void doInBackground() {

try {

Integer count = 0;

while(!isCancelled()) {

count++;

Thread.sleep(100);

publish(count);

}

}catch(InterruptedException e) {}

return null;

}

protected void process(List counts) {

int lastVal = counts.get(counts.size()-1);

countText.setText(String.format("%d",lastVal));

}

}                           [4 marks]

4. Assume  that the University has  stored details  of all  students in a Java class called studentDatabase(). The class has two methods: getName(String matric) and getProgramme(String  matric) which return the students name and   degree programme when passed their matric number. Your task is to design a client / server system that allows clients to query the  studentDatabase() object (stored on the server). Clients should specify if they want the student’s name, or programme.

(a) Describe a suitable application level protocol for this system. The protocol should

be sufficiently detailed that someone else could implement the system.             [4 marks]

(b) Write the code for the server as a class with a main method. Assume that the server is only ever connected to one client at a time.             [6 marks]

(c) Write the code for a client as a class with a main method. Minor syntactical errors will not be penalised. The client should ask for the name of the student with matric number 999999.              [6 marks]

(d) Briefly describe how you would extend your code to allow for multiple clients to connect at once.       [4 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
站长地图