Originally Posted by
rana_marie
Q3.1. Consider the class below which models a counter object that has two methods to increment or
decrement its only instance variable, counter. (15 marks)
a) You are required to apply the wait-notify mechanism in order to make the class threadsafe. Of course the methods need to be synchronized first. Assume that the value of the counter
should be kept between 0 and 10.
b) Repeat (a) using a different technique which allows multiple condition variables for different
locks, and uses the Java.util.concurrent.locks package. Assume that you have two
conditions: notFull and notEmpty.
public class Counter {
private int counter = 0;
public void increment() {
try {
counter++;
} catch(Exception e){
} finally {
}
}
public void decrement() {
try {
counter--;
} catch(Exception e){
} finally {
}
}
}
Q3.2. Consider the MyClient class below. When the main method is executed, the user is asked to
enter his/her name and location. This data is sent to a server located at the “localhost” (i.e. on the
same computer). Copy this code to a new project in NetBeans IDE and run it. (12 marks)
a) Does the code run properly? Justify your answer.
b) Develop a server class, Server_toScreen, to the following specifications:
• The server should first display a message “Waiting for next client connection…”. (use
System.out.println statement here).
• The server should listen for incoming connections at port 5555.
• The server should read data sent to it using an instance of BufferedReader class.
• The server should display the data sent to it on the screen. Use JOptionPane class for
this part.
• The server should keep repeating (a) to (d) in order to receive data from other clients.
In your solution document, you will need to run the project and include screenshots of the results,
before and after writing the server class. The screenshots should include evidences that the code
was run on your own machine. import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class MyClient {
Socket socket;
PrintWriter out;
public void run() throws IOException{
String s;
socket = new Socket("localhost", 5555);
out = new PrintWriter(socket.getOutputStream());
s = JOptionPane.showInputDialog("What is your name?");
out.println(s);
s = JOptionPane.showInputDialog("Where are you now?");
out.println(s);
out.close();
socket.close();
}
public static void main(String[] args) throws IOException {
new MyClient().run();
}
}
Q3.3. In this question, you will re-write the server class in question Q3.2. However, in this time the server
is required to save the received data in a database instead of displaying them on the screen. This
database should be located at the same computer as the server (i.e. localhost). The name of the
database is clientsData, and the data will be saved in a table named CLIENTS which has two
text fields: name and location. Name your server class Server_toDB. (13 marks)
Hints:
• You will need to create the required database in NetBeans before writing your program. To do
this, go to “Window” menu, click on “Services”, then right-click on “Java DB” and select “Create
Database”. Next, build the required table as described in the question. Note the connection
string for later use in your application, e.g. “jdbc:derby://localhost:1527/clientsData”.
• The SQL statement used to insert a new row in a table is “INSERT INTO”. For example:
INSERT INTO CLIENTS VALUES('Ahmed', 'Egypt')
• Don’t forget to run the database server and load the database driver into the JVM before
running your program in NetBeans.
Q3.4. Assume we have an entity bean, ClientsEntity, which models a database table, clients,
with two fields: name and location. This entity bean has getter methods for all its instance
variables. In this question, you need to do the following: (15 marks)
a) Develop a session bean that utilizes the above entity bean in order to retrieve the names of the
clients stored in the database. The session bean is stateless, and it will be accessed remotely. In
your solution document, you need to provide the following:
• The code for the remote interface implemented by the session bean. The interface should
include one abstract method, void getNames().