Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Jimmy Praet

+ Follow
since Sep 06, 2004
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jimmy Praet


I have the need to parse a continuously growing XML file. About once every 30 seconds (although this can variate.. might sometimes be 20, 40, ...), a new record (XML element with sub-elements) will be added to the file and I need to handle that record in my Java program.

Could you give me some pointers as to which parsing API is best to use for this job, and how I can avoid polling for changes and parsing the whole file each time.

Also, the file will not be wellformed since it does not have an end tag for the root element.

Thanks in advance.

Im am currently writing some test code for an online multiplayer snake game. (achtung die kurve - clone).
I am using TCP Sockets atm.

Current situation:

new game round:
I send a short[] array from the server to the clients with their initial random x and y positions. > 2 short per player

running game:
I send left/right pressed/released byte from clients to server. Every 30ms, the server updates the player positions, and sends the x and y offset of each player to its previous position. in a byte[] array. > 1 byte per player

I am using socket.setTcpNoDelay(true); so +/- 33 tcp packets are sent each second, holding a byte[] array of maximum length 6. (max 6 players).

But I read somewhere that the header of a TCP packet is 40bytes? (20 TCP + 20 IP)

(40+6)*33 = 1.5 kb/sec sent to each player makes 9kb/sec upload bandwith on the server? Those 9kb only hold around 1.2kb of data.

How should I reduce this overhead?
Will UDP help? Or do a need to change the way I communicate between client and server.

One thing I tried for now is update the world every 25 ms, and sent one packet holding 2 gamestates only every 50 ms. Then the client shows frame 1, waits 25ms, and then shows frame 2. This seems to work ok, but it's still alot of traffic.

I could send the player movements to all the clients, and let them do the game logic, but then they will all be out of sync? :s


The code is available for download here:


One more question: is there a way to measure the networktraffic that is being used by my java application? I know there are programs such as ethereal or netlimiter to analyze network traffic, but I would like to know it from inside inside the java application, to show average down- and upstream on the screen for example.

Thanks in advance, knot.

I am having some problems in calculating the collision response of the following situation:

I have a rectangle moving at a certain dx and dy velocity inside an octagon (It's a dice game). When it collides with an edge of the octagon I need to calculate the new dx and dy values.

The top and bottom edge is offcourse: dy=-dy
And left and right edge: dx=-dx

But what about the other edges
17 years ago
Why would it not compile if max<min?

If max and min were compile time constants then maybe it would not compile because of unreachable statements, but this isn't the case in the question so..

if max = min or max < min then B is not correct
I believe the answer should be A, but with the print statement inserted ofcourse

Is the same as

imho. The do.. while is incorrect because it is guaranteed to be executed at least once, the for loop isn't.
I think ONLY C is right. From what I've read about constructors, I believe that the default constructor will have the same access modifier as the class.
So the default constructor will be public imho. You could maybe test if you can create an instance of Test from a package outside the Test's package. If that works it proves that the constructor has public access and not default access.
Because the method in c) doesn't have a return type.
Your second piece of code prints 2, because binary numeric promotion occurs.
Both are promoted to ints which gives 50 for the character '2' and 48 for the character '0'.
The equals method defines that two valuepair object are equal, when they contain the same 2 values.
Consider a valuepair (4,2):
it is equal to another valuepair (4,2) , but it is also equal to a valuepair(2,4)

When two objects are equal, their hashcodes must also be equal.

[ September 10, 2004: Message edited by: Jimmy Praet ]
Well the first one (about anonymous inner classes not having constructors) is merely a misinterpretation of the question I think. I suppose they mean that you can not define a constructor for an inner class your self.

The second one: I'll give an example

protected members can be accessed in same package and IN subclasses.
The Sub class is in an other package, but it inherits the protected member.
The inherited protected member can be accessed inside the subclass, but it becomes private to any code outside the subclass. (Except for subclasses of this subclass)

If Super, Sub and Test were all in the same package then sub.getSuperValue(); would be legal.
The garbage collector ignores exceptions that are being thrown by finalize method. But the behaviour inside the finalize method is just as if it were a regular method i.e. when it throws an exception, it is propagated down the call stack, to the garbage collector which in it's turn ignores the exception.
[ September 09, 2004: Message edited by: Jimmy Praet ]
I just finished the java rules roundup game
But I have some remarks/doubts regarding some of the questions, so maybe somebody can clear this up?

Q: an anonymous inner class cannot have a constructor
A: true
-> doesn't it get a default constructor? Since you are creating an instance of the inner class with the new operator, it must have a constructor of some kind? Ofcourse I understand that you cannot specify a constructor yourself, because what would you name it..

Q: methods which are marked protected, can be called on any subclass of the class in which the method is declared
A: true
-> I believe it can be called IN any subclass, but not ON any instance of the subclass, unless it's in the same package. Is that right?

Q: what happens when u divide an integer by a variable with value 0
A: RuntimeException
-> When it's a floating-point variable, no RuntimeException will be thrown

edit: another one that came to mind
Q: member (instance) variables are allways given a default value
A: true
-> Except for final member variables. They must be explicitly initialized (either in the declaration statement, in an initializer block or in each constructor)
[ September 08, 2004: Message edited by: Jimmy Praet ]
An interface's methods are implicitly public abstract.

Class C declares getValue() with default package access, which makes it an illegal implementation of interface I.
[ September 08, 2004: Message edited by: Jimmy Praet ]
int[] b;
Should be read as: declare a reference variable b, that points to a one dimensional int[] array.

int b[];
Here it should be read as: declare a one dimensional array of the specified type type int.

int[][] b[];
Should be read as: declare a one dimensional array of the specified type int[][] -> an array holding other 2 dimensional int arrays

So you don't actually have to think of it as adding dimensions, you declare an array of the specified type.
Member variables (instance variables) are allways given a default value.
Unless it's a final member variable, because those must be initialized explicitly, before the constructor finishes.

Maybe you are mixing up with local variables. They are, in fact, not given a default value, and must be initialized before using them, or you will get a compiler error.
[ September 07, 2004: Message edited by: Jimmy Praet ]