Our social:

Latest Post

Thursday, 24 March 2016

Exception Handling Part2

Now we are going to continue from last post:
Inside a method if any exception occured the method in which it is raised is responsible to create exception object by including the following information 
1. Name of exception
2. Description of exception
3. Location at which exception occurs(stack trace)

After creating exception object, method handovers that object to the jvm.
JVM will check wether the method contains an exception handling code or not. if the method doesnot contain exception handling code then, JVM terminates that method abnormally and removes crossponding entry from the stack.

Then JVM identifies caller methods and checks wether caller method contains any Exception handling code or not. If the caller method doesnot contain any handling code then JVM terminates tthat caller methods also abnormally and removes the crossponding entery from the stack

This process will be continued until main method, if the main() also doesnt contain handling code the JVM will terminates main() abnormally and removes crossponding entry from the stack also.

Then JVM handovers responsibility of exception handling to DefaultExceptionHandler, which is the part of JVM. DefaultExecptionHandler prints exception information in the following formate and terminates program abnormally.

Wednesday, 23 March 2016

Exception Handling in Java

                                 Exception Handling Part1

An unexcepted unwantaed event that disturbs the normal excution flow of a program is called Exception...

EX--TyrePuncturdeException during travelling, SleepingException during class, FileNotFoundException while file reading...e.t.c

->It is highly recommended to handle exception
The main objective of Exception handling is gracefull or normal termination of program...

To defining an alternative way to continue rest of the program normally while exception comes is the concept of Exception Handling.

Exception handling dosent mean reparing an exception, here we have to provide alternative way to continue rest of the program normally is the concept of Exception Handling.

For example:
our programming requirment is to read data from remote file locating at london at runtime if london file is not available our program should not be terminated abnormally..we have to provide some local file to continue rest of the program normally..this way of defining alternative is nothing but exception handling.

 try{
//Read data from Remote File
//Locating at london
    }catch(FileNotFoundException e){
//use local file & continue
//rest of the program normally
               }

                                                                                              Thank you(Smartyy Sunny)

Tuesday, 29 December 2015

Why main() has public static void syntax

The Reason behind

public static void main(String[] arg){}

In the above statement public static void main(String[] arg){} is prototype that is provided by JVM. This static method is given to programmer to write their program execution flow logic like which class we are going to load first, which method execution requires first.  By using main method we are requesting to JVM , hy!!!! jvm you start my program execution in the given fashion.

Now here one question arises that why Main method?
Actually main method is not user defined method, this method is predefined and given by jvm. By following thier prototype we are writing our logic here.
Then another question is why main method has public static void main(){} syntax.

why public?
public is because our program executed by jvm and we all know jvm resides outside of our class and package. In case we define private and protected then, the scope of main() method will be restricted and can not be accessed outside of the package or class will not be unable to execute or load in JVM. that's why we should write main() as public.

why static?
we are using main() as static, why because to execute our program we need some memory in jvm. And as we know that static is special keyword that provide memory of a method at class loading time. So at the time of public class or main method class loading jvm will provide memory to main method. Once main() method gets memory then it will starts execution of our program sequentially.

why void?
main()method not returning any values and there is also no need to return any values. It is only use to execute programs that's why it is declared as void.

NOTE:this is my analyzation and gathered from internet, if i am wrong on any point please correct me through comment then i will try to modify...
                                     Thank you(Smartyy Sunny)

Friday, 25 December 2015

Growable Array Alogrithm programming Example

Tags: How To Create Growable Array, How To Insert Element In Array,How To Insert Add In Array,How To Remove Element In Array,How To Replace Element In Array, Growable Array Algorithm Program.


This is growable Array algorithm program that is able to slove size limitation and type limitation     problem of Array. In this progran we can increase array size if required dynamically" increaseCapacity()" method is responsible to do this work. Here various methods are defined to insert, add, remove and replace to modifying array element


