Pages

Thursday, December 15, 2011

String reverse

public class ReverseStringTest {
  public static void main(String[] args) {
    String str = "SubhanI";
    System.out.println(ReverseString.reverseIt(str));
  }
}

class ReverseString {
  public static String reverseIt(String source) {
    int i, len = source.length();
    StringBuffer dest = new StringBuffer(len);

    for (i = (len - 1); i >= 0; i--)
      dest.append(source.charAt(i));
    return dest.toString();
  }
}

FreeSpace

import java.io.IOException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Subhani.s
 */
public class freespace
{
    public static void main(String args[]) throws IOException
    {
      //1.  Runtime.getRuntime().exec("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
    System.out.println( "Free Memory: "+   Runtime.getRuntime().freeMemory());
    System.out.println( "Total Memory: "+ Runtime.getRuntime().totalMemory());
    System.out.print( "Used Memory: ");
    System.out.println (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
   
    }
   
}

Monday, December 5, 2011

Annotations

Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.
Annotations have a number of uses, among them:
  • Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
  • Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
  • Runtime processing — Some annotations are available to be examined at runtime.
Annotations can be applied to a program's declarations of classes, fields, methods, and other program elements.
The annotation appears first, often (by convention) on its own line, and may include elements with named or unnamed values:
@Author(
   name = "Benjamin Franklin",
   date = "3/27/2003"
)
class MyClass() { }
or
@SuppressWarnings(value = "unchecked")
void myMethod() { }
If there is just one element named "value," then the name may be omitted, as in:
@SuppressWarnings("unchecked")
void myMethod() { }
Also, if an annotation has no elements, the parentheses may be omitted, as in:
@Override
void mySuperMethod() { }

Friday, November 25, 2011

Singleton class

/**
*
* @author Subhani.s
*/
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() {}

/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHolder.INSTANCE, not before.
*/
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}

public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}


}

Thursday, August 25, 2011

xml interview questions

1. What is the difference between SAX parser and DOM parser?
DOM spec defines an object-oriented hieararchy.
The DOM parser creates an internal tree based on the hierarchy of the XML data.
Tree stays in memory until released. The DOM parser is more memory intensive. DOM Parser’s advantage is that it is simple. Pairs nicely with XSLT.
SAX spec defines an event based approach, calling handler functions whenever certain text nodes or processing instructions are found. These events include the start and end of the document, finding a text node, finding child elements, and hitting a malformed element. SAX development is more challenging. SAX can parse gigabytes worth of XML without hitting resource barriers. It’s also faster and more complex. Better for huge XML docs. Best suited for sequential-scan applications.
2. What is the difference between Schema and DTD?
Schema might outphase DTD. XML shemas are in XML. XML Schema has a lot of built-in data types including xs:string,xs:decimal,xs:integer,xs:boolean,xs:date,xs:time. XML schemas can be edited with Schema files, parsed with XML parser, manipulated with XML DOM, and transformed with XSLT.
The building blocks of DTD include elements, attributes, entities, PCDATA, and CDATA.
3. How do you parse/validate the XML document?
By using a SAXParser, DOMParser, or XSDValidator.
4. What is XML Namespace?
Defining a namespace to avoid confusion involves using a prefix and adding an xmlns attribute to the tag to give the prefix a qualified name associated with the namespace. All child elements with the same prefix are associated with the namespace defined in the start tag of an element.
5. What is Xpath?
XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer.
6. What is XML template?
* A style sheets describes transformation rules
* A transformation rule: a pattern + a template
* Pattern: a configuration in the source tree
* Template: a structure to be instantiated in the result tree
* When a pattern is matched in the source tree, the corresponding pattern is generated in the result tree
7. How would you produce PDF output using XSL’s?
FOP it. =)
-transform xml into xsl-fo doc using xslt, or DOM or SAX
-process xsl-fo using a Formatter to convert xsl-fo into a pdf.
8. What are the steps to transform XML into HTML using XSL?
An XSLT processor may output the result tree as a sequence of bytes.The xsl:output element allows stylesheet authors to specify how they wish the result tree to be output. If an XSLT processor outputs the result tree, it should do so as specified by the xsl:output element; however, it is not required to do so.The method attribute on xsl:output identifies the overall method that should be used for outputting the result tree.
The html output method outputs the result tree as HTML; for example,

9. What is XSL?
In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.
10. What is XSLT?
A language for transforming XML documents into other XML documents. XSLT is designed for use as part of XSL, which is a stylesheet language for XML.

Monday, July 25, 2011

JavaScript_Timer

<script type="text/javascript">

