check if given number is prime

This problem in general is very complicated, but when we don’t need to be 100% sure (but probability is near 1) about our results there is a very easy way to do this.

We’ll use method isProblablePrime from class BigInteger.

This method as a argument gets probability level, it’s calculated as 1 – 1/(2^x), where x is argument, so for x = 20, we can be nearly fully sure about result, the probability is 0,99999904632568359375. For smaller numbers this method gives even more sure results.

How to use it:
long x = some_value;
BigInteger bi = BigInteger.valueOf(x);
boolean isPrime = bi.isProbablePrime();

Firstly we set x to some value, then a new BigInteger is created based on x value, next we use method to check if it’s prime.

more information about BigInteger you’ll find here: http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html

adding icon to your MIDlet

Hello, this topic is also not very complicated, so the post will be quite short.
We have to remember about some rules before we add icon to a MIDlet:

  • Only PNG’s are allowed
  • icon’s size is not standarized
  • some phones will expand smaller icons, some won’t
  • the icons that are to big might be shrinked or cut to smaller, we can’t decide
  • I recommend using size 32×32, it is smaller than most phones can hold, so at least we can be sure, that it won’t be cut
  • icon files, if we want to reffer to it as: “/icon.png” should be placed in src directory in project’s path I think it’s the best way to do this (this file will be automatically added to newly created JAR file)
  1. After starting Netbeans we go as before to project’s properties, here we open “Application Descriptor” again, then we select second tab:
  2. Now, after choosing “Edit…” we can select file:
  3. Next, we have to add appropriate attribute to our JAD and manifest. Let’s click “Add…”, automatically first to add will be icon attribute:
  4. At last you can run this MIDlet in the emulator (or upload to your mobile). You should see icon you have selected for your app.

JAD and JAR files

Today’s post will be divided into 2 parts, first will cover JAD files and second will be about JARs.

Part I

JAD stands for Java Application Descriptor, it’s a special file prepared (mostly) by IDE in the process of building/compiling project. It’s used (as the name indicates) to describe file containing application (JAD doesn’t contain application). JAD’s are used to set/provide information about program’s attributes like:

  • information about size
  • application’s name
  • version
  • vendor (author)
  • icon path
  • comment
  • MIDP/CLDC requirements

It will be mostly appreciated by users, who want for example to install program over the internet. They need only to download JAD, than they’ll be able to see programs requirements and size, and decide if they really want do download it or resigin (for example if data transfer costs are to high).

JAD file has to provide informaiton about application’s path, it might be only relative path (like: HelloMIDlet.jar) or location in the web (like: example.com/HelloMIDlet.jar) then jar file will be downloaded (of course after confirmation from user).

Part II

JAR stands for Java Archive, it’s really normal zip archive (you can unzip it using your normal program that you use everyday). It contains compiled versions of your classes (files *.class), additional files (like: graphics, icons, text files). It has also very important file MANIFEST.MF in
META-INF directory. This file is very similar to JAD.

Table containing all attributes names and information where it might/must occur:

Attribute
Manifest
JAD
MIDlet-Name
Must
Must
MIDlet-Version
Must
Must
MIDlet-Vendor
Must
Must
MIDlet-Jar-URL
Must
MIDlet-Jar-Size
Must
MIDlet-Install-Notify
May
May
MIDlet-Delete-Notify
May
May
MIDlet-Delete-Confirm
May
May
MIDlet-Info-URL
May
May
MIDlet-Description
May
May
MIDlet-Icon
May
May
MicroEdition-Profile (1)(2)
Must
Must
MicroEdition-Configuration (1)(3)
Must
Must
MIDlet- (1)(4)
Must
Must
MIDlet-Push- (5)
May
May
MIDlet-Data-Size
May
May
MIDlet-Permissions
May
May
MIDlet-Permissions-Opt
May
May

from: http://developers.sun.com/mobility/midp/ttips/getAppProperty/

For us only some attributes will be useful (they are listed in part II). To learn syntax of files JAD and manifest you can just look at them in our first application.

Soon, I’ll be writing about how to set and modify application’s attributes within Netbeans, what is much easier and pleasant.