Wednesday, June 16, 2010

Odd Number

How do you find whether the given number is odd? Just have a look at below statement. Does it return true if you pass odd number?

(num % 2 == 1)

No, It doesn't. It doesn't return true for all cases even the given number is odd.
Do you know why? Why it will not return true for all cases?

Lets discuss.............

The integer holds positive and negative values. If you pass the negative odd number it will return false because num % 2 returns the negative value, so it will not meet the condition(-1 == 1). When you do any arithmetic operation the result will be assigned with sign(+/-). Please see the sample output at the end.

However, my expectation is to return true if it is an odd number doesn't matter whether it is an positive or negative value. How do you do it? Please have a look at below statement.

(num % 2 != 0)

The above statement will return true even if you pass the negative odd number.
Hence, you should ensure to consider the negative values also while doing this kind of operation.

Code Snippet:

int num = Integer.MAX_VALUE;
System.out.println("Using condition (num % 2 == 1).......");
System.out.printf("\tIs odd number(%d): %b\n", num, (num % 2 == 1));
num = Integer.MAX_VALUE + 2;
System.out.printf("\tIs odd number(%d): %b\n\n", num, (num % 2 == 1));

// So changing the condition to work on all cases
System.out.println("Using condition (num % 2 != 0).......");
System.out.printf("\tIs odd number(%d): %b\n", num, (num % 2 != 0));
num = Integer.MAX_VALUE;
System.out.printf("\tIs odd number(%d): %b\n", num, (num % 2 != 0));
num = Integer.MAX_VALUE-1;
System.out.printf("\tIs odd number(%d): %b\n\n", num, (num % 2 != 0));

Output:
Using condition (num % 2 == 1).......
Is odd number(2147483647): true
Is odd number(-2147483647): false

Using condition (num % 2 != 0).......
Is odd number(-2147483647): true
Is odd number(2147483647): true
Is odd number(2147483646): false

Tuesday, May 4, 2010

Java Bean

There is lot more beyond getters and setters for a java bean to summarise.

1) setters and getters- general properties
2) persistance just like normal objects, even beans have their own persistance api to save and retreive beans which internally uses ObjectOutputStream.
There are some classes in beans api which let you do this
3) listener-event model:when ever there is change in bean properties listeners can be notified

Say the bean has properties
Types of bean properties
a) The notified listeners can veto the change in the bean properties ( Constrained)
b) when ever a property changes there can be another property which is dependant on this this is (bound)so when ever there is change in property the one which is bound is notified about the change
c)( simple properties) no notification onli the property is changed
d) indexed - has range of values like in array


http://java.sun.com/docs/books/tutorial/javabeans/TOC.html

Thursday, April 22, 2010

java synchronized block

example 1
-------------------------------------------------------------------------------------
inc1(String name) {
synchronized(this) {
c1++ --------------------T1 can enter , this cannot be modified else where
}
}


inc2(String name) {
synchronized(this) { --------------------T2 (thread 2 cannot go inside and change)
c2++
}
}

example 2
-------------------------------------------------------------------------------------

inc1() {
synchronized(lock1) { ---------------lock1 cannot be modified else where
c1++;-------------------------------T1 can enter
}
}

inc2() {
synchronized(lock2) { ---------------lock2 cannot be modified else where
c2++; ------------------- T2 also can enter and change
}
}

Thursday, April 8, 2010

Loop In Linked List

Imagine a tortoise which goes around a circular ground and two hares which run in the same ground faster than the tortoise at some point the hare will reach the tortoise.

That means there is a loop in the linked list.

Here is the sample code.

    public static void main(String[] args) {
Node aNode = new Node(1);
makeLoopedLi(aNode);
System.out.println(hasLoop(aNode));
}

private static boolean hasLoop(Node aNode) {
Node torto = aNode;
Node slowHare = torto.next;
Node fastHare = slowHare.next;
while (torto != null && slowHare != null && fastHare != null) {
if ((torto.a == slowHare.a) || (torto.a == fastHare.a)) {
return true;
}
torto = torto.next;
slowHare = fastHare.next;
fastHare = slowHare.next;
}
return false;
}

private static void makeLoopedLi(Node aNode1) {
Node aNode2 = new Node(2);
Node aNode3 = new Node(3);
Node aNode4 = new Node(4);
Node aNode5 = new Node(5);
Node aNode6 = new Node(6);
Node aNode7 = new Node(7);
aNode1.next = aNode2;
aNode2.next = aNode3;
aNode3.next = aNode4;
aNode4.next = aNode5;
aNode5.next = aNode6;
aNode6.next = aNode7;
aNode7.next = aNode2;
}

}

Monday, April 5, 2010

Sunday, December 28, 2008

Cross-Site Scripting (XSS)

  • A web site may be vulnerable if there is a provision for a user to enter javascript through input fields and the inserted data is viewed by another webpage in HTML tags (not in TextArea or TextFields).
  • If there is such a case of displaying the data on a webpage, then the attacker can input the malicious script into database, which hijacks the cookie or session information when executed.
  • When we see the inputted data through another webpage - the script runs, collects the session info and passes the info to the attacker’s website.

An User can be tricked to click a hyperlink, which upon clicking inserts the script into application and executes in application domain bypassing the browser security restrictions.

Ex:

1) WebApp runs on http://webappdomain/webapp/ and a User is logged in.

2) From another website/email or from any other source, a user can be tricked to click the following url.

3) The hyperlink with script if clicked, can execute the script under the domain (webappdomain/webapp/).


<a href="”http://webappdomain/webapp/getdetails?clientno="1&station="1&code="1<SCRIPT">alert(’Hi’); </script>”>http://webappdomain/webapp/xxxx.jsp</a>



4) If the page, which is loaded by the above click, prints the parameter ‘code’ on the webpage, then the scripts gets injected into the webpage and executes.

Tuesday, December 23, 2008

Read and Modify the same XML and using getElementById

<strong>xml a.xml to parse </strong>

<books>
<book id="1">
<name>java
</name>
</book>
<book id="2">
<name>perl
</name>
</book>
<book id="3">
<name>java 2
</name>
</book>
</books>

the XSD where you specify the id so that getElementById method can be used
<!ELEMENT name (#PCDATA)>

<!--- Put your DTDDoc comment here. -->
<!ELEMENT book (name)*>
<!ATTLIST book id ID #REQUIRED >

<!--- Put your DTDDoc comment here. -->
<!ELEMENT books (book)*>

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//parse the xml file
Document doc = docBuilder.parse(new File("a.xml"));
doc.getElementById("1").getChildNodes().item(1).setTextContent("new book name");
Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new File("a.xml"));
//write the dom tree which is updated back to the same xml file
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);