Wednesday, July 22, 2009

Creating Buttons on J2ME Forms

StringItem can be used in order to create buttons on J2ME forms. Define a new Command variable in your Form and set it as defaultCommand for StringItem variable.

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;

public class FormWithButton extends Form implements ItemCommandListener{

private StringItem btnLogin;
private Command cmdLogin;
public FormWithButton(String str) {
super(str);
btnLogin = new StringItem(null, "Login");
cmdLogin = new Command("", Command.OK, 1);
btnLogin.setDefaultCommand(cmdLogin);
btnLogin.setItemCommandListener(this);
append(btnLogin);
}
public void commandAction(Command cmd, Item item) {
LoginForm.show();
}
}

Thursday, July 16, 2009

Singleton Java ME Forms

Some of the mobile forms contain only static elements, which are not interacting with the user. That kind of forms are usually aimed to provide a list of command items on them, which are redirecting users to other forms. So, it is reasonable to design that classess in singleton pattern. Aim of creating a form in singleton pattern is to ensure that a class has only one instance at a time. Instead of creating an instance of that kind of mobile forms each time, you can hide the constructor from global access and make it protected.
The instance that will represent the class is placed as a static field in the class and initialized when it is not created before.
import javax.microedition.lcdui.Form;

public class SingletonForm extends Form {

 private static SingletonForm instance;

 public static SingletonForm createNew() {
  if(instance == null){
   instance = new SingletonForm(""); 
  }
  return instance;
 }
 
 public void showNew() {
  MainMidlet.getInstance().getDisplay().setCurrent(this);
 }
 
 protected SingletonForm(String str){
  super(str);
  append("SingletonForm!");
 }
}


This is the 'not-thread-safe' version of singleton pattern. If you want it to be thread-safe then use locking. In order to call an instance of the above class you can type in your caller:
SingletonForm.createNew().showNew();

Friday, June 12, 2009

Character Frequency Checker in Ruby

Character frequency checker which is implemented in Ruby, can be used to find the number of occurrences of each consecutive series of the same character in a given string. In the inspected string, that consecutive series of the each character is replaced by a two element array where the first element is a string containing the character, and the second element is the number of repetitions. For example,if the method receives a parameter like "aaassssddddfgh", then the result will be stored in an array where its elements are:
[[a,3],[s,4],[d,4],f,g,h]
class CharacterFrequency

  def char_frequency(sequence)
    return_array = Array.new
    if (!sequence.eql?(nil) && sequence.size>0)
      puts "String to check for character frequency is : "+sequence
      last_checked_char_index = 0
      for i in 0...sequence.size
        next_char_index = last_checked_char_index + 1
        repeating_char_count = 1
        if(i >= last_checked_char_index)
          for j in i...sequence.size
            if(sequence[j].eql?(sequence[next_char_index]))
              repeating_char_count = repeating_char_count + 1
              next_char_index = next_char_index + 1
              last_checked_char_index = next_char_index
            else
              last_checked_char_index = next_char_index
              arr = Array.new
              if repeating_char_count > 1
                arr = sequence[i].chr.to_s+","+repeating_char_count.to_s
              elsif repeating_char_count == 1
                arr = sequence[i].chr.to_s
              end
              return_array.push(arr)
            break
          end
        end        
      end      
    end
    if(!return_array.empty? && return_array.length >0)
      for indice in 0...return_array.size
        puts return_array[indice]
      end
    end
    else
      puts "Empty or null sequence!"
  end
  end
end

app = CharacterFrequency.new
app.char_frequency("laaamburtiiifikasssssyon")
app.char_frequency("aaassssddddfgh")
app.char_frequency("abcdeee")
app.char_frequency("a")
app.char_frequency("ab")
app.char_frequency("abBgGvV122")
app.char_frequency("asdadddssf")
app.char_frequency("")
app.char_frequency(nil)


Save your script into frequency.rb file and run as follows. It will loop through the created array and display elements at console. Tested with ruby 1.8.7 on ubuntu9.04.
$ ruby frequency.rb