Thursday, June 11, 2015

Goodbye blog header

Removing my blog's header to make it cleaner. Here's a tribute to good 'ol header


Get it? a drawing of a head for a header. LOL *Die Die die*
*Salute

Week 0 Set-up and Mandatory Hello World-


[draft] Windows

Install java

1

1. Head over to android developer site


2. Install ADT

3. Hello world part

Sunday, March 8, 2015

self.debug 01 : Competitive programming and Math


Bug

Note: I wrote this last year, before I began reviewing my computer science foundations

I found out that I suck in coding solutions for algorithmic problems . I suck so  hard that I can't get the highest score  for the 1st Project Euler problem  . Don't get me wrong, I was able to code a working solution but I wasn't able to use Math ninjutsu  and make it run as fast as O(1).

The problem is described below:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.

My initial solution was to loop through all the numbers less than N ( brute force and the slowest design ever), check if it is a multiple of 3 or 5, then add it to the sum. This would work but the running time is O(n). Hackerrank would only give you 40 out of 100 points.

It took me a half a day (alt+tab between work and the problem ) to come up with a solution. I remembered that this is related to a BASIC arithmetic series whose equation is described below:

f(n) =  n( a1 + an / 2)

fair enough I coded the function and got the sum for the multiples of 3 and 5. The problem is, the two series converge at some point. i.e 15, 30 .. numbers divisible by 15.  I deducted the sum for 15 and tadaaa I got O(1). Actually it's another half day trying to figure out how to make BigDecimal faster!!! So all in all I spent almost day at work (perhaps 3- 4 hours to be exact minus the work stuff) to solve a single Project Euler problem!

*gasp*


!? *gasp*

Steps to replicate

Solve a project Euler challenge in  hackerrank Lie down, roll,  cry...


Root Cause

TL;DR
Yep! I'm pretty sure it's another conscious incompetence moment
  I had to debug myself and listed the following reasons:

The illusion of competence

After reading  "A mind for numbers" I realized that, I was under this illusion that I'm good at Math because I was fast in understanding the concepts. I should have known early on that being good at something  doesn't necessarily require being a fast learner, but what's important is that you have been able to to chunk the concepts and you have them cemented into your long term memory. If I have known memory chucking techniques before, perhaps solving the hacker rank problem had been a different experience

I admit that I was hell of a lazy student. Instead of taking down notes , I would doodle on my notebook , and instead of spaced repetition and practice I would often just study before the exams.

I also never got the chance to use Math in my development career. Don't get me wrong, Math has always been there, but they had been abstracted by some library or API.

So yeah I think that's about it, minus the ego and pride stuff. 

Solution

What I plan to do is to review everything from scratch, This time using proper learning techniques etc. This would be beneficial to my career and to also to my future  kids :P 


Arithmetic sequence and series song



Play this on the  background then sing the lyrics :
https://www.youtube.com/watch?v=SswjdoAAJb8


An arithmetic sequence has a constant difference
The sum of athe sequence is series

In order to get the nth term
you must know the first term
then you add the constant difference

Chorus
That's multiplied, that's multiplied
to n minus one
That is how you get the nth Term

An arithmetic sequence has a constant difference
The sum of the sequence is series

In order to get the sum of the sequence
you must know the first term
then you add the nth term

Chorus
That's multiplied , that's multiplied
by n over two
That is how you get the sum of terms

Useful links:
https://www.khanacademy.org/math/precalculus/seq_induction/seq_and_series/v/explicit-and-recursive-definitions-of-sequences


Idea :
Math formula karaoke
Math funny visual aids




Sunday, November 24, 2013

hibernation cancellation

I bought a graphic tablet last wednesday ! Yey


Clarice in pink <3 p="">

