MT311 TMA1 Cutoff date: 4 Dec 2015 Please submit a hardcopy to

advertisement
MT311 TMA1
Cutoff date: 4 Dec 2015
Please submit a hardcopy to Kevin and a softcopy of the programs at
http://plbpc001.ouhk.edu.hk/tma/login.htm.
A test for the course will be held at 11:00-12:50 8 Dec 2015. The topic covers
everything up to networking and RMI.
Question 1
Consider the following program fragments and describe the problems:
[10 marks]
(a)
.....
int i;
if (a==3) {
i=4;
}
abc.meth(i);
....
[2]
(b)
public class A {
public A(int i) {...}
public void meth() {...}
}
public class B extends A {
public void meth() {...}
}
[2]
(c)
public class A {
public void meth() {...}
}
public class B extends A {
protected void meth() {...}
}
[2]
(d)
public class A {
private int a=0;
static public void meth(int b) {
System.out.println(a+" "+b);
}
}
[2]
(e)
abstract public class A {
abstract void meth() {
System.out.println("this is a test");
}
}
[2]
Question 2
[25 marks]
Write a Java program so that when the program is executed, a window will appear
with white background. The window should be of size 400x400 pixels. Then when
the user clicks at the window, a square of length 10 to 20 pixels will be created. The
square will move linearly in a random direction with a speed of 5 pixels per second.
When the square collides with anything, say another square or the border of the
window, the square will disappear. Note that if the user clicks at a point so that the
newly created square immediately collides with anything, then it will immediately
disappear and cause all other squares to disappear if they collide with the newly
created one.
[Hint: You need a class to represent a square which stores the information of the
square: length, position. Then, you need a thread to update the positions of the
squares according to the specification and continuously check for collision. If a
collision is found, the corresponding squres are deleted from the data structure.]
Question 3
[30 marks]
Write a Java program which reads in a number of text files. It will use a thread to read
from one file. Each line in the file will be put into a fixed size buffer of strings. The
buffer can store at most 10 strings. The buffer is operated as a queue in the
first-in-first-out order. One extra thread is used to get the strings from the buffer
and then print then in standard output. The commend to execute the program looks
like this:
java ReadFiles file1 file2 file3
where file1, file2, etc are name of the files to be read.
In this example, the program
should create three threads for reading the file. The program should terminates
when all the reading threads have finished and the buffer is empty.
[Hint: The buffer should have synchronized methods for getting and putting strings.
There should be counter to record the number of active reading threads. When a
reading thread has finished the reading task, it should use a synchronized method to
decrease the counter by one. In the get method of the buffer, it should check the
case when the buffer is empty after this get and the number of active reading
threads is zero, then the program should be terminated.]
Question 4
[15 marks]
You are given the following class which represents a disk drive:
public class DiskDrive {
synchronized public int write(byte[] data) throws IOException {...}
synchronized public byte[] readAndDelete(int handle) throws IOException {...}
static public DiskDrive getDrive(int handle) {...}
}
The method write(data) writes the data to the drive. It returns a handle number for you to
get back this data later. The method will throw an IOException if not enough space is
available or other IO errors occur.
The method readAndDelete(handle) is used to get back the data with the corresponding
handle number. The data will also be deleted from the disk drive.
If there is any IO error,
an IOException will be thrown.
The static method getDriveNo(handle) returns the disk drive that stores the data according
to handle.
The three methods work like this: a user uses the write(data) method to write some data to
a disk drive. The handle number returned from the method is used to get back the data.
In order to get back the data, you need to invoke the static method getDrive(handle) to get
the disk drive that stores the data. Then, the readAndDelete(handle) method is used to get
back the data.
Now, you need to write another class DiskPool which represents a pool of 5 disk drives:
public class DiskPool {
private DiskDrive[] pool=new DiskDrive[5];
synchronized public int writePool(byte[] data) {....}
synchronized public byte[] readAndDeletePool(int handle) throws IOException {....}
}
The method writePool(data) writes the data in one of 5 disk drive. If it successes in writing
the data in one of the drive, it will return the handle of the data. If it cannot write it
successfully in all 5 drives, it will wait until other threads notify it and then it will try again to
write the data to one of the 5 drives. The method will try until it can successfully write the
data to one of the drive.
The method readAndDeletePool(handle) first gets the drive that stores the data
corresponding to the given handle. Then it reads the data from the drive. If there is any
IOException thrown in reading the data from the drive, this exception will be thrown back to
the caller of the method readAndDeletePool(handle). If the read is successful, it will notify
other threads which are waiting to write data to the pool.
Note that you only need to provide the implementation for DiskPool, not DiskDrive.
Question 5
[20 marks]
Write a multithreaded Java server which has the following properties:


It listens to requests at port 12345.
When a request arrives,
o it creates a new thread to serve the request.
o each client repeatedly sends in integers to the client with the
writeInt() method of DataOutputStream until the client
disconnects from the server. The server will keep a record of the
maximum of all the integers it receives from all the clients. This
record is set to Integer.MIN_VALUE initially. When the server
receives an integer from a client, it will compare the integer with
the record. If the value is larger than the record, the record will
be updated and a string "Your integer is the largest at the
moment." will be sent to the client. Otherwise, the string "The
current maximum is xxx." will be sent to the client where xxx is the
current maximum.
o Both the server and client use the DataInputStream and
DataOutputStream for communication.

Note that you only need to provide the implementation for the server, not
the client.
[Hint: you need a synchronized method to deal with the current maximum value as
multiple threads are trying to change this value.]
Download