Archive for the 'java' Category



Distinction!

Thursday 19 June 2008 @ 4:44 pm

Well so far, anyhow it’s time to celebrate! Well, somewhat… I’ve got a project to do!

Over the summer (read till October,) I’ll be busy working on my project; hopefully it won’t mean a massive lack of activity like my exams caused. In fact I should be able to write up some tips on using J2Me as I go along. I am rather lucky as I managed to persuade my supervisor to let me develop a game using J2Me, which should be lots of fun to make and test :)

I have noticed that although there are a few good sites, tutorials, and guides on developing games for J2Me; there is a distinct lack of help on how to use the Visual Game Designer and Visual Map Designer features that were introduced with Netbeans 6.0. (Correct me if I am wrong, but they weren’t in Netbeans 5.5.) Because these tools haven’t been around very long there is also a lack of books covering them. The only real useful information about using them is the sample ‘game’ included in the Netbeans Mobility pack. So when I get the time I’ll provide a guide on using these tools for a simple ‘game’.

In addition, I should be able to host the odd version of the game. As it will need testing on multiple devices and I can’t just go out and buy a load of phones! Of course the emulators are useful, but there are situations where the device can act differently. I’ve noticed this being the case when using JSR-226: The 2D Vector Graphics API. Annoying really as Vectors can be a great tool for mobile games. With their being a number of resolutions to work with vectors have a great advantage over sprites, but with the low implementation of JSR-226 in devices, I’ll be overlooking them.

Back to beer!




Random picture of the week: Java edition

Monday 31 March 2008 @ 11:42 am

This picture just had to make it one week:

 

Java

 

We got bored waiting for someone to turn up at the pub (and our food,) so we had some fun. The crazy stuff computer science students get up to!




All night java!

Wednesday 19 March 2008 @ 9:57 pm

Is not very productive. So far we’ve fixed 4 bugs, ate pizza, broke it, fixed it, one of us went to the gym, others slept, ate more crisps,  messed about by stealing monitors and keyboards to make the eee pc into a desktop,  did some overclocking, thought about java, and sulked over the lack of a kettle. So all in all a good use of time!




The Java is almost complete

Tuesday 18 March 2008 @ 11:32 pm

The group project is nearing the deadline and the coding side of the work seems to be complete (bar the guaranteed major bug.) I’ll be pulling another all-nighter tomorrow to help finish off the testing and written work.