var alertTimerId = 0;

function alertTimerClickHandler ( )
{
if ( document.getElementById("alertTimerButton").value == "Click me and wait!" )
{
// Start the timer
document.getElementById("alertTimerButton").value = "Click me to stop the timer!";
alertTimerId = setTimeout ( "showAlert()", 6000);
}
else
{
document.getElementById("alertTimerButton").value = "Click me and wait!";
clearTimeout ( alertTimerId );
}
}

function showAlert ( )
{
alert ( "Too late! You didn't stop the timer." );
document.getElementById("alertTimerButton").value = "Click me and wait!";
}

</script>
<body onload="alertTimerClickHandler()">
<input type="button" name="clickMe" id="alertTimerButton" value="Click me and wait!"

onclick="alertTimerClickHandler()"/>

</body>

Thursday, April 15, 2010

javascript

JAVASCRIPT









What is JavaScript?
JavaScript was designed to add interactivity to HTML pages
JavaScript is a scripting language
A scripting language is a lightweight programming language
JavaScript is usually embedded directly into HTML pages
JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
Everyone can use JavaScript without purchasing a license
What can a JavaScript do?
JavaScript gives HTML designers a programming tool - HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small "snippets" of code into their HTML pages
JavaScript can put dynamic text into an HTML page - A JavaScript statement like this: document.write("

" + name + "

") can write a variable text into an HTML page
JavaScript can react to events - A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element
JavaScript can read and write HTML elements - A JavaScript can read and change the content of an HTML element
JavaScript can be used to validate data - A JavaScript can be used to validate form data before it is submitted to a server. This saves the server from extra processing
JavaScript can be used to detect the visitor's browser - A JavaScript can be used to detect the visitor's browser, and - depending on the browser - load another page specifically designed for that browser
JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve information on the visitor's computer
JavaScript's official name is ECMAScript.
ECMA-262 is the official JavaScript standard.
The language was invented by Brendan Eich at Netscape (with Navigator 2.0), and has appeared in all Netscape and Microsoft browsers since 1996.
The development of ECMA-262 started in 1996, and the first edition of was adopted by the ECMA General Assembly in June 1997.
The standard was approved as an international ISO (ISO/IEC 16262) standard in 1998.
The development of the standard is still in progress.
Comment line:






*-***********************
Put your functions in the head section, this way they are all in one place, and they do not interfere with page content.









If you don't want your script to be placed inside a function, or if your script should write page content, it should be placed in the body section.








If you want to run the same JavaScript on several pages, without having to write the same script on every page, you can write a JavaScript in an external file.
Save the external JavaScript file with a .js file extension.








JavaScript is Case Sensitive
Single line comments start with //.



Multi line comments start with /* and end with */.



Rules for JavaScript variable names:
Variable names are case sensitive (y and Y are two different variables)
Variable names must begin with a letter or the underscore character
A variable's value can change during the execution of a script. You can refer to a variable by its name to display or change its value.






The script above declares a variable,
assigns a value to it, displays the value, changes the value,
and displays the value again.






You can declare JavaScript variables with the var statement:
var x;
var carname;

After the execution of the statements above, the variable x will still have the value of 5. The value of x is not reset (or cleared) when you redeclare it.

As with algebra, you can do arithmetic operations with JavaScript variables:

y=x-5;
z=y+5;

y=5;

Operator
Description
Example
Result
+
Addition
x=y+2
x=7
-
Subtraction
x=y-2
x=3
*
Multiplication
x=y*2
x=10
/
Division
x=y/2
x=2.5
%
Modulus (division remainder)
x=y%2
x=1
++
Increment
x=++y
x=6
--
Decrement
x=--y
x=4


Operator
Example
Same As
Result
=
x=y
 
x=5
+=
x+=y
x=x+y
x=15
-=
x-=y
x=x-y
x=5
*=
x*=y
x=x*y
x=50
/=
x/=y
x=x/y
x=2
%=
x%=y
x=x%y
x=0

The + Operator Used on Strings
The + operator can also be used to add string variables or text values together.
To add two or more string variables together, use the + operator.
txt1="What a very";
txt2="nice day";
txt3=txt1+txt2;
After the execution of the statements above, the variable txt3 contains "What a verynice day".


Comparison and Logical operators are used to test for true or false.

Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x=5, the table below explains the comparison operators:
Operator
Description
Example
==
is equal to
x==8 is false
===
is exactly equal to (value and type)
x===5 is true
x==="5" is false
!=
is not equal
x!=8 is true
>
is greater than
x>8 is false
<
is less than
x<8 is true
>=
is greater than or equal to
x>=8 is false
<=
is less than or equal to
x<=8 is true

Comparison operators can be used in conditional statements to compare values and take action depending on the result:
if (age<18) document.write("Too young");

Logical Operators
Logical operators are used to determine the logic between variables or values.
Given that x=6 and y=3, the table below explains the logical operators:
Operator
Description
Example
&&
and
(x < 10 && y > 1) is true
||
or
(x==5 || y==5) is false
!
not
!(x==y) is true


Conditional Operator
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
Syntax
variablename=(condition)?value1:value2 
Example
greeting=(visitor=="PRES")?"Dear President ":"Dear ";
If the variable visitor has the value of "PRES", then the variable greeting will be assigned the value "Dear President " else it will be assigned "Dear".
Conditional Statements
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
In JavaScript we have the following conditional statements:
if statement - use this statement to execute some code only if a specified condition is true
if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false
if...else if....else statement - use this statement to select one of many blocks of code to be executed
switch statement - use this statement to select one of many blocks of code to be executed




Alert Box
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.










Confirm Box
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Syntax
confirm("sometext");













Prompt Box
A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
Syntax
prompt("sometext","defaultvalue");













Note: Do not forget about the importance of capitals in JavaScript! The word function must be written in lowercase letters, otherwise a JavaScript error occurs! Also note that you must call a function with the exact same capitals as in the function name.

The return Statement











The for Loop






The while loop












The break Statement
The break statement will break the loop and continue executing the code that follows after the loop (if any).






The continue Statement
The continue statement will break the current loop and continue with the next value.






JavaScript For...In Statement
The for...in statement loops through the elements of an array or through the properties of an object.








Events are actions that can be detected by JavaScript.
Examples of events:
A mouse click
A web page or an image loading
Mousing over a hot spot on the web page
Selecting an input field in an HTML form
Submitting an HTML form
A keystroke
Note: Events are normally used in combination with functions, and the function will not be executed before the event occurs!
Nuvve nenani anukunnane
manase neeke arpinchane
kalalo elalo nee rupam chusane
pade pade nenna chudalani talechane
kani.............
devaltalu eppudu manatho undarani telesinde
okka shanam nennu chuse aavakasam is the
aa okka shanam kosam veye sarlu chavadaniki siddamga unnane


onLoad and onUnload
The onLoad and onUnload events are triggered when the user enters or leaves the page.
The onLoad event is often used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.
Both the onLoad and onUnload events are also often used to deal with cookies that should be set when a user enters or leaves a page. For example, you could have a popup asking for the user's name upon his first arrival to your page. The name is then stored in a cookie. Next time the visitor arrives at your page, you could have another popup saying something like: "Welcome John Doe!".

onFocus, onBlur and onChange
The onFocus, onBlur and onChange events are often used in combination with validation of form fields.
Below is an example of how to use the onChange event. The checkEmail() function will be called whenever the user changes the content of the field:


onSubmit
The onSubmit event is used to validate ALL form fields before submitting it.
Below is an example of how to use the onSubmit event. The checkForm() function will be called when the user clicks the submit button in the form. If the field values are not accepted, the submit should be cancelled. The function checkForm() returns either true or false. If it returns true the form will be submitted, otherwise the submit will be cancelled:



onMouseOver and onMouseOut
onMouseOver and onMouseOut are often used to create "animated" buttons.
Below is an example of an onMouseOver event. An alert box appears when an onMouseOver event is detected:
W3Schools
The try...catch Statement

The try...catch statement allows you to test a block of code for errors.
The try...catch statement allows you to test a block of code for errors. The try block contains the code to be run, and the catch block contains the code to be executed if an error occurs.












The next example uses a confirm box to display a custom message telling users they can click OK to continue viewing the page or click Cancel to go to the homepage. If the confirm method returns false, the user clicked Cancel, and the code redirects the user. If the confirm method returns true, the code does nothing:












JavaScript Throw Statement

The throw statement allows you to create an exception. If you use this statement together with the try...catch statement, you can control program flow and generate accurate error messages.







JavaScript Special Characters

Code
Outputs
\'
single quote
\"
double quote
\&
ampersand
\\
backslash
\n
new line
\r
carriage return
\t
tab
\b
backspace
\f
form feed

JavaScript Guidelines
JavaScript is Case Sensitive
White Space
Break up a Code Line
You can break up a code line within a text string with a backslash. The example below will be displayed properly:
document.write("Hello \
World!");
However, you cannot break up a code line like this:
document.write \
("Hello World!");