Flat Dinosaur Kid ( 'cause I lost my dinosaur man shirt :( )


Titus as Totoro




Sunday, December 2, 2012

Set-up Amazon EC2 instace[ubuntu] with Tomcat6 and MongoDB

Requirements

1. AWS Account .You may check it here
2. EC2 Instance. Check the online guide here

Enable SSH connection

1. Inside your Amazon EC2 Dashboard, go to Security Group


2. Define inbound connections to your instance.
SSH 
port range :22
HTTP
port range : 80
default Tomcat 
port range : 8080
Source : [IP]/32

be sure to apply the rule changes



3. Get Default Public DNS of your instance



Connect to instance via putty

Check this link for the detailed instructions to connect to your instance via putty

Install Tomcat

1. Connect to your instance via putty (check link above) . the default login for an ubuntu instance is ubuntu lol
2. Install tomcat.
   sudo apt-get install tomcat6 tomcat6-admin
3. Edit users
     sudo vi /etc/tomcat6/tomcat-users.xml
4. Restart tomcat
   sudo /etc/init.d/tomcat6 restart
Notes 
You may deploy your web-applications via SFTP to
/usr/share/tomcat6/webapps
5. Test your installation via browser
http://[PUBLIC DNS]:8080/

Install MongoDB

1. download mongoDB
 curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.2.2.tgz
2. extract tar file
tar -xzf mongodb-linux-x86_64-2.2.2.tgz
3. Start MongoDB
%MONGO_HOME%/mongod --fork --logpath ~/mongod.log  --dbpath /home/ubuntu/db
4. check if the process is running 
ps -ef|grep mongod
note: change the logpath and dbpath
more info : http://www.mongodb.org/display/DOCS/Amazon+EC2

Thats it :D 

Thursday, November 22, 2012

:D

Did this line art when I met Clarice way back December 2009 :) 

Monday, November 19, 2012

MongoDB sample app

Too lazy to write something haha. might as well give the github link

https://github.com/bertanasco/HelloMongoDB

pom.xml

HelloMongoDB.java


Output

 Reference
http://www.mongodb.org/display/DOCS/Java+Tutorial#JavaTutorial-MakingAConnection

Happy coding :D Zzzzzzzz

MongoDB windows set-up

Sharing how I did my  MongoDB set-up :D

1. Download MongoDB

http://www.mongodb.org/downloads

2. Extract zip file

3.  Install and run MongoDB as service


Notes
--dbpath  :  the data folder where mongoDB would store it's Files
--logpath  : the path to the file where the service installation log would be written
--install    : installs mongoDB as a windows Service
--serviceName : obvious :P
--help  for more info

The logs below indicate that you have successfully installed mongoDB as a windowsservice


Open local services  to verify


type net start MongoDB to start the recently installed windows service

4.Check MongoDB

to view the db startup, open the log file that you used during installation. You should have something similar to the picture below:



open web admin console



Links

Saturday, November 17, 2012

Basic TDD with Jersey Test Framework

Decided to do TDD on one of  my mobileTAO initiatives. I hope this may also help others who wish to learn TDD with Jersey test framework

1.Requirements

1. JDK 1.6
2. Eclipse Indigo
3. Jersey
4. Jersey Test Framework
5. Maven 3.0.4 (with m2eclipse plugin)

2. Create maven project

run
mvn archetype:generate -DgroupId=desired.group.id -DartifactId=desiredArtifactId -Darchetype.ArtifactId=maven-archetype-webapp -DinteractiveMode=false

run mvn eclipse:eclipse in order to support eclipse

 Import project to eclipse

3. Setting up Dependencies 

Add the jersey dependency to your pom.xml

4. Create the test 

We will now code our unit test. add the source code to src/test/java


5. Create rest service




Usefull Links
http://jersey.java.net/nonav/documentation/latest/getting-started.html
http://www.parleys.com/#st=5&id=2723&sl=0

Thursday, November 15, 2012

Blog

BLOG
It's been more than a year since my last blog entry. :) I think It's time to fix some of the typos and grammatical errors on my old entries and start blogging again. I'm studying for my OCJP exam and I think blogging about it would benefit me and the readers ( if I have one LOL)

Palindrome

Palindrome

below is my take on the palindrome challenge posted by a friend on FB
Code #2: Make a function 'is_palindrome' where it takes string as an input and returns a Boolean value if it is a palindrome or not. The function should follow recursive function calls so for/while loops should not be seen in the code. 

Monday, September 12, 2011

Method Members

