ShareThis

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, October 7, 2012

[Java] Print out numbers of Fibonacci Sequence

Java - Print out numbers of fibonacci sequence.

In preparation for my Microsoft interview, I am going over some different ways to do the Fib sequence. Here is a simple iterative method to print out n fib numbers.

public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner s = new Scanner(System.in);
  
  System.out.println("Enter n:" );
  int num = s.nextInt();
   int n0 = 1, n1 = 1, n2;
   System.out.print(0 + " " +n0 + " " + n1 + " ");
   for(int i = 3; i < num; i++)
   {
    n2 = n1 + n0;
    System.out.print(n2 + " ");
    n0 = n1;
    n1 = n2;

   }
   
 }

The interviewer could ask a number of questions from this point. E.x. Where might errors arise?

1. No cases defined for n=0, n=1, n=2
2. What if a non-number is given in input?
3. Negative number ?
4. What if a huge number is given in input? 99999999999?

These are all error-prone areas they will expect you to know to test.


Monday, August 13, 2012

[java] Is Number in Fibonacci Sequence Code

Found this code on StackOverflow. This is a very simple iterative solution to see if a given long value "n" is a part of the fibonacci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc...

int isFib (long n)
{
    int pos = 2;
    long last = 1;
    long current = 1;
    long temp;

    while (current < n)
    {
        temp = last;
        last = current;
        current = current + temp;
        pos++;
    }

    if (current == n)
        return pos;
    else
        return 0;
}

Sunday, August 12, 2012

[java] One line Fibonacci Sequence Code!

Here is a cool recursive solution to the fibonacci sequence in one line! However, runtime is O(2^n)... so iterative solution is the better route to take for performance.

int fib(int n){
  return n <= 2 ? 1: fib(n-1) + fib(n-2);
}

Friday, January 13, 2012

Android Expandable lists Java

Multilevel, expandable lists are useful user interface elements and Android does support two-level expandable lists. This fact would not be worth a blog entry if there were no traps using the widget.

Click here to download the example program.

The example program implements a simple, two-level expandable list to display color shade information.



The first thing to note is that our Activity extends android.app.ExpandableListActivity. Otherwise setting up the list is pretty similar to ordinary ListActivity classes. The tricky part is the android.widget.SimpleExpandableListAdapter.

SimpleExpandableListAdapter expListAdapter = 
new SimpleExpandableListAdapter(
 
this,
 
createGroupList(), // groupData describes the first-level entries
 
R.layout.child_row, // Layout for the first-level entries
 new
String[] { "colorName" }, // Key in the groupData maps to display
 
new int[] { R.id.childname }, // Data under "colorName" key goes into this TextView
 createChildList(), // childData describes second-level entries 
R.layout.child_row, // Layout for second-level entries
 
new String[] { "shadeName", "rgb" }, // Keys in childData maps to display
 
new int[] { R.id.childname, R.id.rgb } // Data under the keys above go into these TextViews
 
);


First of all, this adapter requires somewhat complicated (although pretty well-documented) data structures. There are two of them, one describing the first-level group elements, the other describing the second-level string elements.

Here is the first one:
List->Map
Every map describes one first-level group element. The keys in the Map are arbitrary but are specified for SimpleExpandableListAdapter so that it can map the keys in the Map to widget IDs. In our case, the data under key name "colourName" will be set as the value of the TextView with "childname" ID.

The second one is a bit more complicated.
List->List->Map
Every entry in the first List represents a group. Entries in the second List represent entries of the group and each such entry is a Map that describes one child, similarly to the description of groups. In our case, the child Map contains two entries (keys "shadeName" and "rgb") that go to TextViews ("childname" and "rgb").

So far so good. Unfortunately, SimpleExpandableListAdapter has its tricks. First of all, the adapter takes the description of one row from a layout (child_row in our case, located under res/layout/child_row.xml). When the adapter draws the expand button onto the row, it does not consider the content of the row, it simply draws the button over the row. That's the reason child_row.xml defines generous left padding for the first TextView. While this behaviour can be considered just an oddity, the fact that SimpleExpandableListAdapter must be created with identical views for first- and second-level lines is a plain bug that existed at least since m3-rc37a. Having group- and child rows with different styles is a cool and useful feature that is supported by SimpleExpandableListAdapter but does not work.

Replace this fragment:

R.layout.child_row, // Layout for the first-level entries 
new String[] { "colorName" }, // Key in the groupData maps to display
 
new int[] { R.id.childname }, // Data under "colorName" key goes into this TextView
 


with this one:

R.layout.group_row, // Layout for the first-level entries 
new String[] { "colorName" }, // Key in the groupData maps to display
 
new int[] { R.id.groupname }, // Data under "colorName" key goes into this TextView
 


and you will see, how the expandable list gets confused.

Wednesday, December 14, 2011

How to create a folder in java code

This program will create a directory to the given path.

Java Code:
1
2
3
4
5
6
7
8
9
10
import java.io.File;
 
public class MakeDirectory {
 
    public static void main(String[] args) {
        File f = new File("c:\\MyFolder");
        f.mkdir();
 
    }
}