check a number is armstrong or not in java

check a number is armstrong or not  in java :


package com.pkjavacode.com;

public class ArmstrongDemo {

/**
* @auther pradeep
*/
public static void main(String[] args) {

int n = 153, c = 0, a, d;
d = n;
while (n > 0) {
a = n % 10;
n = n / 10;
c = c + (a * a * a);
}
if (d == c)
System.out.println("It is armstrong number");
else
System.out.println("it is not an armstrong number");
}
}

Output :

It is armstrong number

Advertisements

Coupling and Cohesion in java

Coupling :
Coupling is a degree where a class knows about another class. If class A has about class B, is what class B exposed
through its interface, then class A and B are called loosely coupled. And on other hand if class A relies on parts of class B
that are not part of class B’s interface, then the coupling between the class A and B is tighter which is not a good thing.

What is MVC in struts 2 ?

 MVC in struts 2 :

In struts Model, View and Controller(MVC) is a design pattern, which is used to develop web application in java.
Model : It is responsible for retrieving data from the database and saving the data into the database. It also responsible to implements all business logic. When a data entered by the user through view are checked in the model before saving into the database. Data access, data validation and the data saving logic are part of model component.

View: It is represents the user view of the application which is responsible for taking the input from the user, dispatching the request to the controller and then receiving response from the controller and displaying the result back to the user. html, jsp, custom tag libraries, resources files are the part of view component.

Controller: It is intermediary between Model and View components. It is also responsible for receiving the request from client through view. Once request is received from client it executes the appropriate business logic from the Model and then produce the result to the user using the View component. ActionServlet, Action, ActionSupport and struts.xml are the part of Controller.

You can better understand through below diagram :

mvc diagram

 

Unicode System in java

Unicode System :

Unicode is a universal international standard character encoding which is capable of representing all the world’s written
languages.
Why java uses Unicode System?
Prior Unicode system, there were many language standards:

  • ASCII : It stand for American Standard Code for Information Interchange used for the United States.
  • ISO 8859-1 : It was used for Western European Language.
  • KOI-8 : Used for Russian.
  • GB18030 and BIG-5 : Used for chinese, and so on.

With these languages standard arise two problems:

  • A particular code value corresponds to different letters in the various language standards.
  • The encodings for languages with large character sets have variable length. Some common characters are encoded as single bytes, other require two or more byte.

To solve these problems, a new language standard was developed i.e. Unicode System.

In unicode, character holds 2 byte, so java uses 2 byte for characters.

  • lowest value are: \u0000 and
  • highest value are: \uFFFF

Tokenizing in java

Tokenizing :

Tokenizing is a mechanism of taking larg pieces of source data, breaking them into small pieces, and storing these small pieces in variables.

Example :
The most common tokenizing situation is reading a delimited file in order to find the contents of the file and moved into useful places such as objects, arrays and collections.
There are two classes in the java API that provides tokenizing mechanism :

  •  String
  • Scanner                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Both classes provide many methods which is useful in tokenizing.

What is token and delimiter?

Token is just a actual piece of data and delimiters are the expression that used to separate token from each other, it may be a comma, a backslashes or a single whitespace.

Example :


package com.pkjavacode.com;

import java.util.StringTokenizer;

public class TokenizingDemo {

/**
 * @param args
 */
 public static void main(String[] args) {
String str = "I am pradeep kumar yadav";
 String[] tokens = str.split(" ");
 System.out.println("Count tokens" + tokens.length);
 for (String s : tokens) {
 System.out.println(" " + s);
 }
 }
}

Output :

I
am
pradeep
kumar
yadav

Tokenizing with Scanner class :

When you need to do serious tokenizing you go for Scanner. It’s have following feature :

  • Scanners can be constructed using files, streams and Strings as a source.
  • Tokenizing mechanism is performed within a loop so that you can exit the process at any point.
  • Tokens can be converted to their appropriate primitive types automatically.

Example :


package com.pkjavacode.com;

import java.util.Scanner;

public class TokenizingScannerExample {

/**
* @author pradeep
*/
public static void main(String[] args) {
String str = "I am Pradeep Kumar Yadav";
Scanner s = new Scanner(str);
String s2;
while (s.hasNext()) {
s2 = s.next();
System.out.println("" + s2);
}
}
}

Output :

I
am
Pradeep
Kumar
Yadav

Difference between include Directive and include Action in jsp

Difference between include Directive and include action :

The include Directive and include Action both are used to include content into a jsp page. But some differences between them are as follows :

  • The include directive tag include the content in the generated servlet page during the translation phase of jsp life cycle. It used to include file such as html, xml, jsp or .txt file into a jsp page statically. While include action tag include the response generated by executing the given jsp or servlet page during request processing phase.
  • The include directive tag does not allow us to use the expression for giving the url path, to including the resource in jsp page. While include action tag accepts expression.

Include tag in jsp

Include tag  :

Include action tag allows us to include a static or dynamic pages like html or jsp, specified by url during request processing. If page are static then its content is included in the calling jsp page. If requested page are dynamic then its output will be included on calling jsp. When requested page is dynamic then we can use <jsp:param> tag to pass name/value parameter in calling jsp.

Syntax :


<jsp:include attribute>

<!-- one or more <jsp:param> tag   -->

</jsp:include>

<jsp:include> action tag has “page” and “flush” attribute, which used to accept values for the specified jsp page.

page : It takes a relative url, which locate requested page. Here we can include relative url directly or in an expression within page attribute.

Note : Relative url can’t contain protocol, port, or domain name and starts with “/” character that specifying the url is taken relative to context path.

Example :

<jsp:include page=”/myhtml.html”/></pre>
<jsp:include page=”<%=mypath%>”/>
<pre>

flush : It indicate the buffer needs to flushed or not before including the page and takes true or false values. If value is true, the buffer is flushed and its false by default.