import java.io.*;
class ArrayCollection
{
private Object[] arrayObj=new Object[5];
private int index=0;
public int capacity(){
return arrayObj.length;
}

public int size(){
return index;
}

//Add elements to array
public void add(Object ele){
if(this.size()==this.capacity()){
increaseCapacity();
}
this.arrayObj[index]=ele;
index++;
}

//increase capacity method for arrayObject 

public void increaseCapacity(){
int newCapacity=this.capacity()+(this.capacity()/2);
Object[] tempObj=new Object[newCapacity];

for(int i=0;i<arrayObj.length; i++){
tempObj[i]=arrayObj[i];
}
arrayObj=tempObj;
}

//retriving element from collection

public Object get(int index){
if(index<size()){
return arrayObj[index];
}else{

throw new ArrayIndexOutOfBoundsException(""+index);
}
}

//replacing logic in replace method

public void replace(int index, Object ele){
if(index>=this.size()){
throw new ArrayIndexOutOfBoundsException(""+index);
}else{
this.arrayObj[index]=ele;

}
}

//code removing elements from collection

public void remove(int index){
if(index >=this.size()){
throw new ArrayIndexOutOfBoundsException(""+index);
}else{
for(;index<this.size()-1; index++){
arrayObj[index]=arrayObj[index+1];
}
arrayObj[size()-1]=null;
index--;
}
}

//inserting values in Collection

public void insert(int index, Object ele){
if(index>=size()){
throw new ArrayIndexOutOfBoundsException(""+index);
}
if(size()==capacity()){
increaseCapacity();
}
//System.out.println("hiii");
for(int i=size();i>=index;i--){
arrayObj[i+1]=arrayObj[i];
}
arrayObj[index]=ele;
this.index++;
}
// this section for testing purpose........
public static void main(String[] args) 
{
ArrayCollection ar=new ArrayCollection();
System.out.println("Capacity of this array "+ar.capacity());
System.out.println("Size of this array "+ar.size());

ar.add(1);
ar.add(2);
ar.add(3);
ar.add(4);
ar.add(5);
System.out.println("Size of this array "+ar.size());
//System.out.println("value "+ ar.get(2));
//ar.replace(2,8);
//System.out.println("after replacing index 2 value is"+ ar.get(2));
//ar.remove(2);
//System.out.println("after removing index 2 value is"+ ar.get(2));
//System.out.println(ar.size());
ar.insert(2,9);
System.out.println(ar.get(2));
System.out.println(ar.capacity());
System.out.println(ar.size());
}
//you can also test program according to requirment....
}

You can execute this program directly by just copy from here and paste in notepad and save this file with class name with .java extension. And then simply execute from command prompt...
                                                                                    Thank you(Smartyy Sunny)
                                                                                                         Happy Coding......

Thursday, 17 December 2015

Can we typecast implicit variable "this" and "super" if yes then how, if no then why?

:Yes we can typecast implicit variable "this". this variable is a special referenced type variable that is used to denote current object class reference. It will internally stores the value of current object class reference.
code snapshop:


Now our question is if yes the how it is possible.....
Simple, we have a class called  A that bydefault extending Object class so it is good to take this example..Now i have created an object of class A and i want to store class A type variable value in Object type reference variable.
A a=new A();
Object obj1=(Object)this;
here this variable contains A class information  and that is going to be store in obj1 that is of Object type.
so in this case we are able to typecast implicit variable "this"
full code is looks something like this:

------------------------------------------------------------------------------------------------------------------------
class Atest 
{
void m1(){
Object obj1=(Object)this;//here obj1 is Object type super class ref variable 
                        //and we are passing this variable value(subclass type) to super class
System.out.println(obj1.getClass());
}
public static void main(String[] args) 
{
Atest a1=new Atest();
a1.m1();
}
}

According to my analyzation "super" variable type casting is not possible.....
why, i will post in next post..........

                                                                                                             Thank you(Smartyy Sunny)

Saturday, 24 October 2015

How To Purpose Your GirlFriend Using Java


Lebels: Purpose a girl using java language, Velentine Gift for Java Programmer, Write I Love In java,



package org.smartyy.ilove;

