Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2012, 07:23 PM
Registered User
 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation need help solving this question asap plzzzzzzzzzz

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().
 
Old December 17th, 2012, 10:18 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts
Default

rana-marie,
just looking over the first problem, I will only suggest as a practice never name a function the same name as a variable used in your function's content, although the first letter is upper-case as in Counter and counter++. I also suggest that you understand the algorithm first by writing it in pseudo-code or flow chart it and create a solution algorithm. Test the solution algorithm, then convert the solution algorithm to java code.

Hope this helps.
__________________
Disclaimer: The above comments are solely the opinion of one person and not to be construed as a directive or an incentive to commit fraudulent acts.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT - Solving root tag issue deepanp2p XSLT 2 May 11th, 2012 05:53 AM
plz help me in solving problems. naveed77 Beginning VB 6 4 July 5th, 2006 08:31 PM
Help through solving problem rudaba Beginning VB 6 1 January 5th, 2006 06:45 AM
beginner...help solving this error rehman_reck ASP.NET 1.0 and 1.1 Basics 3 December 22nd, 2004 10:49 AM
Help solving elusive key violation roniestein Access 4 October 23rd, 2003 03:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.