Sunday, December 26, 2010
Friday, December 24, 2010
import java.util.*; public class OddEvenThread{ public static void main(String []str){ long no; System.out.println("Enter a no:"); Scanner sc = new Scanner(System.in); no = sc.nextLong(); OddEven oe = new OddEven(no); } } class OddEven implements Runnable{ private Thread t=null; private long _no; long getNo(){ return this._no; } OddEven(long no){ t = new Thread(this); this._no=no; t.start(); } public void run(){ if(_no%2==0){ System.out.println("No is Even"); } else{ System.out.println("No is Odd"); } } }
Thursday, December 16, 2010
Difference between Recursion and Iteration
1) Recursive function – is a function that is partially defined by itself whereas Iterative functions – are loop based imperative repetitions of a process
2) Recursion Uses selection structure whereas Iteration uses repitation structure
3) An infinite loop occurs with iteration if the loop-continuation test never becomes false whereas infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on the base case.
4) Iteration terminates when the loop-continuation condition fails whereas recursion terminates when a base case is recognized
5) When using recursion multiple activation records are created on stack for each call when in iteration everything is done in one activation record
6) Recursion is usually slower then iteration due to overhead of maintaining stack whereas iteration does not use stack so it's faster than recursion
7) Recursion uses more memory than iteration
8) Infinite recursion can crash the system whereas infinite looping uses cpu
cycles repeatedly
9) Recursion makes code smaller and iteration makes code longer
Wednesday, December 8, 2010
How to remove navbar from blog
Step1. Log in to Blogger
Step2. Click on Design
Step3. Go to Edit HTML tab
Step4. Under Edit HTML section find following section of code
-----------------------------------------------
Blogger Template Style
Name: Rounders
Designer: Elinor Finder
URL: www.malhar2010.blogspot.com
Date: 27 Feb 2010
Updated by: Blogger Team
----------------------------------------------- */
Step5. Paste the following snippet
#navbar-iframe {
display: none !important;
}
Step2. Click on Design
Step3. Go to Edit HTML tab
Step4. Under Edit HTML section find following section of code
-----------------------------------------------
Blogger Template Style
Name: Rounders
Designer: Elinor Finder
URL: www.malhar2010.blogspot.com
Date: 27 Feb 2010
Updated by: Blogger Team
----------------------------------------------- */
Step5. Paste the following snippet
#navbar-iframe {
display: none !important;
}
Monday, December 6, 2010
Creating executable jar file in 3 easy steps
Jar stands for Java archive. It's nothing but a compressed zip file which contains all files and folders related to your java program whether it is image, package, class file or anything else.It helps programmer pack file in one file and reduce the size of project.A jar file can be executable.
Jar file can be created using a utility comes with jsdk called jar. You can find it in <Your Java sdk path>\bin folder as jar.exe in Windows.
Here is simple steps to create a jar file of your java project.
Suppose i have a source file called P3.java.
Now follow steps given below :
Step 1. Compile your java source into .class file. My class file is P3.class
Step 2. Now create a file called prg.MF known as manifest file that contains following entries using notepad.
Created-By: 1.2 (Sun Microsystems Inc.)
Manifest-Version: 1.0
Main-Class: P3
In above entries first line tells the version and the vendor of Java implementation.
Second line indicates that our manifest file conforms to the specification version 1.0
Third line describes a name of class file in which main function resides.
Step3. Now the third and final step. Go to command prompt and execute jar utility as described below.
jar cvfm Prog.jar prg.MF *
Here c,v,f,m are different options of jar utility.
c - Creates a new file
v - Generates verbose output to screen (You can omit this option)
f - Specifies archive file name
m - Includes information from our own manifest file
Prog.jar is our archive file name.You can specify any name.
prog.MF is a manifest file created by us.
* indicates that all file should be included in archive.
Check folder for generated Prog.jar and execute it.
Jar file can be created using a utility comes with jsdk called jar. You can find it in <Your Java sdk path>\bin folder as jar.exe in Windows.
Here is simple steps to create a jar file of your java project.
Suppose i have a source file called P3.java.
Now follow steps given below :
Step 1. Compile your java source into .class file. My class file is P3.class
Step 2. Now create a file called prg.MF known as manifest file that contains following entries using notepad.
Created-By: 1.2 (Sun Microsystems Inc.)
Manifest-Version: 1.0
Main-Class: P3
In above entries first line tells the version and the vendor of Java implementation.
Second line indicates that our manifest file conforms to the specification version 1.0
Third line describes a name of class file in which main function resides.
Step3. Now the third and final step. Go to command prompt and execute jar utility as described below.
jar cvfm Prog.jar prg.MF *
Here c,v,f,m are different options of jar utility.
c - Creates a new file
v - Generates verbose output to screen (You can omit this option)
f - Specifies archive file name
m - Includes information from our own manifest file
Prog.jar is our archive file name.You can specify any name.
prog.MF is a manifest file created by us.
* indicates that all file should be included in archive.
Check folder for generated Prog.jar and execute it.
Friday, December 3, 2010
Program that will demonstrate the use of object serialization with different write methods
/* Program : 1 Create an application that will demonstrate the use of object serialization with different write methods Developed by : Malhar Vora Developed on :6-11-2010 Development Status : Completed and tested Email : vbmade2000@gmail.com WebSite : www.malhar2010.blogspot.com ********************************************************************************/ import java.io.*; public class P1 { public static void main(String []str) { ReadWriteData rwdata = new ReadWriteData(); rwdata.writeData("test1.txt"); rwdata.readData("test1.txt"); } } class ReadWriteData { public void writeData(String fname) { FileOutputStream fos = null; ObjectOutputStream oos = null; try{ fos = new FileOutputStream(new File(fname)); oos = new ObjectOutputStream(fos); oos.writeInt(10); oos.writeUTF("Hello World"); } catch(Exception e) { } finally { try { oos.close(); fos.close(); } catch(Exception e) { ; } } } public void readData(String fname) { FileInputStream fis = null; ObjectInputStream ois = null; try{ fis = new FileInputStream(new File(fname)); ois = new ObjectInputStream(fis); System.out.println("Integer " + ois.readInt()); System.out.println("String " + ois.readUTF()); } catch(Exception e) { ; } finally { try { ois.close(); fis.close(); } catch(Exception e) { ; } } } }
Subscribe to:
Posts (Atom)