What day were you born?

14 08 2015

Simple JavaScript function that calculates your birthday day of week:

function getBirthday(myDay)
{
var daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; //array with the days of week
alert("I was born in a: " + daysOfWeek[myDay.getDay()]); //getDay returns the days of week (0-6 day of week. 0 == sunday)
}

var myDay = new Date(1969,10,25);
getBirthday(myDay);

Result:
Screenshot from 2015-08-15 00:18:03





ArnoldC

25 09 2014

ArnoldC is an imperative programming language where the basic keywords are replaced with quotes from different Schwarzenegger movies.

  • Every ArnoldC program must have a main method as following:
IT'S SHOWTIME
[statements]
YOU HAVE BEEN TERMINATED
  • The syntax to print a string, is:
TALK TO THE HAND "happy families are all alike"
  • Keyword for RETURN:
I'LL BE BACK
  • An Hello World, looks like this:
IT'S SHOWTIME
TALK TO THE HAND "hello world"
YOU HAVE BEEN TERMINATED

Check more details about ArnoldC programming language, here: https://github.com/lhartikk/ArnoldC/wiki/ArnoldC

You can search for other existent esoteric programming languages, such as “OOK!” (a programming language derived from Brainfuck and designed to be understood by Orang Utans.), here: http://en.wikipedia.org/wiki/Esoteric_programming_language





How to create and call a subroutine from a sapscript…

26 09 2013

Imagine that you need to get more information to be used in a sapscript form and it’s not possible to change the print program.

There is a way to do it by using subroutines of another program.

To do it, you need to modify your sapscript window and add some code as command line:

/:	 	PERFORM test_subroutine IN PROGRAM Z_SAP_SCRIPT_PERFORMS
/:	 	USING &EKKO-EBELN&
/:	 	CHANGING &TEST1&
/:	 	CHANGING &TEST2&
/:	 	ENDPERFORM

And create a program with the same name you’ve indicated before (e.g.: Z_SAP_SCRIPT_PERFORMS) to include your subroutines logic. For instance:

* the IN and OUT parameters go inside in_tab structure itcsy and out_tab structure itcsy:
form test_subroutine tables in_tab structure itcsy
                            out_tab structure itcsy.
                         
* Normal data declaration
  data: l_in1 type string,
        l_out1 type string.
        l_out2 type string.

* Reading parameters from sapscript form:
  read table in_tab index 1.
  move in_tab-value to l_in1 .

* SELECT SOME DATA into l_out1 e l_out2

* Exporting/Returning the selected data to the sapscript form (TEST1 and TEST2 variables, respectively):
  read table out_tab index 1.
  out_tab-value = l_out1.

  modify out_tab index 1.

  read table out_tab index 2.
  out_tab-value = l_out2.

  modify out_tab index 2.

endform.

That’s it!





Booleans in ABAP

12 09 2013

Unlike other languages (for e.g. Java) in ABAP programming there isn’t a boolean type.

…But you can simulate it by using the ABAP Language Type-Pool.

DATA is_ok TYPE abap_bool. 
... 
is_ok = abap_true. 
... 
IF is_ok = abap_true. 
   ... 
ENDIF.

This is the same as:

DATA is_ok TYPE c LENGTH 1. 
... 
is_ok = 'X'. 
... 
IF is_ok IS NOT INITIAL. 
   ... 
ENDIF.

Go to the ABAP Dictionary  transaction (SE11) and search for “abap” Type-Pool.
There you can confirm that the abap_bool type is defined as:

  • types: abap_bool type c length 1.

You can confirm the abap_true constant, as well:

  • constants: abap_true type abap_bool value ‘X’ (…)




MVC

31 03 2013
MVC Diagram

Src: Wikipedia

MVC stands for Model View Controller and it’s a modern design pattern used for web applications development.

MVC pattern divides an application in different modules:

  • Model – It’s the application module that communicates with the DB (query, alter and data storage) and contains the business logic.
  • View – Is responsible for the presentation or design.
  • Controller – Maps user actions, connecting view with model: Gets the user input and manages that data, sending it to the correct model and selecting view for response.

Main Advantages:

  • Easy to maintain. The developers can easily make changes.
  • All modules (Model, View and Controller) are independent. This allows different developers to work in the same project without knowing what the other modules do.

It’s been used on the most important Web Frameworks, of different technologies nowadays:
Java Spring, PHP Code Igniter, Ruby On Rails or Python Django (this one uses MTV – Model Template View, which is more or less the same thing with a different name. MTV view corresponds to the MVC controller and the MTV template is the MVC view) and many others.





Java: Fibonacci series

19 03 2013
src: wikipedia

src: wikipedia

By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.

The following java code, generates the Fibonacci numbers:

public class Fibonacci 
{

