Sunday, May 6, 2012

Java Tips 1 - Project Organization

Project Organization

Once you are becoming a expert programmer, two things will eventually happen:

You can now make bigger programs for bigger purposes.
You look at other's people code to learn something from them

This is the part when being organized with your code will help you, and other programmers, get along and learn from other people's work and don't get lost in the process. If you follow a few guidelines while writing your program, you will soon realize that after a while you will follow those guidelines almost automatically, and if, for example, you  leave out a project for a couple of months because something important had to be done, and you come back to it, you will be thankful with yourself for following the guidelines.

Now let's see some of the things we can do to stay organized:

Guideline No. 1: Project creation

At this stage try to create a project with a meaningful name, one that describes a little about what is it's main purpose.
Also keep your files organized in folders. The source files in the 'src' folder (same name as the package), the binary files in the 'bin' folder, if you are using images or documents keep them all in a folder name 'data', and the documentation of your project, that you can put it in a folder called 'docs'. This way you will quickly find the information every time you need it.

Once you have this structure created, keep your source files inside their own packages. What I usually do is create a package for the Graphic User Interface (named 'src.gui') and a package for the logic (named 'src.logic'), so most of my projects have two layers of functionality. You can create more layers to include Data Base transactions or Security Layers and so on, but I recommend to have at least two.

Finally, I would recommend not to mix the Eclipse's "workspace" folder with you projects' folders. Eclipse keeps it's session files in it's own workspace, with information about your working session, your preferences, and your projects. I like to keep my projects in my own folders depending on the topic I am working now, that way I can easily find it every time I need it. Doing so, I could properly tell you exactly where is a Java project that I made 5 years ago!!

Guideline No. 2: Naming convention

Monday, April 16, 2012

Welcome Programmers

Java Programming Tutorials and Lessons

Welcome to this new Java tutorial. I hope you like it and learn to program in Java really fast.

Very soon I will also include some Tutorials and Lessons about C++.

I have decided to start this blog as a way to reinforce what I have learned through the years and share with other people the knowledge. Learning Java is the best way to begin in the Object Oriented Programming world, since it is very intuitive and somehow easy to learn.

If you are wondering why learning Java is an important matter you can go ahead and check this article about the Top 10 Programming Languages and the Top 10 Programming Languages to keep you employed. Java is one of the most used programming languages for enterprise custom-application development, and one of the easiest to dive into the object-oriented-programming.

Finally take a look at this graph:
Programming Language Popularity
These are normalized results taken from several 
different result sets featured

With Java you can also create fast and useful applications for yourself or anyone who needs a specific task to be automatized. I have used Java to create some cool stuff from small games to time-saving applications.

Now here you can find the lessons created until now. I will leave this post on top of the blog so you can continue with the lesson you left the last time:

Don't forget to follow me on Twitter to get the latest updates.

-Java Tutorial & Lesson 1 - Getting Started

Set up the required components to start programming in Java, including the Java Development Kit (JDK) and the Eclipse IDE.

-Java Tutorial & Lesson 2 - Setting up a Project on Eclipse

Create a Java project to start using the infinite possibilities of the Eclipse IDE for Java.

-Java Tutorial & Lesson 3 - Writing the first program: Hello World!

Say Hello with these simple steps and create your first Java program.

-Java Tutorial & Lesson 4 - Understanding Variables and Data Types

Learn about storing information with certain variables depending on the type of that information and then learn how to use it. Some examples are included in this and almost all the following posts.

-Java Tutorial & Lesson 5 - Understanding Methods and Attributes

Create the first methods to group functions that you will reuse in the future. Attributes are very useful for these kind of global functions.
Let your program make the right decision when confronted with a set of possibilities.

Please feel free to comment and ask if you have any questions, your feedback will be greatly appreciated.

Java Tutorial & Lesson 6 - Making decisions with Conditionals

How to use Java conditionals if and switch

Conditionals are a very important part in any program language. Very useful particularly when you have many options and have to let the computer choose one of them (making a decision) before operating. These kind of computer-decisions are called conditional expressions, and they are usually an equality or inequality that can be evaluated to either true or false.

If-else conditionals in Java

For example, an equality evaluation in Java goes like this:

if( a == b ) //This expressions evaluate if a is equals to b.
    System.out.println( "a and b are the same.");
else //If they are not equal, the message inside the "else" will be printed.
    System.out.println( "a and b are different.");

This can also be written like this:

if( a == b )
    System.out.println( "a and b are the same.");
else if( a != b ) //In Java, != is the opposite of ==
    System.out.println( "a and b are different.");

The last two expressions will show the same result. Now, an inequality evaluation in Java goes like this: