Saturday, July 11, 2009

How do you find greatest number among three without "conditional operator" and "Math Library Functions"?

I got this question from my friend vengat : How do you find greatest number among three without "conditional operator" and "Math Library Functions"?
First of all,I would like to thank him ..



This is what i done,


import java.io.Writer;
import java.util.Scanner;

public class Teknoturf {
public static void main(String[] args){

System.out
.println("This is Print Greater No without Using < > symbol and Library");
Scanner scan = new Scanner(System.in);
System.out.println("Enter No1------------->>");
int no1 = scan.nextInt();
System.out.println("Enter No2------------->>");
int no2 = scan.nextInt();
System.out.println("Enter No3------------->>");
int no3 = scan.nextInt();

Utility(no1, no2, no3);

}

static void Utility(int a, int b, int c) {
int temp = 0, temp1 = 0;

Integer AminusB = a - b;
String strAminusB = AminusB.toString();
if (strAminusB.substring(0, 1).equalsIgnoreCase("-")) {
temp = (a + b) / 2 - -(a - b) / 2;

} else {
temp = (a + b) / 2 - (a - b) / 2;
}

temp1 = a + b;
temp = temp1 - temp;

int help1 = temp - c;
Integer tempMinusC = temp - c;
String strtempMinusC = tempMinusC.toString();


if (strtempMinusC.substring(0, 1).equalsIgnoreCase("-")) {
temp1 = (temp + c) / 2 - -(temp - c) / 2;
} else {
temp1 = (temp + c) / 2 - (temp - c) / 2;
}

temp = temp + c;
temp1 = temp - temp1;
System.out.println("Greatest Number----------------------->>" + temp1);

}
}


this is only work for positive numbers ....

Friday, July 3, 2009

Vector or ArrayList -- which is better?

Q : Vector or ArrayList -- which is better and why?

A: Sometimes Vector is better; sometimes ArrayList is better; sometimes you don't want to use either. I hope you weren't looking for an easy answer because the answer depends upon what you are doing. There are four factors to consider:

* API
* Synchronization
* Data growth
* Usage patterns



Let's explore each in turn.

API

In The Java Programming Language (Addison-Wesley, June 2000) Ken Arnold, James Gosling, and David Holmes describe the Vector as an analog to the ArrayList. So, from an API perspective, the two classes are very similar. However, there are still some major differences between the two classes.

Synchronization


Vectors are synchronized. Any method that touches the Vector's contents is thread safe. ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe. With that difference in mind, using synchronization will incur a performance hit. So if you don't need a thread-safe collection, use the ArrayList. Why pay the price of synchronization unnecessarily?

Data growth

Internally, both the ArrayList and Vector hold onto their contents using an Array. You need to keep this fact in mind while using either in your programs. When you insert an element into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. It's always best to set the object's initial capacity to the largest capacity that your program will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later. If you don't know how much data you'll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value.

Usage patterns

Both the ArrayList and Vector are good for retrieving elements from a specific position in the container or for adding and removing elements from the end of the container. All of these operations can be performed in constant time -- O(1). However, adding and removing elements from any other position proves more expensive -- linear to be exact: O(n-i), where n is the number of elements and i is the index of the element added or removed. These operations are more expensive because you have to shift all elements at index i and higher over by one element. So what does this all mean?

It means that if you want to index elements or add and remove elements at the end of the array, use either a Vector or an ArrayList. If you want to do anything else to the contents, go find yourself another container class. For example, the LinkedList can add or remove an element at any position in constant time -- O(1).O(i) where i is the index of the element. Traversing an ArrayList is also easier since you can simply use an index instead of having to create an iterator. The LinkedList also creates an internal object for each element inserted. So you have to be aware of the extra garbage being created. However, indexing an element is a bit slower --
Finally, in "PRAXIS 41" from Practical Java (Addison-Wesley, Feb. 2000) Peter Haggar suggests that you use a plain old array in place of either Vector or ArrayList -- especially for performance-critical code. By using an array you can avoid synchronization, extra method calls, and suboptimal resizing. You just pay the cost of extra development time.

Wednesday, July 1, 2009

Print to console without using semi colon

I got this question from my friend : “How can one write to console without using semi colon in Java ?”.

I tried many work-arounds . Even Googling did net help much .

I thought i should raise an exception some how thats it some thing will be written on the console., its did not work well.

Then a stupid workaround was to enable the verbose argument , thats it every class loaded in the JVM is traces on console.

The some how i tried real tricks to print my name on console ,

This is what i got,


public class Tek {

public static void main(String a[]){

if ( System.out.append("Teknoturf") instanceof Object ){

}
}
}

You might be wondering , why i did not use System.out.println() . Because it does not return anything , So it can not be compared in if block.

Friday, May 15, 2009

On Breadth vs. Depth Of Technical Knowledge

Today's posting about balancing the value of learning specific technologies and following technologies you enjoy got Jeffrey Thalhammer thinking about depth vs. breadth of knowledge.

Whenever my colleagues and I discuss our career plans and the job market, someone always asks me whether to learn programming language X, or operating system Y, or framework Z. But I like to point out that time spent learning some new skill is also time not spent honing the skills you already have. And in my opinion, it is both more lucrative and more enjoyable to be a master of one craft, than to be mediocre at several of them.

This is because I've noticed that those who are the best in their chosen fields are always fully employed and highly compensated. Especially during an economic downturn, employers become more selective about who they hire. So when they go looking for a candidate with a particular set of skills, they want to choose the person who is strongest with those skills -- not the person who has the most different skills. And employers are usually willing to pay a premium for top-notch talent, if they can find it.

I've been on the hiring side of the interview table enough times to know this. When a job candidate shows me they have mastered one technology, it also demonstrates to me that they have the potential to master others. But having partial expertise in many technologies may only prove that they own a lot of O'Reilly books. Truly mastering any technology requires a great deal of patience and dedication, and those traits are far more valuable to the team than being able to write code in 16 different languages.

Having said all that, I do acknowledge there is a real tradeoff between the depth and breadth of one's technical skills. Not all job candidates are created equal, and it just isn't possible for everyone to be the "best" in something. I'm sure there is a sweet spot where you can optimize your employability, and this doesn't mean that you should completely ignore other technologies. The industry is constantly evolving so you must stay up-to-date, and learning a little bit about other technologies can give you a fantastic new perspective on the those you already know well. And of course, this all assumes that you actually enjoy the technologies you're working with. If you don't enjoy them, then by all means, go learn some new skills.

But if you do enjoy the technologies you work with, then I urge you to consider mastering those technologies before going off to learn some new bag-of-tricks. To be sure, the road to mastery is long and difficult. It is fraught with frustration and can be boring at times. But it is also challenging, exciting, and deeply rewarding. In the end, I believe it will lead you to a much happier and more prosperous career.

I'd rather be the first-pick candidate for just one position than the second-pick for several.

Jeff Thalhammer has been specializing in Perl software development for over 10 years. He is the senior engineer and chief janitor at Imaginative Software Systems, a small software consultancy based in San Francisco. Jeff is also the creator of Perl-Critic, the leading static analysis tool for Perl.