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]