Date, numbers and currencies

April 21st, 2008 by Boogie

Some code snippets for constructing objects of Date, Calendar, DateFormat, NumberFormat, Locale:

Date d = null;

Calendar c = null; DateFormat df =

null;

out.println(“java.util.Date”);

out.println(“java.util.Calendar”);out.println(“java.text.DateFormat”);

 

NumberFormat nf = null;Locale l =

null;

out.println(“java.text.NumberFormat”);

out.println(“java.util.Locale”);

 

d = new Date();

//d = new Date(”2008-08-08″);// at Runtime produces IllegalArgumentException

out.println(“Date contructor With String argument is Deprecated”);

d = new Date(1123923842L);

out.println(“Date contructor With long argument is not deprecated”);

 

//c = new Calendar();// won’t compile

c = Calendar.getInstance();

//c = Calendar.getInstance(new Locale(”"));// at Runtime produces IllegalArgumentException

 

l = new Locale(“ro”);

l = new Locale(“xyzt”);// works, no runtime exception

out.println(“Because a Locale object is just an identifier for a region, \n” +

” no validity check is performed when you construct a Locale”);

l = new Locale(“ro”, “RO”);// language, country

l = new Locale(“ro”, “RO”, “XXX”);// language, country, variant - vendor and browser specific

 

//df = new DateFormat();// won’t compile

df = DateFormat.getInstance();

df = DateFormat.getDateInstance();

//df = DateFormat.getDateInstance(123L);// won’t compile

//df = DateFormat.getDateInstance(123);// at Runtime produces IllegalArgumentException

df = DateFormat.getTimeInstance();

df = DateFormat.getDateInstance(DateFormat.LONG);

df = DateFormat.getTimeInstance(DateFormat.SHORT);

df = DateFormat.getTimeInstance(DateFormat.SHORT, new Locale(“xyz”));// with Locale also

 

//nf = new NumberFormat();// does not compile

nf = NumberFormat.getInstance();

nf = NumberFormat.getNumberInstance();

nf = NumberFormat.getCurrencyInstance();

nf = NumberFormat.getInstance(new Locale(“ro”));

nf = NumberFormat.getNumberInstance(new Locale(“ro”));nf = NumberFormat.getCurrencyInstance(new Locale(“ro”));

Posted in SCJP | No Comments »

Flow control, exceptions and assertions 2

April 20th, 2008 by Boogie

Continuing going through the chapter 5 from the SCJP book, I find :

  • The “Enhanced For Loop”

int[] a = {1,2,3};

for (int n : a)

out.println(“next element: “ + n);

 

int[][] a2 = {{1},{1,2},{3}};

for (int[] n : a2)

out.println(“next element: “ + n);

for (int n : a2[1])

 

out.println(“next element: “ + n);

for (Object n : a2)out.println(“next element: “ + n);

  • Break and Continue statements

//labl1: int a = 1;// does not compile

 

int a;

labl1: a = 1;// OK, you can label this instruction

 

for (int i=1;i < 5; i++){

//break labl1;// does not compile

}

 

LblOuter:

