Java Thread

Troncoso

VIP Member
That's not really the proper way to do it, to be honest. It might seem to fix it, and you might not notice the problem, but it's probably still there. A lock is the proper way to do it. The general rule I was told was that if you manipulate the same object/var/whatever, use a lock.

Also, I hope you at least catch for a interrupted exception with that sleep call.

In most situations, yeah, that won't help. But, here, I've already got the synchronized lock...thing, which about does the right job. Putting the thread to sleep just sorts out any remaining conflict.
And yeah, I caught the exception.

If the program was doing any more than literally what it does, I'd need to go further into it. But for the sake of getting this project out of my Easter Break, it gets the job done.
 

ayan

New Member
I've been learning C# for the past almost 4 years. Never wrote a single line of Java code. Is it worth it to learn to code in Java? I mean, C# seems better documented, and Microsoft's visual studio is just divine!
What do you need Java for, and there's no way of doing it with other language (or doing it with other languages is much harder) :D
 

Cromewell

Administrator
Staff member
I've been learning C# for the past almost 4 years. Never wrote a single line of Java code. Is it worth it to learn to code in Java? I mean, C# seems better documented, and Microsoft's visual studio is just divine!
What do you need Java for, and there's no way of doing it with other language (or doing it with other languages is much harder) :D

Java is documented pretty well....granted the generated docs look like html from 1993. http://docs.oracle.com/javase/6/docs/api/

Java is useful for all sorts of things, it's just as easy as anything .NET and as far as IDEs go Eclipse is pretty good.

The original advantage of Java was if you wrote it correctly you never needed to recompile it. You could take your class file and run it on any system that had the Java VM.
 

ayan

New Member
Java is documented pretty well....granted the generated docs look like html from 1993. http://docs.oracle.com/javase/6/docs/api/

Java is useful for all sorts of things, it's just as easy as anything .NET and as far as IDEs go Eclipse is pretty good.

The original advantage of Java was if you wrote it correctly you never needed to recompile it. You could take your class file and run it on any system that had the Java VM.

By "never needed to recompile" don't you mean that mechanism that i'm currently learning in c++, that lets me compile just once header files?

Code:
#ifndef
#define HEADERFILE_H_

//function deffinitions and stuff

#endif
 

Cromewell

Administrator
Staff member
If you build a C++ application in windows the executable you have will not run in linux. If I build some java code in windows, I can copy that file to linux and it is runnable.

A header file is different.
 

Troncoso

VIP Member
Hey again. I'm wondering, how could you step through a recursive method in Java? By that, I mean, each time a level of recursion finishes, I want the program to stop until I click the next button or what ever button I choose.
In doing that, wouldn't I effectively have to remove the recursive aspect?
 

NyxCharon

Active Member
Hey again. I'm wondering, how could you step through a recursive method in Java? By that, I mean, each time a level of recursion finishes, I want the program to stop until I click the next button or what ever button I choose.
In doing that, wouldn't I effectively have to remove the recursive aspect?

Use a IDE with a debug function. I use netbeans for instance, and can see the values of everything, step by step.
 

Troncoso

VIP Member
Use a IDE with a debug function. I use netbeans for instance, and can see the values of everything, step by step.

I know I can do that, but I want it to be part of the program. You see, I have a method for the "find the fake coin" riddle. It splits the pile of coins into separate piles and weighs them against each other. Because the fake coin is lighter, it's pile will be lighter. So it isolates that pile and splits it into 3 again. And it does this until there are just three coins left.

I want the program to increment at one weighing at a time, so the user can see each one and see where the lighter pile is.

Maybe I'm asking the wrong question to accomplish this. Haha.
 

Ankur

Active Member
Hello guys here is the code for addition program.

calWS.java
Code:
package mypack;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;

/**
 *
 * @author user
 */
@WebService(serviceName = "calWS")
@Stateless()
public class calWS {
    /**
     * Web service operation
     */
    @WebMethod(operationName = "additionoperation")
    public String additionoperation(@WebParam(name = "num1") String num1, @WebParam(name = "num2") String num2) {
        //TODO write your implementation code here:
        try{
            int x=Integer.parseInt(num1); 
            int y=Integer.parseInt(num2); 
            int z=x+y;
            return z+"";
        }
        catch(Exception e)
        {
          return e.getMessage();  
        }
        
    }
}
index.jsp
Code:
<%-- 
    Document   : index
    Created on : 18 Apr, 2013, 3:28:12 PM
    Author     : Ankur
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form name="ArithmaticOp" action="add.jsp">
            <table border="0">
                <thead>
                    <tr>
                        <th colspan="2">Enter 2 numbers:</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>a=</td>
                        <td><input type="text" id="num1" name="num1" value="0" size="10"/></td>
                    </tr>
                    <tr>
                        <td>b=</td>
                        <td><input type="text" id="num2" name="num2" value="0" size="10"/></td>
                    </tr>
                    <tr>
                        
                        <td colspan="2"><input type="submit" value="Get Result" name="result"/></td>
                    </tr>
                </tbody>
            </table>
        </form>
      </body>
</html>

add.jsp
Code:
<%-- 
    Document   : add
    Created on : 18 Apr, 2013, 3:32:42 PM
    Author     : Ankur
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>    <%-- start web service invocation --%><hr/>
    <%
    try {
	mypack.calWS_Service service = new mypack.calWS_Service();
	mypack.calWS port = service.getCalWSPort();
	 // TODO initialize WS operation arguments here
	java.lang.String num1 =request.getParameter("num1");
	java.lang.String num2 = request.getParameter("num2");
	// TODO process result here
	java.lang.String result = port.additionoperation(num1, num2);
	out.println("Result = "+result);
    } catch (Exception ex) {
	// TODO handle custom exceptions here
    }
    %>
    <%-- end web service invocation --%><hr/>

    </body>
</html>

i am getting a problem at mypack.calWS_Service service = new mypack.calWS_Service();
it says
PWC6197: An error occurred at line: 16 in the jsp file: /add.jsp
PWC6199: Generated servlet error:
cannot find symbol
symbol: class calWS_Service
location: package mypack

PWC6197: An error occurred at line: 16 in the jsp file: /add.jsp
PWC6199: Generated servlet error:
cannot find symbol
symbol: class calWS_Service
location: package mypack
I am not understanding the problem, am beginner in this, please can someone tell me whats going wrong here? How to write the symbol in it?? without changing the whole program?
 

Cromewell

Administrator
Staff member
I think you are missing the import, <%@ page import="package1.myClass1,package2.myClass2,....,packageN.myClassN" %> is the gist of how it's done. I haven't written jsp though so that's just what I think is wrong based on the error and the search results I got back.
 
Top