/*
 * @author
 * @Smartyy Sunny
 *@version 1.0
*/

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.*;
public class LoopLove {


public static void main(String[] args) throws AWTException{
Robot r1=new Robot();
Runtime run=Runtime.getRuntime();
try {


run.exec("notepad");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

r1.delay(1000);
r1.keyPress(KeyEvent.VK_I);
r1.keyRelease(KeyEvent.VK_I);
r1.delay(2000);

r1.keyPress(KeyEvent.VK_SPACE);
r1.keyRelease(KeyEvent.VK_SPACE);

r1.keyPress(KeyEvent.VK_L);
r1.keyPress(KeyEvent.VK_O);
r1.keyPress(KeyEvent.VK_V);
r1.keyPress(KeyEvent.VK_E);
r1.keyRelease(KeyEvent.VK_L);
r1.keyRelease(KeyEvent.VK_O);
r1.keyRelease(KeyEvent.VK_V);
r1.keyRelease(KeyEvent.VK_E);
r1.delay(2000);

r1.keyPress(KeyEvent.VK_SPACE);
r1.keyRelease(KeyEvent.VK_SPACE);

r1.keyPress(KeyEvent.VK_Y);
r1.keyPress(KeyEvent.VK_O);
r1.keyPress(KeyEvent.VK_U);
r1.keyRelease(KeyEvent.VK_Y);
r1.keyRelease(KeyEvent.VK_O);
r1.keyRelease(KeyEvent.VK_U);

r1.delay(2000);

for(int j=0;j<10;j++){

r1.delay(1000);
r1.keyPress(KeyEvent.VK_BACK_SPACE);
r1.keyRelease(KeyEvent.VK_BACK_SPACE);
}
r1.delay(2000);

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

r1.keyPress(KeyEvent.VK_SPACE);
r1.keyRelease(KeyEvent.VK_SPACE);

r1.keyPress(KeyEvent.VK_I);
r1.keyRelease(KeyEvent.VK_I);
r1.delay(2000);

r1.keyPress(KeyEvent.VK_SPACE);
r1.keyRelease(KeyEvent.VK_SPACE);

r1.keyPress(KeyEvent.VK_L);
r1.keyPress(KeyEvent.VK_O);
r1.keyPress(KeyEvent.VK_V);
r1.keyPress(KeyEvent.VK_E);
r1.keyRelease(KeyEvent.VK_L);
r1.keyRelease(KeyEvent.VK_O);
r1.keyRelease(KeyEvent.VK_V);
r1.keyRelease(KeyEvent.VK_E);


r1.keyPress(KeyEvent.VK_SPACE);
r1.keyRelease(KeyEvent.VK_SPACE);

r1.keyPress(KeyEvent.VK_Y);
r1.keyPress(KeyEvent.VK_O);
r1.keyPress(KeyEvent.VK_U);
r1.keyRelease(KeyEvent.VK_Y);
r1.keyRelease(KeyEvent.VK_O);
r1.keyRelease(KeyEvent.VK_U);




r1.delay(3000);

for(int j=0;j<10;j++){
r1.keyPress(KeyEvent.VK_BACK_SPACE);
r1.keyRelease(KeyEvent.VK_BACK_SPACE);
}
r1.delay(1000);

}

}

}

------------------------------------------------------------------------------------------------------------------------
Procedure to make it executable for non-technical people. Let's see if you girlfriend is a non-technical person then set an enviornment to make it simple executable application.

1st Step

  • create a shortcut in desktop
  • in location section write "javaw com.smartyy.ilove.LoopLove"
  • name for the shortcut[LoveApp] and click finish.


2nd Step


  • right click on the shortcut icon
  • click on properties
  • click on shortcut
  • and provide location of the java class 
  • note: download my sorce folder and paste on any location like D: drive 
  • Locate bin folder path in Start-in box
  • choose shortcut key 
  • choose any icon and click ok


download work folder

After downloading copy this folder on D drive and copy patha of bin and paste in START-IN box. Ensure that you have installed JRE or JDK in your computer system. if not installed go to google and first create java enviornment then set up this application...


if you have done all these things:
Now say your girlfriend  to double click on that shortcut icon created by.... and see what happend...
Good luck guysss and enjoyee new approch to express your love....happy cooading

                                                                                                     Thank you (Smartyy Sunny)

Sunday, 18 October 2015

Day to Day operation in java by using java keywords


Day to Day operation in java by using java keywords
Package creation and uses
Class creation
Variables and method creation
Memory allocation
Controlling and modification
Setting access permission
Setting execution level permission
Establishing inheritance relationship
Representing objects and its member
Handling user mistake

Initially java contains 47 keywords. But 3 other keywords are added in other java releases.
In java 1.2 strictfp is a new keyword added in java keyword list. 
In java 1.4 another new keyword assert is added in keyword list.
In java 1.5 another keyword enum is added in java keyword list.
So, now java has totally 50 keywords to perform special operation. Each keyword has special meaning to perform one task.
Now i am going to show all 50 keyword of java as per their uses.