Showing posts with label advanced java. Show all posts
Showing posts with label advanced java. Show all posts

Thursday, 7 December 2017

What Is Developer's Best Practice

Learning Java Programming Made Easy


Developers Best Practices Tutorial


This small tutorial is based on my past 16+ years of experience in software development industry. I have gone through different stages in my career starting from trainee software developer till senior management.
I do not want to keep my learnings to myself, so I had written a small tutorial a few years ago, and after getting a lot of motivation from my dear readers, I thought of revising it and adding few more learnings which may benefit many other software engineers and developers working in this lovely industry.
I'm not going to dictate any of the points, but all the practices listed here contributed a lot in my software development career, so if you think they make some sense for you then try to adopt few. If you have any +/- comments, kindly feel free to write me back in comments:

If you are working in the software industry as a software engineer or a software developer, then I'm sure you are going to enjoy this tutorial. Try to relate the facts mentioned in the tutorial with your day-2-day life and find so many hidden facts, which are very obvious but we never gave our serious attention to them.

Before writing all the practices mentioned in this small tutorial, I have made an assumption that you are working as a software professional and you understand basic software terminologies and atmosphere around a software professional. 


Sunday, 3 December 2017

What is Difference between Method Overloading and Method Overriding in Java Programming

Learning Java Programming Made Easy


Difference between method overloading and method overriding in java:


There are many differences between method overloading and method overriding in java. A list of differences between method overloading and method overriding are given below:

No.Method OverloadingMethod Overriding
1)Method overloading is used to increase the readability of the program.Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
2)Method overloading is performed within class.Method overriding occurs in two classes that have IS-A (inheritance) relationship.
3)In case of method overloading, parameter must be different.In case of method overriding, parameter must be same.
4)Method overloading is the example of compile time polymorphism.Method overriding is the example of run time polymorphism.
5)In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.Return type must be same or covariant in method overriding.




Java Method Overloading example:

  1. class OverloadingExample{  
  2. static int add(int a,int b){return a+b;}  
  3. static int add(int a,int b,int c){return a+b+c;}  
  4. }  

Java Method Overriding example:

  1. class Animal{  
  2. void eat(){System.out.println("eating...");}  
  3. }  
  4. class Dog extends Animal{  
  5. void eat(){System.out.println("eating bread...");}  
  6. }  

Saturday, 2 December 2017

How to get Armstrong Number Java

Learning Java Programming Made Easy


  1. class ArmstrongExample{  
  2.   public static void main(String[] args)  {  
  3.     int c=0,a,temp;  
  4.     int n=153;//It is the number to check armstrong  
  5.     temp=n;  
  6.     while(n>0)  
  7.     {  
  8.     a=n%10;  
  9.     n=n/10;  
  10.     c=c+(a*a*a);  
  11.     }  
  12.     if(temp==c)  
  13.     System.out.println("armstrong number");   
  14.     else  
  15.         System.out.println("Not armstrong number");   
  16.    }  
  17. }  

How to get Factorial Number Program Java

Learning Java Programming Made Easy


  1. class FactorialExample{  
  2.  public static void main(String args[]){  
  3.   int i,fact=1;  
  4.   int number=5;//It is the number to calculate factorial    
  5.   for(i=1;i<=number;i++){    
  6.       fact=fact*i;    
  7.   }    
  8.   System.out.println("Factorial of "+number+" is: "+fact);    
  9.  }  
  10. }  

How to get palindrome Number Program in java programming

Learning Java Programming Made Easy



  1. class PalindromeExample{  
  2.  public static void main(String args[]){  
  3.   int r,sum=0,temp;    
  4.   int n=454;//It is the number variable to be checked for palindrome  
  5.   
  6.   temp=n;    
  7.   while(n>0){    
  8.    r=n%10;  //getting remainder  
  9.    sum=(sum*10)+r;    
  10.    n=n/10;    
  11.   }    
  12.   if(temp==sum)    
  13.    System.out.println("palindrome number ");    
  14.   else    
  15.    System.out.println("not palindrome");    
  16. }  
  17. }  

