Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: need help solving this question asap plzzzzzzzzzz

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    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().


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    Do you have any specific questions or problems with your assignment?
    Please post them and the code that you are having problems with.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    I second Norm's recommendations -- please ask a specific question. Also please leave out the ASAP bit. No one likes to be rushed least of the volunteers who help here, and we believe that all questions are equally important, so no question should ask for or receive preferential treatment.

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    first code.jpg

    this is the first code for Q3.1

    and this is the second code:

    second code.jpg

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    Quote Originally Posted by curmudgeon View Post
    I second Norm's recommendations -- please ask a specific question. Also please leave out the ASAP bit. No one likes to be rushed least of the volunteers who help here, and we believe that all questions are equally important, so no question should ask for or receive preferential treatment.
    good day ,

    sorry for the "asap" thing, didnt mean to be rude, but the thing is that its due today to send it to my tutor and I have been trying to solve it with my colleagues for a couple of days and reached to a dead end.. any help will be really appreciated

    here are the codes for the question..

    first code.jpg

    second code.jpg

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    Please copy and paste the code here on the forum. Don't post images. Source lines can not be copied from an image for testing or for showing errors.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    Quote Originally Posted by Norm View Post
    Please copy and paste the code here on the forum. Don't post images. Source lines can not be copied from an image for testing or for showing errors.




    here is the first code

    //---------
    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 {
    }
    }
    }

    //--------


    and here is the second code :

    //------


    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();
    }
    }

    //-----

    I hope its clearer this way... thanks for your help

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    You have posted some code without any questions about the code or your problems with it.
    Can you explain what problems you are having with the code?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    Quote Originally Posted by rana_marie View Post
    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().


    I forgot to add this part of the question

    • The code for the session bean which includes implementation of the getNames()
    method. This method should query the clients table for all its records. Then it should
    create one string by concatenating all names stored in the name field of the clients
    table. This string should finally be returned by the method.
    Hints:
    • Assume any persistent unit name in your answer.
    • You will need the following SQL query: "SELECT m FROM clients m"
    • Use a for-each statement to access all results you obtain from querying the database
    b) Create a client program that can remotely uses the above session bean and invokes the
    getNames() method. Assume that you have a copy of the remote interface mentioned

    --- Update ---

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    You have posted some code without any questions about the code or your problems with it.
    Can you explain what problems you are having with the code?
    The thing is I dont know how to solve those questions at all.. and I dont know how to wrap my code here thats why I tried sending the codes as images

    will try to find out a way to wrap the code and send it back to you..

     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 { 
            } 
        } 
    }


    the second code

    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(); 
        } 
    }

    I hope am doing it right this time..

  10. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    Still, what specific questions do you have? So far you've posted code and assignment information, but have yet to ask an answerable question.

  11. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: need help solving this question asap plzzzzzzzzzz

    Quote Originally Posted by curmudgeon View Post
    Still, what specific questions do you have? So far you've posted code and assignment information, but have yet to ask an answerable question.
    the questions I have I already posted in my first post, I copied the whole assignment with its coeds that we need to work on, to be honest, I have sent this assignment to many of my friends and no one were able to solve it

    if you can kindly please just read it again, maybe you will have a clear idea on what is required from each question..

    --- Update ---

    Quote Originally Posted by rana_marie View Post
    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().
    I forgot to add this part of the question !!!

    • The code for the session bean which includes implementation of the getNames()
    method. This method should query the clients table for all its records. Then it should
    create one string by concatenating all names stored in the name field of the clients
    table. This string should finally be returned by the method.
    Hints:
    • Assume any persistent unit name in your answer.
    • You will need the following SQL query: "SELECT m FROM clients m"
    • Use a for-each statement to access all results you obtain from querying the database
    b) Create a client program that can remotely uses the above session bean and invokes the
    getNames() method. Assume that you have a copy of the remote interface mentioned

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    Please reread post#10. You need to ask specific questions about problems you are having, not just post the assignment.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: need help solving this question asap plzzzzzzzzzz

    Quote Originally Posted by rana_marie View Post
    the questions I have I already posted in my first post, I copied the whole assignment with its coeds that we need to work on, to be honest, I have sent this assignment to many of my friends and no one were able to solve it
    Let me be clear on this so that there is no doubt: we will answer specific questions you have about things in your code that confuse you, but no one is going to do this assignment for you. Period.

Similar Threads

  1. Thank you for solving the mystery!!
    By Maxi in forum Member Introductions
    Replies: 1
    Last Post: September 19th, 2012, 08:41 PM
  2. Help with solving Maze
    By tcao2 in forum Algorithms & Recursion
    Replies: 9
    Last Post: May 5th, 2012, 09:58 PM
  3. Solving a polynomial
    By arvindbis in forum Java Theory & Questions
    Replies: 9
    Last Post: March 8th, 2012, 12:16 AM
  4. [SOLVED] Begginer Question please help ASAP! (Easy to Answer)
    By Bagzli in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 11th, 2010, 10:00 PM
  5. Problem in recursion for Solving Maze Recursively program
    By _Coder1985 in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 29th, 2009, 05:37 AM