Every book I've read always mentioned about class members and interface members  but there isn't much written about method members (I don't know if that term fit). I did a lot of experiments and was able to come up with the golden rule on how to determine if a declaration on a method is valid:

    "Only inner classes and local variables are allowed inside a method and that the only allowed modifiers  are  final and abstract (for inner classes only )  "
So the following are all valid :
public class MemberTest {

    public void method1() {
        final int x = 1;
        int y = 2;
      
      
        abstract class InnerAbstractClass {
      
        }
      
        class Inner {
      
      
        }
      
        final class InnerFinal {
      
        }
           
    }   

}
note: an inner class is a non-static nested class


Invalid members:

public class InvalidMethodMembers {

    public void method1() {

       
        static class InnerAbstractClass {
       
        }
       

        final static class InnerStatic {
       
        }
       
        enum E { }
        interface I { } //a nested interface is explicitly static
       
       
    }
   
}

By now you might be wodering why I even bothered to come up with that statement LOL. but tell you what, said fact would really be  usefull on say your OCJP exam  where you can encounter tricky questions ,specially about the inner and nested class :) .



MingMing


for Clarice :)

Thursday, September 8, 2011

Inherittance , Overriding, and Hiding

For a while now , I thought overriding was , well overriding , where you override a method on a super class with your method with the same method signature. It was only when I've read about overriding's formal definition on the JLS when I realized that this was only partially true.

This article has the following sub-topics :
[I] formal definition of overriding
[II] hiding
[III] rules for hiding and overriding

[I] Formal definition of overriding


The JLS states that :

An instance method m1 declared in a class C overrides another instance method, m2,
declared in class A if all of the following are true:
  • C is a subclass of A.
  • The signature of m1 is a subsignature (§8.4.2) of the signature of m2.
  • Either:
  • m2 is public, protected, or declared with default access in the same package as C, or
  • m1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3
  • overrides m2.
Moreover, if m1 is not abstract, then m1 is said to implement any and all
declarations of abstract methods that it overrides.

Let's dissect this to see what we can get :

An instance method m1 declared in a class C overrides another instance method, m2,
declared in class A if all of the following are true:

The definition specifically stated that overriding are for instance methods . So what are these methods? simple :P non-static methods (very noob indeed) ( This is the key in differentiating overriding over hiding )

From here on out we've narrowed down the scope of overriding . We just have to keep in mind that overriding are for non-static methods



The code above would print :
test overriding [ baby]

It's pretty obvious that the methods that you would override are from you're superclass, so I guess I'll proceed with the next statement

The signature of m1 is a subsignature (§8.4.2) of the signature of m2.

How could you determine that a method is a subsignature of the other? The JLS states :

The signature of a method m1 is a subsignature of the signature of a method m2 if
either:
• m2 has the same signature as m1, or
• the signature of m1 is the same as the erasure of the signature of m2

Two methods have the same signature if they have the same name and argument
types

oh oh :D so first we only need to worry about non-static methods, now what the JLS is saying is We should always remember that overriding is about methods having the same signatures, anything else is not overriding ( huwow stating the obvious haha)

see code below as an example:


The method print with the ellipses , is not overriding the print method on class A ( Yeah I hear you back there, and yes I admit I'm noob lol , and yeah I know it's overloading)

The next statements after that are boring to discuss lol.

I bet by now you have the following questions in mind,
Q.1 what will happen if the method from the parent class is static and your overriding method is non static?
Q.2 what if the method from parent class is non-static and the the overriding method is static?
Q.3 and what if the overriding and the overridden method are both static?

We have the following answers
Q.1 what will happen if the method from the parent class is static and your overriding method is non static?

public class C extends A{

//instance method, this is overriding
public void print(String x ){

System.out.println(x + " [baby] " ) ;
}


public static void main (String [] args) {

C c = new C () ;
c.print ("test overriding");
}

}

class A {

//isntance method
static public void print(String x ){

System.out.println(x + " [mama] " ) ;
}


}

A.1. This would result into a compile time error . (overridden method is static)

Q.2 what if the method from parent class is non-static and the the overriding method is static?
public class C extends A{

//instance method, this is overriding
public static void print(String x ){

System.out.println(x + " [baby] " ) ;
}


public static void main (String [] args) {

C c = new C () ;
c.print ("test overriding");
}

}

