Tag Archives: coding

Developers Beginner Guide to Public Static Foo

We recently asked out students to create a simple method in Java return if a given number is a prime number or not.

The task included a list of requirements the students had to fulfill. However, after correcting I realized that for many beginners some concepts are not fully clear – even if those concepts have been taught before. These are things an experienced developer sometimes often forgets because one has used these concepts over the years and became second nature of the developers.

Let’s have a look at what we asked for:

  1. Implement a method which returns if a given number is prime or not.
  2. The method has to be accessible from callers outside of the current class.
  3. The method has to be called without the corresponding class to be instantiated.
  4. Name the given number ‘n’.

These points mainly addressed the understanding of basic language concepts of the programming language taught so far.

There are different approaches, how to test number for being prime. In the given example we use the simple 6k +/- 1 optimization method.

public static boolean isPrime(int n) {
if (n <= 3)
return (n > 1);

if (n % 2 == 0 || n % 3 == 0)
return false;

int i = 5;
while (i * i <= n) {
if (n % i == 0 || n % (i+2) == 0)
return false;
i = i + 6;
}
return true;
}

While we have been very indulgent with the algorithms “invented” we have been very strict about the language concepts and syntax used in the solutions. number four of the previous task list should be, however, clear. The thing is called ‘n’.

I reflected this quite a while, trying to come up with explanations to the various issues I have seen in the solutions.

Methods as Return Values

Return values. We asked for a method that returns if a given number is prime or not. First of all, you need to define the type of the return value. In the given example it was boolean. Simply because you want to return a yes or no or – as we say in programming jargontrue or false.

public static boolean isPrime(int n) {
...
return false;
...
return true;
}

Also you make sure that all execution paths in your method provide a return statement. The value returned is either true or false.

For someone new to programming, the concept of methods, functions and so on is not always intuitive. In this case I usually try to come up with basic math. I show a mathematical function like f(x) = x + 1. As long as you know something about math it should be clear what this function does. You provide a value for x and you will receive a value increased by one. Then I write this down as code.

int f(int x) {
return x + 1;
}

You have written your very first method. It eventually does exactly what the mathematical function did before. Well done. Usually I celebrate this with my students.

It doesn’t matter if we are talking about object oriented or functional programming languages. The idea behind methods and functions is always the same. The syntax differs from language to language but the concept stays the same.

In addition I extend this example to the mathematical formula f(g(x)) = x + 1 with g(x) = x *2. Again I show how both methods would look in code.

int g(int x) {
return x * 2;
}

int f(int x) {
return x +1;
}

Now I write down a few lines to show how this would work.

int x = 5; 
int y = f(g(x));

While most compilers would refuse to compile this without some additional syntactical sugar, to the students the concepts should be quite clear by now.

Accessibility – Private Property vs. Public Services

If you are not used to programming yet, accessibility can give you a major headache. Who can access what? Over time you will understand concepts like information hiding, encapsulation or abstraction. All of these concepts will make use of accessibility. Although there are slightly different meanings and keywords in a variety of programming language, the idea is always the same. While there is a tutorial available for Java, this is one thing very hard to understand for those learning programming.

I usually try to find some real-world examples: public phone, a phone within the flat with is protected (to be used by family members only) by walls and doors and my private iPhone.

But also public services to be used by everybody, protected classes to be used only for those members in the course and private property not to be accessed by anybody else than me.

I realized that these concepts are slightly more difficult to non-native English speakers until I talked about the examples above which might include translation to some point.

Static – Just use it