After numerous last minute revisions to the protocol and the server everything back working again! Loving it when someone recodes the server, client, or protocol then comes up to you and says: “By the way, your Gui is broke.” Really? It was working when I updated and went to bed >:( Perhaps that has happened too many times in the last 5 days for me to care anymore…

Since I am knacked, heres a lovely (Java ish related) comic from xkcd to keep you entertained:

Java, java, java…




Netbeans 6.1 looks to be another fine product

Monday 17 March 2008 @ 9:32 am

After installing Netbeans 6 RC 1 I never really turned back to Netbeans 5.5.1, Netbeans 6 enabled me to be far more productive when coding. The highlighting, searching, and better auto commenting features were enough to make me delete Netbeans 5. Especially after the RC was stable enough to work with. Now Netbeans 6.1 beta 2 has recently appeared, it seems I missed out on having a look at beta 1. At least that means I get to test a more stable IDE.

Now the one huge difference with Netbeans 6.1 is the start up speed. It is almost instant, well in Netbeans terms anyway. On my home system — without any tweaks to the IDE — it loads up my last project in just under 10 seconds. Now you probably want to now how long Netbeans 6 takes, well that takes… 17 seconds (yes, I did just count.) So, for me at least, Sun aren’t making things up about the increased speed of the program.

According to Sun:

  • Faster cold start and improved startup sequence. Up to 40% faster (with a project opened). Opening projects does not block startup. Go to Type dialog available even during post-startup scanning of sources.
  • Various optimizations to reduce I/O and file access (touching disk), improving responsiveness in many situations, especially on slower filesystems (e.g. on network).
  • Incremental parsing in java editor speeding up code completion and improving responsiveness of the editor especially with large files (see below).
  • Improvements in JSP parser (caching, memory management, update strategies), leading to faster code completion and editor.
  • Improvements in Visual Web designer — faster page opening and table drop, lower memory usage, fixed memory leaks, and more (see below).

It seems this major speed improvement is new to beta 2, so if you’re still stuck on beta 1 you might want to change.

There’s also some nice eye candy in there, it seems everyones on the transparency train these days!

I’ve only had one hiccup with the IDE so far, a button just wouldn’t delete in my Gui when using Matisse. I kept getting a little warning light about an error, so I happily submitted the problem. I ended up just tossing the button out of my gui as just one more ‘other component’ (the Netbeans equivalent of tossing it overboard!)




Breadth First Tree Traversal in Java

Thursday 13 March 2008 @ 10:50 pm

Coding a breadth first traversal is more complex than a depth first traversal (left to right, or right to left.) Although, the code behind the traversal is not much longer or complex, it just requires a bit of thinking. Source code for the breadth first traversal is at the bottom of the post, alongside source code for the more common depth traversal.

I’ve found that there are little examples - online or in books - of how to do a breadth first traversal, especially for Java. I had to teach myself this from a not too friendly C++ example. I’ll try to explain the basic idea of the traversal, before moving on to the code. I hope for your sake that I can actually explain this…

Suppose you have the following tree:

basic-binary-search-tree

 Now if you were to run a depth traversal you would go in the following order:

1,2,3,4,5,6,7

As the traversal would go to the left most node, then get the root node of that node, then attempt to try this on the right node. As such:

 basic-tree-traversal.jpg

I know, not the greatest of diagrams… that probably just confused you. The red numbers and arrows correspond to the order in which the traversal works. It will go all the way to the left, before going right; this is probably good in the case above, but sometimes you want to get all the values at a certain depth before moving to the next depth (alternatively see this as getting numbers across the breadth of the tree.) For example, if a tutor asked you to get the numbers in order of how close to the root of the tree they were. A normal traversal would not be able to do this, thus you would have to do a breadth first traversal in order to answer the question. Another good example would be searching for a closest ancestor of two people, a basic depth first traversal may get the correct answer on many occasions; however, it would give erroneous answers with more complex trees. If a breadth first traversal was used it would always give the correct answer.

For example, on the tree above a breadth first traversal would give the result:

4,2,6,1,3,5,7

As you can see the numbers are placed in order of their depth, 4 is the root of the whole tree, 2 and 6 are the subnodes of 4. Then finally you get the subnodes of the subnodes (1,3,5, and 7.)

Breadth First Tree Traversal

 

In order to do such a traversal you will need to have two methods and a queue. The first method deals with the root and its subnodes and the second method will deal with all other nodes.

Basically you should dump the subnodes into the queue, then remove one from the queue and repeat the process. The code does a good job in explaining this.

 Now for the code, full source code is available at the bottom of this post.

Basic Traversal

/**
* Simply runs a basic left then right traversal.
*/

public void basicTraversal()
{
//Check if we can go left
if (left != null)
{
left.basicTraversal();
}

//Add the root
list.add(root);

//Check if we can go right
if (right != null)
{
right.basicTraversal();
}
}

 

Breadth traversal

  /**
* Runs a breadth first traversal
* This method runs once to get the root and it’s subnodes, then runs
* another method to continue the traversal
*
* @param list ArrayList to store the roots in
* @return ArrayList of type Integer
*/
public ArrayList<Integer> getBreadthTraversal(ArrayList<Integer> list)
{

//Add the root to the arraylist, we know it is always the first entry.
list.add(root);

//Basically we add the first set of nodes into the queue for
//traversing.

//Query if left exists
if (left != null)
{
//Then add the node into the tree for traversing later
queue.add(left);
}
//Same for right
if (right != null)
{
queue.add(right);
}

//Then we call the traverse method to do the rest of the work
return traverse(list);
}

/**
* Called by getBreadthTraversal, this should not be ran without it.
*
* This method completes the breadth first traversal. It will remove the
* first Tree from the queue and add their nodes (left and right)
* to the back of the queue. Then it will append the Tree to the
* ArrayList for returning.
*
*
* @param list ArrayList to list tree roots in
* @return list ArrayList
*/
private ArrayList<Integer> traverse(ArrayList<Integer> list)
{
//Keep traversing until we run out of people
while (!queue.isEmpty())
{
/**
* We take one from the queue.
* As a linked list is a queue it operates as “First In First Out”
* also known as FIFO. So if you add something to a queue you have
* to wait until all other objects are removed before you can
* access the object again.
*/
Tree p = queue.remove();

//Check if it has any subnodes
if (p.left != null)
{
//Add the subnode to the back of the queue
queue.add(p.left);
}

//Same for left
if (p.right != null)
{
//Same here, no queue jumping!
queue.add(p.right);
}

//Append to the ArrayList
list.add(p.root);
}

//And return
return list;
}

Tree example source code

 




Project management — the tire swing

Monday 3 March 2008 @ 8:07 pm

I randomly found this picture yesterday and it really describes how things are going in the group project I am involved in.

Tire swing project management

You leave the discussions from the last day thinking “Everything is all sorted, and I know how we are going to implement things.” The next day the discussion revolves around the same task after someone suggests an alternate method, yet there was nothing wrong with the first method, in fact both methods are of equal merit. However the group discusses the same thing again and again, and I just want to get round to coding the damn thing.

I’ve ended up just writing the code myself, based on one of the numerous choices, screw discussing it further we have a deadline to meet! All that matters to me now is getting the input and the output working as they should; once the program is up and running then — and only then — should we worry about refining it with more efficient methods of doing components. For example in multi threading we could use manual locks, shifting synchronized methods into their own classes, volatile methods, and so on; they all do the same thing in different ways, lets just use one, or even go for a Taguchi methodology and try two, in a prototype and see how things go. I’d rather spend my time working on a few simple prototypes (that can be utilised for the final build) that highlight the problems than spending the same time (or even longer) discussing which is best…

This post is going on for too long, rant over…