RSS FeedLatest Article

Java Generating Prime Number - August 7th, 2008

/*
This class check the prime number between 1 to 1000 and write the prime number.
*/

public class PrimeNumber
{
// This method tests whether a given number is prime or not.
public static boolean isPrime ( int num )
{
boolean prime = true;
int limit = (int) Math.sqrt ( num );

for ( int i = 2; i <= limit; i++ )
{
if ( num % i == 0 )
{
prime = false;
break;
}
}

return prime;
}

public static void main ( String[] args )
{
// This loop writes out all the prime numbers less than 1000.
for ( int i = 2; i <= 1000; i++ )
{
if ( isPrime ( i ) )
System.out.println ( i );
}
}
}

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Tags: , , , , ,

Posted under Java

Java JDBC with using Oracle

The JDBC is used whenever a Java application should communicate with a relational database for which a JDBC driver exists. JDBC is part of the Java platform standard; all visible classes and interfaces used in the JDBC are placed in package Java
Main JDBC classes:
Download the Java connecting with Oracle Code, you have to [...]

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Java creating custom Exceptions

Java supports the custom, user defined exception. Java provides the well known and common exception classes and gives freedom for developer to create their own exception classes matching to their specific business logic/role. For example, a user not found exception would be implemented by its own.
Download the Java Source code  for creating user custom exceptions
MyException.java  [...]

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Java Object Serialization by using Java I/O Streams - August 4th, 2008

The task is to store different objects of classes in a physically located file on hard disk. For this to store objects and their states (including the values of their data members) we have to make those classes serializeable. Use Standard Java I/O streams FileOutputStream, ObjectOutputStream to write/store to file and FileInputStream, ObjectInputStream streams to read objects from the file. After reading all objects from the file, have to type caste to their original class type and then get data from these casted objects.

Following is the code that you can see and can also download. MyObjects.java

Read the rest of this entry »

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Tags: , ,

Posted under Java

MySQL Server 5.x Installation problem for Windows Vista - August 4th, 2008

MySQL side-by-side error fixing while Running Instance Configuration Wizard

MySQL Instance Side-by-side Configuration  incorrect

To install MySQL Server 5.0 in Windows Vista

  1. Disable the UAC in Windows Control Panel->User Accounts (See the screen shot below)
    Windows Vista UAC turn off
  2. Run the MySQL setup i.e. Use mysql-essential-5.0.51a-win32.msi
  3. In the final step uncheck “Configure MySQL Server now”
  4. Download and run Resource Hacker http://www.angusj.com/resourcehacker/
  5. Open …\MySQL Server 5.0\bin\MySQLInstanceConfig.exe with Resource Hacker
  6. Navigate to 24\1\1033
  7. Change
    <requestedExecutionLevel level=”asAdministrator” uiAccess=”false”>
    to
    <requestedExecutionLevel level=”requireAdministrator” uiAccess=”false”>
  8. Press “Compile script”
  9. Exit Resource Hacker and save the result (overwrite the initial MySQLInstanceConfig.exe)
  10. Now MySQLInstanceConfig.exe should start normally.
  11. Configure the server.
  12. Sometimes the server doesn’t start:
    a) Check Windows Firewall settings (3306/TCP)
    b) Try changing the compability mode for the file …\MySQL Server 5.0\bin\mysqld-nt.exe to Windows XP-SP2.
  13. 13. That’s all.
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Tags: , , ,

Posted under MySQL

Understanding Exceptions in Java - June 30th, 2008

An exception is an error that happens when an application is running. When an exception condition happens, the exception is said to be thrown, and it changes the normal execution control flow of the program. Java offers a very comprehensive and flexible system for exception handling. The main benefit of such a system is that it largely automates the process of error handling, which otherwise would need to be coded into each application: what a waste of resources that would be. All exceptions are represented by classes organized into a hierarchy tree. Let’s take a look at that tree…well, a part of it.

The Exception Tree in Java s they say, everything in Java is a class (or object), and the exceptions are no exception. They are organized into a hierarchy of classes, a part of which is shown in Figure below.

The Exception Tree in Java

Checked Exceptions and Runtime Exceptions

When in a program, if you perform an operation that causes an exception—that is, an exception is hrown—you can always catch the exception (you will see how later in the chapter) and deal with it n the code. This is called handling the exception. Based on whether or not you are required to handle hem, the exceptions in Java are classified into two categories: checked exceptions and runtime xceptions.
Checked Exceptions
This is the category of exceptions for which the compiler checks (hence the name checked exceptions) o ensure that your code is prepared for them: prepare for unwelcome but expected guests. These exceptions are the instances of the Exception class or one of its subclasses, excluding the runtime Exception subtree. Checked exceptions are generally related to how the program interacts with its environment; for example, URISyntaxException and ClassNotFoundException are checked exceptions.
The conditions that generate checked exceptions are generally outside the control of your program, and hence they can occur in a correct program. However, you can anticipate (expect) them, and thus you must write the code to deal with them. The rule is: when a checked exception is expected, either declare it in the throws clause of your method or catch it in the body of your method, or do both; i.e. you can just throw it without catching it, you can catch it and recover from it, or you can catch it and rethrow it after doing something with it. Just throwing it without catching it is also called ducking it. You will get more comfortable with the throw and catch terminology by the end of this chapter.

Runtime Exceptions
The exceptions of type RuntimeException occur due to program bugs. The programmer is not required to provide code for these exceptions because if the programming were done correctly in the first place, these exceptions wouldn’t occur, anyway. Because a runtime exception occurs as a result of incorrect code, catching the exception at runtime is not going to help, although doing so is not illegal. However, you would rather write the correct code to avoid the runtime exceptions than write the code to catch them. An exception represented by the ArithmeticException class is an example of runtime exceptions. Again, you do not need to declare or catch these exceptions.

Runtime exceptions (exceptions of type RuntimeException or its subclasses) and errors (of type Error or its subclasses) combined are also called unchecked exceptions and they are mostly thrown by the JVM, whereas the checked exceptions are mostly thrown programmatically. However, there is no rigid rule.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Tags: , , , ,

Posted under Java

Composition and Aggregation using Java - June 29th, 2008

There is always been confusion about composition and aggregation. While in servicing NetSol Technologies, I came to learn practically what is composition and aggregation. First thing is both are type of association.  Below is the exact definition of each term and then practical Java code, where you can understand exactly how can we implement composition and aggregation in programming.

Composition: Life time of objects got to be the same. For example there is a composition between car-frame and a car. If the car gets destroyed the car-frame would also get destroyed.

Aggregation: Life times of the objects are not the same, one object can live even after the other object has been destroyed.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Tags: , , , ,

Posted under Java

My NetSol Joining as Apprentice Software Engineer - June 16th, 2008

This is my pleasure, luck and great blessings of Allah that I got such a golden chance to join NetSol as my career first Job. I will discuss here, how I go through this process and join the NetSol.

I have finished my BIT(Hos) degree from Punjab University, Lahore in March 2008. Then after that i was doing my final project and was looking for a job in good reputed organization and wish for NetSol.

One Friday, I suddenly  get informed that NetSol conducting open test for candidates for jobs. I rush at run-time with my friends and give there test yet I was nothing with preparation. Then after 1 week I got call from NetSol that you passed the test and come for technical interview. Interview was superb and things go fine.

Again, after 1 week got call from NetSol that you are selected for Apprenticeship program. Let’s also discuss about Apprenticeship Program. Apprenticeship Program is joint venture of PSEB and NetSol. PSEB saying about Apprenticeship program:

“Under the IT Industry Apprenticeship Program, PSEB offers financial subsidy for the companies to recruit graduates possessing the basic skills and knowledge in Information Technology and other related disciplines to provide IT/ITeS services.  These recruits are generally graduates with some experience.  These “apprentices” or trainees are hired by companies as full-time employees and put through a 12-month program, consisting of in-company training, on-the-job training and mentoring.”

So, on Friday 30th May all selected candicates are invited to come in joining cermony. In this session 50 People come, 15 for Java, 15 for .Net and 20 for SQA. I got selected into my favorite field Java Developement.  I enjoyed this event very well.

Now, from 2nd our training was started in NetSol Training Institure(NTI). We are offered total four subject, Java by Sir Sharjeel, Oracle By Sir Akhater,  Software Engineering By Sir Sharjeel and fantastic subject Business Communication by Ma’am Sonia Arooj. Mr. Abid Ali,  Apprenticeship Project Manager, a very energatic person always give us motivation and guidance toward the working hard.

