Sunday, October 7, 2007

Old blogs before Day 0.

// Copy of blogs from some other place. That service is really frustrating. Relocating here.... October 7, 2007 - Sunday Constructor variables! Instance variables should not be re-declared in the constructor. This is easy to do mistakenly...For example: public class Mt{ int height; int length; int iRep; // the following is the wrong way to do it. We are basically redeclaring the instance // vars and height, length, iRep all will be set to zero!!!!!!!!!! // Because height, length and iRep will be set to local variables, the constructor is // nothing more than a method and as such, these variables' set values will die after the // constructor is finished and we will revert back to the default values for these variables // which is zero as the class did not initialize them to anything! // height = length = iRep = 0 is the result of the following! public Mt (int ht, int ln) { int height = ht; int length= ln; int iRep = iRepC (ht, ln); } // Correct way: // get rid of the type declarations.. // The type is already declared in the class. // No need to redeclare... public Mt (int ht, int ln) { height = ht; length= ln; iRep = iRepC (ht, ln); } 4:43 AM - Junit Test Case Framework Current mood: calm Bare Skeleton junit TestCase frame work. We absolutely need the following. First we need to import the junit.framework.TestCase: import junit.framework.TestCase; Then this is the TestClass that is created by Eclipse®. The Class that we are working with here is called 'Whatever'. So, this 'Whatever' Class will extend the TestCase class... public class WhateverTest extends TestCase { The first test is for the Constructor of the class. Imagine there is some static int in the Whatever class that is a class variable, and instantiating any object of whatever class would set this to 1000. And the what() method of Whatever Class would return this value. So, we create a testConstructor method which would create the object 'w' and then assertTrue on checking to see if Whatever.what() returns the expected value of 1000. public void testConstructor ( ) { Whatever w = new Whatever ( ); assertTrue (Whatever.what () == 1000); } Then we have other method tests. Let's have a test on some method of the 'Whatever' class called whyWhatever(). Why whyWhatever does something to that same class var and should return 2000. public void testIncrement ( ) { Whatever w = new Whatever ( ); w.whyWhatever( ) ; assertTrue (w.what() == 2000); } If everything's going Ok, then the test cases will succeed. And we will have the green bars in Eclipse after running the Junit Test cases. 2:12 AM October 6, 2007 - Saturday Arguments are passed by Value in Java!!!!!!! Current mood: curious Arguments in Java are all passed by value. This implies that each argument is copied into the corresponding method parameter in the method being called. If the parameter is a reference, the mechanics of method operation on that parameter is the same when an assignment statement involves two references. In box-and-pointer terms, the copy results in two arrows pointing to the same box. The method call may work with the object reference and do all sorts of things to the reference(s). But at the end of the day, the method parameters live on the frame stack on the heap and they all die when the method is finished. Nothing persists!!! What this means in reality is that an argument of a primitive type can't be changed in a method to which it is passed, since the method is working with not with the actual primitive but rather a copy of that primitive. A reference argument likewise can not be changed either, by the same token. However, variables in the referenced object can be changed. In short for variables within an object to change, some kind of dot operator (.), will be there within the method body. It might not change stuff, but if you don't see the dot operator, then you know that nothing in the object will be changed in this method! 4:05 PM Ok....set the spam meter high... Current mood: aggravated http://profileedit.myspace.com/index.cfm?fuseaction=accountSettings.spam but this is kind of bad as nobody else can send me invites now, the admin should just kick out sex peddlers out of this space or open up a mySEXspace.com or something for people inclined towards that kind of thing! 12:37 AM - 0 Comments - 0 Kudos - Add Comment - Edit - Remove What are all these stupid invites??? Current mood: angry Getting a whole bunch of spam! Just checked my email and there were like 50 emails talking about wanting to be my 'friend'! All of those some scam, it's funny that there is a option to select all and deny but not the option to report the whole bunch as scam/spam! Myspace should have it, otherwise, this website will become unusable. The admin should do something about these spammers. Why send these objectionable peoples' pics and stuff to my control panel or send me email!?!?! 12:31 AM Object Refernces..... Current mood: amused Just to reiterate something that is pretty obvious but seems to trip you off all the time, is this issue with object references. Say we have two objects xA and xB. And each has a two element array. Lets call them xA1, xA2 and xB1 , xB2. Then if we make the following assignments... xB1 = xA1; xB2 = xA2; And then further do the following: xA2 = xA1; Then if we follow the object reference logic, xA1, xA2 and xB2 all would have the same values but xB1 would continue to have the old value of xA2! If we draw the diagram out, it all makes sense! xA -------> xA1 <----------xB2 <--------------xB ---------->xA2 <----------xB1 <-------------- After the last set of assignments: xA---------->xA1 <--------- xA2 ^ xB2 ---------------- xB---------->xB1------------>(old) xA2 This is because after the first set of assignemnts, the object pointed to by xA2 and xB1 are the same, however, when we assign the reference given by xA2 to some other object, this does not automatically reassign the reference given by xB1. They were pointing to the same thing, however, after re-assignment of xA2, xB1 is still pointing to it's old thing and xA2 has moved on. The crutial thing to understand is that xA2 and xB1, i.e. objects or arrays are not actually values but rather references and thus the '=' sign is not mathematically equal sign, it is ASSIGNMENT! And since these are object reference assignments, when one changes to refer to another object, the other does NOT change! p.s. the diagrams are simplifications, xA1 or xB1 does not point to each other or something like that, what is simplified is that, xA1 points to something and xB1 points to that same thing or some other thing. They can't point to each other they can only point to some same object on the garbase collectible heap! ..........and that's the whole point! 11:56 AM October 5, 2007 - Friday String Compare Blues... Current mood: bouncy Finally got the string compare joy thing figured out... The following would return true for mString == FUSD and s == USA. *********** return (mString.substring(1).compareTo (s) == 0) ? true : false ; *********** This is the zems of pearl from javadocs (javadocs is ®of your buddy Sun and I guess opensource and all that, so, quoting should be kosher) If the (javadocs - following) makes 100% sense to you then you must be compareTopublic int compareTo(String anotherString) Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true. substringpublic String substring(int beginIndex) Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. substringpublic String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. charAtpublic char charAt(int index) Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing. 3:03 PM Object recursion Phew!!!!!!!! This code finally worked...my first recursive objects.........took me a while to figure this out. Especially confusing was to get the right object to call the recursive call on! .r{}* There's some junk in this code and needs to get polished up, but it's working and that's good enough for now...As we all know, we can chase down the perfect code all day long and possibly spend a week on a ten liner but have to move on...In my thinking, it's also very important to stop at some point from the dangerous recursive call to beatify(timeAndResourse); :) */ .r{}* @param Call with this and this.mBalance * this is tail recusive, in that f() returns * itself with a smaller problem. */ public int recursiveBalance (Account someAccount , int lance) { if (someAccount.parentAcc != null) { Account tmpy = new Account(0); tmpy = someAccount; return recursiveBalance (tmpy.parentAcc, lance + tmpy.parentAcc.mBalance) ; } else { return lance + mBalance; } } .r{}* Putting the code here so that I can come back later * and appreciate how stupidly I coded, way back when! * err....hopefully...of course! */ 10:40 AM October 4, 2007 - Thursday The Indredible Lightness of Being Current mood: depressed Want to recommend a great book that I finished up... Incredible lightness of being by Milan Kundera A great book to ponder on. There are some really thought provoking motifs explored in the book. After reading this book you keep on thinking, "must it be so?". And that's a very pertinent question in anybody's life. Also another theme about the fact that we get to live only once and whatever we do is like an actor temporizing without rehearsing is very interesting. Reminds you of the Doors line: Into this house we're bornInto this world we're thrownLike a dog without a boneAn actor on a loan It seems emimently possible that there are no second chances and the trajectories that our lives take has no back tracing. We get one chance every moment and at the end of the end, what can we do but do what we do??? 2:00 AM - I/O example .r{}* input and output examples * first is just reading a line from keyboard and spewing it back. * ISR takes System.in and BR takes this and we assign this to the BR object * keybd */ import java.io.*; public class SS01 { .r{}* * @param keybd and call readLine() on it and print it */ public static void main(String[] args) throws Exception { BufferedReader keybd = new BufferedReader (new InputStreamReader (System.in)); System.out.println(keybd.readLine()); } } .r{}* Reading from a webpage. */ import java.io.*; import java.net.*; public class SSWebLine { .r{}* * @param takes an URL as arg and prints out on the console */ public static void main(String[] args) throws Exception { URL u = new URL ("http://www.yahoo.com"); InputStream Ins = u.openStream(); InputStreamReader Isr = new InputStreamReader(Ins); BufferedReader yahooCom = new BufferedReader (Isr); System.out.println(yahooCom.readLine()); } }

No comments:

Just some daily notes ...

Powered By Blogger