Code man! Right now!

It feels so good when girls go gaga when you say your a Masters student. And then more drooling when you say you are from the computer engineering department. No, I am not making up this stuff. Apparently they think about us making the next big Facebook. Aaah. That explains doesn’t it? They ask me “So you’ll get rich pretty soon right?” I don’t have the heart to say otherwise. Basically, I do love all the attention. ūüėõ

Anyways, I was wondering. Why not make it big? I can’t own a Ferrari if I end up working in some company. Its true. But not everyone ends up with a successful start up either. But ya, who cares? I mean, people, we should just go for it. I loved this video. Inspires to learn coding. I’m putting it up on my wall. Because guys, programming is important.

Code man! Right now!

Design Pattern: Strategy

 Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from clients that use it.

Strategy Design Pattern
Strategy Design Pattern

Abstract Class: Duck.java

We create objects  after sub classing it into a concrete class from an abstract type. In our case, is the Duck Class. This provides the basis for all the XxxDuck objects that are created. Two things to be mentioned;

  1. We are programming the different “behavior” to an interface. And this could be an abstract class as well. References to these instance variables are provided as instance variables in the Class Definition.
  2. Public “Setters” are provided so that the behavior of the object can be set or changed dynamically as shown in this example.
package com.npk.strategy;

public abstract class Duck {

FlyBehavior flyBehavior;
QuackBehavior quackBehavior;

public abstract void display();

public void setFlyBehavior(FlyBehavior flyBehavior){
this.flyBehavior = flyBehavior;
}

public void setQuackBehavior(QuackBehavior quackBehavior){
this.quackBehavior = quackBehavior;
}

public void performFly(){
flyBehavior.fly();
}

public void performQuack(){
quackBehavior.quack();
}

public void performSwim(){
System.out.println("All ducks can swim!");
}
}

Interface: FlyBehavior.java and QuackBehavior.java

Any variable behavior of an object are to be programmed to an interface.
This way, you get two things:

  1. Compostion
  2. Loosely coupled objects.

Both of which are good OO Designs aspects.


package com.npk.strategy;

public interface FlyBehavior {

public void fly();
}

package com.npk.strategy;

public interface QuackBehavior {

public void quack();
}

Concrete Implementation: DummyDuck.java

We subclass the abstract Duck class to get a concrete DummyDuck class where we can set the behavior in the class constructor ( optional )


package com.npk.strategy;

public class DummyDuck extends Duck {

public DummyDuck(){
this.setFlyBehavior(new FlyNoWay());
this.setQuackBehavior(new SquekQuack());
}

@Override
public void display() {
System.out.println("This is a dummy duck.");

}

}

Concrete classes: FlyNoWay.java and SquekQuack.java

These form the actual implementation of the FlyBehavior and the QuackBehavior interfaces respectively.


package com.npk.strategy;

public class FlyNoWay implements FlyBehavior {

@Override
public void fly() {
System.out.println("Cannot fly.");

}

}

package com.npk.strategy;

public class SquekQuack implements QuackBehavior {

@Override
public void quack() {
System.out.println("Duck squeking.");

}

}

The Application Launcher We show two things here.

  1. Setting the behavior using the constructor.
  2. Changing the set behavior using the setters provided.

package com.npk.strategy;

public class Launcher {

public static void main(String[] args) {
Duck duck = new DummyDuck();
duck.display();
duck.performFly();
duck.performQuack();

System.out.println("---------------------");
duck.setFlyBehavior(new FlyWithWings());
duck.setQuackBehavior(new MuteQuack());

duck.performFly();
duck.performQuack();
}

}

The final output


This is a dummy duck.
Cannot fly.
Duck squeking.
---------------------
Duck flying.
Mute quack.

Design Pattern: Strategy

Why is it a good idea to Graduate?

  • Fuel your already bloated ego.
  • You have lots of money, don’t know what to do with it.
  • Decide to travel exotic places in the name of education.
  • You came to know about the free food available at conferences and seminars.
  • Your parents happen to be overqualified.
  • Because your friend just graduated.
  • The illusion of a great career thereafter. ( $$$ )
  • The sudden¬†realization¬†that you are too young to work.
  • You decide to¬†pursue¬†that special area of interest you had since kindergarden.
  • The¬†realization¬†that your current discipline sucks and that you desperately need a change.
  • Someone told you that teaching positions open up at universities once you have your masters.
  • Oh oh, I thought I’d never come to this. You love research and decide to¬†pursue¬†a Ph.D
Why is it a good idea to Graduate?