class A {

//isntance method
public void print(String x ){

System.out.println(x + " [mama] " ) ;
}


}
A.2. Same with the first answer, it would produce a compile time error. ( Overriding method is static )


Q.3 - what if the overriding and the overridden method are both static?
public class C2 extends A2{

//class method
public static void print(String x ){

System.out.println(x + " [baby] " ) ;
}


public static void main (String [] args) {

C2 c2 = new C2 () ;
c2.print ("test hiding");
}

}

class A2 {

//class method
public static void print(String x ){

System.out.println(x + " [mama] " ) ;
}


}

A.3 The code would compile, but this is actually not overriding , this one is called hiding. Yes that's the difference , you're probably cursing yourself by now knowing that It's the sole difference lol :))

overriding = non-static
hiding = static :)

[II] Hiding

So basically, Hiding is overriding , except that you use this for static methods

Well that one went well :))

[III] rules for hiding and overriding

We still have these lingering questions in mind ( since we mentioned that only the method signatures should be the same ) :
Q.1 What if they have different return types?
Q.2 What if the overridden method is throwing an exception?
Q.3. What if they have different access modifiers?
Q.4 Why am I still writing this blog entry? LOL


Q.1 What if they have different return types?
A.1 This is allowed , provided that the overriding method's return type is return-type-substitutable for the overridden method.
you didn't see that coming huh? :D all this technical words are getting into you're head huh? makes you wanna scratch your head huh? lol (corny mo bert). how can a lone soul like me determine if a return type is return-type-substitutable?
JLS! Basa!
A method declaration d1 with return type R1 is return-type-substitutable for another
method d2 with return type R2, if and only if the following conditions hold:

  • If R1 is void then R2 is void.
  • If R1 is a primitive type, then R2 is identical to R1.
  • If R1 is a reference type then:
  • R1 is either a subtype of R2 or R1 can be converted to a subtype of R2 by unchecked conversion (§5.1.9), or
  • R1 = |R2|
If R1 is void then R2 is void. The code below fairly displays this rule. if you have a void return type on the parent class' instance method, you're overriding method should also be void. The code below would generate a compile time error (Attempting to use incompatible return types)


class A {

//isntance method
public int print(String x ){

System.out.println(x + " [mama] " ) ;
}


}

public class C extends A{

//instance method, this is overriding
public void print(String x ){

System.out.println(x + " [baby] " ) ;
}

public static void main (String [] args) {

C c = new C () ;
c.print ("test overriding");
}

}

If R1 is a primitive type, then R2 is identical to R1

We all know the primitives, int , short, long, double , float etc. , So the rule is that , they should always be identical (just like the rule about void return types ) , The code below would generate a compile time error (Similar to those on the previous code example)

class A {

//isntance method
public double print(String x ){

System.out.println(x + " [mama] " ) ;
return 1;
}


}

public class C extends A{

//instance method, this is overriding
public int print(String x ){

System.out.println(x + " [baby] " ) ;
return 1;
}

public static void main (String [] args) {

C c = new C () ;
c.print ("test overriding");
}

}

If R1 is a reference type then:

  • R1 is either a subtype of R2 or R1 can be converted to a subtype of R2 by unchecked conversion (§5.1.9), or
  • R1 = |R2|
to give you an example of the rule regarding R1 being a subtype of R2, refer to the code below:


the code would compile just fine. however if we change the return type of the method in C3. we would get a compile time error stating that we are attempting to use incompatible return type:





Q.2 What if the overridden method is throwing an exception?
An overriding class is only allowed to throw an exception that is not new or that is not broader than the exception.

Consider the following code :



The code would compile just fine, but if we interchange the exception types on the overriding and overridden methods we would get the following compile time error:



Q.3. What if they have different access modifiers?

The overriding instance method's access modifier should be less restrictive than the instance method that we are trying to override.
the order of the access modifiers based on restriction is as follows ( descending )

  • private
  • default
  • protected
  • public.
You could check the example below to understand further :



IF you try to use public on the parent class, and use a private access modifier on the child class, you would get the following exception :




Q.4 Why am I still writing this blog entry? LOL
be passionate on what you do and everything else will follow.