Archive for ‘JAVA’

November 18th, 2011

Java Rotate Shift Integer Values Left

Here is a simple method that rotates/shifts the values of an integer to the left by a certain number. The reason why I am using long for the type of the integer is so that the method works with really big numbers. The returned value is the one that has been rotated. Feel free to replace long with int if you are not going to be working with big numbers. Here is the method:

public static long rotateNumber(long d , int rotateBy)

{

String num = Long.toString(d);

int lenght = Long.toString(d).length();

char[] charArray = new char[lenght];

int count = 0;

for (int i = 0; i < lenght; i++) {

if(i < lenght – rotateBy){

charArray[i] = num.charAt(i + rotateBy);

}else{

charArray[i] = num.charAt(count);

count++;

}

}

String newNum =  new String(charArray);

return Long.valueOf(newNum);

}

public static long rotateNumber(long d , int rotateBy)
{
String num = Long.toString(d);
int lenght = Long.toString(d).length();
char[] charArray = new char[lenght];
int count = 0;
for (int i = 0; i < lenght; i++) {
if(i < lenght – rotateBy){
charArray[i] = num.charAt(i + rotateBy);
}else{
charArray[i] = num.charAt(count);
count++;
}
}
String newNum =  new String(charArray);
return Long.valueOf(newNum);
}
December 19th, 2009

Java Organizing And Compiling Guide

Because of its unfussiness, this my favorite way of organizing and compiling Java programs.

Organizing Java

Here is the file SampleOranization.java which contains a class SampleClass and a driver to test it:

/////////////////////////////////////////////////////////////////////
//
//  Java Organizing And Compiling Guide
//
/////////////////////////////////////////////////////////////////////
class TestClass
{
public static void main (String [] args)
{
SampleClass x;
x = new SampleClass();
x.test();
}
} // end class TestClass
/////////////////////////////////////////////////////////////////////
class SampleClass
{
public SampleClass()
{
}
public void test()
{
System.out.println("Test Works!");
}
} // end class SampleClass
/////////////////////////////////////////////////////////////////////

Notice slashes (/////////…) that separate classes. In more complex cases, I would recommend comments at the top of each class.

Similarly, you can separate methods with a row of the form //—–…, and I would recommend comment methods which are not self-explanatory.

Compiling Java

/user/root/java/ &gt; ls
SampleOranization.java

Note that the directory contains only one file, SampleOranization.java. We now compile it.

/user/root/java/ > javac SampleOranization.java

/user/root/java/ &gt; ls
total 6
SampleOranization.java
SampleClass.class
TestClass.class

Notice that I have created two class files, one for each class defined inside the file I just compiled (SampleOranization.java).

Notice that SampleOranization is not the name of a class inside the file SampleOranization.java, so I don’t a file SampleOranization.class does not get created.

Now let’s try runing the program. We use the java command and give it the name of the class which contains the method named main. In our case, this is the class TestClass.

/user/root/java/ &gt; java TestClass
Test Works

This is the output we expect.

December 19th, 2009

Best Coding And Debugging Guide

* Write small simple programs to test constructs you haven’t used before or about which you feel shaky. It’s easier to locate your misunderstanding in a short program than in a long one.

* When you’re debugging a program, pay attention only to the first error. The others may simply be side-effects of that first error.

* When you’re writing a program, keep the specification in front of you. How else can you be sure that you’re solving the correct problem?

* Don’t attempt to write out all the code at once and then try to debug it.

* Always build on existing code which compiles and runs.

* Grow programs incrementally. If you extend a working program and it doesn’t work anymore, then you can localize the error.

* Keep working checkpoint programs as you move along. If you seriously wound your code, you can retreat to a prior version which worked.

* Implement a large program in well-defined steps. Examine the problem and determine how you can implement the required capabilities in an incremental manner.

* Learn how to document errors:

· they must be repeatable;

· the code should be short;

· collect hardcopy of the program, the compile, and the test run.

* Human memory is fallible, and computers are unforgiving, so no one will believe you if you don’t have documentation. Experts can’t help you if you come in with a folktale — “I tried that, but it gave me an error.” You must be able to replicate the error.

* If you encounter an error that you can’t understand, then keep the code which generated the error and find out what was wrong. You may produce a working program by trying another approach, but if you don’t know why you got the error, then there’s a gap in your knowledge.

Write small simple programs to test constructs you haven’t used before or about which
you feel shaky. It’s easier to locate your misunderstanding in a short program than in a
long one.
When you’re debugging a program, pay attention only to the first error. The others may
simply be side-effects of that first error.
When you’re writing a program, keep the specification in front of you. How else can you
be sure that you’re solving the correct problem?
Don’t attempt to write out all the code at once and then try to debug it.
Always build on existing code which compiles and runs.
Grow programs incrementally. If you extend a working program and it doesn’t work
anymore, then you can localize the error.
Keep working checkpoint programs as you move along. If you seriously wound your
code, you can retreat to a prior version which worked.
Implement a large program in well-defined steps. Examine the problem and determine
how you can implement the required capabilities in an incremental manner.
Learn how to document errors:
· they must be repeatable;
· the code should be short;
· collect hardcopy of the program, the compile, and the test run.
Human memory is fallible, and computers are unforgiving, so no one will believe you if
you don’t have documentation. Experts can’t help you if you come in with a folktale —
“I tried that, but it gave me an error.” You must be able to replicate the error.
If you encounter an error that you can’t understand, then keep the code which generated
the error and find out what was wrong. You may produce a working program by trying
another approach, but if you don’t know why you got the error, then there’s a gap in your
knowledge of Java.
December 12th, 2009

JAVA Override Default Avatar Icon Application JFrame Web Start

Java Override Default Avatar

Java Override Default Avatar

This is a simple example of how you can override

the default Java JFrame Application Icon on

the top left corner. Jpeg’s and PNG’s seems

to work great. I would suggest that you use

images with 32×32px dimensions. Also

you can ignore the button with the hot

chick on it. Probably one of the hottest

looking JButtons ever :p. The source code

and web start example are available here:

SOURCE CODE + WEB START DEMO

December 11th, 2009

Java How To Center JFrame Window Application Web Start

Java Center JFrame Window Application

Java Center JFrame Window Application

The following code will help any of you guys that are trying to

center your JFrames in the middle of the screen.

You can try the demo or download the source code.

SOURCE CODE + DEMO HERE

eRunways