Wednesday 5 December 2012

which one runs faster?

I heard that Iterator is much faster than for loop, but after I did a test, I found that for loop runs faster. Here is the code:


import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author chenyi
 */
public class Review {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //ArrayList default: object
        ArrayList <Integer> o = new ArrayList();
        //ArrayList String
        // ArrayList<String> s = new ArrayList();
        //ArrayList Int
        //ArrayList<Integer> in = new ArrayList();
       
        //System.out.println(o.size());
        for(int i = 0; i<90000; i++){
            o.add(i);
          //  o.add(Integer.toString(i+1));
         //   System.out.println(i);
       //     l.add(i+1,Integer.toString(i));
        }
        long forStart = System.currentTimeMillis();
       
        for(int j = 0; j<o.size(); j++){
            o.get(j);
        }
        long forEnd = System.currentTimeMillis();
       // long forend = System.currentTimeMillis();
       
        Iterator<Integer> it = o.iterator();
        long itStart = System.currentTimeMillis();
       
        while (it.hasNext()){
            it.next();
        }
        long itEnd = System.currentTimeMillis();  
        System.out.println("for loop ending time: "+(forEnd-forStart) + "\n"
                + "interator ending time: " + (itEnd - itStart));
     
       
  //      o.iterator();
       
    }
}
And the output is

run:
for loop ending time: 7
interator ending time: 15

anybody knows why?



}

Monday 3 December 2012

Shall I say byebye world this time?

This morning's TUT is my last period of CSC236. Thanks to everybody that helped me in this course. Especially to TAs and Heap, thank you so much for teaching and assisting us. Final exams are coming, hope I can survival...
Thank you again! I am really appreciated.
"Byebye, World"

Saturday 1 December 2012

Table implementation problem solved

We solved this problem by create an interface called "TableCell" which represent a cell that contains the cell data in a Table. Then we created "TableCellInt" and "TableCellStr" that implement this interface and constructed their methods to meet our requirement. Also, in this way, it is easy for us to add on more type in the future. For example, if we want a another type like boolean, we can just create another class called "TypeCellBool", and constrcut whatever features and methods we need. I may post the revalent source code later, if I have time.

Saturday 24 November 2012

Java Code Review Tool

One of the most painful but important steps of a java project, is the code review. checking and reviewing thousands of lines of some body else's code for small things like braces, name style, documentation and overall design can really knock you out and make you feel like there is a heavy rock right on the top of your heart.

fortunately, there are tools to reduce your pain and automate this precudrue as mush as possible. There are 2 tools that I am familiar with, each of them has their own features, and by using them together, really can make your life much easier.

first one I recommand is CheckStyle. But its checking rule is too strick, so you better set up your own one. It checks for the followings in your code:
  1. Java comments and docs
  2. naming conventions
  3. header
  4. imports
  5. size violations
  6. white space
  7. blocks
  8. modifiers
  9. and etc.
second one is PMD, it is meant to check the overall design of your codes. such as:
  1. empty catch block
  2. unused if statement and local variables
  3. duplicate codes
  4. classes that could be singletons
  5. inproperiate variable and method names
  6. and etc.
And also, there are still a lot of tools that you can find in interenet to help you fight, instead of wasting those extremely power tools, from now, pick them as your weapons and armors, and beat the code review.

Saturday 17 November 2012

Checked VS Unchecked exception

Checked exceptions
- any subclass extend Exception, including  Exception itself, excluding RuntimeException
- must be catch or declare in a throws clause.
- represent invalid conditions outside the direct control of the program, Such as wrong format of user input, (which we were dealing a lot during the implementation of A2 in CSC207), database problems, etc 

Unchecked exceptions
- extend RuntimeException only, but notice, RuntimeException itself is a subclass of Exception
- the programmer may not know that this kind of exceptions could be thrown, thus, they are not forced to be declared.

- represent bugs in the program, which cannot be reasonably recovered from at run time. Such as NullPointerException, IndexOutOfBoundsException etc.


Saturday 10 November 2012

When to declare "throws Exception"


 
In Java, when defining a method, we also need to declare in the method head what exception may be throw. That's what I was believing, but, in my instructor's slides, some methods she did this way, some methods she just throw exception right the way. such as following:
public void methodA() throws AnException {   
 //do something    throw new AnException();
}
public void methodA() {  
 //do the same thing    throw new AnException();
}

And I am kind of confusing in which situation we need to declare, 
is there a specific requirement or restriction? or we can do it either way depends
on our mood?
after some google, I find that there are two different major type of exceptions, 
one is checked, which does not extend RuntimeException, and those exceptions must
be declared. In the other hand, unchecked exception is the exception that extends
RuntimeException, and then either way is allowed(declare/not declare). But now I have other doubts like, in which circumstances our exception should extends RuntimeException?
what is the difference between those two? and what is the advantage on each?

Sunday 4 November 2012

leave it behind....review first

Since we are having a Midterm tomorrow, I decided to leave that implementation after I finished our second midterm....XD