	public static void main(String[] args) 
	{
		// printing the fibonacci sequence from 0 to 12
		for(int i = 0; i <= 12; i++)
		{
			System.out.print(fibo(i) + ", ");
		}
	}


public static int fibo(int n)
	{
		int result = 0;
		if(n >= 0)
		{
			int [] nums = new int[n+1];

			for(int i = 0; i < n+1; i++)
			{
				if(i == 0)
				{
					result = 0;
				}else if(i == 1)
				{
					result = 1;
				}else{
					//sum of the previous two elements
					result = nums[i - 2] + nums[i - 1];					
				}
				nums[i] = result;
			}
		}else{
			// only numbers greater or equal to zero are allowed
			return -1;
		}
		return result;
	}

}
  • Output:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144

The interesting thing, is that Fibonacci sequence is a common job technical interview exercise… 😉





Processes vs Threads

13 03 2013

A process can contain multiple threads.
When you double-click on an application (e.g. Power Point) icon, the OS starts a new process and executes the main thread of that app, which can start other threads.
An advantage of multithreading is that an application can execute different tasks at the same time.

The biggest difference between a process and a thread, is that each process has it’s own address space, while threads (of the same process) run in a shared memory space. This means that it’s possible to share data amongst threads (e.g. reading and write to the same variables) which should be carefully done, using synchronization on the shared data.





3 different ways of how to create a Thread in Java…

10 03 2013

There are different ways of how to start a Thread in Java.

Example 1 – Extending the Thread class:

package threadTests;

class ThreadTest1Runner extends Thread
{
	public void run() 
	{
		for(int i = 1; i <= 5; i++)
		{
			System.out.println("Hello " + i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

public class ThreadTest1 
{
	public static void main(String[] args) 
	{
		 System.out.println("Main Thread Started...");
		 ThreadTest1Runner t1 = new ThreadTest1Runner();
		 t1.start();
		 
		 ThreadTest1Runner t2 = new ThreadTest1Runner();
		 t2.start();
		 System.out.println("Main Thread Ended...");
	}
}

Output:

Main Thread Started...
Hello 1
Main Thread Ended...
Hello 1
Hello 2
Hello 2
Hello 3
Hello 3
Hello 4
Hello 4
Hello 5
Hello 5

Example 2 – Implementing the Runnable interface:

package threadTests;

class ThreadTest2Runner implements Runnable
{
	public void run() 
	{
		for(int i = 1; i <= 5; i++)
		{
			System.out.println("Hello " + i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}


public class ThreadTest2 
{
	public static void main(String[] args) 
	{
		System.out.println("Main Thread Started...");
		Thread t1 = new Thread(new ThreadTest2Runner());
		Thread t2 = new Thread(new ThreadTest2Runner());
		t1.start();
		t2.start();
		System.out.println("Main Thread Ended...");
	}
}

Output:

Main Thread Started...
Hello 1
Main Thread Ended...
Hello 1
Hello 2
Hello 2
Hello 3
Hello 3
Hello 4
Hello 4
Hello 5
Hello 5

Example 3 – Implementing the Runnable interface (fastway):

package threadTests;

public class ThreadTest3 
{
	public static void main(String[] args) 
	{
		System.out.println("Main Thread Started...");
		Thread t1 = new Thread(new Runnable()
		{
			public void run() {
				for(int i = 1; i <= 5; i++)
				{
					System.out.println("Hello " + i);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		
		t1.start();
		System.out.println("Main Thread Ended...");
	}
}

Output:

Main Thread Started...
Main Thread Ended...
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5




The best programming languages…

22 02 2013

If you’re a programmer and you want to learn a new programming language, then I think the following tables can help you making a decision.

Are you looking for a consistent technology, used along the past 25 years?

Big Picture: The best programming languages in the last 25 years

Big Picture: 10 best programming languages in the last 25 years

Or do you preffer to start learning the one that stood out last year?

Highest Programming Language Ratings in a year

Highest Programming Language Ratings in a year

Source: http://www.tiobe.com

And you?
Do you agree with these statistics?
What is your preferred programming language?





Foreach in Java…

21 02 2013

Java Foreach syntax example:

for(Integer i : list){
//some code here
}

For some people is better to read than something like:

for(int i=0; i < list.size(); ++i){
//some code here
}

Follows an example of a foreach, looping over an HashMap:

import java.util.HashMap;

public class HashesExample 
{
	public static void main(String[] args) 
	{
		//Instantiate numOfLegsList Hash 
		HashMap numOfLegsList = new HashMap();
		
		//Adding elements to Hash
		numOfLegsList.put("Dog", "4 legs");
		numOfLegsList.put("Fish", "No legs");
		numOfLegsList.put("Chicken", "2 legs");
		numOfLegsList.put("Snake", "No legs");
		numOfLegsList.put("Lion", "4 legs");

		//Java For Each loop (over numOfLegsList elements)
		//Syntax: for(Element e: Collection):{//do something here}
		for(String s: numOfLegsList.keySet()){
			System.out.println(s + ": " + numOfLegsList.get(s));
		}
	}
}

Output:

Snake: No legs
Chicken: 2 legs
Dog: 4 legs
Lion: 4 legs
Fish: No legs