How to get Prime number using Java Programming

Learning Java Programming Made Easy


  1. public class PrimeExample{    
  2.  public static void main(String args[]){    
  3.   int i,m=0,flag=0;      
  4.   int n=3;//it is the number to be checked    
  5.   m=n/2;      
  6.   if(n==0||n==1){  
  7.    System.out.println(n+" is not prime number");      
  8.   }
  9. else{  
  10.    for(i=2;i<=m;i++){      
  11.     if(n%i==0){      
  12.      System.out.println(n+" is not prime number");      
  13.      flag=1;      
  14.      break;      
  15.     }      
  16.    }      
  17.    if(flag==0)  { System.out.println(n+" is prime number"); }  
  18.   }//end of else  
  19. }    
  20. }   

How to get Fibonaci Series in JAVA Programming

Learning Java Programming Made Easy


  1. class FibonacciExample1{  
  2. public static void main(String args[])  
  3. {    
  4.  int n1=0,n2=1,n3,i,count=10;    
  5.  System.out.print(n1+" "+n2);//printing 0 and 1    
  6.     
  7.  for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed    
  8.  {    
  9.   n3=n1+n2;    
  10.   System.out.print(" "+n3);    
  11.   n1=n2;    
  12.   n2=n3;    
  13.  }    
  14.   
  15. }
  16. }

How to use SWITCH case statement in java jsp Programming

Learning Java Programming Made Easy


SWITCH case statement in java jsp Programming


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    
    <%
         //<editor-fold defaultstate="collapsed" desc="comment">
int day
//</editor-fold>
=Integer.parseInt(request.getParameter("enterno")); 
        
      
       
switch(day) {
case 0:
   out.println("It\'s Sunday.");
   break;
case 1:
   out.println("It\'s Monday.");
   break;
case 2:
   out.println("It\'s Tuesday.");
   break;
case 3:
   out.println("It\'s Wednesday.");
   break;
case 4:
   out.println("It\'s Thursday.");
   break;
case 5:
   out.println("It\'s Friday.");
   break;
default:
   out.println("It's Saturday.");
}
%>
</html>

How to get session using java jsp programming code

Learning Java Programming Made Easy

get session using java jsp programming code 

Page 1:

/***********************************************//

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
   
    <body>
         
        <%
        String username=request.getParameter("username");
        //String password=request.getParameter("password");
        out.println("welcome : "+username);
      //  session.setAttribute("user",username);     
        %>
        <a href="sessionsecond.jsp?user=<%=username%>">gotonextpage</a>
    </body>
</html>

/**********************************************//


Page 2:

/**********************************************//
<%-- 
    Document   : sessionsecond
    Created on : Dec 7, 2016, 11:13:13 AM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            //String usermname=session.getAttribute("user").toString();
            String name=request.getParameter("user");
            String pass=request.getParameter("pass");
          out.println("Welcome : "+name);
        %>
        <a href="sessionthird.jsp">gotonextpage</a>
    </body>
</html>

/********************************************//


Page 3:

/***************************************//

<%-- 
    Document   : sessionstartpage
    Created on : Dec 7, 2016, 11:09:47 AM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="sessionfirstppag.jsp" method="post"> 
            <input type="text" name="username"><br/>
            <input type="text" name="password"><br/>
            <input type="submit" value="go">
        </form>
    </body>
</html>

/***************************************//

Page 4:

/****************************************//

<%-- 
    Document   : sessionthird
    Created on : Dec 7, 2016, 11:17:50 AM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       <%
            //String usermname=session.getAttribute("user").toString();
            String usermname=request.getParameter("user");
          out.println("welcome : " +usermname);
        %>
    </body>
</html>


/******************************************//

How to include one jsp file into another jsp file body to display data java jsp programming code

