| Subcribe via RSS

Tutorial : Java Random with scaling

September 2nd, 2010 | No Comments | Posted in Knowledges

I have been playing around with Random class in Java. Unfortunately, this class doesn’t provide us some methods we need. Such as, limit the range between -50,50 or get random double from 1 – 1000. Something like this we can fine tune. I copied some good practice from this website.

Here are some examples of these operations:

* Suppose you are writing a game program that simulates throwing dice, and so need a random integer in the range 1 to 6. “nextInt” can give you one in the range 0 to 5, and you can translate this to the range you need:

int throw = generator.nextInt(6) + 1;

* In drawing a pattern made up of random lines, you want to pick a random angle between 0 and 360 degrees at which to draw a line. The angle can be any real number. The “nextDouble” message will give you a random real number, but between 0 and 1. You can use scaling to turn this into a real number between 0 and 360:

double angle = generator.nextDouble() * 360.0;

* Suppose the same pattern-drawing program also needs to pick random lengths for the lines, but that the lines should never be shorter than 10 units, nor longer than 50. Line lengths can be any real number between these limits. Thus you need random lengths from a 40-unit range starting at 10. You can use scaling and translation together to generate these numbers from “nextDouble”:

double length = generator.nextDouble() * 40.0 + 10.0;

How to : Java add custom annotation

August 13th, 2010 | No Comments | Posted in Java

From Java 5, there is a new feature called annotation that provided easier to tag or mark class or method. For example, in JUnit we can ust @Test above a method to let the JUnit engine knows that this class is a test case. Moreover, we can also implement our own annotation as well.

Reference: http://technicalmumbojumbo.wordpress.com/2008/01/13/java-custom-annotations/

Java Reference: http://download.oracle.com/javase/1.5.0/docs/guide/language/annotations.html

I used that reference to implement my own annotation at the first place, but I’m going to memorise that as well in this post.

First, you need to create a new class and make it annotation.

1
2
3
4
5
@Target(ElementType.TYPE)
public @interface IgnoreClass
{

}

The code above will let the java compiler knows that this class is an annotation. You will notice that instead of writing public class IgnoreClass, we write @interface to let the compiler knows. Above that we see @Target and @Retention. First @Target is a tag where we tell the compiler that this annotation can be used in Class, Method Or Field. This is the options we can use.

TYPE: Class, interface, or enum (not annotation)
FIELD: member fields (including enum values)
METHOD: methods (does not include constrcutors)
PARAMETER: method parameter
CONSTRUCTOR: constructor
LOCAL_VARIABLE: local variable or catch clause
ANNOTATION_TYPE: Annotation types
PACKAGE: java package

That’s it, now we have our own annotation to use, we can also have fields in annotation as well.

So this is how we use the annotation

1
2
3
4
@IgnoreClass
public class AClass
{
}

And this is how we check that the class has such annotation

1
2
3
4
5
6
Class[] classes = {AClass.class};
for (Class classObj : classes)
{
     Annotation[] annotations = classObj.getAnnotations();
     // And then we can loop through this array and cast back to the annotation we want.
}

This is a very easy example for creating our own annotation. There are lots of things you can do with annotation. So, you might want to have a look more at the references above.

Tutorial : J2ME and Netbeans : How to test RecordStore with emulator

August 11th, 2010 | No Comments | Posted in J2ME, Java

I’m using Netbeans 6.9 and implementing RecordStore and I want to test that with emulator. How would I do that? because every time I close the emulator, the state will be deleted or gone in some way.

This is my way of doing that. Basically, you just have to actually install the application in the emulator and when you exit the application you can enter the application again. Just like in a real phone.

So, you just have to use OTA install.

In Netbeans 6.9.
1. right click on your project and click “properties”
2. In running click at “Execute through OTA (Over the air provisioning)”.
3. That’s it, so when you start the application, it will be actually installed inside emulator and you can close and open the application anytime you want.

If XML file is too big for AJAX

August 11th, 2010 | No Comments | Posted in Knowledges

http://www.developer.com/lang/php/article.php/3897851/article.htm

I found this article which is quite useful. What will you do if XML file is too big.

1. Transform the XML documents into a character string.
2. Split the character string into equal chunks (400 characters).
3. Save each chunk as a value in an array.
4. Transfer the chunks one by one using the Ajax mechanism into the final XML document.

How to: Unmanaged C++ read/write with MSXML

August 7th, 2010 | No Comments | Posted in .NET

I have been scouting all over the Internet of how to use MSXML to read/write XML. This is the article I found which is very clear. So I won’t have to write anything else.

http://www.informit.com/articles/article.aspx?p=169461

How to : C++ Convert between various String types

August 6th, 2010 | No Comments | Posted in .NET, Programming

Just to remember.

http://msdn.microsoft.com/en-us/library/ms235631(VS.80).aspx