April 2007 - Posts
I'm currently suffering from a terrible back pain... I wasn't able
to get much sleep before my examination day because I was so nervous
and afraid that I would fail. God on the other hand, was very forgiving
and in spite all the sins I've done he still answered my prayers(during
the exam) and I passed with a passing score of 79% (57 / 79). I'm so
proud that I passed the exam, it was the fruit of my hard work. For
those who are planning to take the exam I would recommend:
- Buying K & B Study guide for Java 5
- Examulator (Marcus Green's) simulator
- Eat a lot of peanuts(the one which has lots of proteins)
- Drink lots of water before your exam, be sure you don't drink too much that you'd have to pee all the time...
- Eat some meat, TAPSILOG did great for me. :)
- Get some sleep(I didn't, don't follow my example on this)
- Fix whatever issue you have to fix with your girlfriend (I was caught up in a fight and that added to my stress factor)
I really wanted to score higher, around 90%. Unfortunately I wasn't good enough to score that high. But I'm happy I passed.
The thing I hate about SQLServer is that it gives me too much headache whenever I use it with Java. Since I already had share of of those from my last company, I already had some precautions.
- Use jtds driver
- Enable TCP/IP on default port 1433
- For SQLServer 2000 be sure to atleast upgrade to SP4
- And many others
Last night I wanted to test the CRUD feature of netbeans 5.5. I needed to generate Entity Classes from the database using one of the wizards. However, although I was able to connect to the database it didn't list the list of tables. I tried to check my JDBC url. I had to set this through the connetion property at the runtime view BTW.
jdbc:jtds:sqlserver://127.0.0.1:1433/ - OK
net.sourceforge.jtds.jdbc.Driver - OK
It took me until today to figure out and remember that my friend Sam set my username as an alias of dbo. What is dbo BTW? Like I care...
Anyway, in the connection properties my schema was set to wpinventorydb which was the name of my database. After a few trial and error I noticed that when I try to create a table through the runtime view (which failed BTW) the error message would say something like:
Unable to execute command:
create table "wpinventorydb."tablename"
{
"id" TINYINT
}
Hmmm... "wpinventorydb."tablename".... That rings a bell. I remember when I created a database through the SQL Server Management Studio(I'm using SQLServer 2005) my tables were saved with a name prefixed by dbo. So I just changed the schema from the connection properties from wpinventorydb to dbo then I became happy.
I'm not sure of the reason why, but it did the job.
The java.util.Comparable and java.util.Comparator interfaces together with the java.util.Collections allow the user to sort a List. An implementation of the Comparable interface allows the user to create a specific strategy to compare with another object(which should be mutually comparable). For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Employee implements Comparable<Employee> { private String name; private int age;
public Employee(String name, int age) { this.name = name; this.age = age; }
public int compareTo(Employee e) { //comparison strategy }
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Main { public static void main(String[] args) { List<Employee> employeeList = new ArrayList<Employee>(); employeeList.add( new Employee("Tim", 10) ); employeeList.add( new Employee("Rolvin", 11) ); employeeList.add( new Employee("Gerald", 12) );
Collections.sort(employeeList); } } |
the Comparator on the other hand doesn't tie you with a single comparison strategy. The java.util.Collections class has an overloaded sort method(i.e. other than sort(List) that accepts a
List and a Comparator as an argument. For the sake of this example, I will not be providing any code for the implemented methods. But just to let you know that the compareTo() method for Comparable and the compare() method for the Comparator interfaces both return an int value. Just to guide you what value you should return here is a guide based on the K & B book for SCJP 5.0
obj1 < obj2, return a negative value
obj1 == obj2, return a zero value
obj1 > obj2, return a positive value
Ok, so as we can see from the above example, we are tied with the Employee class's implementation of compareTo(). If we wanted to sort by employee names, we'd be tied to that for the rest of our lives(unless of course you subclass Employee). This is where the Comparator interface comes in handy. With the Comparator interface you are not tied to a single implementation. So if we wanted to sort the employeeList by name or by age we could create separate classes for each of those.
1 2 3 4 5 6 7 | public class SortByName implements Comparator<Employee> { public int compare(Employee e1, Employee e2) { //comparison strategy here } } |
1 2 3 4 5 6 7 | public class SortByAge implements Comparator<Employee> { public int compare(Employee e1, Employee e2) { //comparison strategy here } } |
and finally be able to use it like this
1 2 3 4 5 6 7 8 9 10 11 | public static void main(String[] args) { List<Employee> employeeList = new ArrayList<Employee>(); employeeList.add( new Employee("Tim", 10) ); employeeList.add( new Employee("Rolvin", 11) ); employeeList.add( new Employee("Gerald", 12) ); Collections.sort(employeeList, new SortByName() );//sort by name; Collections.sort(employeeList, new SortByAge() );//sort by age; } |
I'm still under preparation for the SCJP 1.5 exam. I don't care what others say about it, I'm taking it. Though if I fail I swear never to talk about it again. I was suppose to take it last March but then I thought it would be a good idea to postpone it for a while since the holy week would be a good time for me to gain more solid understanding of the language.
Yes, I know certifications are not ways to determine the capability of the programmer. But believe me, the roadpath that you take while learning stuff you missed during work is worth the effort. It actually happened to me before while working on a statistics-related stuff project and it's good that I read on the narrowing and widening stuff which I could never have known during the timespan of that project if I never studied for the exam.
I believe that the SCJP 1.5 is one of the hardest exams in the I.T. world. If I pass, I plan to take the SCJD afterwards(maybe sometime next year). For those who are not familliar with the SCJD, in nutshell it's a hands-on exam in Java(they say it's one of the unique exams available in the I.T. world).
Ok, let's get to the main purpose of this post. A proof of concept that arrays are indeed Objects.
Take a look at this code; //Oooops not that one
Take a look at this code: //the one below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Main { public static void main(String[] args) { int[] iArray = new int[2];
iArray[0] = 1; iArray[1] = 2;
for (int i : iArray) { System.out.println("Value: " + i); }
}
} |
Not the most elegant code you've seen, I know. But it should compile just fine(untested BTW)
Now, there are strange way of doing things. But it's important that you(actually I) know this for the exam.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Main2 {
public static void main(String[] args) { Object arrObj = new int[]{1, 2};
int[] iArr = (int[])arrObj;
for(int i : iArr) { System.out.println("Value: " + i); }
}
} |
The code above should compile just fine(again, untested). You might be wondering if it's possible to access the elements using an index from the arrObj reference like this
arrObj[0];
NO! It's not possible. Why? Because the int array object or int[] has already been widened to an object. Because of this, the cast at line 8 was necessary. With the cast having done, it would now be possible to access the elements through the iArr reference like this
iArr[0] = 6;
iArr[1] = 23;
and being more weird, it's also possible to do the following without the iArr reference.
( (int[])arrObj )[0] = 27;
Seriously, I'm getting very very very nervous for the exam. But stay tuned for more weird Java stuff. Laterz! ;)