Thursday, April 18, 2013

Howto Use Java Comparator and Comparable by Example

In Java, there are some built-in methods that provides basic sorting of Primitive types array or Wrapper classes but what If we wanted to sort a custom class that we have written for use in one of our Projects? Well the answer is to write a sorting algorithm of our own and use it wherever it's required.

In Java, there are two interfaces that can help us achieve our goal. The reason the language provides two interfaces instead of one giving code developers enough flexibility to choose the one that they find suitable to their requirement.

The first Interface which I prefer to call is the Simple Handy Internal Sorter is available in java.lang.Comparable  . The Second Interface which I prefer to call is the Highly Flexible External Sorter is available in java.util.Comparator.

The former one is Simple and Internal as it needs to be implemented on the class itself and it's simple as it allows the code developers to write only single sorting implementation. The latter one is Highly Flexible and External that means it allows the code developer the flexibility to write as many as required sorting implementation for their custom class as it's declaration and implementation is in a separate class.

Let's start with the Below Example where Custom Class CarMake have both Simple Handy Internal Sorter and Highly Flexible External Sorter.

CarMake.java

As you can see below the Simple Handy Internal Sorter is already implemented on the Custom Class.



CarMakeComparatorAsc.java

Custom Class CarMake's Highly Flexible External Sorter Implementation.




CarMakeComparatorDesc.java

Custom Class CarMake's Highly Flexible External Sorter Implementation.


And, Finally the only thing required is to test both the implementation using a CarMakeSortTest.java.

CarMakeSortTest.java


I Hope this post helps you learn basics of Custom Class Sorting. Please do write your feedback...

Tuesday, April 03, 2012

Favor Composition to Inheritance

As we all know Java is an Object-Oriented Programming Language. OOP was developed in first place to increase code reuse and maintainability.

The Myth

Programmers introduced to OOP Language(s) have a mindset that code reuse can be achieved only by Inheritance (extends) in Java. But are not aware of the alternatives even if they came across them.

For Example:

List<Car> carRef = new ArrayList<Car>();  //Generic List of Car References

Q.  Why Inheritance is considered bad?

A.  Experienced Programmers are aware of the fact and even the Design Patterns states the same using Inheritance across boundaries are considered bad as author of that library may change the internal implementation details which might break our abstraction that depends on it blowing up the whole stack.


Better Alternative - Prefer Composition to Inheritance




-------------------------
I will soon add more on this. Please do post your comments if your thinking differs from me. I will try to incorporate you questions/suggestion here so everyone has a better idea on this concept.


Hope this Helps! Please write your comments it will help me improve.



Sunday, May 22, 2011

Linux Basics Walkthrough Part 1

Nowadays, Linux comes with pretty simple to use GUI. Professionals still prefers CLI to perform there tasks as they know the mantra GUI is simple but CLI is powerful. Let's dig into the basis.

1. Navigation

cd - Change Directory. For Example: cd /home/karan

2. Where I'm
 
pwd - prints the current working directory.


3. Who I'm

whoami - Shows user who is currently issuing command.

4. Feel Secure

passwd - Allows you to change the password for the user who is issuing the command.

Note: passwd <username> Allows Super Users/Admins to change the password of other users.

5. Reading files

cat - Allows user to view content of a file.

P.S. I'll be updating this post soon with lot more commands.

Hope this Helps! Please write your comments it will help me improve.

Tuesday, March 01, 2011

Python Basics: Python Complex Numbers

Python supports Complex Numbers and are built-in to the language. You can create complex numbers either by specifying the number in (real + imagJ) form or using a built-in method complex(a, b).

Complex Numbers:
  1. For Electronic Engineers python uses j or J instead of an i for imaginary numbers.
  2. j or J means square root of -1.
  3. You can perform arithmetic operations on Complex Numbers.
  4. Both real and imagJ part are floating-point numbers.
  5. Complex Number has a nonzero real part.
  6. cmath library. (for your reference.)
(Post in your comments if i left out something important so i can add in here.)

How Complex Numbers Works.

  1 #!/usr/local/bin/python3.2
  2 # Filename: ComplexNum.py
  3
  4 # How Imaginary Number Arithmetic Works.
  5 print('1j + 1j :', 1j + 1j)
  6 print('3j - 1j :', 3j - 1j)
  7 print('2j * 1j :', 2j * 1j)
  8 print('2j / 1j :', 2j / 1j)
  9
 10 # complex(real, imagJ)
 11 print('complex(1, 1) :', complex(1, 1))
 12
 13 # Make Complex Numbers bit More Complex. Arithmetic on real and imagJ parts. :)
 14 print('3 * complex(2, 3) :', 3 * complex(2, 3))
 15 print('2j * complex(4, 6) :', 2j * complex(4, 6))
 16 print('(2 + 4j) * complex(4, 2) :', (2 + 4j) * complex(4, 2))
 17 print('(2 + 4j) * (4 + 2j) :', (2 + 4j) * (4 + 2j))
 18 print('complex(2, 4) * complex(4, 2) :', complex(2, 4) * complex(4, 2))

You can download the file here: ComplexNum.py

Output:

1j + 1j : 2j
3j - 1j : 2j
2j * 1j : (-2+0j)
2j / 1j : (2+0j)
complex(1, 1) : (1+1j)
3 * complex(2, 3) : (6+9j)
2j * complex(4, 6) : (-12+8j)
(2 + 4j) * complex(4, 2) : 20j
(2 + 4j) * (4 + 2j) : 20j
complex(2, 4) * complex(4, 2) : 20j

Explanation:
  1. Line #5 & #6 are pretty much self-explained.
  2. Line #7 2j * 1j = 2j^2. As we know that j here means square root of -1. Now j^2 equals -1. leads to the output of (-2 + 0j). As we know that a complex number has a nonzero real part.
  3. Line #8 2j / 1j = (2+0j). Complex Part division is a bit harder maybe or not. Because we need to multiply but top and bottom by what we call complex conjugate. This is what you get when you change the sign of the imaginary part but not the real part. 2j/1j * -1j/-1j, i.e, -2j^2/-1j^2. Minus signs cancels out we are left with (2 + 0j).
  4. Line #11 uses python interpreter built-in function complex(a, b). But the output has nothing much that i can explain.
I believe rest of it is more or less self -explainable. But if you think they need explanation post in your comments. :)

Hope this Helps! Please write your comments it will help me improve.