This is my dream job, I am learning a lot of new and interesting things here in all subjects. So, I will share more interesting experience  here.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Posted under Personal

How to Take Screenshots in Java - April 30th, 2008

Have you ever wanted to grab a screenshot from your Java application? Here’s a quick tutorial on how to grab a screenshot and save it to a JPEG and PNG file. This shows how to use the Robot class to capture the screen image and the ImageIO API to save it as a JPG and PNG.

First, we need to determine the size of the screen and create a rectangle with the screen dimensions. We can query the toolkit to determine the size of the screen. In our example, we are grabbing the entire screen. You can also use this same method to grab the contents of a window or a specific screen rectangle.

//Get the screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle rect = new Rectangle(0, 0, screenSize.width, screenSize.height);

Next, you will need to create an instance of the Robot and capture the desired rectangle of the screen. The createScreenCapture() method takes a Rectangle and returns a BufferedImage.

Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rectangle);

Finally, we are going to save the screenshot as a PNG and JPG. For this, we will use the ImageIO API to convert from a BufferedImage to a PNG and to a JPG.

//Save the screenshot as a png
file = new File(”screen.png”);
ImageIO.write(image, “png”, file);

//Save the screenshot as a jpg
file = new File(”screen.jpg”);
ImageIO.write(image, “jpg”, file);

The complete screenshot example is below:

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

try
{
//Get the screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle rect = new Rectangle(0, 0,
screenSize.width,
screenSize.height);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rect);
File file;

//Save the screenshot as a png
file = new File(”screen.png”);
ImageIO.write(image, “png”, file);

//Save the screenshot as a jpg
file = new File(”screen.jpg”);
ImageIO.write(image, “jpg”, file);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Tags: , , ,

Posted under Java

Java Buzzwords - April 20th, 2008

You will hear and see quite a few Java buzzwords in the Java world. It’s important to know the Java buzzwords because they represent the factors that have played important roles in shaping the Java language. These words are summarized in Table 1-3.

Buzzword

Description

Architecture neutral The Java compiler compiles the source code into bytecode, whichdoes not depend upon any machine architecture, but can be easilytranslated into a specific machine by a JVM for that machine.
Distributed Java offers extensive support for the distributed environment of theInternet.
Dynamic New code can be added to the libraries without affecting theapplications that are using the libraries, runtime type informationcan be found easily, and so on.
High performance The Just-In-Time (JIT) compilers improve the performance ofinterpreting the bytecode by caching the interpretations.
Interpreted The Java compiler compiles the source code into bytecode, whichcan be executed on any machine by the Java interpreter of anappropriate JVM.
Multithreaded Java provides support for multithreaded programming.
Object oriented Java supports the features and philosophy of object-orientedprogramming.
Portable There are no implementation-dependent aspects of the languagespecifications. For example, the sizes of primitive data types and thebehavior of the arithmetic on them are specified. This contributes tomaking programs portable among different platforms such asWindows, Mac, and Unix.
Robust Java provides support for error checking at various stages: earlychecking at compile time, and dynamic checking at runtime. Thiseliminates some situations that are error prone such as pointers inthe C language.
Secure Because Java supports the distributed environment of the Internet,it also offers multiple security features.
Simple Java omits many clumsy and confusing features of otherprogramming languages such as C++. Also, Java is designed to beable to run stand-alone on small machines. The size of the basicinterpreter and class support is 40 KB.

The most popular buzzwords are architecture neutral (or platform independent), object oriented, and portable.

The three most important takeaways from this chapter are the following:

  • You write a computer program in a high-level language and use the compiler to convert it into an executable program that will actually be executed by the computer.
  • Following the underlying philosophy of object-oriented programming, Java allows you to adapt computing to the problem that you are trying to solve by directly representing the objects in the problem with objects in the Java program. The objects are created from classes that you write.
  • The Java compiler creates bytecode that can be translated by a Java virtual machine (JVM) for a specific platform, such as Windows, Solaris, or Mac, into executable instructions. So, a Java program written once can be run on different platforms by using different JVMs.
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Tags: , , , , ,

Posted under Java