for (int i=1;i < 5; i++){

LblInner:

for (int j=1;j < 5; j++){

if (((i+j)%6) == 0){

out.println(“Breaking out of LblOuter”);break LblOuter;

}

if (((i+j)%3) == 0){

out.println(“Breaking out of LblInner”);break LblInner;

}

out.println(“Going to the next iteration of LblInner”);

continue LblInner;

}

}

    Posted in Uncategorized | No Comments »

    Flow control, exceptions and assertions

    April 20th, 2008 by Boogie

    I got a very low score on the test for flow control, exceptions and assertions. So now I am taking a better look on it and making some  small test programs.

    I’m posting them below, maybe somebody else can find them useful.

    You are free to use them for your own personal development, but note that all content on this site is copyrighted.

    • For statement

    //for (int i = 1, int j = 1; true;){}// does not compile

     

    for (int i = 1, j; i < 1;){i++;}// j is not initialized it’s OK, it is never used !!

     

    for (int i = 1, j = 1; i < 1;){}

     

    int a=2;

    for (int i = 1, j = 1; a < 1;){}// the test can operate on different variables

     

    for (int i = 1; i<1;);

     

    for (int i = 1, j = 1; i < 1 ; out.println(“The increment part” + ” can do something else”)){}

     

    for (;;)out.println(“All parts can be omitted!”);

    • Switch statement

    byte b = 2;

    switch (b){

    case 1: break;

    //case 128: break;// does not compile !

    }

     

    int c = 2;

    switch (c){

    case 1: break;

    case 128: break;// OK

    }

     

    switch (5){// OK

    case 1: break;case 128: break;

    }

     

    switch (5){

    case 1: break;

    //case 1: break;// does not compile !

    }

     

    switch (new Integer(5)){// OK

    case 1: break;

    }

     

    switch (new Byte((byte)5)){// OK

    case 1: break;

    //case 128: break;// does not compile !

    }

     

    switch (5){

    case 1: break;

    case 5: out.println(“5 … & falling through”);

    default: out.println(“not in here”);

    case 6: out.println(“6″);}

    • While statement

    //while (boolean b = false){}// does not compile, has to be declared before !

     

    boolean b;while (b = false){}// OK

    Posted in SCJP | No Comments »

    Kon Tiki - sail on board of a raft

    April 18th, 2008 by Boogie

    From time to time I read some pages from Kon-Tiki: Across the Pacific in a Raft .

    I always stop to look on the images. Not everyday you get to see a man catching a shark with his bare hand, in the middle of the ocean, while on board of a raft!

    Posted in Books | No Comments »

    The Red Queen

    April 18th, 2008 by Boogie

    Recently I finished reading The Red Queen: Sex and the Evolution of Human Nature .

    It’s a very interesting book, I am still trying to see what decisions I can take based on what I found out from it.

    For sure I learned good information about the human nature. Which isn’t at all that pure as we would like to think.

    Posted in Books | No Comments »

    Monthly expense calculator

    April 17th, 2008 by Boogie

    Been reading the The 4-Hour Workweek: Escape 9-5, Live Anywhere, and Join the New Rich book by Tim Ferris.

    I just calculated my monthly expenses, added a buffer of 30% and came up with 2000 Euros ( around 3200 USD). It’s not that much.

    Posted in Books, Life | No Comments »

    Test results from SCJP for Java 5 Study Guide - first pass

    April 17th, 2008 by Boogie

      

    SCJP Sun Certified Programmer for Java 5 Study Guide is the book I am working with. I went through it once, made some annotations and did the tests at the end of each chapter.

     Here are the tests results:

    chap I :        6 /  9            67 %    Declarations and access control

    chap II :       6 / 14            43 %    Object Orientation

    chap III :      7 / 14            50 %    Assignments

    chap IV :     7 / 10            70 %    Operators

    chap V :      5 / 16            31 %    Flow control, Exceptions & Assertions

    chap VI :     3 / 15            20 %    Strings, I/O, Formatting & Parsing

    chap VII :    3 / 16            18 %    Generics & Collections

    chap VIII :   9 / 12            75 %     Inner Classes

    chap IX :     8 / 17            47 %     Threads

    Looks like I have to put serious work, the pass score for this exam is 65 % !

    Posted in SCJP | No Comments »

    On my way to SCJP 1.6

    April 17th, 2008 by Boogie

    SCJP or Sun Certified Java Programmer is an exam which certifies that the one holding it has a good understanding of the Java programming language. It is focused on the standard edition of Java and Sun’s specifications.

    It is not an easy exam but by no means the one holding that certification can be considered a Java expert.

    For me, acquiring this certification is a personal goal, something I’ve been puting aside for far too long.

    Quick review of where I come from: I have always been involved with computers, programming mostly. Basic, QBasic, Pascal, C, Java, these were the main programming languages I’ve been working with over the years.

    The first time I wanted to pass the SCJP I was student in my 4th year in the Computer Science Faculty. It was named SCJP 1.4 back then. Then I got employed, working in Java as a newbie. I convinced people at my company to order Kathy Sierra’s and Bert Bate’s book , for SCJP 1.5. ( version 5 had come out by then).

    I went through that book and at after I read it all once I stopped, with some changes coming in my life.

    Another two years passed and I decided it’s time.

    About two months ago I started preparing for SCJP 1.6. I still have the 1.5 book SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press Study Guides), but I think I’ll manage only with this one.

    My goal is that somewhere around the middle of May I want to be SCJP 1.6 . Currently I’m working on it.

    Posted in SCJP | No Comments »