Saturday, April 23, 2016

CHUCK NORRIS plugin for Jenkins


Jenkins is a server-based software that helps you build and test your Software Projects continuously. It makes your life easier when integrating any changes to your projects. Once you have Jenkins configured , it is soooo easy to build and deploy new code, just a couple clicks and Jenkins will do the rest. You can even schedule builds and deploys, it is awesome.

I've been working with Jenkins for over a year, but just a couple months ago I noticed that there is a Chuck Norris plugin, yeap, now you can have Chuck Norris in your daily builds/deploys.

You can install the Chuck Norris Plugin for some humor. Once installed the plugin using the plugin manager, you can activate it on the job configuration page. Select "Add post-build action" -> "Activate Chuck Norris".


This plugin adds an absolutely delightful feature to Jenkins: depending if your build succeeds, fails, or is unstable, it will show a picture of Chuck Norris auto-adapting (that’s right, computer science it is!) to the build result!





and just in case you didn't know:

  • Chuck Norris can delete the Recycling Bin


Thursday, April 21, 2016

How to install MAVEN on Windows



Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Maven addresses two aspects of building software: first, it describes how software is built, and second, it describes its dependencies. Contrary to preceding tools like Apache Ant, it uses conventions for the build procedure, and only exceptions need to be written down. An XML file describes the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins.