Learning Java Programming Made Easy


Page 1: Hit Counter (jsp page)

/*******hits.jsp*******//
<%-- 
    Document   : hits
    Created on : Nov 30, 2016, 11:54:25 AM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<%
    Integer hitsCount = (Integer)application.getAttribute("hitCounter");
    if( hitsCount ==null || hitsCount == 0 ){
       out.println("first time!");
       hitsCount = 1;
    }else{
       /* return visit */
       out.println("Welcome back!");
       hitsCount += 1;
    }
    application.setAttribute("hitCounter", hitsCount);
%>
<p>Total number of visits: <%= hitsCount%></p>
</body>
</html>



/***********************************************************//


Page 2: (jsp file)


/************main.jsp**************/


<%-- 
    Document   : main
    Created on : Nov 30, 2016, 12:04:52 PM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<jsp:include page="hits.jsp" flush="true" />
</body>
</html>



How to use get and set property in java jsp programming

Learning Java Programming Made Easy



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<jsp:useBean id="test" class="action.testbean" />
<jsp:setProperty name="test" 
                    property="message" 
                    value="Hello JSP..." />
<jsp:getProperty name="test" property="message" />
</body>
</html>

how to use java program for website HIT Counter java jsp programming code

Learning Java Programming Made Easy






<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>

<html>
<body>
<%
    Integer hitsCount = (Integer)application.getAttribute("hitCounter");
    if( hitsCount ==null || hitsCount == 0 ){
       out.println("first time!");
       hitsCount = 1;
    }else{
       /* return visit */
       out.println("Welcome back!");
       hitsCount += 1;
    }
    application.setAttribute("hitCounter", hitsCount);
%>
<p>Total number of visits: <%= hitsCount%></p>
</body>
</html>

How to use checkbox program in java jsp

Learning Java Programming Made Easy

Page 1:


<%-- 
    Document   : check
    Created on : Feb 9, 2017, 11:40:10 AM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
    <form action="checkcall.jsp" method="POST" target="_blank">
    <input type="checkbox" name="a" checked="checked" /> A
    <input type="checkbox" name="b"  /> B
    <input type="checkbox" name="c" checked="checked" /> C
<input type="submit" value="Select Subject" />
</form>
</body>
</html>



/************************************************************************/



Page 2:


<%-- 
    Document   : checkcall
    Created on : Feb 9, 2017, 11:40:30 AM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<b>A Flag:</b><%= request.getParameter("a")%>
<b>B Flag:</b><%= request.getParameter("b")%>
<b>C Flag:</b><%= request.getParameter("c")%>
</body>
</html>

How to send data from one page to other page java jsp programming code

Learning Java Programming Made Easy


Page 1: first page (html/jsp)



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <!--script let java code-->
        <%
            out.println("<h1>hello how are u</h1>");
        %>
        <br>
        <!--expression java code-->
        <%= "hi hello how are u"%>
        <br>
        <!--declaration java code-->
        <%! int age = 18;%>
        <%!

            int sum(int i, int j) {
                return i + j;
            }

        %>  
        <%= "the sum of 2 no=" + sum(4, 5)%><br>
        <%= "the age of emp=" + age%><br>
        <jsp:forward page="forwrod.jsp"></jsp:forward>
    </body>
</html>



Page 2: Second page (HTMl/JSP) 

<%-- 
    Document   : second
    Created on : Jan 31, 2017, 11:33:55 AM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <form action="third.jsp">
            username:<input type="text" name="user"/>
            <input type="submit" value="click"/>
        </form>
           
    </body>
</html>




Page 3: Third page (html/jsp)



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
      <%
      String str=request.getParameter("user");
      out.println("username="+str);
      %>
    </body>
</html>

How to get todays date using java jsp code and programming

Learning Java Programming Made Easy



<%-- 
    Document   : date
    Created on : Feb 14, 2017, 12:12:49 PM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html> 
<body>
<p>
   Today's date: <%= (new java.util.Date().toString())%>
</p>
</body> 
</html>

How to call stored COOKIES using java jsp programming code and get data out

Learning Java Programming Made Easy




<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<body>

    <form action="coock.jsp" method="GET">

First Name: <input type="text" name="first_name">

<br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>

</body>



How to store COOKEIS using java jsp code

Learning Java Programming Made Easy



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
   // Create cookies for first and last names.      
   Cookie firstName = new Cookie("first_name",request.getParameter("first_name"));
   Cookie lastName = new Cookie("last_name",request.getParameter("last_name"));
   // Set expiry date after one hour for both the cookies.
   firstName.setMaxAge(1*1); 
   lastName.setMaxAge(1*1); 
   // Add both the cookies in the response header.
   response.addCookie( firstName );
   response.addCookie( lastName );
%>
<html>
<body>
<b>First Name:</b><%= request.getParameter("first_name")%><br/>
<b>Last  Name:</b><%= request.getParameter("last_name")%>
</body>
</html>

How to get IP using Java jsp programming

Learning Java Programming Made Easy




<%-- 
    Document   : ip
    Created on : Feb 14, 2017, 12:11:42 PM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>

Hoe to upload a file on website unsig java jsp programming

Learning Java Programming Made Easy


HTML/JSP View File:

<%-- 
    Document   : upload
    Created on : Feb 14, 2017, 12:25:46 PM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
Select a file to upload: <br />
<form action="uploadtarget.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>


/**********************************************************/

JSP Controller File:     uploadtarget.jsp

<%-- 
    Document   : uploadtarget'
    Created on : Feb 14, 2017, 12:26:40 PM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page import="java.io.*,java.util.*,javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>
<%
   File file ;
   int maxFileSize = 5000 * 1024;
   int maxMemSize = 5000 * 1024;
   String filePath = "c:/apache-tomcat/webapps/data/";

   String contentType = request.getContentType();
   if ((contentType.indexOf("multipart/form-data") >= 0)) {

      DiskFileItemFactory factory = new DiskFileItemFactory();
      factory.setSizeThreshold(maxMemSize);
      factory.setRepository(new File("c:\\temp"));
      ServletFileUpload upload = new ServletFileUpload(factory);
      upload.setSizeMax( maxFileSize );
      try{ 
         List fileItems = upload.parseRequest(request);
         Iterator i = fileItems.iterator();
         out.println("<html>");
         out.println("<body>");
         while ( i.hasNext () ) 
         {
            FileItem fi = (FileItem)i.next();
            if ( !fi.isFormField () )  {
                String fieldName = fi.getFieldName();
                String fileName = fi.getName();
                boolean isInMemory = fi.isInMemory();
                long sizeInBytes = fi.getSize();
                file = new File( filePath + "yourFileName") ;
                fi.write( file ) ;
                out.println("Uploaded Filename: " + filePath + fileName + "<br>");
            }
         }
         out.println("</body>");
         out.println("</html>");
      }catch(Exception ex) {
         System.out.println(ex);
      }
   }else{
      out.println("<html>");
      out.println("<body>");
      out.println("<p>No file uploaded</p>"); 
      out.println("</body>");
      out.println("</html>");
   }
%>




How to use auto refresh using java jsp program code

Learning Java Programming Made Easy




<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page import="java.io.*,java.util.*;" %>
<html>
<body>
<%
   response.setIntHeader("Refresh", 1);
   Calendar calendar = new GregorianCalendar();

   String am_pm;
   int hour = calendar.get(Calendar.HOUR);
   int minute = calendar.get(Calendar.MINUTE);
   int second = calendar.get(Calendar.SECOND);
   if(calendar.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
   else
      am_pm = "PM";
   String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
   out.println( CT + "\n");
%>
</body>
</html>

Charactor_At_Position

public class Char_At_Position { public static void main(String[] args) { String str = "Wankhede"; String rev=""...