What certification, I mean does certifications even hold any value?

The craze for certifications. Does it prove anything at all? There are a couple of reasons why one may choose to certify.

“I need a job”

Of course, that is important. Maybe you are from a non Computer Science (CS) background and would like to jump in. Now, that’s a valid reason. And yes, the pay.

“I need to be good programmer”

Oooh oh! Okay, now we have a problem. There are a few things that cannot be replaced by hands on experience. I’ve been working for a year and a half now, and I will definitely not say something like that. Am I am better programmer now? Yes. This guy comes to me and says, “I am a kick ass Java programmer.”. I was amused actually. To feed his own ego, and out of devilish curiosity, I did ask him, “How come?”.

“I passed my SCJP exam. 96% \m/ “. Poor thing, I didn’t have the heart to tell him, “You are by no means, a good programmer. You are now an expert at Java syntax and a few important classes.”

“I know this is just a base certification, but this IS a requirement for higher certification from this company.”
Oh yes, of course. One of the saddest parts of certification. Why don’t they let you appear for the final thing? Commerialization. Don’t you think?

“I put in my time and money. It was a costly exam.”
Ya, this a good thing that prospective employers may like. That you are serious about your career and is putting that extra effort on your part.

“I just improved my overall knowledge about the subject”
The one thing I DO like about certifying. You know the stuff in and out now. And even those things you missed out at your workplace. Oh look, now you can apply your new found knowledge! ūüôā

“What about the Comptia “plus” exams”
I have nothing against the company. Or their certifications. They are good. Yes. See this Top 10 2012 Techrepublic. But they are too basic and low level. If you are from a non CS background and have the money :P, please continue by all means.

“What about Sun..I mean Oracle certifications?”
Hmm..we did have a discussion on that. SCJP proves that you have good command over Java. That’s it. It can get a HR at a company to get you noticed. But yes, the ‘higher’ end certifications that require you to have ‘hands on’ or ‘practical stuff’, they are worthwhile. Again, you should have the time and money. I am not talking about the dedication, because you wouldn’t be reading this otherwise.

“I know RHCE is popular”
I know that. SO?

“It should be good, right?”
Depends on what you want to do. Yes, it is the most recognized Linux certification (industry). And more so because, it a hands on exam. But unfortunately, it doesn’t make you a good Admin with just the certification. And yes, I regret saying this, there are lot of corrupt practices going behind the scenes, esp between the testing center and the test taker. I hope its not the same everywhere. Like this guy trying to install (RHCE certified, ahem) tomcat on a Debian box. He goes, ”¬†yum install tom.. ¬†“. Sigh.

“CCNA is good!”
Yes, always comes in the top IT certifications every year. But again, do you wish to go into the networking track?

“So, what then?”
Do a certification if you:
1. Wish to improve your knowledge base.
2. Change from a non IT background.
3. Specialize a certain subject.
4. Your employer is paying for your certification ( By all means, do it  )
5. Glorify your resume (Partly because there is nothing much on it)
6. Boost your ego (Have all those white papers hanging on the wall, show off)
7. Get your first job? (That’s what she said)

What certification, I mean does certifications even hold any value?

Linked Lists in Java

You might want to check out these before continuing:

https://linuxjunkies.wordpress.com/2010/11/18/data-structures-using-java-part-6-linked-lists-part-1/

https://linuxjunkies.wordpress.com/2010/11/26/data-structures-using-java-part-7-linked-lists-part-2-doubly-linked/

Linked List

ListNode Class


public class ListNode {

	Object data;
	ListNode nextNode;
	
	ListNode (Object object, ListNode node){
		data = object;
		nextNode = node;
	}
	
	ListNode (Object object){
		this(object,null);
	}
	
	Object getObject(){
		return data;
	}
	
	ListNode getNext(){
		return nextNode;
	}
}

List Class


public class List {

	private ListNode firstNode;
	private ListNode lastNode;
	private String name;
	
	public List(){
		this("list");
	}
	
	public List(String listname){
		name = listname;
		firstNode = lastNode = null;
	}
	
	public void insertAtFront(Object item){
		if (isEmpty())
			firstNode = lastNode = new ListNode(item);
		else
			firstNode = new ListNode(item, firstNode);
	}
	
	public void insertAtBack(Object item){
		if (isEmpty())
			firstNode = lastNode = new ListNode(item);
		else
			lastNode = lastNode.nextNode = new ListNode(item);
	}
	
	public Object removeFromFront() throws EmptyListException{
		if (isEmpty())
			throw new EmptyListException(name);
		Object removed = firstNode.data;
		
		if( firstNode == lastNode)
			firstNode = lastNode = null;
		else
			firstNode = firstNode.nextNode;
		
		return removed;
	}
	
	public Object removeFromBack() throws EmptyListException{
		if (isEmpty())
			throw new EmptyListException(name);
		
		Object removed = lastNode.data;
		
		if( firstNode == lastNode)
			firstNode = lastNode = null;
		else{
			
			ListNode current = firstNode;
			
			while(current.nextNode != lastNode)
				current = current.nextNode;
			
			lastNode = current;
			current.nextNode = null;	
		}
		return removed;
		
	}
	
	public boolean isEmpty(){
		return firstNode == null;
	}
	
	public void print(){
		if (isEmpty()){
			System.out.println("List is empty");
			return;
		}
		
		ListNode current = firstNode;
		while (current != null){
			System.out.printf("%s --> ", current.data);
			current = current.nextNode;
		}
		System.out.println();
	}
	
	public int length(){
		int length = 0;
		
		if (isEmpty()){
			return 0;
		}
		
		ListNode current = firstNode;
		while (current != null){
			current = current.nextNode;
			length++;
		}
		return length;
	}
	
	public int recursiveSize(){
		return recursiveLength(firstNode);
	}
	
	public int recursiveLength(ListNode current){
		
		if (isEmpty()){
			return 0;
		}
		
		if (current == null)
			return 0;
		
		return (1 + recursiveLength(current.nextNode));
	}
	
}
	

ListTest Class


public class ListTest {

	public static void main(String[] args){
		
		List list = new List();
		
		list.insertAtFront( "four" ); 
		list.print();             
		list.insertAtFront( "two" ); 
		list.print();            
		list.insertAtBack( "six" );  
		list.print();          
		list.insertAtBack( "nine" );  
		list.print();            
		
		System.out.printf("Length: %d%n", list.recursiveSize());
		System.out.printf("Length: %d%n", list.length());
		
		try{
		    System.out.printf( "%s removed\n", list.removeFromFront());
		    list.print();
		    System.out.printf( "%s removed\n", list.removeFromFront());
		    list.print();
		    System.out.printf( "%s removed\n", list.removeFromBack());
		    list.print();
		    System.out.printf( "%s removed\n", list.removeFromBack());
		    list.print();
		}catch ( EmptyListException e ){
			e.printStackTrace();
		}
		
		System.out.printf("Length: %d%n", list.recursiveSize());
		System.out.printf("Length: %d%n", list.length());
		
		       
	}
}

This is what you get

four --> 
two --> four --> 
two --> four --> six --> 
two --> four --> six --> nine --> 
Length: 4
Length: 4
two removed
four --> six --> nine --> 
four removed
six --> nine --> 
nine removed
six --> 
six removed
List is empty
Length: 0
Length: 0
Linked Lists in Java

String Reversal in Java using StringBuffer, toCharacterArray and Recursion

Using StringBuffer Class

new StringBuffer(name).reverse().toString()

Using CharArray and StringBuilder

        public static String charReverse(String data){
		StringBuilder sb = new StringBuilder();
		char[] ca = data.toCharArray();
		
		for (int i=data.length()-1; i>=0;i--){
			sb.append(ca[i]);
		}
		return sb.toString();
	}

Using Recursion

public static String recursiveReverse(String data){
		
		if (data.length() < 2)
			return data;
		
		return recursiveReverse(data.substring(1)) + data.charAt(0);
	}

StringReversal Class

public class StringReversal {

	public static void main (String[] args){
		String name = "linuxjunkies";
		
		System.out.println("StringBuffer: " + new StringBuffer(name).reverse().toString());
		System.out.println("CharacterArray: " + charReverse(name));
		System.out.println("Recursion: " + recursiveReverse(name));
	}
	
	public static String charReverse(String data){
		StringBuilder sb = new StringBuilder();
		char[] ca = data.toCharArray();
		
		for (int i=data.length()-1; i>=0;i--){
			sb.append(ca[i]);
		}
		return sb.toString();
	}
	
	public static String recursiveReverse(String data){
		
		if (data.length() < 2)
			return data;
		
		return recursiveReverse(data.substring(1)) + data.charAt(0);
	}
}

The Output

StringBuffer: seiknujxunil
CharacterArray: seiknujxunil
Recursion: seiknujxunil
String Reversal in Java using StringBuffer, toCharacterArray and Recursion