So, in other words, Maven will do the job managing your project, you just need to specify  the dependencies and it will help you building you project among other tasks, and no, it's not black magic, it's MAVEN, awesome, right?, and you know what is even better? ...it is FREE, so, Awesome X 2, =) (who doesn't like free stuff? I do)

Well, now I'm going to show you how to install Maven on your PC.

In order to install Apache Maven just download the Maven's zip file (here), and then unzip it to your local (I placed it in C:/) and proceed to configure the Windows environment variables:

Tools used:
  • JDK 1.7
  • Maven  

Install JDK, you can download it from here
Add JAVA_HOME to Windows environment variables. Right click on PC icon --> Advanced system settings --> Environment Variables





Now, download, unzip and place you Maven zip file in the folder you want (for example: C:/ )

Add M2_HOME and MAVEN_HOME environment variables:





Update PATH variable. We need to append Maven bin folder  %M2_HOME%\bin. This is going to help you run Maven's command everywhere:





Now, let's proceed to verify that we did everything fine. 
Verify Java, in command prompt type:  java -version
You should get something like this:




Now, let's verify Maven, in command prompt, type: mvn -version
If you get the next screen, you did the installation like a champ!





Now you have everything ready to start working in your Java projects using Maven and you can apply those ninja code moves you learned in YouTube. Enjoy!


Note: Just in case you didn't know, Maven 3.2 requires JDK 1.6 or above, while Maven version 3.0/3.1 requires JDK 1.5 or above.


Wednesday, April 13, 2016

Fizz Buzz game in Java and JUnit


I coded a Fizz Buzz game using Java language and tested it using JUnit.
If you aren't familiar with this game or haven't heard about it, this is the explanation according to our friends from Wikipedia:


"Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz".
Well, the rules for this program are:


  • If multiple of 3, get back "fizz"
  • If multiple of 5, get back "buzz"
  • If multiple of 3 & 5, get back "fizzbuzz"
  • if not, return the same number

It is a simple program, but it has some interesting logic, plus we are using JUnit, which is very useful if we want our code clean and with no errors. You might think that you are "wasting time" creating JUnit tests, but believe me, the effort is worth it!

Every time you make a small or big change in the code, you can make sure that the code is performing well and has not broken any older functionality by executing all JUnit test cases in one go written for that code or functions, That is to say, you write a test case once, and go on using the test case again and again to make sure that everytime the code is modified, it is still working as expected.

If you haven't installed JUnit yet, you might need to add some jars to project classpath: junit-4.12, hamcrest-core-1.3

 junit-4.12  --> https://github.com/junit-team/junit4/wiki/Download-and-Install
hamcrest-core-1.3 -->  https://code.google.com/archive/p/hamcrest/downloads


Also, for this Java exercise, I was using some useful Eclipse short-cuts such as:

New (package, class, project...) alt + shift + N
Run Java class: alt + shift + X, J
Run JUnit class: alt + shift + X, T
Switch between tabs: ctrl + page Up/pageDown
Save current file: ctrl + S
Import classes: ctrl + Shift + O


Here is the video, enjoy!













Monday, April 11, 2016

VB6 - Our first program. How to add two numbers


As I mentioned before, VB is a powerful tool and it is perfect if you want to get into programming. It is intuitive and easy to use, plus you can easily find information about any topic in the World Wide Web.

Today I'm gonna show how to manipulate VB6 and create a simple *.exe step by step. Our program will add two numbers and will give you the result. As simple as that.

First, download VB6 from my GitHub account:

https://github.com/rolando-febrero/Tutorial/commit/5d1c52dc103996f91eb16c17a03d4a0e08438af3

Once you double click on it, you'll see:


There are two options:
Reg Key and Run Visual Basic. If it is the first time you are running this program in your PC, you need to click on the first option. This option will add some information in your registry allowing you to create .exe . So click on that option, and then click on Yes.

Now click on "Run Visual Basic 6"




Select "Standard EXE" and click Open. Now we are going to see the Form1.
In VB "form" is like our panel or window. We can make it visible or not. At the right side, we can see a properties windows with many options available for the object we want to modify. As I said before is very intuitive, so just take a look and play with them.





General tool bar in on the left side. There are all the objects we can use for our program (text fields, buttons, labels, pictureBox, etc) Just drag and drop on the form to add them.

In our case, we need 3 textBox (Sum of two numbers and 1 for the result)




You can modify the text that those objects are showing by selecting them and modify the values in properties windows.

Now, let's start coding our button. Basically, we need to sum the first two values and show the result in the last textBox.

Double click on your button. By doing this, we can see the area where we program the object (button), in other words, the instructions it needs to perform any task. Also in this window you can see two drop down menu on top, one to select the object  ("Command1" our button) and the other to select the "action" (click).

Copy/Paste this code:

Sum = Val(Text1) + Val(Text2)
Text1 = ""
Text2 = ""
Text3 = Sum





As any other programming language, we need variables. In this case "Sum" is the variable that is going to hold the result.

Now, click on "Start" to see how your program works.




Now let's test it. Type the number and then click on the button to get the result.





At this point, we have a functional program. Simple, but it does what we expected to do. Now, it is time to make it an .exe

Go to File and select the option "Make Project1.exe". Select where you want to save it and that's it. Now you have your first executable (.exe). As simple as that.

Of course, our new program doesn't have validation or fancy stuff. You can play with it and start adding more code and make it look better. You can modify the view, or add a timer, or a message, or even change the icon. The limit is your imagination.








Wednesday, April 6, 2016

Visual Basic 6 - Portable Edition (VB6)


The very first "programming experience" I ever had was with Visual Basic (VB) when I was in high school. I can't say that it was "cool" because I was totally lost, didn't have a clue about programming. At that time I didn't care too much about computers, and I thought it was a waste of time.

Howeverrrrrrrrrrrrrr, one day, I changed my mind and got my first computer (around 2001-2002) and decided to be a programmer, yeah!!! 0s and 1s come to me!!!!! (I wanted to be like Neo from Matrix lol, don't blame me, it was a good movie) Of course, new toy, everything was happiness, perfection, but that didn't last too much, by the first week my computer crashed, (yes, first week -__-) What happened? a virus, a computer virus, an evil piece of software got into my computer  and caused some problems. I called a friend and with his help we re-install Windows. Well, after that my PC was like new (for a few days) then I started working with my projects and school stuff, until, my computer crashed (yes, one more time haha). So I called my friend again and we fixed it one more time.

So after that I was fascinated with those "computer viruses" and the way they "attack", so I did a little research and found out that the one that attacked my PC was created using VB. This little guy deleted some files from my System32 folder and didn't let my PC start up properly. Smart, right? just create a software that deletes a couple files and mess up a couple hundred/thousand computers.

Later, I downloaded VB6 and starting playing with the code and I learned VB language. In those years, VB became so popular, because it was really easy to learn and at the same time was a powerful tool. For example, you can get access to some low level system information and PC resources with just a couple commands. (Something that is harder to achieve in Java)

I'm still using VB6 for small personal projects. It is a super old version (1998) but easy to use, especially if you are just starting programming. I use it to create my own desktop applications. I have a portable edition in my flash drive. I don't need to install it, just open up the program and code.

Nowadays is hard to find this VB6 Portable Edition, so I uploaded to my GitHub account. Here is the link:


https://github.com/rolando-febrero/Tutorial/commit/5d1c52dc103996f91eb16c17a03d4a0e08438af3




I run it in Windows XP and 10 and have no compatibility issues, so far so good. It is a hacked version, so the antivirus might report it as a Trojan, but it isn't, This version is reliable.

I'm going to share some VB6 code later, =) but if you can't wait, you can find a lot of  VB info in the Word Wide Web...



Tuesday, April 5, 2016

Java OOP - Abstraction


Here we go, Abstraction in Java. Probably the hardest concept to understand when you start with OOP. I have to admit it took me a while to really figure out how this concept works and how useful can be.

Abstraction is the process of hiding the implementation details and showing only functionality to the user. In Abstraction we deal with ideas rather than events. Still confused? I know your pain =)

Abstraction in Java is achieved by using interface and abstract classes in Java. In this case, interface or abstract class is "something" that is not complete, something not concrete. So if we want to use interface or abstract class, we are going to need to extend and implement an abstract method with concrete behavior. Got it? not yet? for real? ...no problem, I'll keep explaining..

Imagine you have to define a class "table" but, you still don't know what kind of table it would be. It can be a night table, dinner table, 6 legged table, round table, etc... So, what do you do?, you define an abstract class table, why? because you still don't know what you are going to need later; you have the idea but nothing concrete yet. Just define something abstract.

Another example can be declaring an abstract class Vehicle (which is an abstract idea), then we can have "car", "truck" or "boat" class which can extend Vehicle...

Here is an example of the code:

abstract class Vehicle
{
   public abstract void engine();  
}
public class Car extends Vehicle {
    
    public void engine()
    {
        System.out.println("Car engine");
        //car engine implementation
    }
    
    public static void main(String[] args)
    {
        Vehicle v = new Car();
        v.engine();
        
    }
}


Output
Car engine


Things to remember about abstract class:

  • Can not be instantiated, that's why can not be used directly
  • Abstract classes may or may not contain abstract methods
  • But if a class have at least one abstract method, then the class must be declared abstract
  • If any class contains abstract methods then it must implement all the abstract method of the abstract class







Monday, April 4, 2016

Java OOP - Polymorphism


When I was a kid, I used to watch "Mighty Morphin Power Ranger" on TV, best TV show ever!!! I always wanted to be the Red Ranger!! (but please don't tell my secret lol). Well the point here is that this group of teenager had the ability to "morph" into super-powered warrior. In Java we have something similar and it is called "Polymorphism ".

Polymorphism is in programming, the ability OR capability of a method to behave and do different stuff based on the object that is acting upon.

In Java, Polymorphism has two types:

  • Method Overloading - Compile time polymorphism (static binding) &
  • Method Overriding - Runtime polymorphism (dynamic binding) 

Method Overloading: there are several methods present in a class having the same but different types/order/number of parameters. In this case, Java knows which method needs to invoke by checking the method signatures. 

class DisplayOverloading
{
    public void disp(char c)
    {
         System.out.println(c);
    }
    public void disp(char c, int num)  
    {
         System.out.println(c + " "+num);
    }
}

class Sample
{
   public static void main(String args[])
   {
       DisplayOverloading obj = new DisplayOverloading();
       obj.disp('a');
       obj.disp('a',10);
   }
}

Output:

a
a 10


Method Overriding: If subclass (child class) has the SAME method as declared in the parent class, it is know as method overriding. That is to say, if child class provides the specific implementation of the method that has been provided by one of its parent class, well, this is know as method overriding.

Rules for method overriding:
  • must be IS-A relationship (inheritance)
  • method MUST have same name as in the parent class
  • method MUST have same parameter as in the parent class
  • the access level cannot be more restrictive than the overridden method's access level
  • method declared final cannot be overridden
  • constructors cannot be overridden

class Animal{

   public void move(){
      System.out.println("Animals can move");
   }
}

class Dog extends Animal{

   public void move(){
      System.out.println("Dogs can walk and run");
   }
}

public class TestDog{

   public static void main(String args[]){
      Animal a = new Animal(); // Animal reference and object
      Animal b = new Dog(); // Animal reference but Dog object

      a.move();// runs the method in Animal class

      b.move();//Runs the method in Dog class
   }
}

Output:

Animals can move
Dogs can walk and run











Saturday, April 2, 2016

Java OOP - Encapsulation


Encapsulation is the process of wrapping code and data together into a single unit. "But, hey Rolo how do we do this?"

To achieve encapsulation in your beautiful Java code:

  • Declare the variables of a class private
  • Provide public setter and getter methods to modify and view the variables values.
So the code can go as follow:


/* File name : EncapTest.java */
public class EncapTest{

   private String name;
   private String idNum;
   private int age;

   public int getAge(){
      return age;
   }

   public String getName(){
      return name;
   }

   public String getIdNum(){
      return idNum;
   }

   public void setAge( int newAge){
      age = newAge;
   }

   public void setName(String newName){
      name = newName;
   }

   public void setIdNum( String newId){
      idNum = newId;
   }
}


In order to avoid any other class to modify our encapsulated class, variables are declared as private and to get access, should be through its public getters and setters.

This is how we access:

/* File name : RunEncap.java */
public class RunEncap{

   public static void main(String args[]){
      EncapTest encap = new EncapTest();
      encap.setName("James");
      encap.setAge(20);
      encap.setIdNum("12343ms");

      System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());
    }
}

Benefits?

  • Easy to model real-world entities
  • Easy to maintain and understand
  • Control how we access data and how is modified
  • Reusability
  • Flexibility
  • Secure

This short video will help to clarify this concept. Enjoy!