I even don’t know where to start how to explain this to somebody not familiar with object-orientation and higher programming languages. I came from Assembler, C, Pascal, BASIC, LISP to Perl, C++ and finally to Java and .NET (VB, C# some F#), some Ruby, Python and various other languages I just don’t have in mind.

If found

The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs.

and

In C# terms, “static” means “relating to the type itself, rather than an instance of the type”. You access a static member using the type name instead of a reference or a value, e.g. Guid.NewGuid().

and hell ya, this explains everything to a novice. So I explain it a very simple but inaccurate way: When using the static keyword on a method, you can just run the code. If using it on a variable, you can just use the variable. To some point you can think of static as “just use it”.

If you are new to programming and come across this article, I hope this helps .

tl;dr

There’s no short version. Just trying explaining public static foo to someone without a clue about programming.

Fighting Qt Creator develpoing for SailfishOS

Prologue

For some days, I fight quite an epic battle with Qt Creator. A battle, I am losing right now. I recently started developing a SailfishOS app though. A spare time project which actually drives me mad. To be fair, one should say SailfishOS is quite at an early stage compared to other platforms. However, the SDK, the tool chain, including Qt Creator, VirtualBox integration, build and deploy process already work as a charm. It works out of the box. Even for someone like me, pampered by Visual Studio, Eclipse and corresponding platforms existing for a decade and more.

There are some shortcomings in documentation, though. As a developer myself, I know how hard it is to get some good documentation in place. Therefore, kudos for everything available already. Although, I spend 95% percent in digging the darkest places of the internet to find some information. I have to move out of my comfort zone, which is good. I learn a lot by digging around, which is even better. However, I do not get my project out of the starting blocks, which is bad.

Teh Facts

Following the tutorials available, I tried to create a SailfishOS dialog with two text fields as below.

import QtQuick 2.0
import Sailfish.Silica 1.0

Dialog {
    id: addDeviceDialog
    property string deviceName
    property string deviceIdentifier 

    anchors.fill: parent

    DialogHeader {
        title: qsTr("Add a Device")
    }

    TextField {
        id: deviceNameField
        placeholderText: qsTr("Device Name")
    }

    TextField {
        id: deviceIdentifierField
        placeholderText: qsTr("Device ID")
    }

    onDone: {
        if (result == DialogResult.Accepted) {
            deviceName = deviceNameField.text
            deviceIdentifier = deviceIdentifierField.text
        }
    }
}

Qt Creator, however, responds with

Could not resolve the prototype ‘TextBaseItem’ of ‘TextBase’. (M301)

I encounter this issue wit both, TextField and TextArea.

M301

I can remove this issue by adding

import QtQuick.Controls 1.0

But then Qt Creator responds with

Qml module not found

Modules seem to be installed and paths are set. I probably miss something over here or I do deal with the IDE in a terrible wrong way.

QtQuick.Controls 1.0I will dig further and contact the SailfishOS mailing list, looking forward to get some help there to update this article soon.

[Update, 13.07.2014]

There was an almost instant reply on my request on the mailing list, though. You can completely ignore this issue. Gt Creator will build and deploy without any issues. In my very case, I had an additional issue, where I forgot to set the width property of the text field. Once I did this, I was able to see the text field on my deployed app.

SOAP with Python

To evaluate a SOAP service, I decided to increase my Python skills by one by consuming the SOAP service with Python. SOAP Web Services from Dive Into Python seemed a quite good starting point with detailed instructions for setting up your Python environment to do so.

After starting to work thought this article how to install the required SOAP libraries, I realized quickly links are broken and various information became obsolete.  Therefore, I decided to summarize the amendments to set up SOAP libraries for Python, in this article focusing on how to set up PyXML, fpconst and SOAPpy.

Prerequisites
Before starting lets be sure Python is installed. You might check with

where python

to determine which python will be accessed via the PATH variable.In my case it is a Python 2.7 running on Windows 8.0.

Python 2.7 on Windows 8.0PyXML
PyXML can’t be accessed via http://pyxml.sourceforge.net/. Instead you have to access the site via http://pyxml.sourceforge.net/topics/ directly. Unfortunately, you might realize that the latest supported version from there is Python 2.4. You will find a Windows Installer for PyXML 0.8.4 for Python 2.7 at the OddThinking blog instead.

Run through the installer, just make sure, you pick the right Python installation during the process.

PyXML Installation DialogVerifying the installation is quite simple using the Python console, though.

>>> import xml
>>> xml.__version__
'0.8.4'
>>

fpconst
Instead of picking up the fpconst package from Unversity of Washington’s broken link,  you’ll meanwhile find fpconst 0.7.2 within the Python Package Index. Download and extract the archive to any temporary folder. Change folders to the extracted one and run

python setup.py install

from the console.

fpconst Package installationAgain very the installed package with

>>> import fpconst
>>> fpconst.__version__
'0.7.2'
>>>

SOAPpy
SOAPpy is probably the package causing the most headache. I first picked SOAPpy 0.12.0_rc1 from the download section at  http://pywebsvcs.sourceforge.net/. It caused more or less trouble, trying to install the package. In addition, SOAPpy was announced to be merged into the ZSI (Zolera SOAP Infrastructure) web service toolkit. If you are going to try ZSI anyway, download and installation are similar to the example above using

python setup.py install

Even the latest version of ZSI installs fine with Python 2.7.

Anyway, when sticking with SOAPpy, you might want to get the latest version (0.12.5) from the Python Package Index. Extracting the archive and following the steps above will result in an import error, though.

Traceback (most recent call last):
  File "setup.py", line 7, in <module>
    from setuptools import setup, find_packages
ImportError: No module named setuptools

To solve this, navigate to the setuptoools 0.9.8 in the Python Package Index, and download ez_setup.py to your local machine. Run the script with

python ez_setup.py

which will compile quite a bunch of stuff on your local machine. Follow the instructions, and add the C:\Python27\Scripts to your PATH variable.

Once installation is complete, you will find an easy_install.exe program in your Python Scripts subdirectory. For simple invocation and best results, add this directory to your PATH environment variable, if it is not already present.

Head back to the download folder of SOAPpy 0.12.5 and run once again

python setup.py. install

which should finally end up in something similar to

...

Installed c:\python27\lib\site-packages\docutils-0.11-py2.7.egg
Searching for fpconst==0.7.2
Best match: fpconst 0.7.2
Adding fpconst 0.7.2 to easy-install.pth file

Using c:\python27\lib\site-packages
Finished processing dependencies for SOAPpy==0.12.5

For the very last time, verify the installation by

>>> import SOAPpy
>>> SOAPpy.__version__
'0.12.5'
>>>

By now, you should be ready to use all three packages with your Python 2.7 installation.